Skip to content

[Triton/Gluon] Fix ragged-K mask in batched A16WFP4 GEMM - #4181

Merged
valarLip merged 8 commits into
ROCm:mainfrom
mjkvaak-amd:fix-batched-fp4-bmm-ragged-k
Sep 8, 2026
Merged

valarLip merged 8 commits into
ROCm:mainfrom
mjkvaak-amd:fix-batched-fp4-bmm-ragged-k

Conversation

@mjkvaak-amd

@mjkvaak-amd mjkvaak-amd commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

batched_gemm_a16wfp4 produces 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 serving amd/GLM-5.2-MXFP4, where MLA decode uses a batched FP4 BMM with logical K=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 argument K represents 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:

a_ptrs, mask=offs_k_bf16[None, :] < K - k * BLOCK_SIZE_K, other=0

This incorrectly masks against the packed K dimension and can zero valid activation elements. The non-batched gemm_a16wfp4 kernel already uses the correct mask (notice the scaling factor 2 * K):
mask=offs_k_bf16[None, :] < 2 * K - k * BLOCK_SIZE_K,

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) rounds BLOCK_SIZE_K up to 256, so the kernel loads BLOCK_SIZE_K / 32 = 8 e8m0 scale groups while w_scales only holds 192 / 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, but 0xFF is NaN in e8m0 and tl.dot_scaled propagates it through the zeros. The non-batched gemm_afp4wfp4 kernel already masks those loads with fill value 127 (2^0). This PR does the same on the EVEN_K == False path:

scale_mask = offs_scale_k[None, :] < (
    2 * K // SCALE_GROUP_SIZE - k * (BLOCK_SIZE_K // SCALE_GROUP_SIZE)
)
b_scales = tl.load(b_scale_ptrs, mask=scale_mask, other=127)

Pre-existing test shapes K=128 and K=512 stay on EVEN_K == True and 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

python repro_mla_bmm_quant_error.py --path fp4 --k-dim 192 --n-out 512
  • Verifying that the fix returns the expected accuracy in the downstream vLLM serving of amd/GLM-5.2-MXFP4

Test Result

Manual reproducer before fix, for X=(16, 32, 192) and W=(16, 512, 192):

weight_only_ref  rmse=1.54415
aiter_out        rmse=9.85597
aiter-vs-wonly   rmse=9.74326

After the activation-mask fix:

weight_only_ref  rmse=1.54415
aiter_out        rmse=2.18604
aiter-vs-wonly   rmse=1.54492

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:

gsm8k flexible-extract exact_match: 0.0 ± 0.0
gsm8k strict-match     exact_match: 0.0 ± 0.0

vLLM lm-eval after the fix (activation mask + scale mask):

gsm8k flexible-extract exact_match: 0.9469 ± 0.0062
gsm8k strict-match     exact_match: 0.9454 ± 0.0063

Submission Checklist


Co-authored by: @amd-sriram & Cursor

@mjkvaak-amd
mjkvaak-amd requested a review from a team July 10, 2026 09:22
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 4181 --add-label <label>

valarLip
valarLip previously approved these changes Jul 10, 2026
@zufayu
zufayu requested a review from amd-ruitang3 July 13, 2026 01:37
@mjkvaak-amd
mjkvaak-amd requested a review from valarLip July 14, 2026 19:59
@valarLip
valarLip requested a review from k50112113 July 17, 2026 04:12
@github-actions github-actions Bot changed the title Fix ragged-K mask in batched A16WFP4 GEMM [Triton/Gluon] Fix ragged-K mask in batched A16WFP4 GEMM Aug 27, 2026
mjkvaak-amd and others added 2 commits September 2, 2026 05:32
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]>
@mjkvaak-amd

Copy link
Copy Markdown
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.

@mjkvaak-amd
mjkvaak-amd requested a review from valarLip September 7, 2026 11:07
@valarLip
valarLip merged commit 9b41047 into ROCm:main Sep 8, 2026
71 checks passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants