Repository navigation
[Triton/Gluon] Fix a16wfp4 GEMM NaN flake: mask the b_scales K-tail - #8768
ganyi1996ppo wants to merge 7 commits into
Conversation
The _gemm_a16wfp4_kernel loaded b_scales with an unmasked tl.load. When K (scale count) is not a multiple of the per-iteration scale count, the last split-K iteration reads e8m0 scales out of bounds. An OOB byte can be 0xFF (NaN in e8m0) and propagates through tl.dot_scaled even though the matching data is masked to zero (0 * NaN = NaN), so the whole output tile becomes NaN. Because the OOB bytes are whatever happens to sit past the scale tensor, the failure is intermittent: it hits test_gemm_a16wfp4[...-16-4096-11008] (K=11008, BLOCK_SIZE_K=512, EVEN_K False) only when that memory holds a NaN encoding. Mask the b_scales load the same way the sibling gemm_afp4wfp4 kernel already does (mask=scale_mask, other=127 = e8m0 1.0). EVEN_K tiles are unchanged. Co-Authored-By: Claude Opus 4.8 <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
WAITING ON AUTHORPR 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 |
There was a problem hiding this comment.
🟡 Changes recommended
The basic GEMM tests do not reliably catch tail-mask removal and need a ragged-K regression with a 0xFF-filled backing-buffer tail that checks finite, reference-matching output.
1 open issue
What changed in this PR
This PR addresses intermittent NaNs in AITER’s Triton A16WFP4 GEMM by preventing scale reads beyond the valid K range.
Changes:
- Masks scale loads on ragged K tails and fills masked entries with
127, the e8m0 encoding of1.0. - Leaves aligned-K scale loads unchanged and explains why tail scales need masking.
| File | Description |
|---|---|
aiter/ops/triton/_triton_kernels/gemm/basic/gemm_a16wfp4.py |
Adds K-tail scale masking and documents the NaN-safe fill value. |
Review effort: Balanced
The basic gemm_a16wfp4 test lays scale rows back to back, so an unmasked K-tail reads the next row's valid scales and the NaN flake stays hidden. Add a regression on the CI flake shape (16, 4096, 11008) that puts w_scales at the head of a buffer whose per-row K tail is 0xFF (NaN in e8m0). A ragged K tile then reads that poisoned tail, so dropping the b_scales mask turns the output NaN here instead of depending on allocator contents. The test asserts a finite output and agreement with the torch reference. Co-Authored-By: Claude Opus 4.8 <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
The test docstring incorrectly identifies K rather than N as contiguous after the transpose and should be shortened to describe the poisoned K-tail and result checks.
1 open issue
1 resolved since last review
Review effort: Balanced
Drop the incorrect next-row explanation: after the transpose the scale buffer is N-contiguous, so an over-read in the ordinary generator runs off the buffer end, not into a next row. Keep the docstring to the poisoned K-tail and the result checks. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Self-review section is filled in the PR description and the `### Reviewed revision` is updated to 35dea98 (current head). Behavior/intent, validation, the PR-specific boundary and the changed op test are all answered there. |
Pick up ROCm#8790's regenerated MoE-dispatch golden (intermediate_pad field from ROCm#7611) so the shard-2 'test_legacy_dispatch_matches_golden_snapshot' gate clears. This branch only touches the a16wfp4 Triton path, disjoint from the golden. Co-Authored-By: Claude Opus 4.8 <[email protected]>
2 * K / 32 = 688 for K=11008; the 344 real scale columns are K / 32. Comment-only fix per review feedback. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Claude <[email protected]>
There was a problem hiding this comment.
🔵 Needs a closer look
Human verification of head-matched gfx950 poisoned-tail regression results and base/head latency is needed because GPU execution is unavailable in this review environment.
No open issues 🎉
1 resolved since last review
Review effort: Balanced


Motivation
op_tests/triton_tests/gemm/basic/test_gemm_a16wfp4.py::test_gemm_a16wfp4[...-16-4096-11008]fails intermittently in the
Triton Tests (MI35X)CI job with a NaN /assert_closemismatch. It is seeded (
torch.manual_seed(5)), so the inputs are fixed — theintermittency comes from the kernel, not the data. The same shape passes most runs and
fails some, and it has repeatedly blocked unrelated PRs.
Technical details
Root cause: a genuine kernel bug (missing mask on the scale tail).
In
_gemm_a16wfp4_kerneltheb_scaleswere loaded with an unmaskedtl.load:For this shape the wrapper sets
K = w.shape[1] = 5504(packed half-dim) andBLOCK_SIZE_K = 512, sonum_k_iter = cdiv(5504, 256) = 22and the loop reads22 * 16 = 352scale columns while only2*K/32 = 344exist. The last iteration reads8 e8m0 scale columns out of bounds. The data tiles (
a,b) are already masked tozero on this tail, but an OOB scale byte can be
0xFF, which is NaN in e8m0, andtl.dot_scaledpropagates it even though the data is zero (0 * NaN = NaN). The wholeMxNoutput tile then becomes NaN. Because the OOB bytes are whatever happens to sit pastthe scale tensor, the failure only shows up when that memory holds a NaN encoding — hence
the flake.
EVEN_Kshapes are K-aligned and never hit the tail.The fix mirrors what the sibling
_gemm_afp4wfp4_kernelalready does (same file family):mask the scale load on the ragged K-tail with
other=127(e8m01.0).EVEN_Ktiles areunchanged.
Fix category: (a) genuine kernel/numerical bug → fixed in the kernel. Not a tolerance
bump and not a test reseed — the test was already seeded and the tolerance was correct.
Test plan
test_gemm_a16wfp4.pyon gfx950 (MI35X), driving the exact failing shapeM=16, N=4096, K=11008, atomic_add=True.Committed regression
test_gemm_a16wfp4_scale_tail_maskdrives that same shape withw_scalesplaced at the head of a buffer whose per-row K tail is0xFF(e8m0 NaN). Thekernel reads the scale tail along the contiguous K axis, so every out-of-bounds scale group
lands in the poisoned tail; dropping the
other=127mask turns the output NaN. The testasserts a finite output and agreement with the torch reference. It runs in the
Triton Tests (MI35X)CI job on this PR.Test results
Single-GPU gfx950, kernel byte-identical to this change applied on top:
0xFF(worst-case dirty pool), 10 runsPoisoning the allocator with
0xFF(e8m0 NaN) deterministically forces the latent OOB readto surface — exactly what a busy/shared CI runner can leave behind — and the fix removes it
completely. Ragged-K regression subset (
K∈ {192, 320, 640, 896, 1120}) — 24/24 PASS.PR Self-Review
Reviewed revision
35dea98
Behavior and intent
_gemm_a16wfp4_kernelloaded theb_scalesE8M0 tensor with an unmaskedtl.load. On aragged-K shape (
EVEN_KFalse) the last split-K iteration reads scale groups past the end ofw_scales. The old behavior is insufficient because an out-of-bounds byte can be0xFF(NaN in E8M0), which
tl.dot_scaledpropagates even where the matched data is masked tozero, so the whole output tile becomes NaN. The change masks the scale load on the K tail
(
mask=scale_mask, other=127, E8M01.0), the same way the sibling_gemm_afp4wfp4_kerneldoes.
EVEN_Ktiles are unchanged.Validation and unverified scope
The kernel fix was verified on a single gfx950 (MI35X) GPU on the failing shape
M=16, N=4096, K=11008, atomic_add=True: normal pool 40/40 PASS; allocator pre-filled with0xFF10/10 FAIL (65536 NaN each) before the fix and 10/10 PASS (0 NaN) after; ragged-Ksubset (
K∈ {192, 320, 640, 896, 1120}) 24/24 PASS. Command:pytest op_tests/triton_tests/gemm/basic/test_gemm_a16wfp4.py. The committed regressiontest_gemm_a16wfp4_scale_tail_maskruns in theTriton Tests (MI35X)CI job on this PR.Unverified: other archs (the kernel is gfx950/MXFP4 only) and non-
0xFFNaN encodings inthe OOB tail (the mask makes the OOB value irrelevant, so this is covered by construction).
PR-specific question
Most important boundary: the ragged K tail where
2*K/32scale columns are not a multiple ofthe per-iteration scale count, so the final iteration over-reads the
b_scalestensor.Evidence:
test_gemm_a16wfp4_scale_tail_maskpinsw_scalesat the head of a buffer whoseper-row K tail is
0xFF, so the over-read deterministically hits NaN and the test fails ifthe mask is removed; it then asserts a finite output and agreement with the torch reference.
Changed op tests
op_tests/triton_tests/gemm/basic/test_gemm_a16wfp4.py— addstest_gemm_a16wfp4_scale_tail_mask. It covers the ragged-Kb_scalesover-read with a0xFF-poisoned per-row tail on the CI flake shape. It is not redundant: the existingparametrized
test_gemm_a16wfp4leaves the OOB scale read to whatever memory follows thetensor, which is usually finite, so the bug stays hidden; this test forces the over-read to
land on E8M0 NaN.
🤖 Generated with Claude Code