Skip to content

[Triton/Gluon] Mask OOB scale loads in batched_gemm_afp4wfp4 when K is not aligned to BLOCK_SIZE_K - #7236

Closed
erizheng-amd wants to merge 2 commits into
mainfrom
erizheng-fix-batched-afp4wfp4-scale-oob
Closed

erizheng-amd wants to merge 2 commits into
mainfrom
erizheng-fix-batched-afp4wfp4-scale-oob

Conversation

@erizheng-amd

@erizheng-amd erizheng-amd commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

_batched_gemm_afp4_wfp4_kernel reads e8m0 scales past the end of the scale tensors when K is not a multiple of BLOCK_SIZE_K. Garbage 0xFF bytes (NaN in e8m0) then turn whole output rows into NaN. This PR masks the scale loads on the non-EVEN_K path, matching what gemm_afp4wfp4 and batched_gemm_a16wfp4 already do.

Motivation

This error is blocking the CI for PR #6595. For M=256, N=3584, K=2112, _get_config selects BLOCK_SIZE_K=256, NUM_KSPLIT=1. Since 2112 % 256 != 0, the kernel takes the non-EVEN_K path:

  • K=2112 has 2112 / 32 = 66 scale groups per row, and the K loop runs cdiv(2112, 256) = 9 iterations of 8 scale groups each.
  • The last iteration loads scale groups 64–71. Groups 66–71 are out of bounds.
  • The A/B data loads are masked (out-of-range K is read as 0), but the scale loads are not. Whatever bytes follow the scale tensor are used as scales.
  • If any of those bytes is 0xFF (NaN in e8m0), tl.dot_scaled produces NaN even though the matching data is zero, because 0 × NaN = NaN.

Changes

On the non-EVEN_K path, mask the scale loads to the scale groups that actually remain, and fill the masked lanes with 127 (scale 1.0). Masked-off K then contributes 0 × 1.0 = 0. The EVEN_K path is unchanged, so aligned shapes compile to the same code as before.

Testing

  • Unit tests added or updated
  • Performance benchmarks run
Shape (B, M, N, K) Path main Fix Change
2, 256, 3584, 2112 K not aligned GPU memory fault 94.7 µs crash fixed
2, 1, 1280, 2112 K not aligned GPU memory fault 7.75 µs crash fixed
2, 64, 1024, 4160 K not aligned 17.91 µs 17.88 µs −0.1%
8, 1024, 1024, 1056 K not aligned 66.58 µs 67.24 µs +1.0%
2, 256, 3584, 2048 EVEN_K (control) 52.74 µs 52.69 µs −0.1%
8, 1024, 1024, 1024 EVEN_K (control) 37.49 µs 37.32 µs −0.5%
  • Tested on MI250X
  • Tested on MI300X

Documentation

  • Docstrings updated
  • User guide updated
  • Performance guide updated
  • No documentation change is needed, with the reason stated below

Internal kernel correctness fix. There is no change to the public API, arguments or configs.

Dependencies

  • No new third-party dependencies added
  • New dependencies are listed and justified below

Breaking Changes

None

PR Self-Review

Reviewed revision

f88f9f9

Behavior and intent

On the non-EVEN_K path of _batched_gemm_afp4_wfp4_kernel, the A/B data loads were masked but the e8m0 scale loads were not. When K is not a multiple of BLOCK_SIZE_K, the last K iteration read scale groups past the end of the scale tensors. For example, at K=2112 with BLOCK_SIZE_K=256 there are 66 scale groups, and the last iteration reads groups 64 to 71. Out-of-bounds 0xFF bytes are NaN in e8m0, and tl.dot_scaled turns 0 × NaN into NaN, so whole output rows became NaN, or the kernel page-faulted on a fresh allocation. This PR masks the scale loads to the remaining groups and fills the masked lanes with 127 (scale 1.0), which matches gemm_afp4wfp4 and batched_gemm_a16wfp4. The EVEN_K path is unchanged, so aligned shapes compile to the same code as before.

Validation and unverified scope

Hardware: <GPU, e.g. MI355X (gfx950)>, ROCm , Triton .

  • pytest op_tests/triton_tests/gemm/batched/test_batched_gemm_afp4wfp4.py -k unaligned_k_ignores_oob_scales: passed with this PR. On main (145d062), the same command fails with NaN mismatches or a GPU memory fault.
  • pytest op_tests/triton_tests/gemm/batched/test_batched_gemm_afp4wfp4.py: passed, skipped with this PR.
  • Benchmark main vs. this PR, using the table in Testing: the unaligned-K shapes go from a crash to a working kernel and are within ±1% elsewhere. The EVEN_K control shapes are within noise.

