Repository navigation
[FlyDSL] [Perf] gfx1250 MoE: router writes MXFP4 token rows, GEMM1 gathers them (no decode A1 route/quant launch) - #8461
Conversation
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]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
|
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 |
|
Update: pushed 6f6cd37, which reverts the TDM C-store read-back added earlier today: the |
… 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]>
…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]>
6f6cd37 to
3212aa5
Compare
… 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]>
… 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]>
…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]>
…7, C408) Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> # Conflicts: # aiter/ops/flydsl/grouped_gemm_mxfp4.py
Co-Authored-By: Claude Opus 5.5 <[email protected]>
3742141 to
d1fe3ae
Compare
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]>
… 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]>
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:quant_rows=(payload, E8M0), the router writes every token row as MXFP4: exactly the bytes the A1 quant writes for each of that token's routed rows;quant_rows8adds the MXFP8 copy a folded shared expert ([FlyDSL] [Perf] gfx1250 MoE: fold a shared MXFP8 expert into the routed MXFP4 GEMMs #8430) reads;tp_gatherbuild of the TDM task,launch_gemm_a8w4_tdm_tpg) re-derives the TP route in LDS from allT x 4ids the way [FlyDSL] perf(flydsl): fold the gfx1250 TP MoE route into the activation quant #7543's route-in-quant does, gathers its tile's rows, and writespsum,masked_mandtopids_to_rows;router_quant_rows_buffers(tokens, model_dim, device, topk)allocates the rows (None where a call would not take them);moe_router_quant_rows(topk_ids, rows)hands them to the grouped MoE run inside the block (eager, thread-local).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:
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_idstensor -- 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