Repository navigation
[Triton/Gluon] Fix ragged-K mask in batched A16WFP4 GEMM - #4181
Merged
Merged
Conversation
Contributor
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
|
valarLip
previously approved these changes
Jul 10, 2026
Fixing the activation mask alone left the MI35X Triton shard red on the regression shape this branch adds: B=16 M=32 N=512 K=192 fails in 7 of the 8 layout/dtype combinations, with NaN rather than a tolerance miss, and always first at batch index 15. K in this kernel is the packed FP4 weight dimension, so logical K=192 gives K=96 and BLOCK_SIZE_K is rounded up to next_power_of_2(2*96) = 256. w_scales holds 192/32 = 6 e8m0 groups per N row, but the unmasked load reads BLOCK_SIZE_K/32 = 8 of them, so groups 6 and 7 come from outside the tensor. The matching activation and weight elements are masked to zero, which would be harmless for a finite scale -- but 0xFF is NaN in e8m0 and tl.dot_scaled propagates it through the zeroed data. Batches 0-14 survive because their OOB groups alias a neighbouring batch's scales; batch 15 reads past the end of the allocation, so wherever that memory happens to hold 0xFF the output column turns NaN, which is why only the last batch and only a few of the 512 N columns are wrong. Mask the scale load and substitute 127, a scale of 2^0. This is what the non-batched gemm_afp4wfp4 kernel already does, and it documents the same NaN-through-zero reason for doing it. Only the EVEN_K == False path changes. K=128 and K=512, the two shapes already covered by the test file, both resolve to EVEN_K == True and keep the unmasked load, so no tuned shape moves. Signed-off-by: Mikko Tukiainen <[email protected]>
Contributor
Author
|
I found a problem with the previous arithmetics that the Triton tests successfully flagged. Fixed those and rerun GSM8K: still showing strict 0.9454, flexible 0.9469. |
valarLip
approved these changes
Sep 8, 2026
jhu960213
added a commit
to jhu960213/vllm
that referenced
this pull request
Sep 8, 2026
ROCm/aiter#4181 fixes the ragged-K mask that made GLM-5.2 (K=192) return garbage, which is what both workarounds were for. Requires an aiter containing it; older ones return to garbage at non-power-of-two K. Signed-off-by: Jack Hu <[email protected]>
lijinpei-amd
added a commit
to lijinpei-amd/aiter
that referenced
this pull request
Sep 9, 2026
The kernel-side fix for the two OOB reads on the EVEN_K == False path landed separately in ROCm#4181, so only the guard and the regression coverage remain here. Add test_batched_gemm_a16wfp4_odd_k_tail_masks, which poisons the bytes past w_scales with 0xFF (e8m0 NaN) and forces BLOCK_SIZE_K such that EVEN_K is false, so an unmasked b_scales load or an A mask bounded by K instead of 2*K produces a wrong result rather than passing silently. Also assert in the wrapper that the unpacked K is a multiple of 32, which the mxfp4 scale layout assumes. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
lijinpei-amd
added a commit
to lijinpei-amd/aiter
that referenced
this pull request
Sep 9, 2026
The kernel-side fix for the two OOB reads on the EVEN_K == False path landed separately in ROCm#4181, so only the guards and the regression coverage remain here. Add test_batched_gemm_a16wfp4_odd_k_tail_masks, which poisons the bytes past w_scales with 0xFF (e8m0 NaN) and forces BLOCK_SIZE_K such that EVEN_K is false, so an unmasked b_scales load or an A mask bounded by K instead of 2*K produces a wrong result rather than passing silently. In the wrapper, x's K was read and then immediately shadowed by w's, so nothing checked that the two operands agreed on K. Name them apart, assert x.K == 2 * w.K explicitly, and hang the mxfp4 multiple-of-32 scale check off the unpacked K rather than off an implicit contract. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
lijinpei-amd
added a commit
to lijinpei-amd/aiter
that referenced
this pull request
Sep 9, 2026
The kernel-side fix for the two OOB reads on the EVEN_K == False path landed separately in ROCm#4181, so only the guards and the regression coverage remain here. Add test_batched_gemm_a16wfp4_odd_k_tail_masks, which poisons the bytes past w_scales with 0xFF (e8m0 NaN) and forces BLOCK_SIZE_K such that EVEN_K is false, so an unmasked b_scales load or an A mask bounded by K instead of 2*K produces a wrong result rather than passing silently. Split-K is pinned off in the local config rather than asserted on, so a future re-tune of these shapes cannot quietly move the test off the path it is meant to cover. In the wrapper, x's K was read and then immediately shadowed by w's, so nothing checked that the two operands agreed on K. Name them apart and assert x.K == 2 * w.K explicitly. Hang the mxfp4 multiple-of-32 check off the unpacked K rather than off an implicit contract, and check the w_scales shape too: the kernel bounds those loads from K rather than from the tensor, so an undersized scale tensor reads out of bounds instead of failing. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
lijinpei-amd
added a commit
to lijinpei-amd/aiter
that referenced
this pull request
Sep 9, 2026
The kernel-side fix for the two OOB reads on the EVEN_K == False path landed separately in ROCm#4181, so only the guards and the regression coverage remain here. Add test_batched_gemm_a16wfp4_odd_k_tail_masks, which poisons the bytes past w_scales with 0xFF (e8m0 NaN) and forces BLOCK_SIZE_K such that EVEN_K is false, so an unmasked b_scales load or an A mask bounded by K instead of 2*K produces a wrong result rather than passing silently. Split-K is pinned off in the local config rather than asserted on, so a future re-tune of these shapes cannot quietly move the test off the path it is meant to cover. In the wrapper, x's K was read and then immediately shadowed by w's, so nothing checked that the two operands agreed on K. Name them apart and assert x.K == 2 * w.K explicitly. Hang the mxfp4 multiple-of-32 check off the unpacked K rather than off an implicit contract, and check the w_scales shape too: the kernel bounds those loads from K rather than from the tensor, so an undersized scale tensor reads out of bounds instead of failing. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
lijinpei-amd
added a commit
to lijinpei-amd/aiter
that referenced
this pull request
Sep 9, 2026
The kernel-side fix for the two OOB reads on the EVEN_K == False path landed separately in ROCm#4181, so only the guards and the regression coverage remain here. Add test_batched_gemm_a16wfp4_odd_k_tail_masks, which poisons the bytes past w_scales with 0xFF (e8m0 NaN) and forces BLOCK_SIZE_K such that EVEN_K is false, so an unmasked b_scales load or an A mask bounded by K instead of 2*K produces a wrong result rather than passing silently. Split-K is pinned off in the local config rather than asserted on, so a future re-tune of these shapes cannot quietly move the test off the path it is meant to cover. In the wrapper, x's K was read and then immediately shadowed by w's, so nothing checked that the two operands agreed on K. Name them apart and assert x.K == 2 * w.K explicitly. Hang the mxfp4 multiple-of-32 check off the unpacked K rather than off an implicit contract, and check the w_scales shape too: the kernel bounds those loads from K rather than from the tensor, so an undersized scale tensor reads out of bounds instead of failing. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
lijinpei-amd
added a commit
to lijinpei-amd/aiter
that referenced
this pull request
Sep 9, 2026
The kernel-side fixes for the EVEN_K == False path landed separately in ROCm#4181, so only the guards and the regression coverage remain here. Add test_batched_gemm_a16wfp4_odd_k_tail_masks, covering both bugs that path had, which were not the same kind. A's mask compared unpacked offsets against the packed K, so it was too tight and silently dropped valid activations. The w_scales load was unmasked and genuinely read off the end of the scale tensor; the test poisons those bytes with 0xFF, which is NaN in e8m0 and which dot_scaled propagates even where the masked-off data is zero. BLOCK_SIZE_K is forced so EVEN_K is false, and split-K is pinned off in the local config rather than asserted on, so a future re-tune of these shapes cannot quietly move the test off the path it is meant to cover. In the wrapper, x's K was read and then immediately shadowed by w's, so nothing checked that the two operands agreed on K. Name them apart and assert x.K == 2 * w.K explicitly. Hang the mxfp4 multiple-of-32 check off the unpacked K rather than off an implicit contract, and check the w_scales shape too: the kernel bounds those loads from K rather than from the tensor, so an undersized scale tensor reads out of bounds instead of failing. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
nsusanto
pushed a commit
to lijinpei-amd/aiter
that referenced
this pull request
Sep 30, 2026
The kernel-side fixes for the EVEN_K == False path landed separately in ROCm#4181, so only the guards and the regression coverage remain here. Add test_batched_gemm_a16wfp4_odd_k_tail_masks, covering both bugs that path had, which were not the same kind. A's mask compared unpacked offsets against the packed K, so it was too tight and silently dropped valid activations. The w_scales load was unmasked and genuinely read off the end of the scale tensor; the test poisons those bytes with 0xFF, which is NaN in e8m0 and which dot_scaled propagates even where the masked-off data is zero. BLOCK_SIZE_K is forced so EVEN_K is false, and split-K is pinned off in the local config rather than asserted on, so a future re-tune of these shapes cannot quietly move the test off the path it is meant to cover. In the wrapper, x's K was read and then immediately shadowed by w's, so nothing checked that the two operands agreed on K. Name them apart and assert x.K == 2 * w.K explicitly. Hang the mxfp4 multiple-of-32 check off the unpacked K rather than off an implicit contract, and check the w_scales shape too: the kernel bounds those loads from K rather than from the tensor, so an undersized scale tensor reads out of bounds instead of failing. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
nsusanto
pushed a commit
to lijinpei-amd/aiter
that referenced
this pull request
Oct 1, 2026
The kernel-side fixes for the EVEN_K == False path landed separately in ROCm#4181, so only the guards and the regression coverage remain here. Add test_batched_gemm_a16wfp4_odd_k_tail_masks, covering both bugs that path had, which were not the same kind. A's mask compared unpacked offsets against the packed K, so it was too tight and silently dropped valid activations. The w_scales load was unmasked and genuinely read off the end of the scale tensor; the test poisons those bytes with 0xFF, which is NaN in e8m0 and which dot_scaled propagates even where the masked-off data is zero. BLOCK_SIZE_K is forced so EVEN_K is false, and split-K is pinned off in the local config rather than asserted on, so a future re-tune of these shapes cannot quietly move the test off the path it is meant to cover. In the wrapper, x's K was read and then immediately shadowed by w's, so nothing checked that the two operands agreed on K. Name them apart and assert x.K == 2 * w.K explicitly. Hang the mxfp4 multiple-of-32 check off the unpacked K rather than off an implicit contract, and check the w_scales shape too: the kernel bounds those loads from K rather than from the tensor, so an undersized scale tensor reads out of bounds instead of failing. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
lijinpei-amd
added a commit
to lijinpei-amd/aiter
that referenced
this pull request
Oct 14, 2026
The kernel-side fixes for the EVEN_K == False path landed separately in ROCm#4181, so only the guards and the regression coverage remain here. The coverage goes into the existing test rather than a test of its own. ROCm#4181 fixed two bugs, which were not the same kind. A's mask compared unpacked offsets against the packed K, so it was too tight and silently dropped valid activations. The w_scales load was unmasked and genuinely read off the end of the scale tensor. The existing K=192 shape only caught the second one by luck, whenever the bytes past the allocation happened to be 0xFF. generate_batched_gemm_a16wfp4_inputs now places w_scales at the head of a 0xFF-filled buffer, which is NaN in e8m0 and which dot_scaled propagates even where the masked-off data is zero, so every shape in the file fails loudly on an out-of-bounds scale read. K=192 also hits the tail in the first and only K iteration. Add two K=1120 shapes, which tuned configs run as four full iterations and a ragged fifth. 1120 is 35 * 32, so it leaves a tail under any BLOCK_SIZE_K >= 64, and no config override is needed to keep a re-tune from moving these shapes onto EVEN_K == True. Re-injecting either ROCm#4181 bug fails all 24 ragged-K cases. In the wrapper, x's K was read and then immediately shadowed by w's, so nothing checked that the two operands agreed on K. Name them apart and assert x.K == 2 * w.K explicitly. Hang the mxfp4 multiple-of-32 check off the unpacked K rather than off an implicit contract, and check the w_scales shape too: the kernel bounds those loads from K rather than from the tensor, so an undersized scale tensor reads out of bounds instead of failing. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
azaidy
pushed a commit
that referenced
this pull request
Oct 16, 2026
The kernel-side fixes for the EVEN_K == False path landed separately in #4181, so only the guards and the regression coverage remain here. The coverage goes into the existing test rather than a test of its own. #4181 fixed two bugs, which were not the same kind. A's mask compared unpacked offsets against the packed K, so it was too tight and silently dropped valid activations. The w_scales load was unmasked and genuinely read off the end of the scale tensor. The existing K=192 shape only caught the second one by luck, whenever the bytes past the allocation happened to be 0xFF. generate_batched_gemm_a16wfp4_inputs now places w_scales at the head of a 0xFF-filled buffer, which is NaN in e8m0 and which dot_scaled propagates even where the masked-off data is zero, so every shape in the file fails loudly on an out-of-bounds scale read. K=192 also hits the tail in the first and only K iteration. Add two K=1120 shapes, which tuned configs run as four full iterations and a ragged fifth. 1120 is 35 * 32, so it leaves a tail under any BLOCK_SIZE_K >= 64, and no config override is needed to keep a re-tune from moving these shapes onto EVEN_K == True. Re-injecting either #4181 bug fails all 24 ragged-K cases. In the wrapper, x's K was read and then immediately shadowed by w's, so nothing checked that the two operands agreed on K. Name them apart and assert x.K == 2 * w.K explicitly. Hang the mxfp4 multiple-of-32 check off the unpacked K rather than off an implicit contract, and check the w_scales shape too: the kernel bounds those loads from K rather than from the tensor, so an undersized scale tensor reads out of bounds instead of failing.
4 of 6 tasks
3 of 6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
batched_gemm_a16wfp4produces incorrect results for ragged logical-K shapes where the FP4 packed weight K dimension is not aligned with the kernel tile assumptions. This was observed in vLLM servingamd/GLM-5.2-MXFP4, where MLA decode uses a batched FP4 BMM with logicalK=192; the outputs of this model are garbage. After this fix, the same vLLM configuration recovers expected GSM8K accuracy.Technical Details
In
batched_gemm_a16wfp4, the kernel argumentKrepresents the packed FP4 weight dimension (logical_K / 2). The BF16 activation tensor still has logical K elements.The ragged-K load mask for the BF16 activation currently uses:
aiter/aiter/ops/triton/_triton_kernels/gemm/batched/batched_gemm_a16wfp4.py
Line 192 in 3910ab3
This incorrectly masks against the packed K dimension and can zero valid activation elements. The non-batched
gemm_a16wfp4kernel already uses the correct mask (notice the scaling factor2 * K):aiter/aiter/ops/triton/_triton_kernels/gemm/basic/gemm_a16wfp4.py
Line 149 in 3910ab3
This PR applies the same logical-K mask to the batched kernel.
Fixing the activation mask alone is not enough on the GLM regression shape.
K=96(packed) roundsBLOCK_SIZE_Kup to 256, so the kernel loadsBLOCK_SIZE_K / 32 = 8e8m0 scale groups whilew_scalesonly holds192 / 32 = 6. Groups 6 and 7 are out of bounds. The matching activation/weight elements are already masked to zero, which would be harmless for a finite scale, but0xFFis NaN in e8m0 andtl.dot_scaledpropagates it through the zeros. The non-batchedgemm_afp4wfp4kernel already masks those loads with fill value 127 (2^0). This PR does the same on theEVEN_K == Falsepath:Pre-existing test shapes
K=128andK=512stay onEVEN_K == Trueand keep the unmasked scale load.This PR also adds a regression shape matching the GLM MLA case in
op_tests/triton_tests/gemm/batched/test_batched_gemm_a16wfp4.py: (B=16, M=32, N=512, K=192).Test Plan
Added a regression case to
op_tests/triton_tests/gemm/batched/test_batched_gemm_a16wfp4.py.Attaching also my manual reproducer used during debugging, which compares 1) BF16 reference, 2) weight-only MXFP4 quant/dequant reference, 3) AITER batched_gemm_a16wfp4 output
amd/GLM-5.2-MXFP4Test Result
Manual reproducer before fix, for X=(16, 32, 192) and W=(16, 512, 192):
After the activation-mask fix:
The GLM regression shape then failed Triton CI with NaNs until the OOB scale mask above was added. Reran GSM8K after that change.
vLLM lm-eval before the fix:
vLLM lm-eval after the fix (activation mask + scale mask):
Submission Checklist
Co-authored by: @amd-sriram & Cursor