Skip to content

[FlyDSL] [Perf] gfx1250 MoE: defer the decode gather-reduce into the k5 all-reduce + RMSNorm - #8457

Draft
XiaobingSuper wants to merge 15 commits into
ROCm:mainfrom
XiaobingSuper:xiaobingsuper/gfx1250-moe-tail-in-k5-ar
Draft

XiaobingSuper wants to merge 15 commits into
ROCm:mainfrom
XiaobingSuper:xiaobingsuper/gfx1250-moe-tail-in-k5-ar

Conversation

@XiaobingSuper

@XiaobingSuper XiaobingSuper commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

What

moe_defer_tail() (aiter.ops.flydsl.moe_deferred_tail): inside it, a gfx1250 grouped MoE call skips its last launch (the gather-reduce, plus the routed scale and shared-expert add) and returns its output unwritten, registered with the launch's operands. The all-reduce that takes that tensor (tensor_model_parallel_fused_allreduce_rmsnorm{,_mxfp8_quant}) either computes the rows in its prologue -- the k5 one-shot kernel (#8456), CustomAllreduce.k5_fused_ar_rms_tail, the same bits as the gather-reduce followed by the AR -- or, on every other path, runs the skipped launch first. The registry keeps the operands alive until the consumer takes them, eager or under graph capture. Outside moe_defer_tail() nothing changes.

Why

At decode sizes the gather-reduce only feeds the next AR + RMSNorm, which reads every row once anyway; folding it into that load removes one launch per MoE layer (about 2 us plus a kernel switch).

Evidence

MiniMax-M3.1 TP4 decode on 4x MI455X, with the ATOM side (routed GEMM2 + router on the AR's stream and this tail): decode step 4-6% shorter at bs 4 / 8 / 10 / 16; bs 1 80.5 -> 75.1 us per layer. Serving gate passes; GSM8K (1319, 5-shot) 0.9545.

Gated on

Only inside moe_defer_tail() (the ATOM MiniMax-M3 decode MoE); the k5 tail where k5 takes the call (gfx1250, TP4, hidden 6144, <= 256 rows), otherwise the skipped launch runs first. Producer and consumer must run eagerly, in program order.

Needs GPU check before ready: rebased onto the reworked #8456 (the tail now always uses k5's fused MXFP8 output) and onto the current #8430; not run on GPU since.

PR Self-Review

Reviewed revision

bab1256

Behavior and intent

New opt-in-by-caller deferral of the decode MoE tail into the following all-reduce; no change outside moe_defer_tail().

Validation and unverified scope

Measured on an earlier aiter base (576d3f0); this revision: hipcc -fsyntax-only on the k5 kernel files, ruff 0.16.0 clean. Not run on GPU.

PR-specific question

Stacked on #8456 and #8430 (shared expert fold, with #8429 and #8364), merged; please review only the top commit.

Changed op tests

op_tests/multigpu_tests/test_ar_k5_tail_gfx1250.py (new): out / residual_out / scale bytes against the gather-reduce followed by the AR on every rank, eager and CUDA graph, over random, duplicated, padded, hot-expert, tied and special-value routings.

🤖 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 <[email protected]>
…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 <[email protected]>
…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 <[email protected]>
@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 8457 --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.

XiaobingSuper and others added 2 commits November 23, 2026 01:34
…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 <[email protected]>
tensor_model_parallel_fused_allreduce_rmsnorm_quant(quant_type="mxfp8")
(and tensor_model_parallel_fused_allreduce_rmsnorm_mxfp8_quant) returns E4M3
values with row-major group-32 E8M0 scales. On gfx1250 up to 128 rows where
the Lamport one-shot applies it runs the Gluon AR + RMSNorm + MXFP8 one-shot;
otherwise the Lamport / existing fused AR + RMSNorm followed by
per_1x32_mx_quant_hip. The op is registered with mutates_args=[] (it writes
no argument; Inductor cannot remove an auto_functionalized_v2 that returns
E8M0).

Hidden 6144 bf16, TP4, Gemma norm, CUDA graphs, 4x MI455X, us per call
(cap at 128 rows vs. one-shot everywhere up to 4 MiB):
  rows    128   192   256   320   384   512
  before 14.6  25.8  26.1  28.5  26.7  29.1
  after  14.7  18.3  19.5  25.9  26.3  29.3

Co-authored-by: Cursor <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@XiaobingSuper
XiaobingSuper force-pushed the xiaobingsuper/gfx1250-moe-tail-in-k5-ar branch from d195bd2 to 1587558 Compare November 23, 2026 08:43
XiaobingSuper added a commit to XiaobingSuper/ATOM that referenced this pull request Nov 23, 2026
…irst, tail in the AR

With aiter's moe_defer_tail (ROCm/aiter#8457) and the k5 decode all-reduce
(ROCm/aiter#8456), MiniMax-M3's decode MoE (glue epilogue or shared fold, TP >
1, one PP stage, up to 256 rows, nothing but the next all-reduce reading the
layer's output) leaves its gather-reduce (+ routed scale + shared add) to the
all-reduce + RMSNorm that reads it. With the glue epilogue and the fused router
on the dual-stream path, the router launches before the shared MLP's kernels,
so the routed chain is not queued behind them. Same kernels and bytes; other
configurations keep the existing order and the separate gather-reduce.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
XiaobingSuper added a commit to XiaobingSuper/ATOM that referenced this pull request Nov 23, 2026
…irst, tail in the AR

With aiter's moe_defer_tail (ROCm/aiter#8457) and the k5 decode all-reduce
(ROCm/aiter#8456), MiniMax-M3's decode MoE (glue epilogue or shared fold, TP >
1, one PP stage, up to 256 rows, nothing but the next all-reduce reading the
layer's output) leaves its gather-reduce (+ routed scale + shared add) to the
all-reduce + RMSNorm that reads it. With the glue epilogue and the fused router
on the dual-stream path, the router launches before the shared MLP's kernels,
so the routed chain is not queued behind them. Same kernels and bytes; other
configurations keep the existing order and the separate gather-reduce.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
XiaobingSuper and others added 5 commits November 23, 2026 09:49
fused_allreduce_rmsnorm on gfx1250 fell back to RCCL + a separate add +
RMSNorm above the Lamport kernels' range. Add a pushed two-shot all-reduce +
residual add + (Gemma) RMSNorm kernel (ar2s_rms_gfx1250) and route bf16
inputs of 32-288 MiB to it at TP4:

- phase 1 pushes each row chunk to its owner's RS inbox, phase 2 sums the own
  chunk in fp32 in rank order, rounds once to bf16, pushes it to every peer's
  AG inbox and normalizes it, phase 3 normalizes the peers' rows;
- block b only waits for block b of its peers (a flag per phase, sender and
  block, epoch counters), so no barrier is needed, eager or in a CUDA graph;
- row-local barriers are LDS-only and only a flag waits for the store drain;
  the three phases are pipelined per step; phase 2 prefetches the next row's
  summands into LDS asynchronously; flag polls are bounded (no hang);
- out / residual_out follow the Lamport fused kernel step by step.

TP4, hidden 6144 bf16, Gemma norm, 4x MI455X, eager, us per call, measured
with the first, unpipelined version of this kernel (same layout, flags and
arithmetic, so the same outputs):
  tokens   2752  4096  6144  8192  12288  24576
  before  108.1 147.9 258.2 299.7  368.1  537.4
  after    85.3 104.9 145.5 182.7  256.1  502.4

residual_out matches an fp32 reference bit for bit, out is within 1 bf16 ulp
for < 1e-5 of the values (4 ranks, eager and graph, 13 row counts including
non-multiples of 4 and a split launch). Gated on gfx1250, TP4 (the measured
world size), bf16, hidden a multiple of 256 up to 8192, 32-288 MiB.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Extend the pushed two-shot AR + RMSNorm (ar2s_rms_gfx1250) with ar2s_rms_ex
and three modes, exposed as sequence-parallel tensor_model_parallel_sp_* ops
for prefill:

- FP8: the norm output is written as MXFP8 (E4M3 + row-major E8M0 per 32),
  the bytes per_1x32_mx_quant_hip gives for the bf16 out (the group max is
  floored at kAmaxFloor as there, so an all-zero group gets scale 85).
- SP: each rank keeps the residual of its own row chunk only; the owner
  normalizes (and quantizes) its rows and pushes the result, so the
  all-gather moves MXFP8 and phase 3 is a copy. The normed output is whole
  on every rank and bit-identical to the replicated path.
- AUX (with SP): no norm, the whole all_reduce(x) + residual on every rank.

tensor_model_parallel_sp_fused_allreduce_rmsnorm{,_mxfp8_quant} and
tensor_model_parallel_sp_allreduce_add_residual take the SP path at the ar2s
sizes and are the replicated ops below it, so every call of a forward takes
the same branch (mutates_args=[]).

The TP4 per-launch cap is 192 MiB, one launch for a 14336-token chunk of
hidden 6144.

TP4, hidden 6144, Gemma norm, 4x MI455X: AR + RMSNorm + MXFP8 at 14336 rows
347 -> 226 us a call (SP + MXFP8 all-gather vs ar2s + separate quant);
sp_fp8 at 14336 / 32768 rows 189.0 / 401.2 us, bit-exact.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-256 rows

A one-shot all-reduce + residual add + Gemma/RMS norm kernel for decode
sizes (hidden 6144, up to 256 rows) with an MXFP8 epilogue (E4M3 +
row-major E8M0 per 32). On gfx1250 TP4 it takes the calls the Gluon one-shot
(<= 128 rows) or the Lamport fused kernel (above) would take, with their
bits: lamport_order picks the summation order of the path it replaces, and
the MXFP8 epilogue floors the group max at kAmaxFloor as
per_1x32_mx_quant_hip does, so it replaces the Lamport kernel + quant pair
in one launch.

- Its inbox (3 x 4 x rows x hidden) and counters follow the Lamport AR +
  RMSNorm inbox in the same IPC allocation: no IPC buffer of its own.
- Flag polls are bounded; a timeout is counted (k5_ar_errors) instead of
  hanging.

4x MI455X, TP4, CUDA graph of 10 calls, us per call (production path ->
k5): bf16 80 rows 8.29 -> 7.46, 128 rows 9.83 -> 8.64, 192 rows 15.75 ->
11.61; MXFP8 80 rows 8.94 -> 8.16, 128 rows 11.28 -> 9.07, 192 rows 18.14 ->
12.09.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ROCm#8456 (k5 decode AR)

# Conflicts:
#	aiter/ops/flydsl/grouped_moe_gfx1250.py
#	aiter/ops/flydsl/moe_kernels.py
…o the k5 all-reduce + RMSNorm

At decode sizes the grouped MoE's last launch, the gather-reduce (+ routed
scale + shared-expert add through moe_output_epilogue or the shared fold),
only feeds the next all-reduce + RMSNorm, which reads every row of it once.

Inside moe_defer_tail() (aiter.ops.flydsl.moe_deferred_tail) the grouped
MoE call skips that launch: its output tensor is returned unwritten and a
MoeDeferredTail with the launch's operands is registered under the output's
storage. The all-reduce that takes that tensor either computes the rows in
its prologue -- the k5 one-shot kernel (CustomAllreduce.k5_fused_ar_rms_tail,
bf16 or MXFP8 out, the same bits) -- or, on every other path and entry
point, runs the skipped launch first (MoeDeferredTail.materialize). The
registry keeps the operands alive until the consumer takes them, so no
allocation reuses their storage in between, eager or under graph capture.
Outside moe_defer_tail() nothing changes.

op_tests/multigpu_tests/test_ar_k5_tail_gfx1250.py compares out /
residual_out / scale bytes against the gather-reduce followed by the AR on
every rank, eager and in a CUDA graph, over random, duplicated, padded,
hot-expert, tied and special-value routings, and times both.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@XiaobingSuper
XiaobingSuper force-pushed the xiaobingsuper/gfx1250-moe-tail-in-k5-ar branch from 1587558 to bab1256 Compare November 23, 2026 09:50
XiaobingSuper and others added 4 commits November 23, 2026 10:02
…t an epilogue

`ptr_arg(None)` raised AttributeError on the plain (no addend) path and in
test_gather_reduce. Pass a 0-element tensor, as num_valid_tokens already does.
Also sort MoeOutputEpilogue.__slots__ (ruff RUF023).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With a bf16 output the one-shot k5 is slower than the path it replaces above
128 rows (4x MI455X, graphs: 15.06 vs 14.18 us at 160, 18.05 vs 17.06 at
256); with the MXFP8 output it is faster at every size up to 256 rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
XiaobingSuper added a commit to XiaobingSuper/ATOM that referenced this pull request Nov 30, 2026
…irst, tail in the AR

With aiter's moe_defer_tail (ROCm/aiter#8457) and the one-shot decode
all-reduce (ROCm/aiter#8456), MiniMax-M3's decode MoE with the glue
epilogue (TP > 1, one PP stage, up to 256 rows, nothing but the next
all-reduce reading the layer's output) leaves its gather-reduce (+ routed
scale + shared add) to the all-reduce + RMSNorm that reads it. A forward
hook on the layer or a forward pre-hook on the next one (DSpark / Eagle3
taps) keeps the gather-reduce in the MoE.

With the glue epilogue and the fused router on the dual-stream path, the
shared MLP still forks at the MoE input but launches right after the
router (FusedMoE runs moe_after_router_hook there), so the routed chain
is not queued behind the shared MLP's kernels.

Same kernels and bytes; other configurations keep the existing order and
the separate gather-reduce.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
No behavior change.
- op_tests/multigpu_tests/test_ar2s_rms_gfx1250.py and
  test_ar2s_mxfp8_zero_group.py (from ROCm#8428's earlier head): no shebang on
  a non-executable file (EXE001), the timed lambdas bind their loop
  variables through functools.partial (B023), unused unpacked values are
  _-prefixed (RUF059), one combined `with` (SIM117), imports sorted with
  aiter first-party (I001).
- moe_deferred_tail.py: __all__ and __slots__ sorted (RUF022, RUF023; nothing
  reads the slots' order), and materialize's imports sorted (I001).
- communicator_cuda.py: the deferred-tail imports one aliased name a
  statement (I001), and the three `# noqa: E731` dropped (RUF100: E731 is
  not enabled).
- custom_all_reduce.py: the k5 tail entry's imports sorted (I001).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

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