Unverified:

  • MI300X (gfx942) was not tested, because the test skips there (is_fp4_avail() only allows gfx950 and gfx1250).
  • CI Triton shard 0, which owns this test file, timed out in "Setup Aiter and Triton" before running any test, so CI coverage of this test is still pending a re-run.

PR-specific question

On main, test_batched_gemm_afp4_wfp4_unaligned_k_ignores_oob_scales fails. Each shape has K that is not a multiple of the selected BLOCK_SIZE_K, and the scales are copied into a buffer followed by 0xFF bytes. The unmasked scale load in the last K iteration therefore reads NaN scales for the last batch, and assert_close fails, or the run page-faults. The canonical test_batched_gemm_afp4_wfp4 already includes an unaligned shape, (2, 256, 3584, 2112), but its scales come from a fresh torch.randint allocation. The bytes after that allocation are whatever the caching allocator left there, so whether that test fails depends on allocator state. That is why it has long passed on main but blocked CI on #6595. The new test makes the failure deterministic.

Changed op tests

op_tests/triton_tests/gemm/batched/test_batched_gemm_afp4wfp4.py: adds _place_before_nan_scales and test_batched_gemm_afp4_wfp4_unaligned_k_ignores_oob_scales. That is 5 unaligned-K shapes × {fp16, bf16} × {TN, NT}, for 20 cases. The helper keeps the transposed (B, K//32, D) scale layout that the generator produces, and only controls what follows the scale storage. That makes it a deterministic regression test for out-of-bounds scale reads on the non-EVEN_K path. It is not redundant with the existing parametrized test, which uses the same shapes but uncontrolled trailing memory and so cannot reliably detect the bug. No existing test was modified.

…aligned to BLOCK_SIZE_K

The non-EVEN_K path masked the A/B data loads but loaded the e8m0 scales
unmasked, so the last K iteration read scale groups past the end of the
scale tensors. Garbage 0xFF bytes (NaN in e8m0) propagate through
tl.dot_scaled even where the data is zero, producing NaN rows in the
output, and fresh allocations can page-fault. Mask the scale loads and
fill with 127 (scale 1.0), matching gemm_afp4wfp4 and batched_gemm_a16wfp4.

Co-authored-by: Cursor <[email protected]>
@erizheng-amd
erizheng-amd requested review from a team and a max review from Copilot October 30, 2026 20:25
@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: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 gfx1201 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 7236 --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.

@erizheng-amd
erizheng-amd requested a review from azaidy October 30, 2026 20:25
@github-actions github-actions Bot changed the title [Triton] Mask OOB scale loads in batched_gemm_afp4wfp4 when K is not aligned to BLOCK_SIZE_K [Triton/Gluon] Mask OOB scale loads in batched_gemm_afp4wfp4 when K is not aligned to BLOCK_SIZE_K Oct 30, 2026
@aiter-bot

aiter-bot commented Oct 30, 2026 •

Copy link
Copy Markdown

NOT READY FOR HUMAN REVIEW

PR Self-Review

Revision f88f9f9528e1 · mode shadow

Author self-review accepted. Readiness report:

Author answers

  • PASS — current-SHA answers are present and structurally specific
Show the author's self-review answers

Behavior and intent

On the non-`EVEN_K` path of `_batched_gemm_afp4_wfp4_kernel`, the A/B data loads were masked but the e8m0 scale loads were not. When K is not a multiple of `BLOCK_SIZE_K`, the last K iteration read scale groups past the end of the scale tensors. For example, at `K=2112` with `BLOCK_SIZE_K=256` there are 66 scale groups, and the last iteration reads groups 64 to 71. Out-of-bounds `0xFF` bytes are NaN in e8m0, and `tl.dot_scaled` turns 0 × NaN into NaN, so whole output rows became NaN, or the kernel page-faulted on a fresh allocation. This PR masks the scale loads to the remaining groups and fills the masked lanes with 127 (scale 1.0), which matches `gemm_afp4wfp4` and `batched_gemm_a16wfp4`. The `EVEN_K` path is unchanged, so aligned shapes compile to the same code as before.

Validation and unverified scope

Hardware: <GPU, e.g. MI355X (gfx950)>, ROCm <version>, Triton <version>.

- `pytest op_tests/triton_tests/gemm/batched/test_batched_gemm_afp4wfp4.py -k unaligned_k_ignores_oob_scales`: <N> passed with this PR. On main (`145d062`), the same command fails with NaN mismatches or a GPU memory fault.
- `pytest op_tests/triton_tests/gemm/batched/test_batched_gemm_afp4wfp4.py`: <N> passed, <N> skipped with this PR.
- Benchmark main vs. this PR, using the table in Testing: the unaligned-K shapes go from a crash to a working kernel and are within ±1% elsewhere. The `EVEN_K` control shapes are within noise.

Unverified:
- MI300X (gfx942) was not tested, because the test skips there (`is_fp4_avail()` only allows gfx950 and gfx1250).
- CI Triton shard 0, which owns this test file, timed out in "Setup Aiter and Triton" before running any test, so CI coverage of this test is still pending a re-run.

PR-specific question

On main, `test_batched_gemm_afp4_wfp4_unaligned_k_ignores_oob_scales` fails. Each shape has K that is not a multiple of the selected `BLOCK_SIZE_K`, and the scales are copied into a buffer followed by `0xFF` bytes. The unmasked scale load in the last K iteration therefore reads NaN scales for the last batch, and `assert_close` fails, or the run page-faults. The canonical `test_batched_gemm_afp4_wfp4` already includes an unaligned shape, `(2, 256, 3584, 2112)`, but its scales come from a fresh `torch.randint` allocation. The bytes after that allocation are whatever the caching allocator left there, so whether that test fails depends on allocator state. That is why it has long passed on main but blocked CI on #6595. The new test makes the failure deterministic.

Changed op tests

`op_tests/triton_tests/gemm/batched/test_batched_gemm_afp4wfp4.py`: adds `_place_before_nan_scales` and `test_batched_gemm_afp4_wfp4_unaligned_k_ignores_oob_scales`. That is 5 unaligned-K shapes × {fp16, bf16} × {TN, NT}, for 20 cases. The helper keeps the transposed `(B, K//32, D)` scale layout that the generator produces, and only controls what follows the scale storage. That makes it a deterministic regression test for out-of-bounds scale reads on the non-`EVEN_K` path. It is not redundant with the existing parametrized test, which uses the same shapes but uncontrolled trailing memory and so cannot reliably detect the bug. No existing test was modified.

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • FAIL — an active Copilot finding has no substantive reply from the PR author
  • ADVISORY — Copilot could not review the current head (for example, quota or service failure)

Code and op-test hygiene

  • PASS — no committed artifact or conflict marker detected

Required CI

  • PASS — Black: passed
  • PASS — Ruff: passed
  • PASS — Repository dependency: passed
  • FAIL — Aiter Test Gate: failed
  • Required CI failed; fix it before requesting human review.

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

…wfp4

Places the e8m0 scales in a buffer followed by 0xFF (NaN in e8m0) bytes,
so shapes whose K is not a multiple of BLOCK_SIZE_K fail deterministically
if the kernel reads scale groups past the end of the scale tensors.

Co-authored-by: Cursor <[email protected]>

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 non-aligned-K test can miss scale-load mask regressions because the bytes after both scale tensors are uncontrolled rather than padded with 0xFF.

1 open issue
What changed in this PR

This PR fixes out-of-bounds scale reads in AITER’s batched FP4 GEMM when K is not aligned to the kernel tile size.

Changes:

  • Masks invalid A and B scale loads and fills those lanes with scale 1.0 to prevent NaN propagation.
  • Leaves the aligned-K load path unchanged.
File Description
aiter/​ops/​triton/​_triton_kernels/​gemm/​batched/​batched_gemm_afp4wfp4.py Adds bounds masks and neutral fill values for scale loads in partial K tiles.

Review effort: Max

Copilot AI max review requested due to automatic review settings October 30, 2026 20:32

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@erizheng-amd
erizheng-amd requested a balanced review from Copilot October 30, 2026 20:33

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@erizheng-amd

Copy link
Copy Markdown
Contributor Author

Closing, error already fixed by #7047

erizheng-amd added a commit that referenced this pull request Nov 3, 2026
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]>
@erizheng-amd
erizheng-amd removed this pull request from stack #7237 November 3, 2026 22:12
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