Repository navigation
[Triton/Gluon] Mask OOB scale loads in batched_gemm_afp4wfp4 when K is not aligned to BLOCK_SIZE_K - #7236
[Triton/Gluon] Mask OOB scale loads in batched_gemm_afp4wfp4 when K is not aligned to BLOCK_SIZE_K#7236erizheng-amd wants to merge 2 commits into
Conversation
…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]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
NOT READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
…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]>
There was a problem hiding this comment.
🟡 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
|
Closing, error already fixed by #7047 |
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]>

Summary
_batched_gemm_afp4_wfp4_kernelreads e8m0 scales past the end of the scale tensors when K is not a multiple ofBLOCK_SIZE_K. Garbage0xFFbytes (NaN in e8m0) then turn whole output rows into NaN. This PR masks the scale loads on the non-EVEN_Kpath, matching whatgemm_afp4wfp4andbatched_gemm_a16wfp4already do.Motivation
This error is blocking the CI for PR #6595. For
M=256, N=3584, K=2112,_get_configselectsBLOCK_SIZE_K=256, NUM_KSPLIT=1. Since 2112 % 256 != 0, the kernel takes the non-EVEN_K path:0xFF(NaN in e8m0),tl.dot_scaledproduces NaN even though the matching data is zero, because 0 × NaN = NaN.Changes
On the non-
EVEN_Kpath, mask the scale loads to the scale groups that actually remain, and fill the masked lanes with127(scale 1.0). Masked-off K then contributes 0 × 1.0 = 0. TheEVEN_Kpath is unchanged, so aligned shapes compile to the same code as before.Testing
Documentation
Internal kernel correctness fix. There is no change to the public API, arguments or configs.
Dependencies
Breaking Changes
None
PR Self-Review
Reviewed revision
f88f9f9
Behavior and intent
On the non-
EVEN_Kpath 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 ofBLOCK_SIZE_K, the last K iteration read scale groups past the end of the scale tensors. For example, atK=2112withBLOCK_SIZE_K=256there are 66 scale groups, and the last iteration reads groups 64 to 71. Out-of-bounds0xFFbytes are NaN in e8m0, andtl.dot_scaledturns 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 matchesgemm_afp4wfp4andbatched_gemm_a16wfp4. TheEVEN_Kpath 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.EVEN_Kcontrol shapes are within noise.Unverified:
is_fp4_avail()only allows gfx950 and gfx1250).PR-specific question
On main,
test_batched_gemm_afp4_wfp4_unaligned_k_ignores_oob_scalesfails. Each shape has K that is not a multiple of the selectedBLOCK_SIZE_K, and the scales are copied into a buffer followed by0xFFbytes. The unmasked scale load in the last K iteration therefore reads NaN scales for the last batch, andassert_closefails, or the run page-faults. The canonicaltest_batched_gemm_afp4_wfp4already includes an unaligned shape,(2, 256, 3584, 2112), but its scales come from a freshtorch.randintallocation. 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_scalesandtest_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_Kpath. 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.