Repository navigation
Conversation
2c8902d to
3499407
Compare
The TP prefill route gate (_tp_route_quant_takes_route) read this switch from os.environ on every MoE call, as the A1 quant dispatch it has to agree with also does. token_multidest_quant_enabled() in moe_kernels reads it once (functools.cache) and both use it, so the gate and the dispatch cannot see different values. Same default and accepted values (1/true/True); a test that changes the variable clears the cache. ROCm#7543 adds the same helper, byte-identical at the same place, so the two PRs merge cleanly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The TP prefill route gate and the A1 quant dispatch (_compact_ascale) each called _use_fused_quant_preshuffle with the same arguments. From the compact-scale minimum up it builds the quant layout and costs about 6 us (fp4), so an eager MiniMax-M3 prefill paid it twice per MoE layer. _grouped_a8w4_tdm_moe now evaluates it at most once per call and hands the answer to the gate; same result, same order of evaluation. CPU (MiniMax-M3 TP4: E=129, topk 5, dim 6144, fp4 activations), gate plus dispatch question per MoE call, with the env commit: 512-1000 tokens 2.47 -> 1.62 us, 3073-8192 tokens 14.0 -> 7.6 us, of which 6.4 us is the dispatch's own question. The memo block and the _compact_ascale line are byte-identical to the ones ROCm#7543 adds. Co-Authored-By: Claude Opus 5.5 <[email protected]>
d8b5e99 to
b3edd0d
Compare
On gfx1250 one grouped MoE call (fused_moe -> grouped_gemm_gfx1250_a8w4 -> _grouped_a8w4_tdm_moe -> flydsl_grouped_gemm_a8w4_masked) read about 35 environment switches through os.environ.get: the twelve AITER_TDM_* tile overrides, the gemm cluster/persistent/TDM selectors, the activation-dtype bounds and the route/quant switches. One read costs about 0.3 us, so an eager MiniMax-M3 prefill (57 MoE layers) spent about 0.6 ms of host time per forward re-reading variables that do not change. Decode replays a HIP graph and does not pay it. aiter.utility.env_once.env_once(name, default) is os.environ.get cached per (name, default); 33 per-call sites in fused_moe, grouped_moe_gfx1250, grouped_gemm_mxfp4 and moe_kernels use it. It clears itself on any change made through os.environ (setitem/delitem, so monkeypatch.setenv, mock.patch.dict, pop and in-process tuning sweeps), so every caller still sees what os.environ.get would return now; no test needed a change. token_multidest_quant_enabled() keeps its form (shared with ROCm#7543) and is registered with clear_with_env_once, so one mechanism governs every cached switch on this path. One MI455X, eager fused_moe host time per call at MiniMax-M3 TP4 shapes (a4w4, E=129, topk 5), this PR before -> after, 3 interleaved rounds: 165.7 -> 156.5 us at 16 tokens, 168.4 -> 158.5 at 128, 173.9 -> 162.8 at 600, 184.1 -> 173.0 at 4536. CPU: the route gate decides the same in 640 cases under 5 env settings, and the aiter.aot.flydsl.moe dry-run replay of the MiniMax-M3 table selects the same kernels at all 40 replayed token counts. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The TP prefill route gate (_tp_route_quant_takes_route) read this switch from os.environ on every MoE call, as the A1 quant dispatch it has to agree with also does. token_multidest_quant_enabled() in moe_kernels reads it once (functools.cache) and both use it, so the gate and the dispatch cannot see different values. Same default and accepted values (1/true/True); a test that changes the variable clears the cache. #7543 adds the same helper, byte-identical at the same place, so the two PRs merge cleanly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The TP prefill route gate and the A1 quant dispatch (_compact_ascale) each called _use_fused_quant_preshuffle with the same arguments. From the compact-scale minimum up it builds the quant layout and costs about 6 us (fp4), so an eager MiniMax-M3 prefill paid it twice per MoE layer. _grouped_a8w4_tdm_moe now evaluates it at most once per call and hands the answer to the gate; same result, same order of evaluation. CPU (MiniMax-M3 TP4: E=129, topk 5, dim 6144, fp4 activations), gate plus dispatch question per MoE call, with the env commit: 512-1000 tokens 2.47 -> 1.62 us, 3073-8192 tokens 14.0 -> 7.6 us, of which 6.4 us is the dispatch's own question. The memo block and the _compact_ascale line are byte-identical to the ones #7543 adds. Co-Authored-By: Claude Opus 5.5 <[email protected]>
On gfx1250 one grouped MoE call (fused_moe -> grouped_gemm_gfx1250_a8w4 -> _grouped_a8w4_tdm_moe -> flydsl_grouped_gemm_a8w4_masked) read about 35 environment switches through os.environ.get: the twelve AITER_TDM_* tile overrides, the gemm cluster/persistent/TDM selectors, the activation-dtype bounds and the route/quant switches. One read costs about 0.3 us, so an eager MiniMax-M3 prefill (57 MoE layers) spent about 0.6 ms of host time per forward re-reading variables that do not change. Decode replays a HIP graph and does not pay it. aiter.utility.env_once.env_once(name, default) is os.environ.get cached per (name, default); 33 per-call sites in fused_moe, grouped_moe_gfx1250, grouped_gemm_mxfp4 and moe_kernels use it. It clears itself on any change made through os.environ (setitem/delitem, so monkeypatch.setenv, mock.patch.dict, pop and in-process tuning sweeps), so every caller still sees what os.environ.get would return now; no test needed a change. token_multidest_quant_enabled() keeps its form (shared with #7543) and is registered with clear_with_env_once, so one mechanism governs every cached switch on this path. One MI455X, eager fused_moe host time per call at MiniMax-M3 TP4 shapes (a4w4, E=129, topk 5), this PR before -> after, 3 interleaved rounds: 165.7 -> 156.5 us at 16 tokens, 168.4 -> 158.5 at 128, 173.9 -> 162.8 at 600, 184.1 -> 173.0 at 4536. CPU: the route gate decides the same in 640 cases under 5 env settings, and the aiter.aot.flydsl.moe dry-run replay of the MiniMax-M3 table selects the same kernels at all 40 replayed token counts. Co-Authored-By: Claude Opus 5.5 <[email protected]>
b3edd0d to
74de31b
Compare
31dff9b to
41ef9b2
Compare
READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
valarLip
left a comment
There was a problem hiding this comment.
⚠️ Action required before merge (8)
- Stop calling
_use_fused_quant_preshuffleon paths that never use it.aiter/ops/flydsl/grouped_moe_gfx1250.py:860-864now runs it on every call; main only ran it inside_compact_ascaleafternot _compact / not _prequantized / not a_preshuffled / _ep_nvr is None. Measured on CPU (A/B against main, base-vs-base ±0.7 µs): EP +4.7..13 µs per call, prequantized EP +6..12.5 µs, and tuned default TP prefill witha_preshuffle=1(dsr1 7168, minimax_m3 6144, dsv41 5120) +10.9 µs. Compute_prequantizedabove the gate, compute_compact_ascaleonce with main's guards, and gate onnot _compact_ascale(measured +0.04 µs). - Read
AITER_FLYDSL_TOKEN_MULTIDEST_QUANTlast again.moe_kernels.py:3804reads it before theandchain, so every route-indexed quant call pays it: TP decode <64 tokens and E>256 models (e.g. Kimi-K2) +1.0..1.4 µs. Put it back as the last term of both chains. - Merge the two token-multidest branches.
moe_kernels.py:3828-3859repeats:3886-3915(same ksplit, getter, dispatch, grid). Use one branch:tp_route_slots=_tp_route_slots(numel) if fuse_tp_route else 0,tdm_hidden_chunks=token_multidest_tdm_chunks(..., md_ksplit)(drops the literal0at :3840), requiremd_ksplit > 1only when fused, append the 5 TP operands only when fused. The default build keeps the same compile key. - Don't copy the kernel limits.
_FUSE_TP_ROUTE_QUANT_MAX_EXPERTS = 256(grouped_moe_gfx1250.py:2198) copiesTP_ROUTE_MAX_EXPERTS(moe_fused_route_quant_scatter.py:126); import it, asmoe_kernels.py:3730already does._tp_route_slots(moe_kernels.py:3502) hard-codes256forBLOCK_THREADS, andgrouped_moe_gfx1250.py:863re-derives_quant_mode(:1184); use the sources. - Split the PR. +1010/−129 over 5 files is above the 1000-line first-review limit. The
_quant_ctx/_hidden_pk8_loadrefactor of the default token-multidest kernel can go first, with the ISA-equality evidence the description mentions.
Nits (also required):
6. Fix the docstrings: _emit_tp_route (kernel :167) has five barriers, not four; _tp_route_slots (moe_kernels.py:3499): whole slots past numel skip their load; kernel :133/:166 say "copy of moe_route_psum_fused", but rows within an expert are in route order here, atomic order there.
7. kernel_bench_callable "quant_a1" (grouped_moe_gfx1250.py:1519-1540) omits tp_route_params, so it times a different kernel from the one launched. Pass it.
8. Add or row_to_token is not None to the TP-fusion refusal (moe_kernels.py:3739-3751): with it set, the call falls through to the route-indexed build and writes grouped-layout scales into a token-sized buffer. Only the grouped gate prevents this today. In tests, run the compact pad-row case with the switch at its default, and give test_public_moe_fused_route_bitwise a model_dim ≥ 1024 case: at 256 (MOE, :27) ksplit is always 1, so public fused_moe never reaches token_multidest_tp_route_kernel.
Also: #7547 changes the same three files in a different way (route_expert_bits versus tp_route_slots on build_moe_token_multidest_quant_module), and the two conflict. Please say how the two designs fit together, or which one should go in first.
Reviewed at 41ef9b2 (Deep; 2 kernel/host hunters + independent pass B + 2 verifiers + miss pass). The route-in-quant kernels matched a CPU reference on ~2,500 random cases. The perf table is route+quant only; numbers for the whole MoE layer would help. CI: Standard Tests (1 GPU) / build_aiter_wheels were cancelled (superseded run), not failures from this PR.
…-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 <[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]>
41ef9b2 to
6c179fe
Compare
|
Thanks for the review. All eight items are addressed in a reworked branch, split into two PRs as item 5 asked. Both are rebased on main Split (item 5)
1.
2.
3. One token-multidest branch
4. No copied kernel limits
6. Docstrings
7. 8.
Also in this round
GPU (MI455X), this head
Host cost (CPU)
#7547
|
6c179fe to
a6fd1de
Compare
Move the per-warp _emit_quant_block_loop context of the token-multidest quant into a _quant_ctx closure in its builder. The kernel now only loads its routes' rows and hands them over with the hidden-row staging. Pull the pk8 hidden-row load of _emit_quant_block_loop into _hidden_pk8_load. No behaviour change: every quant build that goes through _emit_quant_block_loop compiles to the same gfx1250 ISA as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t context _quant_ctx took the fields that say how the hidden row arrives through a **staging catch-all, and _emit_quant_block_loop reads hidden_chunks, chunk_prefetch, hidden_lds_load and mx_group_base back with getattr defaults. A misspelled keyword at a call site would take the default without an error: with hidden_lds_load set and hidden_chunks misspelled, every chunk would be read from LDS slot 0. They are now seven required keyword-only parameters, so a missing or misspelled name raises TypeError when the kernel is built. No behaviour change: the call site already passes all seven, and every quant build that goes through _emit_quant_block_loop compiles to the same gfx1250 ISA as before. Co-Authored-By: Claude Fable 5.2 <[email protected]>
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, as a tp_route_slots build option (0 = off): the route-indexed kernel, and the K-split build of the token-multidest kernel (from 64 tokens). The token-multidest route build is a second entry point next to the default one and shares its quant context. The wrapper keeps one token-multidest branch: the route adds its operands and needs a K-split build, else the batch takes the route-indexed kernel, whose route build is K-split too. 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. moe_route_psum_fused 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, strided or other-device topids_to_rows, masked_m or psum, too few grouped rows), tile_m <= 0, a route count that is 0, above TP_ROUTE_MAX_NUMEL (2048) or not a multiple of topk, an expert count outside 1..TP_ROUTE_MAX_EXPERTS, and the modes the fused route does not cover (prequantized, A-preshuffle, remap, EP psum, row-major scales, num_valid_routes, row_to_token). topk_ids must not share storage with the outputs. 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 topk <= E <= TP_ROUTE_MAX_EXPERTS, bf16 input, at most TP_ROUTE_MAX_NUMEL routes, 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. _compact_ascale (with _quant_mode and _prequantized) moves above the TP route gate and keeps its guards, so EP, prequantized and A-preshuffled calls still skip _use_fused_quant_preshuffle. The kernel-bench quant_a1 callable passes tp_route_params, so it times the launched kernel. 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 with the route in its own launch and in the quant, and the compact-scale case with the switch at its default, and checks 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, and at the kernel limits (E = 256, top-8 uniform ids with repeats within a token, up to 2048 routes): - 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 and modes the kernel cannot serve are refused: strided or host outputs, too few rows, tile_m 0, 0, 19 or 2050 routes, a negative expert count, a compact-scale row_to_token; - 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. At model_dim 1024 and 80 tokens that call reaches the token-multidest route build. python3 op_tests/test_moe_tp_route_quant_fused.py runs the split vs fused check over a token sweep at dim 1024 and 6144 and prints the split and fused route + quant times. Co-Authored-By: Claude Opus 5.5 <[email protected]>
a6fd1de to
2616ddb
Compare
|
The
Card
Step 8 gates |
moe_fused_route_quant_scatter.py imports _warp_inclusive_prefix_i32 from kernels/topk/topk_common.py, which imports kernels/dpp_utils.py. Main has since put the quant builders of that file behind AOT caches, so with the import undeclared the five caches built from it (moe_fused_quant_preshuffle_route_ksplit, moe_token_multidest_quant, moe_token_multidest_compact_quant, moe_apre_quant, moe_apre_scatter) fail the declared-sources check, a prebuild raises and every launch falls back to a JIT trace. Both files join _GFX1250_MOE_ROUTE_QUANT. Co-Authored-By: Claude Fable 5.2 <[email protected]>
- tp_route_slots() always returned one constant and repeated the route limit the quant wrapper already checks; the wrapper passes TP_ROUTE_SLOTS. - The compact-scale decision is made once before the routes are built (not _is_ep is what _ep_nvr is None said there: every EP branch sets it), where the route choice of a later change can read it too. - tp_route_params without topids_to_rows is refused in the row-indexed branch it would reach, not ahead of every routed call. - python3 op_tests/test_moe_tp_route_quant_fused.py takes -p/--pattern, so "wide" (E = 256, top-8) times the batches at the 2048-route limit. No kernel changes: the route builds keep their names and ISA. Co-Authored-By: Claude Fable 5.2 <[email protected]>
278 -> 215 lines, 51 -> 8 cases. - fused_moe with the fold off and on, bitwise, is the main check: 4 cases (the route-indexed and the token-multidest route build, fp4 and fp8 activations) instead of 8. - The kernel-level check keeps the four cases no MoE config reaches: one token, out-of-range ids, and the limits (E = 256, top-8, 2048 routes). The 42-case tokens x pattern x tile sweep, its graph replay and the refusal-by-refusal contract test are gone. - One id generator and one CPU reference, inlined where it is used. Co-Authored-By: Claude Fable 5.2 <[email protected]>
The _quant_ctx / _hidden_pk8_load refactor is no longer needed, and the net diff against main has none of it. - Token-multidest quant: the route operands are a tuple operand of the one kernel, tp_route=() (empty with the route off, so that build, its name and its launcher are main's, untouched). The route step and the early hidden-row loads sit in the kernel body under a compile-time condition; a route build adds only its launcher. There is no second kernel and nothing to share between two entries. - Route-indexed quant: the row load is main's again, and the route build replaces its result. - _emit_quant_block_loop is main's plus the hidden_preloaded hook. Outside the route additions the file reads as on main: +434/-1 against main, was +613/-119. Final ISA (compile-only, gfx1250): the 81 kernels without the route are byte-identical to main, the six route-indexed route builds to the previous head. The six token-multidest route builds gain main's "block owns a token" guard (s_cmp_ge_u32 + s_cbranch_scc1) and split one s_lshl3_add_u32 into shift + add, 2 or 3 instructions on 926-988; the rest is the same instructions with other register numbers and order. Co-Authored-By: Claude Fable 5.2 <[email protected]>
ROCm#7543 no longer needs the _quant_ctx refactor: its route is a tuple operand of main's token-multidest kernel. Conflict in kernels/moe_fused_route_quant_scatter.py: this PR's row decode sat between the row loads and _quant_ctx. It is re-expressed the same way, as a second tuple operand of the one kernel, route_rows=() (row_starts, final_rows), whose decode feeds the kernel body the rows it would otherwise load. The _token_multidest_entry shell and the second kernel are gone; the route build adds only its launcher. Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
Card
Step 8 gates |
|
The
Card
Step 8 gates |
|
Reply to the review of 2026-11-19 (valarLip, at 41ef9b2). Status of the eight items at
On #7547: the two are route modes of the same builder and are exclusive there. This PR's What ran at this head, on one idle MI455X (gfx1250),
Still open:
|
|
Reply to the
|
valarLip
left a comment
There was a problem hiding this comment.
⚠️ Action required before merge (4)
Thanks for redoing this in place on main's quant kernels. The 11-19 asks are addressed (see below). Four things remain:
-
Post before/after numbers for the shapes the default-on gate takes.
_fuse_tp_route_quant(aiter/ops/flydsl/grouped_moe_gfx1250.py:1221-1231) now takes every gfx1250 TP call up to 2048 routes with topk ≤ E ≤ 256, bf16 input, noa1_scaleand no A-preshuffle. Only MiniMax-M3 (E=129, top-5) is timed, and it has no tuned gfx1250 rows. Its gain already drops to -9% at 2045 routes (dim 6144). The tuned rows the gate does take are untimed: 4096/E256/top6 and 7168/E96/top6. E256 top-8 at the cap is untimed too. Please run-p wide -t 128 256 -d 4096 7168, plus a top-6 pattern at 64..341 tokens, against main. Lower the cap (on numel, or numel × ksplit) wherever the gain is ≤ 0. Keep the cap inside the range you measured: 2048 is the crossover of the separate route kernel, not of this one. -
Share the hidden-row load instead of copying it.
_emit_hidden_prefetch(aiter/ops/flydsl/kernels/moe_fused_route_quant_scatter.py:319-337) repeats three pieces of_emit_hidden_load: the hidden-row V# (:612-615),_mx_block_of(:634-637) and thecol_base/buffer_load(:654-671). Its result then replaces that load. The comment at:617-620warns that a second copy of this address math "would fail silently". Please put the address/load in one helper that both call, then re-check the 81-kernel ISA cmp. -
Fix the squash text. aiter squashes with COMMIT_MESSAGES. Several commit messages describe code and tests that are no longer in the PR:
- d727139 and 32de0e6 describe the
_quant_ctxrefactor. - 8a285d0 describes a "second entry point ... shares its quant context".
- 2616ddb describes 51 cases plus graph-replay and refusal tests.
- 9fcefee names
tp_route_slots(). - The merge commit 49b4c09 would also land.
Please squash with the PR body.
- d727139 and 32de0e6 describe the
-
Run the new default-on path in a gfx1250 CI lane.
op_tests/test_moe_tp_route_quant_fused.pyskips off gfx1250, and it is in neither.github/test-manifests/mi450-gfx1250-p0.txtnor-p1.txt, so no CI job runs it. Please add its 8 nodes to p1 (or p0). Please also loop[80-1024-a4w4]with the fold on and off on a shared GPU, and post the HIP error if the SIGABRT you saw comes back.
Prior asks (11-19): 1-4 and 6-8 are fixed (e.g. _compact_ascale computed once at grouped_moe_gfx1250.py:1046-1055 and read at :1228; the env read is the last term again at moe_kernels.py:3927). Ask 5 (split the PR) is withdrawn: the refactor is gone, and the diff is about 925 changed lines. #7547 is stacked on this PR. CI is green, and there are no open threads. The PR's merge commit is a clean merge-tree of its parents, and the head merges cleanly with current main. zhuyuhua-v's review-pr scan is right that timing evidence is missing (= item 1).
Root cause vs patch
The root problem is the fixed per-layer cost of the separate moe_route_psum_fused launch. Folding the route into the quant removes it at the source, and the other 81 kernels are unchanged. The default-on range, though, reuses the separate kernel's 2048 crossover. Here every block recounts every route, so the cap has to come from this kernel's own measurements.
Verdict: right direction, with a patch-like gate.
Reviewed at 5689d66 (round 2, pass A + orchestrator check of items 2 and 4).
What this PR does
On gfx1250 tensor-parallel MoE, small batches currently route in a launch of their own before the activation quant:
moe_route_psum_fusedcounts, scans and remaps every route, and then the quant scatters the bf16 rows into expert order. This PR moves that route into the quant launch, as the EP path already does with its psum. A MoE call goes from 5 kernels (route, quant, gemm1, gemm2, gather-reduce) to 4 (quant, gemm1, gemm2, gather-reduce).The route is folded into main's kernels in place; the net diff against main touches
kernels/moe_fused_route_quant_scatter.pyonly for the route (+434/-1).topk_idsentry and builds three things in LDS: the per-expert counts, the number of routes before its own, and the tile-aligned expert starts.topids_to_rows. Block (0, 0) also writesmasked_mandpsum.tp_route_slotsbuild option (0 = off): the route-indexed quant, and the K-split build of the token-multidest quant (from 64 tokens).tp_route=(): empty with the route off, so that build, its name, its launcher and its ISA are main's; a route build adds only its launcher. The route step and the early hidden-row loads sit in the kernel body under a compile-time condition. The route-indexed quant reuses operands it already has.TP_ROUTE_MAX_NUMEL(2048) routes.moe_route_psum_fusedorders them by LDS atomics, which differ from call to call, and no GEMM or gather-reduce result depends on that order.TP_ROUTE_MAX_EXPERTS(256), at mostTP_ROUTE_MAX_NUMELroutes, bf16 input, noa1_scale, no A-preshuffle, and not the compact-scale quant. Every other batch keeps its current launches._compact_ascaleis computed once, with main's guards, and the gate reuses it. EP, prequantized and A-preshuffled calls therefore skip_use_fused_quant_preshuffle, as on main.AITER_FLYDSL_TOKEN_MULTIDEST_QUANTis again read last.kernels/topk/topk_common.pyandkernels/dpp_utils.py, which the route emitter imports, are declared inaiter/aot/flydsl/sources.pyfor the five AOT caches built from the quant file.AITER_TDM_FUSE_TP_ROUTE_QUANT=0is a temporary kill-switch that restores the separate route launch.tile_m <= 0, an expert count outside 1..256, and a route count that is 0, above 2048 or not a multiple of topk.topk_idsmust not share storage with the outputs (documented; a pointer check would break the AOT dry-run replay). It also refuses the modes the in-quant route does not cover: prequantized, A-preshuffle, remap, EP psum, row-major scales,num_valid_routesandrow_to_token.Benefit
Route + quant device time, this head.
rocm/fw-bringup:gfx1250-minimax-m3-20261112with FlyDSL 0.5.3,python3 op_tests/test_moe_tp_route_quant_fused.py(exit 0, one run).moe_route_psum_fusedlaunch, then the quant. Payload and scales are equal in all 48 rows (err 0).fused_moe) time was not re-measured at this head; the numbers of earlier heads are dropped, since the route builds changed.Host cost per MoE call (CPU), measured at
9fcefee8. The two later commits change only the kernel file and the test.grouped_gemm_gfx1250_a8w4runs with the tuned rows. Each process is pinned to one core with ASLR off. Main (ea578b41), the branch and main again (the A/A control) were interleaved over 10 rounds. Each value is the median of 9 × 400 calls.sys.monitoring), branch minus main. No path makes an extra function call except the kill-switch read.os.environ.get, +1.4 µs.Testing
rocm/fw-bringup:gfx1250-minimax-m3-20261112, FlyDSL 0.5.3:python3 -m pytest -q -s op_tests/test_moe_tp_route_quant_fused.py: 8 passed. It checks:fused_moeoutput, bitwise equal with the switch off and on and with no route launch when on: 3 and 80 tokens at dim 256, 80 tokens at dim 1024, a4w4 and a8w4, with TP pad rows;test_public_moe_fused_route_bitwise[80-1024-a4w4], run alone: 1 passed, and 1 passed withAITER_TDM_FUSE_TP_ROUTE_QUANT=0.python3 op_tests/test_moe_tp_route_quant_fused.py: exit 0 (the table above).python3 -m pytest -q op_tests/flydsl_tests/test_flydsl_grouped_gemm.py: 56 passed, 7 failed, twice. The 6 pad-row cases this PR changes pass. See Unverified for the 7.aiter.aot.flydsl.dry_run("gfx1250")withCOMPILE_ONLY=1andFLYDSL_DUMP_DIR; the final*_final_isa.sfiles compared withcmp):ea578b41): 81 kernels from 96 builds; a main-vs-main control is identical too.2616ddb6, the previously published head: the 6 route-indexed builds are byte-identical. The 6 token-multidest route builds are not: each gains main's "block owns a token" guard around the quant (s_cmp_ge_u32+s_cbranch_scc1) and has ones_lshl3_add_u32split intos_lshl_b32+s_add_co_i32, 926-988 instructions before, 2 or 3 more after.aot_cache._undeclared_importsover the caches whose sources includekernels/moe_fused_route_quant_scatter.pyfinds nothing.python -m aiter.aot.flydsl.moeon gfx1250 grouped rows (E = 256, top-6, dim 4096, 4–256 tokens): 12 jobs ok, withlaunch_token_multidest_tp_routeamong the launchers.--checkthen passes.row_to_token,tile_m0, experts -1, 19 and 2050 routes, and a host-sidepsum.black --checkandruff checkare clean on the 6 changed files.pytest op_tests/test_moe_route_rows.py op_tests/flydsl_tests/test_route_psum_fused.py op_tests/flydsl_tests/test_flydsl_grouped_gemm.py op_tests/test_grouped_gemm_stream.py op_tests/flydsl_tests/test_ep_e8m0_scale.py tests/test_flydsl_aot_cache.pyon the CPU gives the same outcome per test as main: only GPU cases fail, with no device.Not covered
python3 op_tests/test_moe_tp_route_quant_fused.py -p wide -t 128 256 -d 4096 7168times the E = 256 top-8 case; the benchmark has no pattern for the top-6 shapes. If the gain turns negative there, the gate's route limit comes down.tests/test_flydsl_aot_launch.pydoes not import on the CPU host (no Triton driver), so the declared-sources check was run directly, not through that test.PR Self-Review
Reviewed revision
5689d66
Behavior and intent
moe_route_psum_fusedlaunch is folded into the activation quant, so 5 kernels per MoE call become 4.TP_ROUTE_MAX_EXPERTS, bf16 input, noa1_scale, no A-preshuffle and no compact-scale quant.topk_idsand writes its own routes' rows.masked_mandpsum.moe_route_psum_fuseduses atomic order.AITER_TDM_FUSE_TP_ROUTE_QUANT=0.Validation and unverified scope
2616ddb6:ea578b41, no conflicts.tp_route=()on the token-multidest kernel); the_quant_ctx/_hidden_pk8_loadrefactor is gone from the net diff.aiter/aot/flydsl/sources.py:kernels/dpp_utils.pyandkernels/topk/topk_common.pyjoin_GFX1250_MOE_ROUTE_QUANT.tp_route_slots()is the constantTP_ROUTE_SLOTS; the compact-scale decision moves above the route choice; thetp_route_paramsrefusal for a call withouttopids_to_rowsmoves into the row-indexed branch; the test's benchmark takes-p/--pattern.op_tests/test_moe_tp_route_quant_fused.pygoes from 278 to 215 lines and from 51 to 8 cases: thefused_moeon/off comparison is the main check, and the kernel-level check keeps only the cases no MoE config reaches.rocm/fw-bringup:gfx1250-minimax-m3-20261112, FlyDSL 0.5.3:python3 -m pytest -q -s op_tests/test_moe_tp_route_quant_fused.py: 8 passed.python3 -m pytest -q -s -x "op_tests/test_moe_tp_route_quant_fused.py::test_public_moe_fused_route_bitwise[80-1024-a4w4]": 1 passed; withAITER_TDM_FUSE_TP_ROUTE_QUANT=0: 1 passed.python3 op_tests/test_moe_tp_route_quant_fused.py: exit 0; 48 rows, err 0 (route + quant table).python3 -m pytest -q op_tests/flydsl_tests/test_flydsl_grouped_gemm.py: 56 passed, 7 failed (two runs, same result).aiter.aot.flydsl.dry_run("gfx1250")withCOMPILE_ONLY=1,cmpon the final ISA, main-vs-main control identical: 81/81 kernels without the route equal to mainea578b41; 6/6 route-indexed route builds equal to2616ddb6; 6/6 token-multidest route builds differ from it by 2 or 3 instructions.aot_cache._undeclared_imports: clean.python -m aiter.aot.flydsl.moeon three grouped rows: 12 ok; again with--check: 12 ok.black --check,ruff check: clean. CPUpytestof the neighbouring tests: same outcomes as main.9fcefee8(host code unchanged since).test_flydsl_grouped_gemm.pyis not a usable regression signal in that image: its 7 failures are all "FLYDSL_RUNTIME_RUN_ONLY=1 but no usable AOT cache", and mainea578b41in the same container fails the same first 5 and then aborts (rc 134);test_public_moe_fused_route_bitwise[80-1024-a4w4]; the cause is not identified, and it did not recur in 3 runs on an idle card;fused_moetime at this revision, and perf near the 2048-route limit on E = 96 / E = 256 shapes;--checkran);PR-specific question
What behavior would each changed op test fail on before this PR, and why is that coverage not already provided by an existing canonical test?
test_moe_tp_route_quant_fused.py(new) calls the quant withtp_route_paramsand theAITER_TDM_FUSE_TP_ROUTE_QUANTswitch, neither of which exists before this PR. It asserts that the route launch is gone with the switch on; main always runs it.test_grouped_skips_pad_rows_routed_to_minus_one[*-0-1]cases assert that the quant ran withtp_route_slots > 0. Before this PR the route-in-quant path does not exist, so that assertion fails.test_route_psum_fused.pychecks only the standalone route kernel. No existing test checks that a quant-side route reproducesmasked_m,psumandtopids_to_rows, or that it skips -1 pad rows.Changed op tests
op_tests/test_moe_tp_route_quant_fused.py(new, 215 lines, 8 cases)fused_moebitwise equal with the switch off and on, and no route launch with it on: 3 and 80 tokens at dim 256 (route-indexed build) and 80 tokens at dim 1024 (token-multidest route build), fp4 and fp8 activations, with TP pad rows;op_tests/flydsl_tests/test_flydsl_grouped_gemm.py(modified)fuse_route=1case fails its path assertion.