FBGEMM icon indicating copy to clipboard operation
FBGEMM copied to clipboard

matmul_fp8_block: wrong result when a tile spans multiple scale blocks

Open jeffchen006 opened this issue 2 weeks ago • 2 comments

Describe the bug

matmul_fp8_block(..., 128, 128, 128) can select a matrix tile wider than a scale block. A tile is the matrix region processed by one kernel program. The public call accepts these block sizes, which the library's blockwise benchmark also uses. When BLOCK_M=256, one tile covers two row-scale blocks but applies only the first block's scale.

For the script's 512-by-2048 output, the retained (256, 64, 32) tile gives 524,288 wrong elements. Rows 128 through 255 contain 256 instead of 512. Rows 384 through 511 contain 1024 instead of 2048. The column scales alternate between 1 and 2, so a tile with BLOCK_N=256, such as (64, 256, 32), also gives 524,288 wrong elements. The script prints the selected tile. The selected tile depends on autotuning and its cache.

The fast-accumulation kernel and slow-accumulation kernel compute one scale index per tile. The pruner and autotune key do not enforce row/column scale boundaries or include scale-block sizes in the cache key. Kernel assert statements do not enforce this with debug mode disabled. The linked lines are unchanged on main as of 2026-11-17. Related: #6159 and #6160 concern the last K-scale block, a separate condition.

Reproduce

import torch
from fbgemm_gpu.experimental.gemm.triton_gemm.fp8_gemm import (
    _kernel_matmul_fp8_block_fastacc, matmul_fp8_block,
)

dev = "cuda" if torch.cuda.is_available() else "cpu"
torch.set_default_device(dev)
torch.manual_seed(0)
m, n, k = 512, 2048, 256
a = torch.ones(m, k).to(torch.float8_e4m3fn)
b = torch.ones(n, k).to(torch.float8_e4m3fn)
a_scale = torch.tensor([[1., 1.], [2., 2.], [4., 4.], [8., 8.]])
# Alternate the column-block scales so wide BLOCK_N tiles also show the defect.
b_scale = torch.tensor([1., 2.]).repeat(n // 256)[:, None].expand(n // 128, k // 128).contiguous()
observed = matmul_fp8_block(a, b, a_scale, b_scale, 128, 128, 128,
                            dot_out_dtype=torch.float32)
a_ref = a.float() * a_scale.repeat_interleave(128, 0).repeat_interleave(128, 1)
b_ref = b.float() * b_scale.repeat_interleave(128, 0).repeat_interleave(128, 1)
expected = a_ref @ b_ref.T
cfg = _kernel_matmul_fp8_block_fastacc.best_config.kwargs
print("selected tile (BLOCK_M, BLOCK_N, BLOCK_K):", (cfg["BLOCK_M"], cfg["BLOCK_N"], cfg["BLOCK_K"]))
rows = [0, 127, 128, 255, 256, 383, 384, 511]
print("sample rows:", rows)
print("observed first column:", observed[rows, 0].tolist())
print("expected first column:", expected[rows, 0].tolist())
print("wrong output elements:", (observed != expected).sum().item())
assert torch.equal(observed, expected), "a matmul tile spans two different scale blocks"

Output:

TMA benchmarks will be running with experimental grid constant TMA descriptor.
selected tile (BLOCK_M, BLOCK_N, BLOCK_K): (256, 64, 32)
sample rows: [0, 127, 128, 255, 256, 383, 384, 511]
observed first column: [256.0, 256.0, 256.0, 256.0, 1024.0, 1024.0, 1024.0, 1024.0]
expected first column: [256.0, 256.0, 512.0, 512.0, 1024.0, 1024.0, 2048.0, 2048.0]
wrong output elements: 524288
AssertionError: a matmul tile spans two different scale blocks

Expected behavior

Match the PyTorch matrix product after expanding and applying each input's block scales. The four 128-row regions must contain 256, 512, 1024, and 2048, respectively. All autotune candidates must preserve those scale boundaries.

Suggested fix

Prune tiles unless each scale-block dimension is divisible by the corresponding tile dimension. Apply the rule to both accumulation kernels. Include all three scale-block sizes in each autotune key.

Versions / environment

FBGEMM commit adec0b0 (2026-11-17). Checked against main on 2026-11-17. torch 2.14.1, triton 3.8.0. Reproduced with Triton's interpreter mode (TRITON_INTERPRET=1). The scale-index arithmetic is identical on CUDA when the same tile is selected. The interpreter run selects the retained (BLOCK_M, BLOCK_N, BLOCK_K) = (256, 64, 32) candidate. CUDA autotuning chooses its tile from measured timings. Use normal compilation with TRITON_DEBUG unset.

jeffchen006 avatar Nov 17 '26 23:11 jeffchen006

I reproduced this on an RTX 4090 Laptop GPU in both accumulation modes. The two 256-wide tiles from the report each gave 524,288 wrong values out of 1,048,576. The candidate fix indexes scales by row or column when a tile crosses a scale boundary and includes the scale-block sizes in the autotune key. Both tiles then matched an FP32 reference on the power-of-two inputs, including when switching scale geometry at the same matrix shape.

One tradeoff showed up in the generated code. A fixed 64 × 128 × 32 fast-accumulation tile kept the same register count and zero spills with aligned 128 × 128 × 128 scales, but spilled with 47 × 65 × 64 scales. The patch keeps the scalar load for aligned tiles. I haven't measured H100 latency.

ZenAlexa avatar Nov 19 '26 18:11 ZenAlexa

Thanks for reproducing this in both accumulation modes and for checking what happens when the scale geometry changes. I appreciate you calling out the register spills for the unaligned case too.

jeffchen006 avatar Nov 20 '26 13:11 jeffchen006