Skip to content

[FlyDSL] mxfp8 8-wave GEMM: cover the padded rows of preshuffled e8m0 scales - #9040

Merged
XiaobingSuper merged 1 commit into
ROCm:mainfrom
MengqingCao:mengqing/m31-mxfp8-8wave-psc-rows
Dec 3, 2026
Merged

XiaobingSuper merged 1 commit into
ROCm:mainfrom
MengqingCao:mengqing/m31-mxfp8-8wave-psc-rows

Conversation

@MengqingCao

@MengqingCao MengqingCao commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Re-filed from #8588 (author Jasen2201), cherry-picked onto the current main (6b13649) with no conflicts. The commit keeps its original author; the change is unchanged.


Ready for review. No blocking issues.

Why

With preshuffled e8m0 scales, the gfx950 8-wave MXFP8 GEMM loses the scales of the last, partial 32-row group whenever M or N is not a multiple of 32. At M 8191 on a 9856 x 6144 weight, output rows 8160 to 8190 come out wrong: max abs error 2.79 against an fp32 reference, 0.03 with row-major scales.

What changed

  • ScalePreshuffledS2R sizes its scale buffer for the rows padded to 256, the size flydsl_mxfp8_8wave_gemm already requires of a 1-D scale. It stopped at rows x K/32 bytes, so the partial group's scale words loaded as 0.
  • Row-major scales, the only kind aiter's own dispatch passes, build the same kernel as before.
  • test_gemm_8wave_preshuffled_scales in op_tests/test_gemm_a8w8_mxfp8_bpreshuffle.py (gfx950) feeds the kernel random fp8 data and random scales per row and 32-K group, row-major and preshuffled by shuffle_scale. Both results must match the fp32 reference and each other bit for bit.

Review focus

The first two Decided items in the Dev record: the 256-row pad, and the new check living in the existing op test.

Testing

The 24 default points cover the MiniMax-M3.1 TP1 qkv, o_proj and shared-expert shapes at ragged and aligned M, plus N 6160 for the weight scales and M 1, 17 and 37 inside one M tile. All pass bit for bit, and main's kernel fails every ragged point.

branch @ 8d508da on MI355X: 24/24 pass, err 0; main's kernel: 16/24 fail. CPU pre-checks 154 passed, as on main. black, ruff 0.16.0 clean.

Follow-ups

  • Upstream ROCm/FlyDSL kernels/gemm/mxfp8_gemm_8wave.py, which this kernel was ported from, has the same rows x K/32 bound.
