Skip to content

[Triton/Gluon] Mask OOB e8m0 scales on the ragged-K path of batched_gemm_afp4wfp4 - #7047

Merged
vgokhale merged 2 commits into
ROCm:mainfrom
Ragua1:batched-afp4wfp4-ragged-k-scale-mask
Oct 31, 2026
Merged

vgokhale merged 2 commits into
ROCm:mainfrom
Ragua1:batched-afp4wfp4-ragged-k-scale-mask

Conversation

@Ragua1

@Ragua1 Ragua1 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

_batched_gemm_afp4_wfp4_kernel loads a_scales and b_scales without a mask, also when EVEN_K is false. When K is not a multiple of BLOCK_SIZE_K, the last K block reads scale groups past K // 32. The values of that block are masked to zero, but a byte of 0xFF there is the e8m0 NaN and tl.dot_scaled propagates it, so output rows or columns turn NaN depending on what lies after the scale tensors. #4181 fixed the same defect in batched_gemm_a16wfp4.

Seen in test_batched_gemm_afp4wfp4.py on gfx1101, shape B=2, M=256, N=3584, K=2112. In three full runs of the module, with two different configs, 2 to 3 of its 8 cases failed with NaN in the output, 3584 to 67329 of 1835008 elements mismatched, and the layouts that failed changed from run to run. The other cases passed or failed on the default tolerance or out of memory. B=5, M=4864, N=8192, K=4160 failed with NaN the same way in two of the runs.

Technical Details

  • aiter/ops/triton/_triton_kernels/gemm/batched/batched_gemm_afp4wfp4.py: on the EVEN_K == False path the scale loads use mask=offs_scale_k[None, :] < (2 * K // SCALE_GROUP_SIZE - k * (BLOCK_SIZE_K // SCALE_GROUP_SIZE)) with other=127, a scale of 2^0, as [Triton/Gluon] Fix ragged-K mask in batched A16WFP4 GEMM #4181 does for batched_gemm_a16wfp4 and as gemm_afp4wfp4 already does. The EVEN_K == True path keeps the unmasked load, so shapes that resolve to it do not change.
  • op_tests/triton_tests/gemm/batched/test_batched_gemm_afp4wfp4.py: test_batched_gemm_afp4_wfp4_ragged_k_scales places each scale tensor in turn in front of a full block of 0xFF bytes and runs K = 2112, with a partial last block, and K = 2048 with an explicit config of BLOCK_SIZE_K 256. The result no longer depends on what the allocator left after the tensor.

Test Plan

gfx1101 (Radeon RX 7800 XT), Windows 11, TheRock ROCm 10.2 (torch 2.15.0a0+rocm10.2.0a20260926), triton-windows 3.8.0.post29, AITER_TRITON_ONLY=1, aiter main @ 44b4fea6 with this change. On upstream main the batched FP4 tests skip on gfx1101, so the run added locally the gfx1101 gate and test tolerance proposed in #6685 and gfx1101 configs; these are not part of this PR.

On Windows the tests ran through a local pytest wrapper. It does not modify kernels, configs or test code. It:

  • replaces aiter.ops.enum with an empty module, so module_aiter_core is not JIT-built, which needs Ninja and hipcc;
  • makes chip_info._detect_native() return the arch from torch.cuda.get_device_properties(0).gcnArchName;
  • binds aiter.dtypes, which aiter/__init__.py leaves unbound under AITER_TRITON_ONLY;
  • caps the PyTorch allocator at 90 % of the 16 GiB card.
  1. test_batched_gemm_afp4_wfp4_ragged_k_scales without and with the kernel change.
  2. test_batched_gemm_afp4wfp4.py, full module, with the kernel change.

Test Result

test_batched_gemm_afp4_wfp4_ragged_k_scales:

K Scale tensor followed by 0xFF Without the change With the change
2112 x_scales failed, all 4096 elements mismatched, NaN in the output passed
2112 w_scales failed, all 4096 elements mismatched, NaN in the output passed
2048 x_scales passed passed
2048 w_scales passed passed

test_batched_gemm_afp4wfp4.py with the change: 2603 passed, 65 failed. All 65 are out of memory in the test's own PyTorch code (input generation, the reference or assert_close), with 5.08 to 14.23 GiB already allocated on the 16 GiB card; no case failed on a mismatch or NaN. Out of memory also hit one of the 8 cases at K = 2112 and two of the 8 at K = 4160, so 7 and 6 of them ran.

Not tested on gfx950 or gfx1250, where tl.dot_scaled is native. The unmasked load and the mask do not depend on the arch.

Not included

  • _gemm_a16wfp4_kernel loads b_scales the same way (aiter/ops/triton/_triton_kernels/gemm/basic/gemm_a16wfp4.py:140); [triton][gemm] gemm_a16wfp4: mask the b_scales load when EVEN_K is false #4689 (open) masks it. On gfx1101 the same kind of control turned every output column NaN at K = 4352 with BLOCK_SIZE_K 512.
  • aiter/ops/triton/_triton_kernels/gemm/fused/fused_gemm_afp4wfp4_a16w16.py:192-193 loads a_scale and b_scale without a mask as well; not tested.

Related

@Ragua1
Ragua1 requested a review from a team October 27, 2026 21:48
@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 7047 --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.

@github-actions github-actions Bot changed the title [Triton] Mask OOB e8m0 scales on the ragged-K path of batched_gemm_afp4wfp4 [Triton/Gluon] Mask OOB e8m0 scales on the ragged-K path of batched_gemm_afp4wfp4 Oct 27, 2026
@Boss2002n
Boss2002n requested a balanced review from Copilot October 28, 2026 01: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.

🟡 Changes recommended

The test’s inline configuration violates the no-configs-in-tests rule and bypasses the wrapper’s architecture-specific JSON configuration loader.

1 open issue
What changed in this PR

This PR prevents out-of-bounds e8m0 scale reads in Triton batched FP4 GEMM when the final K block is partial. It applies the same masking approach used for batched A16WFP4 in #4181.

Changes:

  • Mask both scale loads on the ragged-K path, using 127 (scale 1) for invalid groups.
  • Keep scale loads unchanged on the aligned-K path.
  • Add regression tests with trailing 0xFF bytes for each scale tensor and an aligned-K control.
File Description
op_tests/​triton_tests/​gemm/​batched/​test_batched_gemm_afp4wfp4.py Adds controlled scale-tail tests for partial and aligned K blocks.
aiter/​ops/​triton/​_triton_kernels/​gemm/​batched/​batched_gemm_afp4wfp4.py Masks invalid scale groups to prevent NaN propagation.

Review effort: Balanced

@zufayu
zufayu requested review from a team and Boss2002n October 28, 2026 02:31
Tests use the config that the wrapper loads. The gfx950 and gfx1250 tables give BLOCK_SIZE_K 256 for M = 32, so K = 2112 still takes the EVEN_K == False path and K = 2048 the EVEN_K == True one.

Signed-off-by: Martin Domanský <[email protected]>
@aiter-bot

Copy link
Copy Markdown

NOT READY FOR HUMAN REVIEW

PR Self-Review

Revision 593d26c6988e · 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 — missing ### Reviewed revision in the PR description
  • FAIL — missing ### Behavior and intent in the PR description
  • FAIL — missing ### Validation and unverified scope in the PR description
  • FAIL — missing ### PR-specific question in the PR description
  • FAIL — missing ### Changed op tests in the PR description
  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA
  • FAIL — Behavior and intent has no substantive answer
  • FAIL — Validation and unverified scope has no substantive answer
  • FAIL — PR-specific question has no substantive answer
  • FAIL — changed op test not explained in Changed op tests: op_tests/triton_tests/gemm/batched/test_batched_gemm_afp4wfp4.py
  • FAIL — Changed op tests must explain why each changed op test is necessary and non-redundant

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

@vgokhale
vgokhale merged commit 0dd2716 into ROCm:main Oct 31, 2026
58 checks passed
ZLkanyo009 added a commit that referenced this pull request Oct 31, 2026
Pick up #7047 (mask OOB e8m0 scales on the ragged-K path of batched_gemm_afp4wfp4), which fixes the MI35X Shard 5 NaNs in test_batched_gemm_afp4wfp4.

Co-authored-by: Cursor <[email protected]>
@Ragua1
Ragua1 deleted the batched-afp4wfp4-ragged-k-scale-mask branch October 31, 2026 22:23
solinzby1 pushed a commit that referenced this pull request Nov 2, 2026
…emm_afp4wfp4 (#7047)

* [Triton] Mask OOB e8m0 scales on the ragged-K path of batched_gemm_afp4wfp4

Signed-off-by: Martin Domanský <[email protected]>

* [Triton] Use the wrapper config in the ragged-K scale test

Tests use the config that the wrapper loads. The gfx950 and gfx1250 tables give BLOCK_SIZE_K 256 for M = 32, so K = 2112 still takes the EVEN_K == False path and K = 2048 the EVEN_K == True one.

Signed-off-by: Martin Domanský <[email protected]>

---------

Signed-off-by: Martin Domanský <[email protected]>
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]>
shantipriya-amd pushed a commit to shantipriya-amd/aiter that referenced this pull request Nov 18, 2026
…emm_afp4wfp4 (ROCm#7047)

* [Triton] Mask OOB e8m0 scales on the ragged-K path of batched_gemm_afp4wfp4

Signed-off-by: Martin Domanský <[email protected]>

* [Triton] Use the wrapper config in the ragged-K scale test

Tests use the config that the wrapper loads. The gfx950 and gfx1250 tables give BLOCK_SIZE_K 256 for M = 32, so K = 2112 still takes the EVEN_K == False path and K = 2048 the EVEN_K == True one.

Signed-off-by: Martin Domanský <[email protected]>

---------

Signed-off-by: Martin Domanský <[email protected]>
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.

5 participants