ao icon indicating copy to clipboard operation
ao copied to clipboard

`int_scaled_matmul` reads out of bounds and returns wrong results when K is even but not a multiple of `BLOCK_K

Open truong-v opened this issue 4 months ago • 1 comments

Description

scaled_matmul_kernel_with_block_pointers turns off the K-axis bounds mask when EVEN_K is true. The host sets EVEN_K = (K % 2 == 0), but what the kernel actually needs is K % BLOCK_K == 0. When K is even and is not a whole number of BLOCK_K tiles, the last K tile is loaded with no mask: it reads past the end of A and B and adds whatever is there into the result.

Host predicate — torchao/kernel/intmm_triton.py:311:

EVEN_K=(K % 2 == 0),

Kernel — torchao/kernel/intmm_triton.py:227-233:

for k in range(K, 0, -BLOCK_K):
    if EVEN_K:
        a = tl.load(A)                                  # <-- no mask
        b = tl.load(B)                                  # <-- no mask
    else:
        a = tl.load(A, mask=rk[None, :] < k, other=0.0)
        b = tl.load(B, mask=rk[:, None] < k, other=0.0)

K % 2 == 0 does not imply K % BLOCK_K == 0 — for example K=34 with BLOCK_K=32.

Worth noting why this bites immediately: the M and N axes are protected (rm % M / rn % N wraparound plus a masked store), so an over-read there folds back into the tensor. The K axis has no protection at all, so a wrong K predicate is a straight out-of-bounds read.

Steps to reproduce

import torch
import triton

from torchao.kernel.intmm_triton import int_scaled_matmul_kernel

M = N = 64
K, BLOCK_K = 34, 32  # K is even, but 34 % 32 == 2

# Pad A and B with a sentinel so the out-of-bounds read is deterministic.
a_pad = torch.full((M, K + BLOCK_K), 127, dtype=torch.int8, device="cuda")
a_pad[:, :K] = torch.randint(-128, 128, (M, K), dtype=torch.int8, device="cuda")
b_pad = torch.full((K + BLOCK_K, N), 127, dtype=torch.int8, device="cuda")
b_pad[:K, :] = torch.randint(-128, 128, (K, N), dtype=torch.int8, device="cuda")
a, b = a_pad[:, :K], b_pad[:K, :]

scales = torch.ones((M, 1), dtype=torch.float32, device="cuda")
c = torch.zeros((M, N), dtype=torch.float32, device="cuda")

# Pin the config: BLOCK_K is normally picked by timing, so an autotuned run
# could pass by luck on one machine and fail on another.
config = triton.Config(
    {"BLOCK_M": 32, "BLOCK_N": 32, "BLOCK_K": BLOCK_K, "GROUP_M": 8},
    num_stages=1,
    num_warps=2,
)

out = int_scaled_matmul_kernel(a, b, scales.expand(M, N), c, config)
ref = (a.float() @ b.float()) * scales
print("max abs error:", (out - ref).abs().max().item())

## Output:
##
## max abs error: 483870.0
##

Environment

torchao `main` @ `b7f01f18` (`intmm_triton.py` unchanged since `3bad6a2`)
torch 2.13.0+cu126, triton 3.7.1
NVIDIA H100 NVL (sm90)

truong-v avatar Jul 29 '26 03:07 truong-v

This appears to be resolved as a side effect of #4717 ("Delete experimental custom Triton autotuner from torchao/kernel", merged 2026-08-07), which deleted torchao/kernel/intmm_triton.py (and intmm.py) entirely — the buggy scaled_matmul_kernel_with_block_pointers/EVEN_K code no longer exists on main.

The current int_scaled_matmul implementation (torchao/quantization/quantize_/workflows/int8/kernels.py) uses torch._int_mm/safe_int_mm instead of a hand-rolled Triton kernel, so this bug class doesn't apply to it.

Confirmed by checking out main and searching for intmm_triton.py / scaled_matmul_kernel_with_block_pointers — neither exists anymore. Feel free to close, unless there's a reason to keep this open for tracking against the deleted file's history.

🤖 Generated with Claude Code

amanyagami avatar Aug 19 '26 20:08 amanyagami