Skip to content

[FlyDSL] MoE stage 2: fix the fp8 route-out scale group for tile_n=128 - #7949

Merged
Bernard-Liu merged 6 commits into
ROCm:mainfrom
cpersson-amd:moe-s2-fp8-route-out-evec
Nov 30, 2026
Merged

Bernard-Liu merged 6 commits into
ROCm:mainfrom
cpersson-amd:moe-s2-fp8-route-out-evec

Conversation

@cpersson-amd

@cpersson-amd cpersson-amd commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes the v1 FlyDSL stage-2 fp8 route-out epilogue when the tile body is 128
columns wide: tile_n=128 tiles, and FHMoE tile_n=256 tiles whose shared
expert runs as two 128-column halves. With AITER_FLYDSL_STAGE2_FP8=1 these
gave 20–44% relative error.

Motivation

The fp8 epilogue quantizes one CShuffle fragment and writes one e8m0 byte per
8-column group, but the fragment is min(body_tile_n // 32, 8) = 4 columns
wide when body_tile_n is 128. Two fragments then write different scales to
the same byte, and half of every group is dequantized with the wrong scale.

body_tile_n is 128 for:

  • tile_n=128 tiles: atomic tiles the flag moves to reduce (e.g. DSv4.1 TP4
    t16x128x128_atomic_bnt2, T=17-128 and 1025-2048) and rows already tuned
    as reduce (e.g. in tuned_fhmoe.csv, minimax_m3_mxfp8, dsv4_fp8fp4; not
    checked row by row);
  • FHMoE tile_n=256 tiles with a shared expert: serial_shared_n runs the
    shared expert as two 128-column halves (e.g. tuned_fhmoe.csv
    t32x256x128_reduce_bnt2_persist). Found by @msaffari-amd.

Non-shared tile_n=256 tiles and the layout (v2) kernels are not affected.

Changes

  • mixed_moe_gemm_2stage_common.py: for fp8 output, use 8-wide fragments and
    cshuffle_nlane = min(32, body_tile_n // 8), so one fragment is one MXFP8
    group. body_tile_n covers the FHMoE halves, and the min(32, ...) keeps
    tile_n=512 valid. Non-fp8 epilogues are unchanged.
  • op_tests/test_fhmoe.py: test_dsv4_fhmoe_fp8_route_out_matches_fp32, which
    CI runs. DSv4 FHMoE with the flag at the tuned tile_n=128 row (4 tokens)
    and the tile_n=256 shared-halves row (8 tokens), against the fp32
    reference and the BF16 route-row output.

Performance

Not applicable: correctness fix. The epilogue issues the same stores with a
different fragment width, only for fp8 output with a 128-column body.

Testing

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X

Documentation

  • No documentation change is needed, with the reason stated below

Internal kernel fix.

Dependencies

  • No new third-party dependencies added

Breaking Changes

None

PR Self-Review

Reviewed revision

867a206

Behavior and intent

With AITER_FLYDSL_STAGE2_FP8=1, v1 FlyDSL stage-2 tiles whose body is 128
columns now write correct fp8 route rows. Before, the epilogue quantized
4-column CShuffle fragments (min(body_tile_n // 32, 8)) but stored one e8m0
byte per 8-column MXFP8 group, so two fragments wrote different scales to the
same byte and half of every group was dequantized with the wrong scale. This
hits tile_n=128 tiles (about 44% relative error) and FHMoE tile_n=256
tiles with a shared expert, whose shared halves are 128 columns (about 21%).
Non-shared tile_n=256, the v2 kernels and non-fp8 epilogues are unchanged.

Validation and unverified scope

All on MI355X (gfx950), FlyDSL 0.5.3, rebased on main 930e954.

  • pytest op_tests/test_fhmoe.py -k "fp8_route_out or dsv4_production_reduce":
    the new test, relative L2 error against the fp32 reference with FP8 route
    rows:

    Tokens Tuned stage 2 Before After BF16 route rows
    4 t32x128x128_reduce_persist 0.426 (fails) 0.031 0.016
    8 t32x256x128_reduce_bnt2_persist (shared halves) 0.207 (fails) 0.033 0.019

    "Before" is this branch with only the kernel file taken from main.

  • Full pytest op_tests/test_fhmoe.py (as CI runs it): 121 passed.

  • DSv4.1 TP4 fused_moe with the flag, T = 1..16384, both MoE shapes (384
    experts top-6, 128 experts top-3): worst relative error against BF16 route
    rows is 2.7% after the fix (about 44% before).

  • vLLM DSv4.1-Flash TP4 with the flag: gsm8k (full, 5-shot) 0.9265-0.9272
    (BF16 route rows 0.9189-0.9295); niah_single_2 32k/64k 1.0/1.0.

  • Unverified: the serving run used a branch with other MoE changes (inter-dim
    padding, XCD remap, [FlyDSL] Fix LDS race in MoE sorting p23 kernel #7349). Other models' tile_n=128 reduce rows were not
    run individually. MI250X/MI300X were not run.

PR-specific question

What is the most important boundary or failure mode introduced by this
change, and what evidence covers it?

The boundary is the fragment width in the fp8 epilogue: it must equal the
8-column MXFP8 group for every body_tile_n, including the 128-column shared
halves of an FHMoE tile_n=256 tile. test_dsv4_fhmoe_fp8_route_out_matches_fp32
covers both 128-column cases through CI on the tuned DSv4 FHMoE rows: before
the fix it fails at 0.426 and 0.207 relative error, after it passes at 0.031
and 0.033, within the 5e-2 bound of the existing DSv4 reduce test. It also
asserts the tuned stage 2 it runs, so a table change that moves these rows
off the 128-column paths fails loudly instead of passing vacuously, and that
FP8 route rows were actually used. The 8-token row's routed tiles keep a
256-column body, where the 8-wide fragment was already correct, so the same
test also guards that path. The other direction, tile_n=512,
is kept valid by min(32, body_tile_n // 8); no shipped fp8 route-out row
uses it.

Changed op tests

  • op_tests/test_fhmoe.py: adds test_dsv4_fhmoe_fp8_route_out_matches_fp32[4|8],
    the only CI test that runs the v1 fp8 route-out epilogue against a
    reference, at a tile_n=128 row and a tile_n=256 shared-halves row. The
    existing FHMoE tests leave the flag unset (BF16 route rows), and
    test_flydsl_stage2_classification_covers_plain_and_fhmoe_wrappers only
    checks dispatch.

AI disclosure: written with Claude Opus 5.5 (Cursor); reviewed and run by the
author.

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

@cpersson-amd cpersson-amd changed the title [FlyDSL] MoE stage 2: fix the fp8 route-out scale group for tile_n=128 [FlyDSL] MoE stage 2: fix fp8 route-out for tile_n=128, add AITER_FLYDSL_STAGE2_FP8_KEEP_ATOMIC Nov 13, 2026
@cpersson-amd cpersson-amd changed the title [FlyDSL] MoE stage 2: fix fp8 route-out for tile_n=128, add AITER_FLYDSL_STAGE2_FP8_KEEP_ATOMIC [FlyDSL] MoE stage 2: fix the fp8 route-out scale group for tile_n=128 Nov 13, 2026
@cpersson-amd
cpersson-amd marked this pull request as ready for review November 20, 2026 08:00
@cpersson-amd
cpersson-amd requested a review from a team November 20, 2026 08:00
@aiter-bot

aiter-bot commented Nov 20, 2026 •

Copy link
Copy Markdown

READY FOR HUMAN REVIEW

PR Self-Review

Revision 867a20609e70 · 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

With `AITER_FLYDSL_STAGE2_FP8=1`, v1 FlyDSL stage-2 tiles whose body is 128
columns now write correct fp8 route rows. Before, the epilogue quantized
4-column CShuffle fragments (`min(body_tile_n // 32, 8)`) but stored one e8m0
byte per 8-column MXFP8 group, so two fragments wrote different scales to the
same byte and half of every group was dequantized with the wrong scale. This
hits `tile_n=128` tiles (about 44% relative error) and FHMoE `tile_n=256`
tiles with a shared expert, whose shared halves are 128 columns (about 21%).
Non-shared `tile_n=256`, the v2 kernels and non-fp8 epilogues are unchanged.

Validation and unverified scope

All on MI355X (gfx950), FlyDSL 0.5.3, rebased on main 930e954bc.

- `pytest op_tests/test_fhmoe.py -k "fp8_route_out or dsv4_production_reduce"`:
  the new test, relative L2 error against the fp32 reference with FP8 route
  rows:

  | Tokens | Tuned stage 2 | Before | After | BF16 route rows |
  |---|---|---|---|---|
  | 4 | `t32x128x128_reduce_persist` | 0.426 (fails) | 0.031 | 0.016 |
  | 8 | `t32x256x128_reduce_bnt2_persist` (shared halves) | 0.207 (fails) | 0.033 | 0.019 |

  "Before" is this branch with only the kernel file taken from main.
- Full `pytest op_tests/test_fhmoe.py` (as CI runs it): 121 passed.
- DSv4.1 TP4 `fused_moe` with the flag, T = 1..16384, both MoE shapes (384
  experts top-6, 128 experts top-3): worst relative error against BF16 route
  rows is 2.7% after the fix (about 44% before).
- vLLM DSv4.1-Flash TP4 with the flag: gsm8k (full, 5-shot) 0.9265-0.9272
  (BF16 route rows 0.9189-0.9295); niah_single_2 32k/64k 1.0/1.0.
- Unverified: the serving run used a branch with other MoE changes (inter-dim
  padding, XCD remap, #7349). Other models' `tile_n=128` reduce rows were not
  run individually. MI250X/MI300X were not run.

PR-specific question

*What is the most important boundary or failure mode introduced by this
change, and what evidence covers it?*

The boundary is the fragment width in the fp8 epilogue: it must equal the
8-column MXFP8 group for every `body_tile_n`, including the 128-column shared
halves of an FHMoE `tile_n=256` tile. `test_dsv4_fhmoe_fp8_route_out_matches_fp32`
covers both 128-column cases through CI on the tuned DSv4 FHMoE rows: before
the fix it fails at 0.426 and 0.207 relative error, after it passes at 0.031
and 0.033, within the 5e-2 bound of the existing DSv4 reduce test. It also
asserts the tuned stage 2 it runs, so a table change that moves these rows
off the 128-column paths fails loudly instead of passing vacuously, and that
FP8 route rows were actually used. The 8-token row's routed tiles keep a
256-column body, where the 8-wide fragment was already correct, so the same
test also guards that path. The other direction, `tile_n=512`,
is kept valid by `min(32, body_tile_n // 8)`; no shipped fp8 route-out row
uses it.

Changed op tests

- `op_tests/test_fhmoe.py`: adds `test_dsv4_fhmoe_fp8_route_out_matches_fp32[4|8]`,
  the only CI test that runs the v1 fp8 route-out epilogue against a
  reference, at a `tile_n=128` row and a `tile_n=256` shared-halves row. The
  existing FHMoE tests leave the flag unset (BF16 route rows), and
  `test_flydsl_stage2_classification_covers_plain_and_fhmoe_wrappers` only
  checks dispatch.

AI disclosure: written with Claude Opus 5.5 (Cursor); reviewed and run by the
author.

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply
  • ADVISORY — Copilot did not review the current head SHA

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
  • PASS — Aiter Test Gate: passed

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

@msaffari-amd

msaffari-amd commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

This likely also fixes FHMoE tile_n=256 rows with a shared expert, not only tile_n=128. With a shared expert, serial_shared_n runs the shared expert as two body_tile_n = 128 halves, which had the same 4-wide fragment problem. On gfx950 the tuned_fhmoe.csv row t32x256x128_reduce_bnt2_persist compiles to the 4-wide pattern at the base commit and to the 8-wide one with this PR (ISA only, not run numerically). Could you update the Summary and self-review, which currently say tile_n=256 is not affected?

@msaffari-amd

Copy link
Copy Markdown
Contributor

Nit: the Changes section and the self-review say cshuffle_nlane = tile_n // 8, but the code uses min(32, body_tile_n // 8). The difference matters for readers: body_tile_n is what covers the FHMoE half-tiles, and the min(32, ...) keeps tile_n=512 valid. Could you update the two sentences to match the code?

@msaffari-amd

Copy link
Copy Markdown
Contributor

For reference: #8174 adds a per-call fused_moe(stage2_fp8=True) that routes stage 2 to the same v1 FP8 route-out epilogue this PR fixes. If it lands before this PR, its tile_n=128 reduce rows (and the FHMoE tile_n=256 shared-expert halves) would still get the conflicting e8m0 bytes. It may be worth merging this fix first.

cpersson-amd and others added 5 commits November 26, 2026 09:32
The fp8 route-out epilogue quantizes one fragment to e4m3 and stores a
single e8m0 byte per 8-column group, but the fragment width is
min(tile_n // 32, 8), i.e. 4 for tile_n=128. Two fragments then write
different scales to the same byte and half of every group is dequantized
with the wrong scale (~44% relative error with AITER_FLYDSL_STAGE2_FP8=1
on the v1 tiles). Use 8-wide fragments for fp8 output and shrink the
CShuffle N lanes to match.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Carl Persson <[email protected]>
Fails before the scale-group fix (tile_n=128), passes after.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Carl Persson <[email protected]>
AITER_FLYDSL_STAGE2_FP8=1 moves every fp4/fp8 atomic tile to reduce. With AITER_FLYDSL_STAGE2_FP8_KEEP_ATOMIC=1 atomic tiles stay atomic and only tiles tuned for reduce use FP8 route rows. The default is unchanged.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Carl Persson <[email protected]>
…-in"

Forced and keep-atomic modes measured the same on DeepSeek-V4.1-Flash serving (TTFT, decode throughput, gsm8k), so the extra switch is not needed.

This reverts commit 1ad8079.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Carl Persson <[email protected]>
…es rows

Runs DSv4 FHMoE with AITER_FLYDSL_STAGE2_FP8=1 at the tuned
t32x128x128_reduce_persist row (4 tokens) and the
t32x256x128_reduce_bnt2_persist row (8 tokens), whose shared expert runs
as two 128-column halves, against the fp32 reference and the BF16
route-row output. Before the scale-group fix the relative error is 0.426
and 0.207; after it, 0.031 and 0.033. Unlike flydsl_tests/test_flydsl_moe.py,
test_fhmoe.py runs in CI.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Carl Persson <[email protected]>
@cpersson-amd
cpersson-amd force-pushed the moe-s2-fp8-route-out-evec branch from 9613075 to 09a4d37 Compare November 26, 2026 10:11
@cpersson-amd

Copy link
Copy Markdown
Contributor Author

Thanks for the review @msaffari-amd! All addressed in 09a4d37 (rebased on current main):

  • CI coverage: added test_dsv4_fhmoe_fp8_route_out_matches_fp32 to op_tests/test_fhmoe.py, which CI runs with pytest. It runs DSv4 FHMoE with AITER_FLYDSL_STAGE2_FP8=1 at the tuned t32x128x128_reduce_persist row (4 tokens) and the t32x256x128_reduce_bnt2_persist row (8 tokens), and compares against the fp32 reference and the BF16 route-row output. It also asserts the tuned stage 2 it runs and that FP8 route rows were actually used. I kept the direct stage-2 cases in test_flydsl_moe.py as the minimal non-shared reproduction, rather than pulling that whole file into CI.
  • FHMoE tile_n=256 with a shared expert: confirmed numerically, not just in the ISA. Relative L2 against fp32 with FP8 route rows, before → after: 0.426 → 0.031 at the tile_n=128 row and 0.207 → 0.033 at the tile_n=256 shared-halves row (BF16 route rows: 0.016 / 0.019). The Summary, Motivation and self-review now cover both cases and credit you.
  • The shared-halves test: that's the 8-token case of the new test above.
  • cshuffle_nlane: the description now says min(32, body_tile_n // 8) and explains both the body_tile_n and the min(32, ...) parts.
  • [MoE] Add fused_moe(stage2_fp8=True): per-call FP8 stage-2 route output #8174: happy for this to merge first, but as far as I can tell [MoE] Add fused_moe(stage2_fp8=True): per-call FP8 stage-2 route output #8174 doesn't reach this epilogue. Its stage2_fp8 only reaches _flydsl_v2_stage2_wrapper (layout kernels) and the MXMOE metadata, the v1 _flydsl_stage2_wrapper branch doesn't get it, and FHMoE rejects the argument. Only AITER_FLYDSL_STAGE2_FP8=1 hits the bug.

@Bernard-Liu

Copy link
Copy Markdown
Contributor

Is test_flydsl_stage2_a8w4_fp8_route_out in op_tests/flydsl_tests/test_flydsl_moe.py still needed? I see it was kept as the minimal non-shared reproduction, but that file is not collected by CI, and test_dsv4_fhmoe_fp8_route_out_matches_fp32 already covers the same epilogue there: at 4 tokens both the routed and the shared tiles run with body_tile_n=128, and the test fails before the fix (rel L2 0.426). If you don't rely on the direct case as a manual repro, it could be dropped so the PR adds only the CI-run test.

test_flydsl_moe.py is not collected by CI, and
test_dsv4_fhmoe_fp8_route_out_matches_fp32 in test_fhmoe.py covers the
same epilogue: 128-column routed and shared bodies at 4 tokens, and
256-column routed bodies at 8 tokens.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Carl Persson <[email protected]>
@cpersson-amd

Copy link
Copy Markdown
Contributor Author

@Bernard-Liu Agreed, it isn't needed. Dropped in 867a206, so the PR now adds only the CI-run test. The FHMoE test covers both parametrizations of the direct case: at 4 tokens the routed and shared tiles both run with a 128-column body, and at 8 tokens the routed tiles keep a 256-column body (the path where the 8-wide fragment was already correct) while the shared expert runs as two 128-column halves. Description updated to match.

@Bernard-Liu
Bernard-Liu merged commit 3486e43 into ROCm:main Nov 30, 2026
64 checks passed
cpersson-amd added a commit to cpersson-amd/aiter that referenced this pull request Nov 30, 2026
ROCm#7949 dropped this test on main: test_fhmoe.py covers the tile_n 128 and
256 FP8 route-out epilogue. test_flydsl_stage2_fp8_route_out_name still
checks that the FP8 name is bit-exact with AITER_FLYDSL_STAGE2_FP8=1 and
close to the reference.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Carl Persson <[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.

5 participants