Repository navigation
Description
Version
main 6b44cea (source build, reports 9.9.99); the same saturation with + and with the addend as the mma accumulator on 1.7.0 (pip; the releases have no ct.wrapping_add)
Version
13.5 (tileiras 13.5.37 from the nvidia-cuda-tileiras wheel); 13.4.92 with 1.7.0
Which installation method(s) does this occur on?
Source, Pip
Describe the bug.
ct.wrapping_add of an int8 ct.mma result and an int32 constant does not wrap around on overflow: the sums saturate at INT32_MAX.
acc = ct.mma(a, b, ct.full((32, 32), 0, ct.int32)) # int8 x int8 -> int32; |acc| <= 420 here
ct.store(Out, index=(0, 0), tile=ct.wrapping_add(acc, ct.full((32, 32), 2147483645, ct.int32)))617 of the 1024 elements differ from (acc + 2147483645) mod 2**32 (483 of the sums overflow): 370 are exactly INT32_MAX, and the others are INT32_MAX plus the rest of the dot product. The same ct.wrapping_add on the same product loaded from memory is exact, and so is the product stored on its own.
The Tile IR is what the program says: the add is a separate addi without overflow flags after the mmai, and the Tile IR spec gives integer arithmetic wrap-around semantics on overflow (addi, and mmai is defined as sum_k A_ik * B_kj + C_ij):
%1 = mmai %tile, %tile_35, %cst_0_i32_37 signed signed : tile<32x32xi8>, tile<32x32xi8>, tile<32x32xi32>
%cst_2147483645_i32 = constant <i32: 2147483645> : tile<32x32xi32>
%2 = addi %1, %cst_2147483645_i32 : tile<32x32xi32>
tileiras folds the addi into the accumulator of a saturating tensor-core MMA. The SASS (sm_89) loads the constant into the MMA's C registers and uses IMMA ... .SAT:
/*0740*/ IMAD.MOV.U32 R12, RZ, RZ, 0x7ffffffd ;
/*0820*/ IMAD.MOV.U32 R13, RZ, RZ, 0x7ffffffd ;
/*0970*/ IMMA.16832.S8.S8.SAT R16, R8.ROW, R22.COL, R12 ;
/*09b0*/ IMMA.16832.S8.S8.SAT R8, R8.ROW, R6.COL, R12 ;
The wrong results are exactly those of an int32 accumulator that starts at the constant and is clamped to [INT32_MIN, INT32_MAX] after every 16 elements of K (8 for uint8 inputs).
Expected: the wrap-around result (acc + 2147483645) mod 2**32 (here e.g. product 207 -> -2147483444), as ct.wrapping_add documents; the same for acc + C and for ct.mma(a, b, C) with C as the accumulator, which saturate in the same way (also on 1.7.0 with tileiras 13.4.92).
Minimum reproducible example
"""ct.wrapping_add on the result of an int8 ct.mma saturates at INT32_MAX instead of wrapping around."""
import torch
import cuda.tile as ct
C = 2**31 - 3 # x + C overflows int32 for every x >= 3
@ct.kernel
def mma_then_wrap(A, B, Out):
a = ct.load(A, index=(0, 0), shape=(32, 32))
b = ct.load(B, index=(0, 0), shape=(32, 32))
acc = ct.mma(a, b, ct.full((32, 32), 0, ct.int32)) # the exact product: |acc| <= 2048 here
ct.store(Out, index=(0, 0), tile=ct.wrapping_add(acc, ct.full((32, 32), 2147483645, ct.int32)))
@ct.kernel
def load_then_wrap(P, Out):
p = ct.load(P, index=(0, 0), shape=(32, 32))
ct.store(Out, index=(0, 0), tile=ct.wrapping_add(p, ct.full((32, 32), 2147483645, ct.int32)))
def wrap32(t):
t = t % 2**32
return torch.where(t >= 2**31, t - 2**32, t)
torch.manual_seed(0)
A = torch.randint(-8, 9, (32, 32), dtype=torch.int8, device="cuda")
B = torch.randint(-8, 9, (32, 32), dtype=torch.int8, device="cuda")
product = A.cpu().long() @ B.cpu().long()
expected = wrap32(product + C)
out = torch.zeros(32, 32, dtype=torch.int32, device="cuda")
# the same product, computed on the host and loaded: wraps as documented
ct.launch(torch.cuda.current_stream(), (1,), load_then_wrap, (product.to(torch.int32).cuda(), out))
torch.cuda.synchronize()
print("load + wrapping_add:", int((out.cpu().long() != expected).sum()), "of 1024 elements wrong", flush=True)
out.zero_()
ct.launch(torch.cuda.current_stream(), (1,), mma_then_wrap, (A, B, out))
torch.cuda.synchronize()
got = out.cpu().long()
wrong = got != expected
print("mma + wrapping_add: ", int(wrong.sum()), "of 1024 elements wrong,", int((got == 2**31 - 1).sum()),
"equal to INT32_MAX", flush=True)
for i, j in wrong.nonzero()[:3].tolist():
print(f" [{i}, {j}]: product {int(product[i, j])}, got {int(got[i, j])}, expected {int(expected[i, j])}", flush=True)Relevant log output
$ python repro.py # cutile-python main 6b44ceae + nvidia-cuda-tileiras 13.5.37, sm_89
load + wrapping_add: 0 of 1024 elements wrong
mma + wrapping_add: 617 of 1024 elements wrong, 370 equal to INT32_MAX
[0, 0]: product 207, got 2147483647, expected -2147483444
[0, 2]: product 186, got 2147483647, expected -2147483465
[0, 3]: product 103, got 2147483647, expected -2147483548Full env printout
Summary instead of the full print_env.sh output:
OS: Ubuntu 22.04.4 LTS, x86_64
Python 3.11.18, torch 2.4.0+cu124
cutile-python main 6b44ceae (source build); nvidia-cuda-tileiras / nvidia-nvvm / nvidia-cuda-nvcc 13.5.37 (pip)
GPU: NVIDIA vGPU-32GB (sm_89), driver 560.35.03 (CUDA 12.6); launches through a small in-process loader for CUDA 12 drivers (controls/cuda12_loader in the attachment: it rewrites the cubin ELF header for the CUDA 12 driver and changes no instruction)
Also: cuda-tile 1.7.0 (pip) + tileiras 13.4.92 on an RTX 4060 Laptop GPU (sm_89), driver 566.26, Ubuntu 24.04 on WSL2Other/Misc.
- Variants (
controls/variants.py, main + 13.5.37):ct.wrapping_addafterct.mma,+afterct.mma, an addend loaded from memory, and the constant as thect.mmaaccumulator all give the same wrong results (617 of 1024 for 32x32x32 int8, 3274 of 4096 for 64x64x128, all 1024 for uint8), each equal to the clamped accumulation;ct.wrapping_sub(acc, -2147483645)is correct (not folded), and a 16x16x64 int8 tile is correct in every form.controls/release.pyshows the+and accumulator forms on 1.7.0 + 13.4.92 (same counts). - The SASS above comes from the cubin of
mma_then_wrap; the full listing, the Tile IR and tileiras' remarks ("MMA operation successfully optimized to use Tensor Cores") are in the attachment. attachment.zip:repro.py,controls/(variants.py,release.py,cuda12_loader/) and the outputs, SASS and Tile IR invalidation-evidence/.
Contributing Guidelines
- I agree to follow cuTile Python's contributing guidelines
- I have searched the open bugs and have found no duplicates for this bug report
Activity