Skip to content

[FlyDSL] [Perf] gfx1250 MoE: fold a shared MXFP8 expert into the routed MXFP4 GEMMs - #8430

Draft
XiaobingSuper wants to merge 4 commits into
ROCm:mainfrom
XiaobingSuper:xiaobingsuper/gfx1250-moe-shared-expert-fold
Draft

XiaobingSuper wants to merge 4 commits into
ROCm:mainfrom
XiaobingSuper:xiaobingsuper/gfx1250-moe-shared-expert-fold

Conversation

@XiaobingSuper

@XiaobingSuper XiaobingSuper commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Do not enable by default until fixed. The fold's GEMM2 launch is nondeterministic (see "Known issue" below). Run with ATOM_MINIMAX_M3_SHARED_FOLD=0 (the default) until the fix lands.

What

moe_shared_expert_fold(weights, scale) runs a dense MXFP8 shared expert inside the next gfx1250 grouped MoE call (_grouped_a8w4_tdm_moe). The shared expert is treated as one more expert that every token routes to, with weight 1.

The two workgroup classes share one LDS arena and never wait on each other.

To support the dense tiles, build_gemm_a8w4_tdm_task gains four knobs:

  • b_fp8: row-major FP8 B, staged and read like the FP8 activation.
  • inline_task: one tile per call, as an inlinable helper.
  • dense_rows: a single expert over rows 0 .. rows, with no tile map.
  • c_stage_apart: stage C over input stage 1 instead of stage 0.

All of them default to 0, so the routed tiles compile exactly as before.

prepare_shared_fold_weights() lays the weights out once at load: gate/up interleaved, scales in n32k4 order, a byte permutation only. The result lives in storage the fold owns, so a caller may still shuffle its own parameters in place afterwards. This was a real bug in our first integration: .contiguous() aliased a parameter that ATOM later shuffled.

The fold is only taken for unclustered decode tiles without EP, compact or prequantized A1, or bias. Otherwise consumed stays False and the caller runs the shared expert itself.

Why

MiniMax-M3.1 runs its shared expert as two more GEMMs plus quant launches beside the routed MoE: on a second stream, or serially. At decode sizes these launches are latency-bound. Folding them into the routed launches removes four launches and the stream join.

Performance

MiniMax-M3.1 TP4 MoE layer (E128 top-4 MXFP4 routed + MXFP8 shared, hidden 6144), 4x MI455X, CUDA graph, us per layer:

T routed + shared, two streams routed + shared, serial fold
64 69.1 76.1 65.6
128 78.4 83.5 72.7
192 81.7 88.7 78.0
256 82.6 90.8 81.0