Dev record
  • Found: the descriptor bound (rows x K/32 bytes) ignored the 256-row padding of the layout the reader walks. It was found while verifying an ATOM MiniMax-M3.1 TP1 prefill prototype that feeds this kernel preshuffled scales. There, real-weight qkv at M 8191 gave relL2 7.4e-3 and max abs 261, against 1.66e-3 and 8.1 on the row-major path (M 3001: relL2 3.4e-2).
  • Decided: pad to 256 rows, the size the launcher checks (-(-M // 256) * 256 * K // 32 bytes for a 1-D x_scale). A 32-row pad would also be correct: rows past M feed only output rows the store drops.
  • Decided: extend op_tests/test_gemm_a8w8_mxfp8_bpreshuffle.py rather than add a suite under op_tests/flydsl_tests/. CI shards run the top-level op_tests/test_*.py files as scripts, and this file already tests the 1x32 MXFP8 bpreshuffle GEMM. The check runs after the existing sweep, on gfx950 only, and has its own shape list (-mnk_preshuffled) and summary table.
  • Decided: the preshuffled scales come from aiter.ops.shuffle.shuffle_scale, flattened to 1-D. It is the producer aiter ships for this layout, so the kernel is checked against it rather than a test-local copy.
  • Decided: random fp8 values and random e8m0 codes per row and 32-K group instead of the quant kernel, so a scale read for the wrong row or group changes the output. The N 6160 point uses M 1024 so that only the weight-scale side is ragged. M 1 and 17 lie inside one partial 32-row group, and M 37 (K 7168) puts a full and a partial group inside one M tile.
  • Surprised: none.
  • Left undone: the file's existing tuned-shape sweep, unchanged here, ran on two shapes only, not on every gfx950 row.
Evidence

Setup: rocm/atom-dev:nightly_202611211511 (torch 2.10.0+rocm7.2.4) with aiter replaced by this branch (8d508da on main 526972d, fresh AITER_JIT_DIR) and FlyDSL 0.5.3 (the requirements.txt pin). One MI355X (gfx950, 256 CUs).

Fixed: python3 op_tests/test_gemm_a8w8_mxfp8_bpreshuffle.py -mnk 1000,9856,6144 8191,6144,3072, exit 0. test_w_scale_layout OK, the existing sweep passed on both shapes, and the new check printed (err 0 for both layouts at every point; the bit-identity assert passed):

M N K row-major us preshuffled us
1000 9856 6144 77.4 72.4
3001 9856 6144 171.5 158.3
8191 9856 6144 438.8 392.0
8192 9856 6144 439.1 391.4
16384 9856 6144 873.8 780.1
1000 6144 8192 85.2 76.8
3001 6144 8192 173.7 161.4
8191 6144 8192 307.3 296.7
8192 6144 8192 306.1 291.7
16384 6144 8192 607.1 578.8
1000 6144 6144 68.4 62.0
3001 6144 6144 137.9 127.5
8191 6144 6144 241.4 228.8
8192 6144 6144 243.1 228.5
16384 6144 6144 477.0 449.7
1000 6144 3072 38.8 36.3
3001 6144 3072 78.2 74.2
8191 6144 3072 140.9 134.8
8192 6144 3072 140.4 135.0
16384 6144 3072 266.9 256.8
1024 6160 3072 39.7 37.6
1 6144 3072 30.2 28.8
17 7168 2048 22.2 21.1
37 2112 7168 61.6 58.0

Revert-red: the same tree with the kernel file at main, each default point run alone. The 8 points with M and N multiples of 32 pass; the other 16 fail the fp32 check. Outputs that differ from the row-major path, and the error against fp32:

M N K preshuffled output that differs preshuffled relL2 / max abs row-major relL2 / max abs
1000 9856 6144 rows 992-999 7.6e-2 / 6.28 1.7e-3 / 0.028
3001 9856 6144 rows 2976-3000 4.2e-2 / 4.71 1.7e-3 / 0.031
8191 9856 6144 rows 8160-8190 1.2e-2 / 2.79 1.7e-3 / 0.031
1000 6144 8192 rows 992-999 7.7e-2 / 7.49 1.7e-3 / 0.031
3001 6144 8192 rows 2976-3000 4.5e-2 / 4.65 1.7e-3 / 0.031
8191 6144 8192 rows 8160-8190 1.1e-2 / 2.24 1.7e-3 / 0.031
1000 6144 6144 rows 992-999 7.9e-2 / 6.25 1.7e-3 / 0.028
3001 6144 6144 rows 2976-3000 4.4e-2 / 4.49 1.7e-3 / 0.029
8191 6144 6144 rows 8160-8190 1.1e-2 / 2.32 1.7e-3 / 0.028
1000 6144 3072 rows 992-999 8.0e-2 / 4.97 1.7e-3 / 0.016
3001 6144 3072 rows 2976-3000 4.4e-2 / 3.35 1.7e-3 / 0.016
8191 6144 3072 rows 8160-8190 1.2e-2 / 2.71 1.7e-3 / 0.016
1024 6160 3072 columns 6144-6159 3.6e-2 / 3.93 1.7e-3 / 0.016
1 6144 3072 row 0 1.0 / 3.82 1.7e-3 / 0.0077
17 7168 2048 rows 0-16 7.1e-1 / 3.55 1.7e-3 / 0.016
37 2112 7168 rows 32-36 3.4e-1 / 6.18 1.7e-3 / 0.016

Speed: the fix does not change the preshuffled kernel's time. Median of 4 alternating runs each, M and N multiples of 32, where both kernels are right:

M N K row-major us preshuffled, this PR preshuffled, main's kernel
8192 9856 6144 440.4 391.9 391.5
8192 6144 8192 308.5 293.5 293.2
8192 6144 6144 241.9 229.0 228.9
8192 6144 3072 142.3 136.0 136.3
16384 9856 6144 881.6 787.5 786.4
16384 6144 8192 608.8 573.6 574.5
16384 6144 6144 473.3 450.4 450.3
16384 6144 3072 267.5 257.4 257.3

With this fix, that ATOM prototype matched ATOM's row-major path bit for bit on 99/99 GEMM points (M 2048-32768, real checkpoint weights) and 112/112 linear points (M 1024-32768, ragged 1025-16383 included). The integrated build's 48/48 linear checks (6 real-weight shapes x M 1000-16384) include M 3001 and 8191.

CPU: python3 -m pytest -q tests/test_plugin.py tests/test_plugin_contracts.py tests/test_plugin_discovery.py tests/test_plugin_overhead.py: 154 passed on this branch and on main 526972d, no FAILED on either.

PR Self-Review

Reviewed revision

e1134d0

Behavior and intent

With preshuffled (1-D) e8m0 scales, the gfx950 8-wave MXFP8 GEMM now reads the scale words of a partial last 32-row group of A or B. Its buffer descriptor stopped at rows x K/32 bytes, so those words loaded as 0. The rows (M % 32 != 0) or columns (N % 32 != 0) of that group came out wrong. Row-major scales build the same kernel as before.

Validation and unverified scope

  • Validated on one MI355X (gfx950): python3 op_tests/test_gemm_a8w8_mxfp8_bpreshuffle.py -mnk 1000,9856,6144 8191,6144,3072 exits 0, and the new check passes at its 24 default points (err 0, preshuffled equals row-major bit for bit).
  • With the kernel file at main, the 16 ragged points fail and the 8 aligned ones pass. Preshuffled timing is unchanged (median of 4 alternating runs, 8 aligned shapes).
  • At 00715a5 (this commit plus a module constant since dropped): Standard Tests shard 3 (MI35X) ran the new check, 24/24 points, err 0 for both layouts; op_tests/test_gemm_a8w8_mxfp8_bpreshuffle.py also passed on one MI355X.
  • Unverified: the existing tuned-shape sweep ran on two shapes only (this PR does not change its row-major path). gfx942 and gfx1250 run only the existing checks, since the 8-wave kernel is gfx950-only.

PR-specific question

Before this PR, test_gemm_8wave_preshuffled_scales fails at every point whose M or N is not a multiple of 32 (16 of 24). There, the rows or columns of the last partial 32-row group are off the fp32 reference. No existing test feeds the 8-wave kernel preshuffled scales: test_gemm passes row-major scales through the dispatcher.

Changed op tests

op_tests/test_gemm_a8w8_mxfp8_bpreshuffle.py adds test_gemm_8wave_preshuffled_scales (gfx950). It runs the 8-wave kernel with row-major scales and with the same scales preshuffled by aiter.ops.shuffle.shuffle_scale, on random e8m0 codes per row and 32-K group. Both must match the fp32 reference and each other bit for bit. The file's existing sweep covers row-major scales only.

🤖 Generated with Claude Code

… scales

ScalePreshuffledS2R sized its buffer descriptor at rows * K / 32 bytes, but the
preshuffled layout it reads (aiter.ops.shuffle.shuffle_scale, flattened) spans
the rows padded to 256, as the launcher validates. With M (or N) not a multiple
of 32, the scale words of the last, partial 32-row group lie past that bound and
load as 0 (2^-127), so those output rows (or columns) come out wrong. Size the
descriptor for the padded rows. On gfx950, test_gemm_a8w8_mxfp8_bpreshuffle.py
now also runs the 8-wave kernel with scales preshuffled by shuffle_scale and
requires the row-major-scale result bit for bit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 8d508da)
@github-actions

github-actions Bot commented Dec 2, 2026

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 9040 --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.

@aiter-bot

aiter-bot commented Dec 2, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision e1134d0b6574 · 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.

@Jasen2201 Jasen2201 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.

Reviewed at 00715a5.

The fix looks right to me. c_m / c_n are runtime fx.Int32 and ceildiv handles them. The padded bound, ceil(rows / 256) * 256 * K / 32 bytes, is exactly the 1-D scale size flydsl_mxfp8_8wave_gemm already requires (gemm_a8w8_mxfp8_8wave.py:230-246), so the descriptor now covers the whole allocation and nothing past it. n_i32 is built from the same padded rows, so the two lengths in ScalePreshuffledS2R agree. The tail split never runs with preshuffled scales (tail_split_n1 returns None), so no other path hands this reader an unpadded buffer.

CI: the new check ran in Standard Tests shard 3 (MI35X) at this head: 24/24 points, err 0 for both layouts. Shards 6 and 7 are red, but their logs list only passing tests and stop mid-run in tests this PR does not touch (test_per_tensor_quant_exact, test_activation). A re-run should clear them.

No changes requested.

@Jasen2201

Copy link
Copy Markdown
Contributor

Follow-up: I ran aiter's own validator (.claude/skills/validate-kernel-pr/validate_pr.sh) on one MI355X (gfx950). Setup: rocm/atom-dev:nightly_202612011644 (FlyDSL 0.5.3). The base was main 21ef589 with this patch reversed, on the same worktree and GPU.

  • Perf (the validator's deterministic stage): no regression. The median head/base speedup on TB/s, the worst column, is 1.004 over 1506 matched rows, with 3 runs per side and a threshold of 0.95.
  • Correctness: in each of the 3 head runs, the new check printed err 0 for both layouts at all 24 points, the same table as CI shard 3. For example, at 8191x9856x6144 preshuffled scales take 389.4 us and row-major scales 434.5 us.
  • Why the verdict says INCONCLUSIVE: the validator picked pytest as the runner. pytest collects the script-style test_gemm(m, n, k) and fails with "missing 3 required positional arguments", on main as well as on this branch. CI runs this file as a script, so there is nothing to change here.

Still no changes requested.

@MengqingCao
MengqingCao force-pushed the mengqing/m31-mxfp8-8wave-psc-rows branch from 00715a5 to e1134d0 Compare December 3, 2026 07:33

@solinzby1 solinzby1 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.

LGTM

@XiaobingSuper
XiaobingSuper merged commit a0af09d into ROCm:main Dec 3, 2026
262 of 264 checks passed
MengqingCao added a commit to ROCm/ATOM that referenced this pull request Dec 3, 2026
…ial-row fix; aiter's shuffle_scale for the w4 copy

psc_layout now also needs aiter's 8-wave MXFP8 GEMM to set
PRESHUFFLED_SCALE_PARTIAL_ROWS (ROCm/aiter#9040): without that fix a ragged M
reads the scales of its last, partial 32-row group as 0 and gets wrong rows.
An aiter without the marker keeps row-major scales, logged once.

linear.py builds the weight scales' w4 copy with aiter's
aiter.ops.shuffle.shuffle_scale(...).view(-1), byte-identical to ATOM's copy
(127 pad, same permute), which is dropped; the layout test now pins aiter's.

Co-Authored-By: Claude <[email protected]>
MengqingCao added a commit to ROCm/ATOM that referenced this pull request Dec 3, 2026
…ows, not an aiter marker

Replaces the PRESHUFFLED_SCALE_PARTIAL_ROWS gate. An aiter 8-wave MXFP8 GEMM
without ROCm/aiter#9040 drops the preshuffled scales of a partial last 32-row
group; ATOM now never hands it one. The psc GEMM runs psc_rows(m) = m padded
to 32 rows and returns the first m rows. The MXFP8 producers (mx8_quant,
norm_mx's MX8 outputs, the SwiGLU w4 quant) write their e4m3 rows into buffers
of psc_rows(m) rows, so the GEMM reads them in place; an activation without
that storage is copied padded. The w4 scale buffer already covers 256-row
padding. psc_layout keys on the real M as before and also needs N % 32 == 0.

Co-Authored-By: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants