Skip to content

[Triton/Gluon] Fix a16wfp4 GEMM NaN flake: mask the b_scales K-tail - #8768

Open
ganyi1996ppo wants to merge 7 commits into
ROCm:mainfrom
ganyi1996ppo:fix/gemm-a16wfp4-scale-tail-mask
Open

ganyi1996ppo wants to merge 7 commits into
ROCm:mainfrom
ganyi1996ppo:fix/gemm-a16wfp4-scale-tail-mask

Conversation

@ganyi1996ppo

@ganyi1996ppo ganyi1996ppo commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

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_close
mismatch. It is seeded (torch.manual_seed(5)), so the inputs are fixed — the
intermittency 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_kernel the b_scales were loaded with an unmasked tl.load:

b_scales = tl.load(b_scale_ptrs)   # before: no K-tail mask

For this shape the wrapper sets K = w.shape[1] = 5504 (packed half-dim) and
BLOCK_SIZE_K = 512, so num_k_iter = cdiv(5504, 256) = 22 and the loop reads
22 * 16 = 352 scale columns while only 2*K/32 = 344 exist. The last iteration reads
8 e8m0 scale columns out of bounds. The data tiles (a, b) are already masked to
zero on this tail, but an OOB scale byte can be 0xFF, which is NaN in e8m0, and
tl.dot_scaled propagates it even though the data is zero (0 * NaN = NaN). The whole
MxN output tile then becomes NaN. Because the OOB bytes are whatever happens to sit past
the scale tensor, the failure only shows up when that memory holds a NaN encoding — hence
the flake. EVEN_K shapes are K-aligned and never hit the tail.

The fix mirrors what the sibling _gemm_afp4wfp4_kernel already does (same file family):
mask the scale load on the ragged K-tail with other=127 (e8m0 1.0). EVEN_K tiles are
unchanged.

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.py on gfx950 (MI35X), driving the exact failing shape
M=16, N=4096, K=11008, atomic_add=True.

Committed regression test_gemm_a16wfp4_scale_tail_mask drives that same shape with
w_scales placed at the head of a buffer whose per-row K tail is 0xFF (e8m0 NaN). The
kernel reads the scale tail along the contiguous K axis, so every out-of-bounds scale group
lands in the poisoned tail; dropping the other=127 mask turns the output NaN. The test
asserts 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:

scenario before fix after fix
normal pool, 40 independent runs 40/40 PASS PASS
GPU memory pre-filled with 0xFF (worst-case dirty pool), 10 runs 10/10 FAIL, 65536 NaN each 10/10 PASS, 0 NaN

Poisoning the allocator with 0xFF (e8m0 NaN) deterministically forces the latent OOB read
to 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_kernel loaded the b_scales E8M0 tensor with an unmasked tl.load. On a
ragged-K shape (EVEN_K False) the last split-K iteration reads scale groups past the end of
w_scales. The old behavior is insufficient because an out-of-bounds byte can be 0xFF
(NaN in E8M0), which tl.dot_scaled propagates even where the matched data is masked to
zero, so the whole output tile becomes NaN. The change masks the scale load on the K tail
(mask=scale_mask, other=127, E8M0 1.0), the same way the sibling _gemm_afp4wfp4_kernel
does. EVEN_K tiles 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 with
0xFF 10/10 FAIL (65536 NaN each) before the fix and 10/10 PASS (0 NaN) after; ragged-K
subset (K ∈ {192, 320, 640, 896, 1120}) 24/24 PASS. Command:
pytest op_tests/triton_tests/gemm/basic/test_gemm_a16wfp4.py. The committed regression
test_gemm_a16wfp4_scale_tail_mask runs in the Triton Tests (MI35X) CI job on this PR.
Unverified: other archs (the kernel is gfx950/MXFP4 only) and non-0xFF NaN encodings in
the 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/32 scale columns are not a multiple of
the per-iteration scale count, so the final iteration over-reads the b_scales tensor.
Evidence: test_gemm_a16wfp4_scale_tail_mask pins w_scales at the head of a buffer whose
per-row K tail is 0xFF, so the over-read deterministically hits NaN and the test fails if
the 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 — adds
test_gemm_a16wfp4_scale_tail_mask. It covers the ragged-K b_scales over-read with a
0xFF-poisoned per-row tail on the CI flake shape. It is not redundant: the existing
parametrized test_gemm_a16wfp4 leaves the OOB scale read to whatever memory follows the
tensor, which is usually finite, so the bug stays hidden; this test forces the over-read to
land on E8M0 NaN.

🤖 Generated with Claude Code

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]>
@ganyi1996ppo
ganyi1996ppo requested review from a team and a balanced review from Copilot November 26, 2026 13:20
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

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

Extended tests (opt-in via labels):

Label Tests
ci:mi300x Run Aiter standard tests on MI300X in PRs; main branch always runs them
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the Radeon R9700 ROCm runner
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
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: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
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 8768 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

Reviews:
A reviewer is usually assigned within one working day after the PR is ready for review (see Code Review Process). If nobody has responded for about a week, mention the area contact listed in CONTACTS.md. Want to help review? See REVIEWERS.md.

@ganyi1996ppo ganyi1996ppo self-assigned this Nov 26, 2026
@aiter-bot

aiter-bot commented Nov 26, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision 8d467f8748ca · mode shadow

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

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. What behavior would each changed op test fail on before this PR, and why is that coverage not already provided by an existing canonical test?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

@github-actions github-actions Bot changed the title [Triton] Fix a16wfp4 GEMM NaN flake: mask the b_scales K-tail [Triton/Gluon] Fix a16wfp4 GEMM NaN flake: mask the b_scales K-tail Nov 26, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 of 1.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]>
Copilot AI balanced review requested due to automatic review settings November 26, 2026 13:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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]>
Copilot AI balanced review requested due to automatic review settings November 26, 2026 14:07
@ganyi1996ppo

Copy link
Copy Markdown
Contributor Author

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed to determine whether the unpublished comment-length rule violation should affect approval.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@zufayu
zufayu requested review from a team and Dewei-Wang-sh November 27, 2026 01:38
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]>
Copilot AI balanced review requested due to automatic review settings November 30, 2026 02:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The scale-count comment incorrectly doubles the logical K dimension, yielding 688 instead of 344, and should use K / 32 to match num_scale_k.

1 open issue

Review effort: Balanced

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]>
Copilot AI balanced review requested due to automatic review settings November 30, 2026 02:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because a comment-style finding passed the severity filter but was omitted from the published review.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings December 1, 2026 16:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The comment at gemm_a16wfp4.py:145 exceeds the Triton convention of one or two sentences and should be shortened while retaining the NaN-risk and neutral-scale explanation.

1 open issue

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings December 1, 2026 17:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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

This branch has not been deployed

No deployments
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.

3 participants