Correctness

  • Not bitwise the separate shared MLP. The fold does not round the gate/up and SwiGLU outputs to bf16 before its MXFP8 quant, and its K order differs.
  • Shared-expert accuracy. rel-L2 vs an fp32 reference is 2.0e-3 for the fold and 1.7e-2 for the separate MLP.
  • MoE output. rel-L2 vs the current path is 1.4e-5.
  • Routed rows. The routed GEMM bytes are unchanged with and without dense tiles in the launch.
  • Model. MiniMax-M3.1 TP4 with the ATOM wiring (perf(minimax-m3): fold the shared expert into the routed MXFP4 MoE launches (opt-in) ATOM#3681): GSM8K (200 questions) scores 0.970 / 0.975 with the fold vs 0.970 without, and real-weight multi-layer rel-L2 is 3.2e-3 to 5.9e-3.

Known issue: nondeterministic fold GEMM2 launch (T <= 64, also seen at T = 80)

At decode T <= 64 the shared-fold GEMM2 launch is nondeterministic. That launch is the routed 16x128x128 tile with 4 buffers, plus the 64x64 shared tiles.

  • Symptom. With hot-expert routing (every token on the same 4 experts), about 16% of launches write garbage into a few 16-byte pieces of routed rows: |x| up to about 4e27, sometimes NaN. The pieces are columns 16-31 of a wave's second 32-column WMMA block, lanes 0/1/8/9.
  • Inputs are stable. Upstream tensors are byte-identical from run to run.
  • Other routings are clean. Random, duplicated and partly-hot routing at T = 64, and T = 96 / 128, measured 0/300.
  • Depends on the previous kernel. GEMM2 launched alone measured 0/300. GEMM1 followed by GEMM2 measured 20-47/300, even with a full device sync between them. The most likely cause is the launch reading LDS that GEMM2 never wrote.
  • Padding. Adding 16 KB of LDS padding removes it; 8 KB does not.
  • Also above 64 tokens. A later audit (11-22) found it at T = 80 as well: 4 of 3300 hot-routing launches, |x| up to 2.6e27. The GEMM2 binary used for all of T = 65-256 (the routed 16x128x128 3-buffer K768 tile with 32x64x128 shared tiles) has the same bf16 epilogue: C is written to LDS and then stored from LDS by TDM. The T <= 64 floor therefore does not make the fold safe.
  • Leading root cause. The TDM store reads LDS before the last ds_store_b128 writes are visible, despite s_wait_dscnt 0 and s_barrier, so it picks up stale LDS left by the previous kernel (GEMM1). A fix that stores C directly from registers is in progress.

Status: do not enable by default until fixed. ROCm/ATOM#3681 no longer enters the fold for batches of 64 tokens or fewer, which removes the most frequent case but does not make the fold safe (see T = 80 above). Until the register-store fix lands, run with ATOM_MINIMAX_M3_SHARED_FOLD=0.

End-to-end (combined stack, not this PR alone)

MiniMax-M3.1 on 4x MI455X, TP4: agentic multi-turn trace replay (AgentX) at concurrency 64, 900 s per run, same node, same afternoon.

The stack is our 1120 integration image plus all the changes in this series, with every flag on. That covers ROCm/aiter PRs 8273, 8310 and 8427-8432 and ROCm/ATOM PRs 3680-3683. It also includes pieces not yet in a PR: the ATOM wiring for the fused QKV chain, the fast o_proj and the FlyDSL indexer CP scorer, and the paged-attention split-KV fused reduce. Speculative acceptance length was forced to 4.04 for this A/B; the same configuration passed our serving gate with real acceptance.

tok/s/GPU P90 interactivity (tok/s/user) median interactivity ITL p50 / p90 TTFT p50 / p90
baseline 99,473 164.6 249.7 4.0 / 6.1 ms 0.52 / 1.16 s
combined stack 103,140 192.8 289.5 3.5 / 5.2 ms 0.46 / 1.05 s

Baseline P90 interactivity varied from 164.6 to 181.8 across three baseline runs that day (mean 173.4; mean median 253.7). Our best estimate is therefore about +11% P90 and +14% median interactivity against the mean baseline. That is for the whole stack; this PR's own share is not separated out.

Dependencies

Depends on #8364 (MXFP8 copy of the input from the A1 quant) and #8429 (gather-reduce epilogue); please review only the top commit. The two commits below it are those PRs, unmodified apart from being rebased onto main.

Needs re-validation (rebase changed code)

This was validated on our gfx1250 integration stack. Since then main refactored mxfp4_preshuffle_gfx1250_tdm.py: per-kernel explicit-VGPR attributes, load_b_half, place_vgpr, and DS_B = 4 for FP4. The port:

  • merges the FP8 load_b branch into main's load_b;
  • sets DS_B = 4 if a_is_fp4 or b_fp8 else 2;
  • sizes the B register slots with B_NDW inside main's USE_EXPLICIT_VGPR_PARTITION block;
  • drops the launcher-wide waves_per_eu/maxnreg hints and passes both tasks' kernel_attrs to the fold launch instead (empty for decode tiles);
  • replaces the stack's env_once() helper with os.environ.get.

Port compiles; GPU re-validation on main pending. It compiles offline for gfx1250 (COMPILE_ONLY) at the production GEMM1/GEMM2 decode configs.

Validation status: validated on the 1120 integration stack (aiter 576d3f0 / ATOM 6c20f00); not yet re-validated on main after rebase.

🤖 Generated with Claude Code

ruili and others added 3 commits November 21, 2026 14:14
…n the same pass

A shared expert run beside the routed MXFP4 MoE (MiniMax-M3.1) quantizes the
same bf16 rows to MXFP8 in its own launch. The routed A1 quant already reads
each row and reduces every 32-block's amax, so it can write that copy too:
fused_moe / flydsl_moe_fused_quant_preshuffle take `mxfp8_copy_out=(payload,
scale)`, row-major (M, K) E4M3 + (M, K/32) E8M0, the bytes of
per_1x32_mx_quant_hip(quant_dtype=fp8, scale_type=fp8_e8m0) (amax floored at
kAmaxFloor, RoundUp exponent against 448, v_cvt_scalef32_pk8_fp8_bf16).

The three quant builds the M3.1 grouped MoE dispatches take a `mxfp8_copy`
build flag (`_mx8cp` builds with two more pointers): token-multidest (plain
and K-split), the compact-scale quant, and the route-indexed quant (each
source row's first route writes it, mapped or not). Any other A1 path, or a
non-grouped MoE / FHMoE, fills the copy with the HIP quant instead. Without
the flag every build is unchanged: the kernel bodies moved into a @flyc.jit
body inlined into either signature.

fused_moe's plugin contract moves to version 3 (two more outputs a v2
provider would leave unwritten, so v2 is not superseded);
tests/plugin_contracts.json records it.

op_tests/test_moe_quant_shared_mxfp8.py: routed outputs bitwise unchanged and
the copy bitwise the split quant (and a torch model of the rule) for every
variant at 8..32768 tokens, the public fused_moe path, and timing of the fused
launch against routed quant + split quant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…shared-expert add

Add an optional output epilogue to the grouped MoE gather-reduce:
out = bf16(bf16(bf16(sum_k w * rows) * scale) + addend), each step rounded
like the separate torch ops it replaces (the multiply is skipped at scale
1.0), so the bytes are unchanged.

moe_output_epilogue(addend, scale, before=None) is a context manager that
hands the pending epilogue to the next _grouped_a8w4_tdm_moe call in the
block; `before` runs right before the gather-reduce launch (e.g. a wait for
the stream that produced addend) and `consumed` tells the caller whether a
gather-reduce took it. EP calls and other MoE paths leave it unconsumed and
the caller applies the scale and add itself. Python-side state: for eager
callers, not torch.compile tracing.

MiniMax-M3.1 TP4 shape (hidden 6144, E128 top-4, bf16), 4x MI455X, us:
  T                       64    128   192   256
  gather-reduce+mul+add  5.41  5.61  6.09  6.50
  fused gather-reduce    1.84  1.91  2.19  2.21
Output bitwise identical to the separate ops (eager and Inductor).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ed MXFP4 GEMM launches

moe_shared_expert_fold(weights, scale) runs a dense shared expert inside the
next grouped MoE call (_grouped_a8w4_tdm_moe), as one more expert every
token routes to with weight 1:

- GEMM1: launch_gemm_a8w4_tdm_shared_fold puts the shared gate/up tiles in
  the routed GEMM1 launch (leading workgroups, FP8 x FP8 on the MXFP8 copy of
  the input the A1 quant writes, SwiGLU and MXFP8 quant in the same
  epilogue), the routed tiles unchanged behind them;
- GEMM2: the shared down tiles in the routed GEMM2 launch (BF16 rows);
- the shared rows are added after the routed scale in the gather-reduce
  (moe_output_epilogue).

The MXFP4 TDM GEMM gains b_fp8 (row-major FP8 B, staged like the FP8
activation), inline_task, dense_rows and c_stage_apart knobs for the dense
tiles; the routed tiles compile as before. prepare_shared_fold_weights()
lays the weights out once (gate/up interleaved, n32k4 scales) into storage
it owns, so a caller may shuffle its own parameters in place afterwards. The
fold is taken only for unclustered decode tiles without EP, compact or
prequantized A1; elsewhere `consumed` stays False and the caller runs the
shared expert itself.

MiniMax-M3.1 TP4 MoE layer (E128 top-4 MXFP4 routed + MXFP8 shared,
hidden 6144), 4x MI455X, us per layer:
  T                         64    128   192   256
  routed + shared (2 streams) 69.1  78.4  81.7  82.6
  fold                       65.6  72.7  78.0  81.0
Shared-expert rel-L2 vs fp32: 2.0e-3 (separate MLP: 1.7e-2, which rounds the
gate/up and SwiGLU outputs to BF16 before its quant). Not bitwise the
separate MLP for that reason; MoE output rel-L2 vs the current path 1.4e-5.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@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 8430 --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.

…stores, not a TDM store

On gfx1250 a TDM store (tensor_store_from_lds) can read LDS bytes that
predate ds_stores the workgroup issued before `s_wait_dscnt 0` and
`s_barrier`. In the shared-fold GEMM2 (routed 16x128 with the 64x64 /
32x64 dense tiles), the C pieces of each wave's last ds_store, in the
first rows of its 8-row TDM box, sometimes came out as the input stage's
older A/B bytes. That happened in 0.2-10% of launches at T 48-80 under
hot-expert routing on MI455X. A ds_load after the same barrier always sees
the new data.

New build option c_lds_store (bf16/f16 C without EP scatter): after the
barrier that follows the C restage, the waves read the tile back from LDS
in whole-row 16-byte chunks and write it with buffer_store_b128. Rows past
mn_oob are dropped, as the TDM store's row extent drops them.
launch_gemm_a8w4_tdm_shared_fold turns it on for GEMM2. The kernel name
gains `_cldsst`.

MI455X, T 48-256, hot-expert routing: 0 bad launches in 84,000 (the TDM
store: up to 10%), bitwise the clean output, +0.0-0.6 us per GEMM2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@XiaobingSuper

XiaobingSuper commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Update: pushed 2d81a36, the fix for the nondeterministic shared-fold GEMM2 launch described in the body.

  • Root cause. On gfx1250, a TDM store (tensor_store_from_lds) can read LDS bytes that predate ds_stores the workgroup issued before s_wait_dscnt 0 + s_barrier. In the fold's GEMM2, the C pieces of each wave's last ds_store came out as the input stage's older A/B bytes. They landed in the first rows of each wave's 8-row TDM box. A ds_load after the same barrier always sees the new data.
  • Fix. A new build option, c_lds_store, is on for the shared-fold GEMM2. After the barrier, the waves read the bf16 C tile back from LDS in whole-row 16-byte chunks and write it with buffer_store_b128 instead of one TDM store per wave. The kernel name gains _cldsst.
  • Measured on MI455X, T 48-256, hot-expert routing:
    • 0 bad launches in 84,000, against up to 10% with the TDM store at T 48-80;
    • bitwise the clean output;
    • +0.0-0.6 us per GEMM2.
  • Validation. Kernel-level stress only. The ATOM side (perf(minimax-m3): fold the shared expert into the routed MXFP4 MoE launches (opt-in) ATOM#3681) still keeps its T <= 64 floor until the fold is re-validated in-model with this fix.
  • The TDM / LDS ordering gap has been written up separately for the compiler / hardware team.

@XiaobingSuper

Copy link
Copy Markdown
Contributor Author

Compile check for 2d81a36 (the GEMM2 C-store fix). FlyDSL compile-only for gfx1250 in a GPU-less container. These launch_gemm_a8w4_tdm_shared_fold builds compiled to gfx1250 code objects with the new c_lds_store epilogue:

  • GEMM2, routed 16x128x128 with 2 waves, 3 and 4 stages (the T 64 / 128-256 decode rows), each with the 64x64 and the 32x64 shared tiles;
  • GEMM1, routed 16x256x128 with the 64x64 shared tile. Its epilogue is unchanged (c_lds_store is off for GEMM1).

The 4-wave T=32 routed tile (16x256x128 w1x4) is rejected by the fold's own thread-count check, as before. _shared_fold_tile does not fold launches that are not 2 waves, so that tile is never built. Not re-run on GPUs.

XiaobingSuper added a commit to XiaobingSuper/aiter that referenced this pull request Nov 23, 2026
…-rows branch

The bugfix branch (C tile leaves LDS by a ds_load read-back + buffer stores)
sits on ROCm#8430; this branch sits on ROCm#8430 + ROCm#8431 + ROCm#7543. Conflicts were
context only, both sides kept: build_gemm_a8w4_tdm_task takes both the
tp_gather parameters and cq_lds_store, and the kernel name carries both the
`_cldsst` / `_cqldsst` and `_tpg` suffixes; grouped_gemm_mxfp4 keeps both
the tp_gather helpers and `_c_lds_store`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants