Skip to content

[FlyDSL] [Perf] gfx1250 MoE: router writes MXFP4 token rows, GEMM1 gathers them (no decode A1 route/quant launch) - #8461

Draft
XiaobingSuper wants to merge 15 commits into
ROCm:mainfrom
XiaobingSuper:xiaobingsuper/gfx1250-moe-router-quant-rows
Draft

XiaobingSuper wants to merge 15 commits into
ROCm:mainfrom
XiaobingSuper:xiaobingsuper/gfx1250-moe-router-quant-rows

Conversation

@XiaobingSuper

@XiaobingSuper XiaobingSuper commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

What

For the decode TP grouped MoE on gfx1250, the fused router launch (#8431's flydsl_moe_router_topk) also quantizes its input, so the grouped MoE skips its A1 route/quant launch:

Why

At decode the A1 launch re-reads the hidden rows and writes them in expert order: a launch and a kernel switch (about 0.8 us idle in a CUDA graph on MI455X) on the critical path. The router already reads every row; writing the quantized rows costs it 0.3-0.9 us.

Evidence

MI455X, one MiniMax-M3.1 TP4 routed-MoE decode layer (hidden 6144, E128 top-4, MXFP4), CUDA graph, us:

T router (base / + rows) layer MoE glue
64 3.95 / 4.52 56.74 -> 55.32 11.23 -> 9.81
128 3.96 / 4.54 61.78 -> 58.92 10.64 -> 7.78
192 4.05 / 4.72 65.74 -> 63.27 13.33 -> 10.86
256 4.16 / 4.80 71.63 -> 69.51 13.25 -> 11.13

MoE output byte-identical with and without the router rows, eager and in a CUDA graph: 154/154 cases (14 token counts from 64 to 512, ten routing kinds), and bit-exact at T = 8-64. MiniMax-M3.1 TP4 with the ATOM wiring (ROCm/ATOM#3714): serving gate passes, GSM8K (1319, 5-shot) 0.9545.

Stack

On #7543 (lirui927), #8431 and #8430; please review only the top commit. The two merges below it combine those branches unchanged (the #7543 merge keeps both #7543's TP route-in-quant and #8430's MXFP8 copy in the A1 quant builders).

PR Self-Review

Reviewed revision

3212aa5

Behavior and intent

Default (no switch): taken wherever the A1 launch would be #7543's TP route-in-quant and the caller passes rows for the same topk_ids tensor -- topk 4, MXFP4 experts, decode batches of 8 to 512 tokens (the fused route's psum capacity), one K wave, no A-scale prologue or row-major scales; with a shared fold only if the router also wrote the MXFP8 copy at the fold's capacity. Every other call keeps its launches.

Validation and unverified scope

Measured on an earlier aiter base (576d3f0) with an earlier revision of #7543. Since then only FlyDSL compile-only checks; needs a GPU check before ready: the router-rows MoE output against the A1 path, byte for byte, at T = 8-512, eager and CUDA graph.

PR-specific question

Only gfx1250 TP grouped A8W4 MoE calls that are handed router rows change; the rows are the A1 quant's own bytes, so GEMM1 output is identical.

Changed op tests

None in this commit.

🤖 Generated with Claude Code

lirui927 and others added 7 commits November 16, 2026 10:21
Add tp_route_params to flydsl_moe_fused_quant_preshuffle. With it, the
quant launch does the work of moe_route_psum_fused itself: every quant
workgroup loads all expert ids, counts them in LDS (all routes, and the
routes before its own), scans the tile-aligned counts, and gives its own
routes their rows. It writes topids_to_rows, and block 0 writes masked_m
and psum, so gemm1, gemm2 and the gather-reduce read what they read before.
No workgroup waits on another, and nothing is left in global memory between
calls.

Both quant kernels the route batches use take it: the route-indexed kernel,
and the K-split build of the token-multidest kernel (from 64 tokens), which
gets a tp_route_slots build flag; its two kernel entry points share one
quant context. The id loads and the
lane's hidden-row loads are issued before the first barrier, so the route
work hides under them. Each thread loads 8 id slots, enough for the 2048
routes the fused TP route serves, and skips the slots a smaller call does
not use, so one build serves every batch.

Rows within an expert follow route order, the same in every block. The
single-workgroup route assigns them with LDS atomics, so its order varies
from call to call; each route's quantized row, masked_m and psum are bitwise
those of the split path, and the MoE output too, because no GEMM row or
gather-reduce result depends on where a row sits.

The wrapper refuses buffers the kernel cannot write in place (non-int32 or
strided topids_to_rows, masked_m or psum, too few grouped rows) and the
modes the fused route does not cover (prequantized, A-preshuffle, remap,
EP psum, row-major scales, num_valid_routes, more than 256 experts). The
quant kernels without the route compile to the same ISA as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Small TP batches (token_num * topk <= 2048) build their routes with
moe_route_psum_fused, one workgroup that counts, scans and remaps every
route, and then quantize the bf16 hidden rows into expert order in a second
launch. On MiniMax-M3 TP4 decode (E = 129, top-5 with the shared expert on
every token) that route launch runs once per MoE layer.

The grouped MoE now hands those batches to the quant launch with
tp_route_params, so a MoE call runs 4 kernels instead of 5. It is taken
for the fused TP route batches with bf16 input, E <= 256, no a1_scale, no
A-preshuffle and not the compact-scale quant pair; every other batch keeps
its launches. AITER_TDM_FUSE_TP_ROUTE_QUANT=0 restores the separate route
launch, like AITER_TDM_FUSE_PSUM_QUANT does for the EP psum.

The compact-scale decision is now made once per call and shared by this
gate and the quant dispatch.

test_grouped_skips_pad_rows_routed_to_minus_one checked that a 128-token
TP batch with pad rows routed to -1 reaches the token-multidest quant; that
batch now takes the route-in-quant build. The test now runs it both ways,
with the route in its own launch and in the quant, and checks the same
zero pad rows and unchanged real rows on each.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
op_tests/test_moe_tp_route_quant_fused.py compares the fused route + quant
with fused_route_psum_remap followed by the quant, at 1 to 409 tokens with
M3 routing (E = 129, top-4 plus a shared expert), padded TP rows and
dropped or out-of-range ids:

- masked_m, psum and topids_to_rows equal a CPU reference;
- payload and scales equal the split path's once each route's row is moved,
  with sentinel-filled outputs so an unwritten row fails;
- a HIP graph replayed with new routes rewrites every route output;
- buffers the kernel cannot write in place are refused;
- the public fused_moe output (a4w4 and a8w4) is bitwise equal with the
  switch on and off, and only the switch-off call launches the route.

python3 op_tests/test_moe_tp_route_quant_fused.py runs the same checks over
a token sweep and prints the split and fused route + quant times.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…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]>
…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]>
@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 8461 --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

XiaobingSuper commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Reverted (see the next comment, 6f6cd37): the read-back described below is no longer on this branch.

Update: two commits on top of this branch (fast-forward):

Why. This is the fix for the stray token-0 bug, also applied to the MXFP8 GEMM in #8273. Our validated stack carried it in this launcher too. Re-checked: FlyDSL compile-only for gfx1250 (GPU-less container): the tp_gather GEMM1 at 16x256 / 16x384 / 32x384 with cq_lds_store 0 and 1, plus the plain GEMM1 / GEMM2 launcher, 32 of 32 compile; py_compile, black and ruff clean. Not re-run on GPUs.

@XiaobingSuper

Copy link
Copy Markdown
Contributor Author

Update: pushed 6f6cd37, which reverts the TDM C-store read-back added earlier today: the cq_lds_store wiring for launch_gemm_a8w4_tdm_tpg (f3c9c30) and the merge of the grouped-MoE read-back (77be2e9). Scope decision: only the gemm_a8w8_gfx1250 read-back ships (in #8273, where all failures were located). The grouped MoE read-back cost about 4% of prefill time (uncached prefill TTFT +3.7-4.5%, C64 P90 274.7 vs 281-283), with no observed failure in these kernels, and is recorded as a latent risk in #8273's hardware / compiler note. The branch tree is again that of 9baf599 (no force-push).

XiaobingSuper and others added 4 commits November 23, 2026 08:31
… launch

Add flydsl_moe_router_topk: the decode MoE front pair
tgemm.mm(x, w_router) (the gfx1250 FlyDSL A16W16 split-K GEMM, bf16
logits) + topk_gating(..., score_func="sigmoid") in one FlyDSL launch with
the same bytes:

- logits: each 16-row token tile is one workgroup cluster of
  split_k x expert groups; the WMMA chains, operands and split order are
  those of the tuned FlyDSL GEMM, and the partials are reduced after the
  cluster barrier (no flags or polling);
- top-k: topk_gating_kernel_opt's sigmoid, bias, sorting network, lowest-lane
  tie break and renormalization, one wave per row.

router_split_k(m, n, k, dtype, otype) reads the tuned A16W16 config and
returns the split_k to use, or None when the shape is not served by the
gfx1250 FlyDSL FT/HT GEMM (the caller keeps the pair then). The kernel can
also write per-8-row-group expert counts (group_counts, optional).

MiniMax-M3.1 TP4 router (E128, K 6144, top-4), 4x MI455X, CUDA graph,
us per call:
  T              64    128   192   256   320   512
  GEMM + gating 6.17  6.39  7.09  7.17  7.90  7.46
  fused         3.93  4.02  4.14  4.25  6.77  6.06
op_tests/test_flydsl_moe_front.py: topk_weights / topk_ids bitwise equal to
tgemm.mm + topk_gating for T = 1..512 with random, tied, saturated and
padded (num_valid_rows) inputs (283/283 cases on the validation stack), and
the group counts against a histogram of the ids.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…#8430 + ROCm#8431

# Conflicts:
#	aiter/ops/flydsl/grouped_moe_gfx1250.py
#	aiter/ops/flydsl/kernels/moe_fused_route_quant_scatter.py
#	aiter/ops/flydsl/moe_kernels.py
…thers them

For the decode TP grouped MoE on gfx1250, the fused router launch
(flydsl_moe_router_topk) also quantizes every token row to MXFP4 (and, for a
folded shared expert, the MXFP8 copy): the bytes the A1 route/quant writes
per routed row. A caller hands the rows to the grouped MoE with
moe_router_quant_rows(topk_ids, rows); GEMM1 then re-derives the TP route
from topk_ids in a tp_gather build of the TDM task and gathers the rows, and
the A1 launch is skipped.

Taken by default where the A1 launch would be ROCm#7543's TP route-in-quant:
topk 4, MXFP4 experts, decode batches of 8 to 512 tokens (the fused route's
psum capacity), one K wave, no A-scale prologue or row-major scales, and
only for a call routed by the same topk_ids tensor. Every other call keeps
its launches.

MI455X, one MiniMax-M3.1 TP4 routed-MoE decode layer (hidden 6144, E128
top-4): MoE output byte-identical with and without the router rows in 154/154
cases (T 64-512, ten routing kinds; T 8-64 bit-exact as well), eager and in a
CUDA graph; MoE glue 11.23 -> 9.81 / 10.64 -> 7.78 / 13.33 -> 10.86 /
13.25 -> 11.13 us at T = 64 / 128 / 192 / 256.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@XiaobingSuper
XiaobingSuper force-pushed the xiaobingsuper/gfx1250-moe-router-quant-rows branch from 6f6cd37 to 3212aa5 Compare November 23, 2026 08:33
XiaobingSuper added a commit to XiaobingSuper/ATOM that referenced this pull request Nov 23, 2026
… for GEMM1

With aiter's router_quant_rows_buffers / moe_router_quant_rows
(ROCm/aiter#8461), the fused router (flydsl_moe_router_topk) also writes the
MoE input as token-major MX rows, and the MXFP4 experts' decode GEMM1 gathers
them in place of the A1 route/quant launch. aiter decides per batch (token
range, top-k); where it does not take them, or an MXFP8 copy of the input is
requested, the A1 route/quant runs as before. GEMM1 and the MoE output are
byte-identical.

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

With aiter's router_quant_rows_buffers / moe_router_quant_rows
(ROCm/aiter#8461), the fused router (flydsl_moe_router_topk) also writes the
MoE input as token-major MX rows, and the MXFP4 experts' decode GEMM1 gathers
them in place of the A1 route/quant launch. aiter decides per batch (token
range, top-k); where it does not take them, or an MXFP8 copy of the input is
requested, the A1 route/quant runs as before. GEMM1 and the MoE output are
byte-identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
XiaobingSuper and others added 2 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]>
XiaobingSuper added a commit to XiaobingSuper/aiter that referenced this pull request Nov 23, 2026
Co-Authored-By: Claude Opus 5.5 <[email protected]>

# Conflicts:
#	aiter/ops/flydsl/grouped_gemm_mxfp4.py
@XiaobingSuper
XiaobingSuper force-pushed the xiaobingsuper/gfx1250-moe-router-quant-rows branch from 3742141 to d1fe3ae Compare November 23, 2026 10:04
Same shape as ROCm#8431's simplified op test, on this branch's router_split_k API.

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

With aiter's router_quant_rows_buffers / moe_router_quant_rows
(ROCm/aiter#8461), the fused router (flydsl_moe_router_topk, on the
calls flydsl_moe_router_topk_supported takes) also writes the MoE input
as token-major MXFP4 rows, and the MXFP4 experts' decode GEMM1 gathers
them in place of the A1 route/quant launch. aiter decides per batch
(token range, top-k) by returning no buffers; then the A1 route/quant
runs as before. GEMM1 and the MoE output are the same bytes.

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