Repository navigation
[Triton/Gluon] Mask OOB e8m0 scales on the ragged-K path of batched_gemm_afp4wfp4 - #7047
Conversation
…p4wfp4 Signed-off-by: Martin Domanský <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
There was a problem hiding this comment.
🟡 Changes recommended
The test’s inline configuration violates the no-configs-in-tests rule and bypasses the wrapper’s architecture-specific JSON configuration loader.
1 open issue
What changed in this PR
This PR prevents out-of-bounds e8m0 scale reads in Triton batched FP4 GEMM when the final K block is partial. It applies the same masking approach used for batched A16WFP4 in #4181.
Changes:
- Mask both scale loads on the ragged-K path, using 127 (scale 1) for invalid groups.
- Keep scale loads unchanged on the aligned-K path.
- Add regression tests with trailing
0xFFbytes for each scale tensor and an aligned-K control.
| File | Description |
|---|---|
op_tests/triton_tests/gemm/batched/test_batched_gemm_afp4wfp4.py |
Adds controlled scale-tail tests for partial and aligned K blocks. |
aiter/ops/triton/_triton_kernels/gemm/batched/batched_gemm_afp4wfp4.py |
Masks invalid scale groups to prevent NaN propagation. |
Review effort: Balanced
Tests use the config that the wrapper loads. The gfx950 and gfx1250 tables give BLOCK_SIZE_K 256 for M = 32, so K = 2112 still takes the EVEN_K == False path and K = 2048 the EVEN_K == True one. Signed-off-by: Martin Domanský <[email protected]>
NOT READY FOR HUMAN REVIEWPR Self-ReviewRevision Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision. Questions to answer in the PR description
Edit the Missing or invalid answers
Shadow mode reports what would block but keeps the status successful. Set |
Pick up #7047 (mask OOB e8m0 scales on the ragged-K path of batched_gemm_afp4wfp4), which fixes the MI35X Shard 5 NaNs in test_batched_gemm_afp4wfp4. Co-authored-by: Cursor <[email protected]>
…emm_afp4wfp4 (#7047) * [Triton] Mask OOB e8m0 scales on the ragged-K path of batched_gemm_afp4wfp4 Signed-off-by: Martin Domanský <[email protected]> * [Triton] Use the wrapper config in the ragged-K scale test Tests use the config that the wrapper loads. The gfx950 and gfx1250 tables give BLOCK_SIZE_K 256 for M = 32, so K = 2112 still takes the EVEN_K == False path and K = 2048 the EVEN_K == True one. Signed-off-by: Martin Domanský <[email protected]> --------- Signed-off-by: Martin Domanský <[email protected]>
Resolve the batched_gemm_afp4wfp4 kernel and test conflicts to main's version. The same ragged-K scale-mask fix landed on main in #7047, so the commits merged in from erizheng-fix-batched-afp4wfp4-scale-oob (#7236, closed) are no longer needed. Co-authored-by: Cursor <[email protected]>
…emm_afp4wfp4 (ROCm#7047) * [Triton] Mask OOB e8m0 scales on the ragged-K path of batched_gemm_afp4wfp4 Signed-off-by: Martin Domanský <[email protected]> * [Triton] Use the wrapper config in the ragged-K scale test Tests use the config that the wrapper loads. The gfx950 and gfx1250 tables give BLOCK_SIZE_K 256 for M = 32, so K = 2112 still takes the EVEN_K == False path and K = 2048 the EVEN_K == True one. Signed-off-by: Martin Domanský <[email protected]> --------- Signed-off-by: Martin Domanský <[email protected]>

Motivation
_batched_gemm_afp4_wfp4_kernelloadsa_scalesandb_scaleswithout a mask, also whenEVEN_Kis false. When K is not a multiple ofBLOCK_SIZE_K, the last K block reads scale groups pastK // 32. The values of that block are masked to zero, but a byte of 0xFF there is the e8m0 NaN andtl.dot_scaledpropagates it, so output rows or columns turn NaN depending on what lies after the scale tensors. #4181 fixed the same defect inbatched_gemm_a16wfp4.Seen in
test_batched_gemm_afp4wfp4.pyon gfx1101, shapeB=2, M=256, N=3584, K=2112. In three full runs of the module, with two different configs, 2 to 3 of its 8 cases failed with NaN in the output, 3584 to 67329 of 1835008 elements mismatched, and the layouts that failed changed from run to run. The other cases passed or failed on the default tolerance or out of memory.B=5, M=4864, N=8192, K=4160failed with NaN the same way in two of the runs.Technical Details
aiter/ops/triton/_triton_kernels/gemm/batched/batched_gemm_afp4wfp4.py: on theEVEN_K == Falsepath the scale loads usemask=offs_scale_k[None, :] < (2 * K // SCALE_GROUP_SIZE - k * (BLOCK_SIZE_K // SCALE_GROUP_SIZE))withother=127, a scale of 2^0, as [Triton/Gluon] Fix ragged-K mask in batched A16WFP4 GEMM #4181 does forbatched_gemm_a16wfp4and asgemm_afp4wfp4already does. TheEVEN_K == Truepath keeps the unmasked load, so shapes that resolve to it do not change.op_tests/triton_tests/gemm/batched/test_batched_gemm_afp4wfp4.py:test_batched_gemm_afp4_wfp4_ragged_k_scalesplaces each scale tensor in turn in front of a full block of 0xFF bytes and runs K = 2112, with a partial last block, and K = 2048 with an explicit config ofBLOCK_SIZE_K256. The result no longer depends on what the allocator left after the tensor.Test Plan
gfx1101 (Radeon RX 7800 XT), Windows 11, TheRock ROCm 10.2 (
torch 2.15.0a0+rocm10.2.0a20260926), triton-windows 3.8.0.post29,AITER_TRITON_ONLY=1, aitermain@44b4fea6with this change. On upstreammainthe batched FP4 tests skip on gfx1101, so the run added locally the gfx1101 gate and test tolerance proposed in #6685 and gfx1101 configs; these are not part of this PR.On Windows the tests ran through a local pytest wrapper. It does not modify kernels, configs or test code. It:
aiter.ops.enumwith an empty module, somodule_aiter_coreis not JIT-built, which needs Ninja and hipcc;chip_info._detect_native()return the arch fromtorch.cuda.get_device_properties(0).gcnArchName;aiter.dtypes, whichaiter/__init__.pyleaves unbound underAITER_TRITON_ONLY;test_batched_gemm_afp4_wfp4_ragged_k_scaleswithout and with the kernel change.test_batched_gemm_afp4wfp4.py, full module, with the kernel change.Test Result
test_batched_gemm_afp4_wfp4_ragged_k_scales:x_scalesw_scalesx_scalesw_scalestest_batched_gemm_afp4wfp4.pywith the change: 2603 passed, 65 failed. All 65 are out of memory in the test's own PyTorch code (input generation, the reference orassert_close), with 5.08 to 14.23 GiB already allocated on the 16 GiB card; no case failed on a mismatch or NaN. Out of memory also hit one of the 8 cases at K = 2112 and two of the 8 at K = 4160, so 7 and 6 of them ran.Not tested on gfx950 or gfx1250, where
tl.dot_scaledis native. The unmasked load and the mask do not depend on the arch.Not included
_gemm_a16wfp4_kernelloadsb_scalesthe same way (aiter/ops/triton/_triton_kernels/gemm/basic/gemm_a16wfp4.py:140); [triton][gemm] gemm_a16wfp4: mask the b_scales load when EVEN_K is false #4689 (open) masks it. On gfx1101 the same kind of control turned every output column NaN at K = 4352 withBLOCK_SIZE_K512.aiter/ops/triton/_triton_kernels/gemm/fused/fused_gemm_afp4wfp4_a16w16.py:192-193loadsa_scaleandb_scalewithout a mask as well; not tested.Related
batched_gemm_a16wfp4.gemm_a16wfp4.arch_info.is_sw_emulated_fp4_avail()for Triton MXFP4 on gfx11 GPUs without native FP4 #6685 proposes enabling the FP4 GEMMs on software-emulated FP4.