Skip to content

[FlyDSL] perf(flydsl): fold the gfx1250 TP MoE route into the activation quant - #7543

Open
lirui927 wants to merge 10 commits into
ROCm:mainfrom
lirui927:fuse/f9-moe-route-quant
Open

lirui927 wants to merge 10 commits into
ROCm:mainfrom
lirui927:fuse/f9-moe-route-quant

Conversation

@lirui927

@lirui927 lirui927 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

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_fused counts, 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.py only for the route (+434/-1).

  • How it works.
    • Each quant workgroup loads every topk_ids entry and builds three things in LDS: the per-expert counts, the number of routes before its own, and the tile-aligned expert starts.
    • From these it gives each of its own routes a row and writes topids_to_rows. Block (0, 0) also writes masked_m and psum.
    • No workgroup waits on another. Nothing is kept in global memory between calls, so graph replay needs no reset.
  • Kernels.
    • Two quant kernels take a new tp_route_slots build option (0 = off): the route-indexed quant, and the K-split build of the token-multidest quant (from 64 tokens).
    • There is one kernel per builder. In the token-multidest quant the route operands are a tuple operand of main's kernel, 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.
    • One build serves every batch up to TP_ROUTE_MAX_NUMEL (2048) routes.
    • The wrapper has one token-multidest branch. The route appends its operands, and a row that does not split K (one K group, e.g. dim 256) takes the route-indexed kernel instead.
  • Same results.
    • gemm1, gemm2 and the gather-reduce read the same buffers as before.
    • The MoE output is bitwise equal with the fusion on or off.
    • Rows within an expert are now in route order. moe_route_psum_fused orders them by LDS atomics, which differ from call to call, and no GEMM or gather-reduce result depends on that order.
  • When it applies. Only where the fused TP route runs today (token_num × topk ≤ 2048, no EP). In addition it needs topk ≤ E ≤ TP_ROUTE_MAX_EXPERTS (256), at most TP_ROUTE_MAX_NUMEL routes, bf16 input, no a1_scale, no A-preshuffle, and not the compact-scale quant. Every other batch keeps its current launches.
  • Host side.
    • _compact_ascale is 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_QUANT is again read last.
    • The kernel limits (experts and routes) are imported from the kernel module, not copied.
    • kernels/topk/topk_common.py and kernels/dpp_utils.py, which the route emitter imports, are declared in aiter/aot/flydsl/sources.py for the five AOT caches built from the quant file.
  • Rollback. AITER_TDM_FUSE_TP_ROUTE_QUANT=0 is a temporary kill-switch that restores the separate route launch.
  • Checks. The wrapper refuses buffers the kernel cannot write in place (non-int32, strided or other-device outputs, too few grouped rows), 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_ids must 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_routes and row_to_token.

Benefit

Route + quant device time, this head.

  • One idle MI455X (gfx1250), rocm/fw-bringup:gfx1250-minimax-m3-20261112 with FlyDSL 0.5.3, python3 op_tests/test_moe_tp_route_quant_fused.py (exit 0, one run).
  • MiniMax-M3 TP routing (E = 129, top-4 routed + 1 shared expert), MXFP4.
  • The baseline is main's path in the same build: the moe_route_psum_fused launch, then the quant. Payload and scales are equal in all 48 rows (err 0).
tokens dim 1024: main route + quant (µs) dim 1024: this PR (µs) change dim 6144: main (µs) dim 6144: this PR (µs) change
1 5.16 2.78 -46% 5.37 2.76 -49%
4 5.40 2.77 -49% 5.58 2.91 -48%
16 5.49 2.78 -49% 5.68 3.00 -47%
32 5.66 2.86 -49% 6.15 3.40 -45%
64 5.70 3.23 -43% 6.26 3.41 -46%
128 6.30 3.45 -45% 6.94 3.68 -47%
256 7.23 3.98 -45% 8.53 6.19 -27%
409 8.05 5.98 -26% 9.84 8.98 -9%
  • With TP pad rows the change is -39% to -50% at dim 1024 and -14% to -50% at dim 6144. With dropped or out-of-range ids it is -31% to -49% and -12% to -50%.
  • At dim 6144 the gain shrinks as the batch grows: more K-split blocks each redo the whole route. It stays positive up to 409 tokens.
  • The whole-layer (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.

  • Method: every FlyDSL launch is stubbed to a no-op, tensors are on the CPU, and grouped_gemm_gfx1250_a8w4 runs 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.
  • The bytecode column is exact: Python instructions executed per call (sys.monitoring), branch minus main. No path makes an extra function call except the kill-switch read.
call (CPU host time) main µs A/A: main − main branch − main bytecodes per call
EP, M3 shape, 64 tok 145.7 +0.17 (-0.53..+1.65) +0.68 (+0.36..+1.22) +46 of 15032
EP, M3 shape, 512 tok 147.3 -0.25 (-1.90..+0.63) +0.38 (-0.29..+0.96) +46
prequantized EP, 64 tok 148.7 +0.52 (-0.35..+2.12) +0.66 (-0.19..+1.47) +46
prequantized EP, 512 tok 149.2 -0.61 (-1.18..+0.72) +0.95 (-0.18..+1.39) +46
TP prefill a_preshuffle=1, dsr1 7168, 4096 tok 154.3 -1.06 (-1.52..+1.30) -0.38 (-1.09..+0.82) +42
TP prefill a_preshuffle=1, M3 6144, 4096 tok 158.4 +0.12 (-0.98..+0.93) +0.11 (-0.95..+1.26) +42
TP prefill a_preshuffle=1, dsv41 5120, 4096 tok 156.6 +0.47 (-1.10..+2.81) -0.56 (-1.23..+1.12) +42
TP decode M3, 1 tok (fused) 108.3 -0.69 (-1.19..+0.14) -1.60 (-2.14..-0.83) -337
TP decode M3, 16 tok (fused) 108.9 -0.19 (-0.65..+0.53) -1.45 (-2.00..-0.73) -337
TP decode M3, 32 tok, switch=0 108.4 -0.38 (-0.95..+0.08) +1.39 (+0.59..+2.53) +162
TP decode Kimi-K2 E=384, 4 tok 110.0 -0.05 (-0.51..+0.34) +0.58 (+0.29..+1.14) +69
TP decode Kimi-K2 E=384, 32 tok 120.2 -0.05 (-1.04..+1.22) +0.36 (-0.49..+1.35) +69
  • Deltas are same-round differences in µs: the median over 10 rounds, with the interquartile range in parentheses.
  • Calls that do not fuse run 42 to 69 more bytecodes than on main (0.3–0.5%): the gate's terms, in the same order as main's. Their time deltas are inside the A/A spread or up to +0.7 µs.
  • Fused decode calls are 1.5 µs cheaper on the host than on main, because the route launch and its buffers are gone. With the switch off the call pays one os.environ.get, +1.4 µs.

Testing

  • On one idle MI455X (gfx1250), this head, 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:
      • the public fused_moe output, 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;
      • at the kernel, what no MoE config reaches: one token, out-of-range ids and the limits (E = 256, top-8 with repeated ids, 2048 routes), route outputs exactly against a CPU reference, payload and scales against main's quant.
    • The single case test_public_moe_fused_route_bitwise[80-1024-a4w4], run alone: 1 passed, and 1 passed with AITER_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.
  • On CPU, this head (no GPU):
    • gfx1250 ISA, FlyDSL compile-only (aiter.aot.flydsl.dry_run("gfx1250") with COMPILE_ONLY=1 and FLYDSL_DUMP_DIR; the final *_final_isa.s files compared with cmp):
      • The quant kernels without the route are byte-identical to main (ea578b41): 81 kernels from 96 builds; a main-vs-main control is identical too.
        • Coverage: token-multidest (K-split and TDM-staged), routeks, prequantized and row-major EP, the compact pair, row-indexed quant, and the route + quant + scatter kernels.
        • Each for fp4/fp8, wmma_rep 1/2 and K = 1024/6144.
      • Route builds against 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 one s_lshl3_add_u32 split into s_lshl_b32 + s_add_co_i32, 926-988 instructions before, 2 or 3 more after.
    • AOT declared sources: aot_cache._undeclared_imports over the caches whose sources include kernels/moe_fused_route_quant_scatter.py finds nothing.
    • AOT: python -m aiter.aot.flydsl.moe on gfx1250 grouped rows (E = 256, top-6, dim 4096, 4–256 tokens): 12 jobs ok, with launch_token_multidest_tp_route among the launchers. --check then passes.
    • A dry-run dispatch check confirms which build each call takes:
      • 16 tokens: the routeks route build.
      • 80 tokens at dim 1024: the token-multidest route build (ks4).
      • 80 tokens at dim 256: the routeks route build.
      • Without the route: the unchanged builds.
    • The same check confirms the refusals: row_to_token, tile_m 0, experts -1, 19 and 2050 routes, and a host-side psum.
    • black --check and ruff check are 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.py on the CPU gives the same outcome per test as main: only GPU cases fail, with no device.

Not covered

  • No whole-model accuracy, TP8 or serving run.
  • CI has no gfx1250 runner, so the new test runs only by hand.
  • Perf near the 2048-route limit on wider shapes (E = 96 top-6 at dim 7168, E = 256 top-6 at dim 4096, E = 256 top-8) is not measured. python3 op_tests/test_moe_tp_route_quant_fused.py -p wide -t 128 256 -d 4096 7168 times 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.py does not import on the CPU host (no Triton driver), so the declared-sources check was run directly, not through that test.
  • Size: +878/-47 against main, 925 changed lines in 6 files.

PR Self-Review

Reviewed revision

5689d66

Behavior and intent

  • On gfx1250 TP MoE small batches, the separate moe_route_psum_fused launch is folded into the activation quant, so 5 kernels per MoE call become 4.
    • These are the batches with token_num × topk ≤ 2048, no EP, topk ≤ E ≤ TP_ROUTE_MAX_EXPERTS, bf16 input, no a1_scale, no A-preshuffle and no compact-scale quant.
  • Each quant workgroup rebuilds the counts, prefix and tile-aligned starts in LDS from all topk_ids and writes its own routes' rows.
    • Block (0, 0) writes masked_m and psum.
    • There are no inter-workgroup waits and no global state.
  • The MoE output is bitwise equal with the fusion on or off. Rows within an expert are in route order; moe_route_psum_fused uses atomic order.
  • Host gating matches main on paths that do not fuse: the compact-scale decision is made once with main's guards, the multidest env is read last, and the kernel limits are imported.
  • The default kernels are main's, untouched (ISA-identical).
  • Why: at decode sizes the route launch is a fixed cost. Against main's route launch + quant, route + quant device time is 43–49% lower at 1–128 tokens and 9–26% lower at 409 tokens (table above).
  • Rollback: AITER_TDM_FUSE_TP_ROUTE_QUANT=0.

Validation and unverified scope

  • This revision against the previously published head 2616ddb6:
    • A merge of main ea578b41, no conflicts.
    • The route is folded into main's kernels in place (tuple operand tp_route=() on the token-multidest kernel); the _quant_ctx / _hidden_pk8_load refactor is gone from the net diff.
    • aiter/aot/flydsl/sources.py: kernels/dpp_utils.py and kernels/topk/topk_common.py join _GFX1250_MOE_ROUTE_QUANT.
    • Host side, no kernel change: tp_route_slots() is the constant TP_ROUTE_SLOTS; the compact-scale decision moves above the route choice; the tp_route_params refusal for a call without topids_to_rows moves into the row-indexed branch; the test's benchmark takes -p/--pattern.
    • op_tests/test_moe_tp_route_quant_fused.py goes from 278 to 215 lines and from 51 to 8 cases: the fused_moe on/off comparison is the main check, and the kernel-level check keeps only the cases no MoE config reaches.
  • GPU, one idle MI455X (gfx1250), this revision, 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; with AITER_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).
  • CPU, this revision:
    • ISA, aiter.aot.flydsl.dry_run("gfx1250") with COMPILE_ONLY=1, cmp on the final ISA, main-vs-main control identical: 81/81 kernels without the route equal to main ea578b41; 6/6 route-indexed route builds equal to 2616ddb6; 6/6 token-multidest route builds differ from it by 2 or 3 instructions.
    • Declared sources, aot_cache._undeclared_imports: clean.
    • python -m aiter.aot.flydsl.moe on three grouped rows: 12 ok; again with --check: 12 ok.
    • The dry-run dispatch and refusal check (Testing above).
    • black --check, ruff check: clean. CPU pytest of the neighbouring tests: same outcomes as main.
    • Host cost against main with an A/A control, at 9fcefee8 (host code unchanged since).
  • Unverified:
    • test_flydsl_grouped_gemm.py is not a usable regression signal in that image: its 7 failures are all "FLYDSL_RUNTIME_RUN_ONLY=1 but no usable AOT cache", and main ea578b41 in the same container fails the same first 5 and then aborts (rc 134);
    • one earlier run of the PR test on a GPU shared with another job aborted (native SIGABRT) in 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;
    • whole-layer fused_moe time at this revision, and perf near the 2048-route limit on E = 96 / E = 256 shapes;
    • graph replay of the folded route and the wrapper's refusals have no test in the PR; the refusals were checked on CPU by the dry-run check;
    • a launch through a prebuilt AOT cache on a device (only the prebuild and --check ran);
    • whole-model, TP8 and serving runs; CI has no gfx1250 runner.

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 with tp_route_params and the AITER_TDM_FUSE_TP_ROUTE_QUANT switch, neither of which exists before this PR. It asserts that the route launch is gone with the switch on; main always runs it.
  • The new test_grouped_skips_pad_rows_routed_to_minus_one[*-0-1] cases assert that the quant ran with tp_route_slots > 0. Before this PR the route-in-quant path does not exist, so that assertion fails.
  • test_route_psum_fused.py checks only the standalone route kernel. No existing test checks that a quant-side route reproduces masked_m, psum and topids_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)
    • Covers:
      • fused_moe bitwise 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;
      • what no MoE config reaches, at the kernel: one token, out-of-range ids, and the limits (E = 256, top-8 with repeated ids, 2048 routes). Route outputs exactly against a CPU reference; payload and scales against main's quant, with sentinel-filled outputs.
    • Before this PR: the API and the switch do not exist.
    • Not redundant: the existing route tests cover only the separate route launch.
  • op_tests/flydsl_tests/test_flydsl_grouped_gemm.py (modified)
    • Covers: the existing pad-row test now runs with the separate route, with the route in the quant, and with compact scales at the default switch, and it asserts which path ran.
    • Before this PR: the fuse_route=1 case fails its path assertion.
    • Not redundant: it extends the canonical pad-row test.

@lirui927
lirui927 force-pushed the xiaobingsuper/m3-mi455-perf-stack branch from 2c8902d to 3499407 Compare November 10, 2026 07:46
lirui927 pushed a commit to lirui927/aiter that referenced this pull request Nov 10, 2026
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]>
lirui927 pushed a commit to lirui927/aiter that referenced this pull request Nov 10, 2026
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]>
@lirui927
lirui927 force-pushed the fuse/f9-moe-route-quant branch from d8b5e99 to b3edd0d Compare November 10, 2026 08:21
lirui927 pushed a commit to lirui927/aiter that referenced this pull request Nov 10, 2026
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]>
lirui927 pushed a commit that referenced this pull request Nov 10, 2026
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]>
lirui927 pushed a commit that referenced this pull request Nov 10, 2026
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]>
lirui927 pushed a commit that referenced this pull request Nov 10, 2026
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]>
@lirui927
lirui927 changed the base branch from xiaobingsuper/m3-mi455-perf-stack to main November 12, 2026 04:47
@lirui927
lirui927 force-pushed the fuse/f9-moe-route-quant branch from b3edd0d to 74de31b Compare November 12, 2026 04:47
@lirui927
lirui927 changed the base branch from main to xiaobingsuper/m3-mi455-perf-stack November 12, 2026 04:50
@lirui927
lirui927 changed the base branch from xiaobingsuper/m3-mi455-perf-stack to main November 12, 2026 04:50
@lirui927
lirui927 force-pushed the fuse/f9-moe-route-quant branch 2 times, most recently from 31dff9b to 41ef9b2 Compare November 16, 2026 14:06
@lirui927
lirui927 marked this pull request as ready for review November 16, 2026 14:06
@lirui927
lirui927 requested a review from a team November 16, 2026 14:06
@github-actions github-actions Bot changed the title perf(flydsl): fold the gfx1250 TP MoE route into the activation quant [FlyDSL] perf(flydsl): fold the gfx1250 TP MoE route into the activation quant Nov 16, 2026
@aiter-bot

aiter-bot commented Nov 16, 2026 •

Copy link
Copy Markdown

READY FOR HUMAN REVIEW

PR Self-Review

Revision 5689d6643565 · mode shadow

Author self-review accepted. Readiness report:

Author answers

  • PASS — current-SHA answers are present and structurally specific
Show the author's self-review answers

Behavior and intent

- On gfx1250 TP MoE small batches, the separate `moe_route_psum_fused` launch is folded into the activation quant, so 5 kernels per MoE call become 4.
  - These are the batches with token_num × topk ≤ 2048, no EP, topk ≤ E ≤ `TP_ROUTE_MAX_EXPERTS`, bf16 input, no `a1_scale`, no A-preshuffle and no compact-scale quant.
- Each quant workgroup rebuilds the counts, prefix and tile-aligned starts in LDS from all `topk_ids` and writes its own routes' rows.
  - Block (0, 0) writes `masked_m` and `psum`.
  - There are no inter-workgroup waits and no global state.
- The MoE output is bitwise equal with the fusion on or off. Rows within an expert are in route order; `moe_route_psum_fused` uses atomic order.
- Host gating matches main on paths that do not fuse: the compact-scale decision is made once with main's guards, the multidest env is read last, and the kernel limits are imported.
- The default kernels are main's, untouched (ISA-identical).
- Why: at decode sizes the route launch is a fixed cost. Against main's route launch + quant, route + quant device time is 43–49% lower at 1–128 tokens and 9–26% lower at 409 tokens (table above).
- Rollback: `AITER_TDM_FUSE_TP_ROUTE_QUANT=0`.

Validation and unverified scope

- This revision against the previously published head `2616ddb6`:
  - A merge of main `ea578b41`, no conflicts.
  - The route is folded into main's kernels in place (tuple operand `tp_route=()` on the token-multidest kernel); the `_quant_ctx` / `_hidden_pk8_load` refactor is gone from the net diff.
  - `aiter/aot/flydsl/sources.py`: `kernels/dpp_utils.py` and `kernels/topk/topk_common.py` join `_GFX1250_MOE_ROUTE_QUANT`.
  - Host side, no kernel change: `tp_route_slots()` is the constant `TP_ROUTE_SLOTS`; the compact-scale decision moves above the route choice; the `tp_route_params` refusal for a call without `topids_to_rows` moves into the row-indexed branch; the test's benchmark takes `-p/--pattern`.
  - `op_tests/test_moe_tp_route_quant_fused.py` goes from 278 to 215 lines and from 51 to 8 cases: the `fused_moe` on/off comparison is the main check, and the kernel-level check keeps only the cases no MoE config reaches.
- GPU, one idle MI455X (gfx1250), this revision, `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; with `AITER_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).
- CPU, this revision:
  - ISA, `aiter.aot.flydsl.dry_run("gfx1250")` with `COMPILE_ONLY=1`, `cmp` on the final ISA, main-vs-main control identical: 81/81 kernels without the route equal to main `ea578b41`; 6/6 route-indexed route builds equal to `2616ddb6`; 6/6 token-multidest route builds differ from it by 2 or 3 instructions.
  - Declared sources, `aot_cache._undeclared_imports`: clean.
  - `python -m aiter.aot.flydsl.moe` on three grouped rows: 12 ok; again with `--check`: 12 ok.
  - The dry-run dispatch and refusal check (Testing above).
  - `black --check`, `ruff check`: clean. CPU `pytest` of the neighbouring tests: same outcomes as main.
  - Host cost against main with an A/A control, at `9fcefee8` (host code unchanged since).
- Unverified:
  - `test_flydsl_grouped_gemm.py` is not a usable regression signal in that image: its 7 failures are all "FLYDSL_RUNTIME_RUN_ONLY=1 but no usable AOT cache", and main `ea578b41` in the same container fails the same first 5 and then aborts (rc 134);
  - one earlier run of the PR test on a GPU shared with another job aborted (native SIGABRT) in `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;
  - whole-layer `fused_moe` time at this revision, and perf near the 2048-route limit on E = 96 / E = 256 shapes;
  - graph replay of the folded route and the wrapper's refusals have no test in the PR; the refusals were checked on CPU by the dry-run check;
  - a launch through a prebuilt AOT cache on a device (only the prebuild and `--check` ran);
  - whole-model, TP8 and serving runs; CI has no gfx1250 runner.

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 with `tp_route_params` and the `AITER_TDM_FUSE_TP_ROUTE_QUANT` switch, neither of which exists before this PR. It asserts that the route launch is gone with the switch on; main always runs it.
- The new `test_grouped_skips_pad_rows_routed_to_minus_one[*-0-1]` cases assert that the quant ran with `tp_route_slots > 0`. Before this PR the route-in-quant path does not exist, so that assertion fails.
- `test_route_psum_fused.py` checks only the standalone route kernel. No existing test checks that a quant-side route reproduces `masked_m`, `psum` and `topids_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)
  - Covers:
    - `fused_moe` bitwise 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;
    - what no MoE config reaches, at the kernel: one token, out-of-range ids, and the limits (E = 256, top-8 with repeated ids, 2048 routes). Route outputs exactly against a CPU reference; payload and scales against main's quant, with sentinel-filled outputs.
  - Before this PR: the API and the switch do not exist.
  - Not redundant: the existing route tests cover only the separate route launch.
- `op_tests/flydsl_tests/test_flydsl_grouped_gemm.py` (modified)
  - Covers: the existing pad-row test now runs with the separate route, with the route in the quant, and with compact scales at the default switch, and it asserts which path ran.
  - Before this PR: the `fuse_route=1` case fails its path assertion.
  - Not redundant: it extends the canonical pad-row test.

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply
  • ADVISORY — Copilot did not review the current head SHA

Code and op-test hygiene

  • PASS — no committed artifact or conflict marker detected

Required CI

  • PASS — Black: passed
  • PASS — Ruff: passed
  • PASS — Repository dependency: passed
  • PASS — Aiter Test Gate: passed

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

@lirui927
lirui927 requested review from coderfeli and zufayu November 16, 2026 14:52
@zufayu

zufayu commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Review routing reminder: @yadaish is the primary reviewer for this FlyDSL MoE scope. Please route the next technical review/action there; the direct request to @zufayu does not replace the component-owner review.

@lirui927
lirui927 requested a review from yadaish November 17, 2026 08:18

@valarLip valarLip left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Action required before merge (8)

  1. Stop calling _use_fused_quant_preshuffle on paths that never use it. aiter/ops/flydsl/grouped_moe_gfx1250.py:860-864 now runs it on every call; main only ran it inside _compact_ascale after not _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 with a_preshuffle=1 (dsr1 7168, minimax_m3 6144, dsv41 5120) +10.9 µs. Compute _prequantized above the gate, compute _compact_ascale once with main's guards, and gate on not _compact_ascale (measured +0.04 µs).
  2. Read AITER_FLYDSL_TOKEN_MULTIDEST_QUANT last again. moe_kernels.py:3804 reads it before the and chain, 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.
  3. Merge the two token-multidest branches. moe_kernels.py:3828-3859 repeats :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 literal 0 at :3840), require md_ksplit > 1 only when fused, append the 5 TP operands only when fused. The default build keeps the same compile key.
  4. Don't copy the kernel limits. _FUSE_TP_ROUTE_QUANT_MAX_EXPERTS = 256 (grouped_moe_gfx1250.py:2198) copies TP_ROUTE_MAX_EXPERTS (moe_fused_route_quant_scatter.py:126); import it, as moe_kernels.py:3730 already does. _tp_route_slots (moe_kernels.py:3502) hard-codes 256 for BLOCK_THREADS, and grouped_moe_gfx1250.py:863 re-derives _quant_mode (:1184); use the sources.
  5. Split the PR. +1010/−129 over 5 files is above the 1000-line first-review limit. The _quant_ctx/_hidden_pk8_load refactor 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.

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

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

Co-Authored-By: Claude Opus 5.5 <[email protected]>
XiaobingSuper added a commit to XiaobingSuper/aiter that referenced this pull request Nov 23, 2026
…#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
XiaobingSuper added a commit to XiaobingSuper/aiter that referenced this pull request Nov 23, 2026
…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]>
@lirui927
lirui927 force-pushed the fuse/f9-moe-route-quant branch from 41ef9b2 to 6c179fe Compare November 23, 2026 10:23
@lirui927

Copy link
Copy Markdown
Contributor Author

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 bf599489. Line numbers below refer to the new head of this PR, 6c179fe5.

Split (item 5)

  • PR A, [FlyDSL] refactor(flydsl): share the token-multidest quant context #8496: refactor(flydsl): share the token-multidest quant context. It contains the _quant_ctx / _hidden_pk8_load refactor of the default token-multidest kernel and nothing else (+115/-110, one file).
    • ISA evidence: FlyDSL compile-only for gfx1250 on CPU, 96 builds giving 81 kernels. All 81 are byte-identical to main (22_final_isa.s, compared with cmp).
    • The builds cover token-multidest K-split and TDM-staged, routeks with and without K-split, prequantized and row-major, the compact pair, row-indexed quant with and without activation, and the route + quant + scatter kernels. Each is built for fp4/fp8, wmma_rep 1/2 and K = 1024/6144.
    • These are FlyDSL kernels, so there is no hipcc step; the final-ISA dump is the backend output the runtime loads.
  • This PR is rebased on PR A and now carries only the route-in-quant fusion plus the fixes below (3 commits after PR A's, +995/-55 over 5 files). Its kernels without the route still compile to the same ISA as main: 81/81.

1. _use_fused_quant_preshuffle on paths that never use it

  • _compact_quant_pair is gone. _quant_mode, src_a1_scale and _prequantized (grouped_moe_gfx1250.py:1088-1108) and _compact_ascale with main's guards (:1113-1121) moved above the TP gate. The gate (:1127-1137) tests not _compact_ascale.
  • EP, prequantized and A-preshuffled calls therefore never call _use_fused_quant_preshuffle, as on main. A CPU path check confirms 0 calls for those cases on the branch, against 1 per call at 41ef9b2. The host-cost A/B is below.

2. AITER_FLYDSL_TOKEN_MULTIDEST_QUANT read last

  • token_multidest_on is gone. The env read is again the last term of the one use_token_multidest chain (moe_kernels.py:3915-3925), and the fused path has no chain of its own.

3. One token-multidest branch

  • One branch (moe_kernels.py:3964). md_ksplit and tdm_hidden_chunks=token_multidest_tdm_chunks(..., md_ksplit) serve both builds, so the literal 0 is gone. md_ksplit > 1 is required only when fused, and the five TP operands are appended only when fused.
  • The default build passes tp_route_slots=0, so it keeps its module name and kernel arguments. Its ISA is identical to main, as above.

4. No copied kernel limits

  • _FUSE_TP_ROUTE_QUANT_MAX_EXPERTS is gone. The grouped gate reads TP_ROUTE_MAX_EXPERTS and TP_ROUTE_MAX_NUMEL through _tp_route_limits() (grouped_moe_gfx1250.py:506-516), a cached import, so the per-call gate pays no import, and the gate checks the same route limit the wrapper enforces.
  • _tp_route_slots moved next to the limits it depends on, as tp_route_slots() in moe_fused_route_quant_scatter.py:138-148. It uses BLOCK_THREADS, and the wrapper imports it with the limits (moe_kernels.py:3824-3828).
  • _quant_mode is now computed once, at its original definition, which moved up (item 1).
  • The routeks builder takes the same knob as the token-multidest builder: tp_route_slots, with 0 = off. The separate fuse_tp_route flag is gone.

6. Docstrings

  • _emit_tp_route (moe_fused_route_quant_scatter.py:172) now says five barriers.
  • tp_route_slots (:138) says that a slot wholly past numel skips its load, and that in the last, partly used slot, threads past numel load the last id and count nothing.
  • _TpRouteStorage (:150) and _emit_tp_route say that the counts and starts are those of moe_route_psum_fused, but that rows within an expert are in route order here and in atomic order there. The routeks comment matches.

7. kernel_bench_callable "quant_a1" now passes tp_route_params (grouped_moe_gfx1250.py:1586).

8. row_to_token refusal and tests

  • or row_to_token is not None is added to the TP-fusion refusal (moe_kernels.py:3843), so the compact branch can no longer see a fused call. test_tp_route_params_contract checks the refusal.
  • test_grouped_skips_pad_rows_routed_to_minus_one runs its compact case with AITER_TDM_FUSE_TP_ROUTE_QUANT at its default (test_flydsl_grouped_gemm.py:991-996). It checks that the compact pair still runs, with no route build.
  • test_public_moe_fused_route_bitwise gains a (80 tokens, model_dim 1024) case (test_moe_tp_route_quant_fused.py:217) and asserts that fused_moe reaches the token-multidest route build there (:254). The dim-256 cases still cover the fallback to routeks.

Also in this round

  • The wrapper also refuses tile_m <= 0, outputs on another device, an expert count outside 1..256, and a route count that is 0, above 2048 or not a multiple of topk. The grouped gate checks the same route limit and topk <= E, so it never hands the quant a call it refuses.
  • The PR test adds the kernel limits (E = 256, top-8 with repeated ids within a token, 2048 routes) and seeds its inputs. Its python3 table takes --dim and sweeps dim 1024 and 6144.

GPU (MI455X), this head

  • The PR test passed 51 cases.
  • Neighbouring tests match main:
    • test_flydsl_grouped_gemm.py -k 'pad_rows or route': 11 passed.
    • test_route_psum_fused.py, test_ep_e8m0_scale.py and test_grouped_gemm_stream.py: 46 passed and 1 failed. The failure is test_missing_private_api_uses_public_fallback, which also fails on main.
    • test_flydsl_moe_aux.py exits 0.
  • Route + quant (M3 routing, MXFP4), against main's route + quant:
    • dim 1024: 2.91–6.47 µs against 5.67–8.89 µs at 1–409 tokens, -27% to -51%.
    • dim 6144: -40% to -51% up to 128 tokens, -26% at 256 and -7% at 409.
  • The whole-MoE-layer time you asked for. Public fused_moe, MiniMax-M3 TP4 shape (model_dim 6144), switch off vs on:
tokens 1 4 16 64 128 256 409
change -17.8% -13.0% -10.2% -2.6% -2.3% -3.2% -3.0%

Host cost (CPU)

  • Method: every FlyDSL launch is stubbed, tensors are on the CPU, and grouped_gemm_gfx1250_a8w4 runs with the tuned rows. Each process is pinned to one core with ASLR off. Main, this head and main again (A/A) were interleaved over 10 rounds.
  • Same-round deltas, median and interquartile range in µs. GPU jobs shared the machine, so the A/A spread is wide.
call A/A this head − main
EP M3 64 / 512 tok -0.50 / +0.41 +1.09 (-0.33..+2.37) / +1.90 (+0.16..+3.56)
prequantized EP 64 / 512 tok -0.67 / +0.71 +1.52 (+0.61..+8.09) / +2.42 (+1.00..+3.94)
A-preshuffle prefill dsr1 / M3 / dsv41 +1.07 / -3.35 / -0.27 -1.55 / +0.65 / +1.26 (IQRs span 0)
TP decode M3 1 / 16 tok (fused) +0.21 / +0.06 +1.95 / +1.87
TP decode M3 32 tok, switch=0 +0.56 +4.03 (+2.82..+8.69)
TP decode Kimi-K2 E=384 4 / 32 tok +0.55 / +0.93 +1.44 / +2.61
  • Against 41ef9b2, the A-preshuffle prefill regression from item 1 is gone: +11.8 and +15.4 µs there, inside the noise here.
  • Several non-fused paths still show +1 to +2.6 µs.
    • They make no extra _use_fused_quant_preshuffle call: 0 calls for EP, prequantized and A-preshuffle, as on main.
    • cProfile puts the difference in _grouped_a8w4_tdm_moe self time, not in any one call.
    • Prequantized EP stops at the gate's first term, so I cannot yet tell this apart from a code-layout effect, and I am still looking at it.
  • On the fused decode path, the call saves 2.6 µs (1 token) to 4.0 µs (16 tokens) of device time per MoE layer, against +1.9 µs of host time.

#7547
This PR lands first, and #7547 rebases onto its builder. The two designs fit as route modes on one builder:

  • Builder. build_moe_token_multidest_quant_module keeps one shared quant setup (_quant_ctx, from PR A). A route mode only decides where each warp's route rows come from:
    • default: rows loaded from topids_to_rows;
    • tp_route_slots > 0 (this PR): a second entry point that derives the rows in-block. It needs the K-split build.
    • route_expert_bits > 0 ([FlyDSL] perf(flydsl): route the gfx1250 TP prefill MoE in one launch #7547): a decode step on the loaded rows in the default entry point, starts[expert] + slot, followed by publishing the final rows, before _quant_ctx.
    • The two modes are exclusive; the builder will refuse both at once.
  • Wrapper. route_expert_bits joins the same token-multidest branch and appends its row_starts / final_rows operands only when it is on, as the TP operands do here. The default build then keeps its name, ABI and ISA.
  • Grouped MoE. The gates are disjoint: this PR serves _fused_tp_route batches (numel <= 2048), and [FlyDSL] perf(flydsl): route the gfx1250 TP prefill MoE in one launch #7547 serves larger ones. Both test the _compact_ascale computed once here.

@lirui927
lirui927 requested a review from valarLip November 23, 2026 10:23
@lirui927
lirui927 force-pushed the fuse/f9-moe-route-quant branch from 6c179fe to a6fd1de Compare November 25, 2026 07:46
lirui927 and others added 5 commits November 25, 2026 11:20
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]>
@zhuyuhua-v

Copy link
Copy Markdown
Contributor

The review-pr scan (aiter skill, from main) at 2616ddb reports ⚠️ NEEDS WORK: 2 warnings and 1 note; all 8 Step 8 gates pass, and an independent reader upheld all three findings. At 1294 lines / 5 files the PR is over the review bar, and the design gate recommends a split.

  • Warning (code): the PR adds an import of kernels/topk/topk_common.py to moe_fused_route_quant_scatter.py. Since this PR's base, main has put that file's quant builders behind AOT caches ([FlyDSL] [gfx1250] Launch the GEMM and grouped-MoE kernels through the AOT cache #8678, [FlyDSL] [AOT] Port the remaining AOT-replayed launchers to export_to_c #8911), so once both are merged, five AOT caches have topk_common.py (and then kernels/dpp_utils.py) as undeclared sources. tests/test_flydsl_aot_launch.py::test_every_cache_declares_the_kernel_files_its_sources_import then fails, the AOT prebuild raises, and these kernels always fall back to JIT. This was checked statically on main plus the diff; main alone is clean. Rebasing and adding both files to _GFX1250_MOE_ROUTE_QUANT in aiter/aot/flydsl/sources.py fixes it.
  • Warning (evidence, inferred): the new default-on gate covers every gfx1250 TP batch up to 2048 routes with E ≤ 256, but perf was measured only for MiniMax-M3 routing (E=129, top-5), and the gain already drops to -7% at 2045 routes. The tuned dim-7168 E=96 top-6 and dim-4096 E=256 top-6 rows and batches near the cap are untimed, and the -17.8% whole-layer number has no script. Numbers near the cap, with the gate capped if the gain turns negative, would settle it.
  • Note: the new test runs only on gfx1250 and no CI job runs gfx1250. The only GPU results are from head 6c179fe, two revisions back, so a gfx1250 rerun at the rebased head is worth asking for.
Card

ROCm/aiter PR #7543 — [FlyDSL] perf(flydsl): fold the gfx1250 TP MoE route into the activation quant

On gfx1250 tensor-parallel MoE batches of at most 2048 routes, this PR drops the separate route launch: every activation-quant workgroup recomputes the per-expert counts and tile-aligned starts in LDS from all topk_ids and writes its own routes' rows, so a MoE call runs 4 kernels instead of 5.

Review (advisory): ⚠️ NEEDS WORK
Validation (deterministic): NOT RUN — runtime change, target op_tests/test_moe_tp_route_quant_fused.py (added by this PR, gfx1250-only). This review ran no GPU work. The auto-run on this host (MI355X, gfx950) stopped at runtime_compat (no torch), and its report was rejected because it names no kernel route. Environment gap, not a PR defect.
Perf (advisory): NOT RUN — the only harness is new (no base side to compare) and gfx1250-only, and this review ran no GPU work. The PR's own tables are from MI455X at 6c179fe5, not this head.
Size: 1294 lines / 5 files — over the review bar
Design (advisory): SPLIT — (1) #8496's _quant_ctx / _hidden_pk8_load refactor (its own PR, still in this diff until it merges); (2) the opt-in kernel side: _emit_tp_route, tp_route_slots on both quant builders, tp_route_params in flydsl_moe_fused_quant_preshuffle, plus the kernel-level tests; (3) the default-on _fuse_tp_route_quant gate in grouped_moe_gfx1250.py plus the public fused_moe and pad-row tests.
Human: recommend valarLip review whether every quant workgroup (and every K-split slice) should recompute the whole TP route by default up to 2048 routes in _fuse_tp_route_quant, and how tp_route_slots should coexist with #7547's route_expert_bits on build_moe_token_multidest_quant_module, for PR #7543.

⚠️ [verified] The new import in aiter/ops/flydsl/kernels/moe_fused_route_quant_scatter.py (from aiter.ops.flydsl.kernels.topk.topk_common import _warp_inclusive_prefix_i32) becomes an undeclared AOT source once this PR is merged with current main. After this PR's base, main put build_moe_token_multidest_quant_module and build_moe_fused_quant_preshuffle_route_ksplit_module behind @aot_factory (#8678, #8911). On main da53451 plus this diff, five caches have kernels/topk/topk_common.py undeclared: the three created in this file (moe_fused_quant_preshuffle_route_ksplit, moe_token_multidest_quant, moe_token_multidest_compact_quant) and moe_apre_quant / moe_apre_scatter, which declare this file as a source. Once that is declared, its own import kernels/dpp_utils.py is undeclared too. I checked this with an AST copy of aot_cache._undeclared_imports run on both trees: on main alone it finds nothing. Impact: tests/test_flydsl_aot_launch.py::test_every_cache_declares_the_kernel_files_its_sources_import fails, and an AOT prebuild raises _UndeclaredSourceError for these caches. At run time every missed entry, including the default token-multidest and routeks quant kernels, logs that error and falls back to a JIT trace, so these caches can never fill. Author must rebase onto main, add kernels/topk/topk_common.py and kernels/dpp_utils.py to _GFX1250_MOE_ROUTE_QUANT in aiter/aot/flydsl/sources.py, and re-run the AOT replay with --check.

⚠️ [inferred] The new default gate in aiter/ops/flydsl/grouped_moe_gfx1250.py (_fuse_tp_route_quant) takes every gfx1250 TP batch with topk <= E <= 256 and at most 2048 routes, but perf was measured only for MiniMax-M3 routing (E=129, top-5) at dim 1024 and 6144. Every quant block re-reads and re-counts all routes, and K-split multiplies the block count. At dim 6144 the route+quant gain already falls from -26% at 256 tokens to -7% at 409 tokens (2045 routes). None of these were timed: the tuned gfx1250 rows the gate admits in aiter/configs/tuned_grouped_fmoe.csv (model_dim 7168 / E=96 / top-6 at 8–256 tokens and model_dim 4096 / E=256 / top-6 at 1–256 tokens, up to 1536 routes), and the untuned batches near the cap, such as 341 tokens at top-6 or 256 tokens at top-8 (about 2048 routes). The wide E=256 top-8 case in op_tests/test_moe_tp_route_quant_fused.py checks correctness only, and the -17.8% whole-layer table has no script in the PR. Impact: if the redundant route work outgrows the saved launch on wider rows near the bound, those batches become slower by default with nothing to flag it; nothing was re-timed in this review. Reviewer should ask for route+quant or whole-layer numbers at dim 7168 near the 2048-route cap (E=96 top-6 at 256–341 tokens, and a top-8 E<=256 shape at 256 tokens), plus the whole-layer harness. If the gain crosses zero, cap the gate's route count below that point.

📝 [verified] The new test op_tests/test_moe_tp_route_quant_fused.py skips on anything but gfx1250. None of the 30 passing checks on this PR runs on gfx1250. The file is not in .github/test-manifests/mi450-gfx1250-p0.txt, and the label-gated ffm-bringup.yaml runs only the PA-decode, HERD and Triton tests. So the only GPU evidence is the author's MI455X run at 6c179fe5, two heads back. This head was checked only on CPU (ISA-equality compile, CPU pytest, black and ruff), and main has changed 212 lines of grouped_moe_gfx1250.py since this PR's base. Impact: a regression in the default-on _fuse_tp_route_quant path would reach gfx1250 users without any automated signal. Reviewer should ask for the 51-case run of op_tests/test_moe_tp_route_quant_fused.py, plus test_flydsl_grouped_gemm.py -k 'pad_rows or route', on gfx1250 at the rebased head before merge.

Step 8 gates
answers      ANSWERS COMPLETE: 6/6
diagnostic   DIAGNOSTIC COMPLETE: 6/6
corefiles    CORE FILES: none in this diff, declared
ledger       LEDGER COMPLETE: 39/39 rules adjudicated with evidence
card         CARD NAILED DOWN: 3 findings, each traced to an adjudication and anchored in a changed file; every FIRE accounted for
refutations  REFUTATIONS COMPLETE: 3 attempted, 3 survived, 0 killed before reaching the card
independent  INDEPENDENTLY REFUTED: 3 judged, 3 defended, 0 killed by a reader who had not seen the reasoning
design       DESIGN ROUTED: size 1294 lines / 5 files -- over the review bar

lirui927 and others added 5 commits December 2, 2026 06:39
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]>
lirui927 pushed a commit to lirui927/aiter that referenced this pull request Dec 2, 2026
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]>
@lirui927

lirui927 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

review-pr scan (aiter skill from main ea578b41: fetch.sh + triage.py, REVIEW_AUTO_VALIDATE=0) at head 5689d6643565e0017165c5708ca59d1c3aeea2e2: ⚠️ NEEDS WORK, 2 warnings and 1 note. 39 of 68 rules derived, 925 lines / 6 files, the diff applies to main 0dbaa95c. All 8 Step 8 gates pass; an independent reader upheld the three findings and corrected details, which are folded in. Advisory only; no GPU work was run by the scan.

Card

ROCm/aiter PR #7543 — [FlyDSL] perf(flydsl): fold the gfx1250 TP MoE route into the activation quant

On gfx1250 tensor-parallel MoE batches of at most 2048 routes, this PR drops the separate route launch: every activation-quant workgroup recomputes the per-expert counts and tile-aligned starts in LDS from all topk_ids and places its own routes, so a MoE call runs 4 kernels instead of 5.

Review (advisory): ⚠️ NEEDS WORK
Validation (deterministic): NOT RUN — auto-validation is disabled (REVIEW_AUTO_VALIDATE=0), so there is no head-matched report; the target would be op_tests/test_moe_tp_route_quant_fused.py (added by this PR, gfx1250-only). Environment gap, not a PR defect. The PR body reports 8 passed on one MI455X at this head; that run was not reproduced here.
Perf (advisory): NOT RUN — this review ran no GPU work, and the only harness is added by this PR, so base has nothing to time. The PR's own table (MI455X, this head, one run) times main's route launch + quant against the fold in one build: -43% to -49% at 1–128 tokens, -26% (dim 1024) and -9% (dim 6144) at 409 tokens.
Size: 925 lines / 6 files
Design (advisory): every quant workgroup (and every K-split slice) recomputes the whole TP route in LDS from all topk_ids, default-on up to TP_ROUTE_MAX_NUMEL = 2048 routes, instead of one separate route launch
Human: recommend valarLip review whether the per-workgroup route recompute behind _fuse_tp_route_quant should stay default-on up to 2048 routes for every E/topk/dim, or be capped until wider shapes are timed, for PR #7543

⚠️ [inferred] The default gate _fuse_tp_route_quant in aiter/ops/flydsl/grouped_moe_gfx1250.py takes every gfx1250 TP batch with topk <= E <= 256 and at most 2048 routes (bf16 input, no a1_scale, no A-preshuffle, not the compact-scale pair), but the only timing is MiniMax-M3 routing (E = 129, top-5) at dim 1024 and 6144 from op_tests/test_moe_tp_route_quant_fused.py. Each quant block re-reads and re-counts all routes, and K-split multiplies the block count: at dim 6144 the PR's own gain falls from -47% at 128 tokens to -27% at 256 and -9% at 409 tokens (2045 routes). The wider shapes the PR lists as untimed (E = 96 top-6 at dim 7168, E = 256 top-6 at dim 4096, E = 256 top-8 at the 2048-route limit) have no posted result: the benchmark's -p wide pattern covers only E = 256 top-8, and nothing in it times the top-6 shapes; also the whole-layer fused_moe time was not re-measured at this head. Impact: if the repeated route work outgrows the saved launch on a wider row near the limit, those batches get slower by default with nothing to flag it. Reviewer should ask for python3 op_tests/test_moe_tp_route_quant_fused.py -p wide -t 128 256 -d 4096 7168 numbers plus timings of the two top-6 shapes, and for the gate's route limit to come down if the gain crosses zero.

⚠️ [inferred] The PR body reports one native SIGABRT in op_tests/test_moe_tp_route_quant_fused.py::test_public_moe_fused_route_bitwise[80-1024-a4w4], the case that takes the token-multidest route build, on a GPU shared with another job; the cause is not identified and it did not recur in 3 runs on an idle GPU. Impact: if the abort comes from the folded route rather than from the other job, it would hit default-on TP batches of 64 tokens and more that split K, up to 2048 routes; the test body also runs the fold-off pass before the fold-on one, so the abort is not attributed to either, and three clean runs on an idle card do not separate the two. Reviewer should ask for a repeat loop of that case, fold on and with AITER_TDM_FUSE_TP_ROUTE_QUANT=0, while another job shares the GPU, and for the HIP error or backtrace if it recurs.

📝 [verified] No CI job runs the new path: op_tests/test_moe_tp_route_quant_fused.py skips off gfx1250, the test is in neither .github/test-manifests/mi450-gfx1250-p0.txt nor -p1.txt, and .github/workflows/ffm-bringup.yaml, the only workflow that runs tests on gfx1250, does not run it. The green checks therefore say nothing about the fold, and the changed pad-row test in op_tests/flydsl_tests/test_flydsl_grouped_gemm.py gives no base-vs-head signal in the author's image (7 AOT-cache failures on the branch; main aborts there). Impact: a regression in this default-on path reaches gfx1250 users with no automated signal. Reviewer should ask who reruns the 8 cases on gfx1250 before merge, or accept the author's log at this head.

Step 8 gates
answers      ANSWERS COMPLETE: 6/6
diagnostic   DIAGNOSTIC COMPLETE: 6/6
corefiles    CORE FILES: none in this diff, declared
ledger       LEDGER COMPLETE: 39/39 rules adjudicated with evidence
card         CARD NAILED DOWN: 3 findings, each traced to an adjudication and anchored in a changed file; every FIRE accounted for
refutations  REFUTATIONS COMPLETE: 3 attempted, 3 survived, 0 killed before reaching the card
independent  INDEPENDENTLY REFUTED: 3 judged, 3 defended, 0 killed by a reader who had not seen the reasoning
design       DESIGN ROUTED: size 925 lines / 6 files

@zhuyuhua-v

Copy link
Copy Markdown
Contributor

The review-pr scan (aiter skill, from main) at 5689d66 reports ⚠️ NEEDS WORK: 1 warning and 2 notes; all 8 Step 8 gates pass, and an independent reader upheld all three. No code or description change is required.

  • The AOT warning from the 2616ddb scan is resolved: aiter/aot/flydsl/sources.py now adds kernels/dpp_utils.py and kernels/topk/topk_common.py to _GFX1250_MOE_ROUTE_QUANT. A static copy of the AOT declared-sources check on main 1eb4d68 plus this diff finds no undeclared import or env read in any of the 65 caches; as a control, main's sources.py reports topk_common.py missing in exactly the five caches.
  • Warning (evidence, not resolved): the default-on gate still covers every gfx1250 TP batch with topk ≤ E ≤ 256 up to 2048 routes, but timing covers only MiniMax-M3 routing (E=129, top-5). At dim 6144 the gain falls from -47% at 128 tokens to -27% at 256 and -9% at 409, because every quant block and K-split slice recounts all routes. Untimed in-gate shapes include E=256 top-8 at the 2048-route cap, the tuned E=256 top-6 dim 4096 and E=96 top-6 dim 7168 rows, dsv41_flash E=96 top-6 dim 5120, and gptoss E=128 top-4 dim 3072. The PR's own -p wide -t 128 256 -d 4096 7168 run plus one top-6 shape near 2048 routes would settle it, with the route cap lowered wherever the gain turns negative.
  • Notes: the author's GPU runs are now at this head (8 passed on MI455X), but the test skips everywhere except gfx1250 and is in neither mi450-gfx1250 manifest. Is it worth adding to the P0 manifest? The body also reports one unexplained SIGABRT in test_public_moe_fused_route_bitwise[80-1024-a4w4] on a shared GPU. That case runs the new token-multidest route build, where reading the kernel found no out-of-range store or LDS aliasing. A repeat loop with the fold on and with AITER_TDM_FUSE_TP_ROUTE_QUANT=0 while another job shares the GPU would help.
Card

ROCm/aiter PR #7543 — [FlyDSL] perf(flydsl): fold the gfx1250 TP MoE route into the activation quant

On gfx1250 tensor-parallel MoE batches of at most 2048 routes, this PR drops the separate route launch: every activation-quant workgroup recomputes the per-expert counts and tile-aligned starts in LDS from all topk_ids and places its own routes, so a MoE call runs 4 kernels instead of 5.

Review (advisory): ⚠️ NEEDS WORK
Validation (deterministic): NOT RUN — auto-validation is off for this scan (REVIEW_AUTO_VALIDATE=0, no GPU work on a shared node); the target would be op_tests/test_moe_tp_route_quant_fused.py, added by this PR and gfx1250-only. Environment gap, not a PR defect. The PR body reports 8 passed on one MI455X at this head; not reproduced here.
Perf (advisory): NOT RUN — no GPU work in this scan, and the only harness is added by this PR (no base side) and gfx1250-only. The PR's own table (MI455X, this head, one run) times main's route launch + quant against the fold in one build: -43% to -49% at 1–128 tokens, -26% (dim 1024) and -9% (dim 6144) at 409 tokens.
Size: 925 lines / 6 files
Design (advisory): every activation-quant workgroup, and every K-split slice of it, recomputes the whole TP route in LDS from all topk_ids instead of one separate route launch, default-on for gfx1250 TP batches up to 2048 routes and E ≤ 256, with rows inside an expert moving from atomic order to route order
Human: recommend valarLip review whether the per-workgroup route recompute behind _fuse_tp_route_quant in PR #7543 should ship default-on for every E/topk/dim up to 2048 routes, or be capped to the timed MiniMax-M3 range until wider shapes are measured

⚠️ [inferred] The default-on gate _fuse_tp_route_quant in aiter/ops/flydsl/grouped_moe_gfx1250.py takes every gfx1250 TP batch with topk ≤ E ≤ 256 and at most 2048 routes (bf16 input, no a1_scale, no A-preshuffle, not the compact-scale pair), but the only timing, bench_route_quant in op_tests/test_moe_tp_route_quant_fused.py, is MiniMax-M3 routing (E = 129, top-5) at dim 1024 and 6144. Every quant block, and every K-split slice, reloads and recounts all routes, so the gain shrinks as the batch grows: at dim 6144 it goes from -47% at 128 tokens to -27% at 256 and -9% at 409 tokens (2045 routes). Untimed inside the same gate: E = 256 top-8 at the 2048-route cap (the benchmark's wide pattern, run only for correctness), and the gfx1250 tuned rows E = 256 top-6 at dim 4096 and E = 96 top-6 at dim 7168 in aiter/configs/tuned_grouped_fmoe.csv, E = 96 top-6 at dim 5120 (dsv41_flash) and E = 128 top-4 at dim 3072 (gptoss) whenever they are served without EP. The whole-layer fused_moe time was not re-measured at this head either. Impact: if the recompute outgrows the saved launch on a wider row near the cap, those batches get slower by default with nothing to flag it. Reviewer should ask for python3 op_tests/test_moe_tp_route_quant_fused.py -p wide -t 128 256 -d 4096 7168 numbers plus one top-6 shape near 2048 routes, and for the gate's route cap to come down wherever the gain crosses zero.

📝 [verified] No CI job runs the new path: op_tests/test_moe_tp_route_quant_fused.py skips unless get_gfx_runtime() is gfx1250, it is in neither .github/test-manifests/mi450-gfx1250-p0.txt nor mi450-gfx1250-p1.txt (the internal nightly MI450 selections), and ffm-bringup.yaml, the only public gfx1250 job, runs other tests. The MI35X checks on this head therefore say nothing about the fold. The author's GPU results are now from this head (8 passed on MI455X), which settles the staleness half of the earlier note. Impact: a regression in this default-on path reaches gfx1250 users with no automated signal. Reviewer should ask whether the 8 nodes should join the mi450-gfx1250 P0 manifest, or accept the author's log at this head.

📝 [inferred] The PR body reports one native SIGABRT in test_public_moe_fused_route_bitwise[80-1024-a4w4], the case that takes the new token-multidest route build (K-split 4), on a GPU shared with another job; the cause is not identified and 3 reruns on an idle card passed. Reading _emit_tp_route and the token-multidest route block in aiter/ops/flydsl/kernels/moe_fused_route_quant_scatter.py found no out-of-range store (rows stay under the wrapper's n_rows bound, masked_m/psum are written for tid < experts, topids_to_rows only for valid tokens) and no LDS aliasing (the K-split build has no TDM staging), which is consistent with a fault from the other job; but the case calls fused_moe three times in one process (default env, fold off, fold on), so the abort is not attributed to either path. Impact: if it is the fold, default-on TP batches of 64+ tokens that split K would abort. Reviewer should ask for a repeat loop of that case with the fold on and with AITER_TDM_FUSE_TP_ROUTE_QUANT=0 under GPU contention, and for the HIP error if it recurs.

Step 8 gates
answers      ANSWERS COMPLETE: 6/6
diagnostic   DIAGNOSTIC COMPLETE: 6/6
corefiles    CORE FILES: none in this diff, declared
ledger       LEDGER COMPLETE: 39/39 rules adjudicated with evidence
card         CARD NAILED DOWN: 3 findings, each traced to an adjudication and anchored in a changed file; every FIRE accounted for
refutations  REFUTATIONS COMPLETE: 4 attempted, 3 survived, 1 killed before reaching the card
independent  INDEPENDENTLY REFUTED: 3 judged, 3 defended, 0 killed by a reader who had not seen the reasoning
design       DESIGN ROUTED: size 925 lines / 6 files

@lirui927

lirui927 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Reply to the review of 2026-11-19 (valarLip, at 41ef9b2). Status of the eight items at 5689d664 (main ea578b41 merged in); line numbers are at that head.

  1. _use_fused_quant_preshuffle on paths that never use it: fixed. _compact_ascale is computed once with main's guards (grouped_moe_gfx1250.py:1046-1055), so EP, prequantized, compact-plan and A-preshuffled calls short-circuit before the call, and the gate reads not _compact_ascale (:1228). Host cost against main with an A/A control (CPU, at 9fcefee8; host code unchanged since): EP +0.4..0.7 µs, prequantized EP +0.7..0.95, A-preshuffle prefill -0.6..+0.1 (A/A -1.1..+0.5); 42-69 more Python bytecodes per call on paths that do not fuse (0.3-0.5%), no extra function call. Table in the description.
  2. AITER_FLYDSL_TOKEN_MULTIDEST_QUANT read last: fixed, moe_kernels.py:3919-3929.
  3. One token-multidest branch: fixed, moe_kernels.py:3956-3980. The route appends its operands, and md_ksplit > 1 is required only when fused (:3968).
  4. Kernel limits not copied: fixed. _tp_route_limits() imports TP_ROUTE_MAX_EXPERTS / TP_ROUTE_MAX_NUMEL (grouped_moe_gfx1250.py:598-607), and the wrapper imports them too (moe_kernels.py:3828-3832). _tp_route_slots is gone: the slot count is the constant TP_ROUTE_SLOTS, and TP_ROUTE_MAX_NUMEL = TP_ROUTE_SLOTS * BLOCK_THREADS (moe_fused_route_quant_scatter.py:135-137). _quant_mode is derived once (grouped_moe_gfx1250.py:1022).
  5. Split: not done, because there is nothing left to split out. The route is now a tuple operand of main's token-multidest kernel (tp_route=(), empty with the route off), and the _quant_ctx / _hidden_pk8_load refactor is gone from the diff. [FlyDSL] refactor(flydsl): share the token-multidest quant context #8496 is closed and is not a dependency. The kernel file differs from main by +434/-1, and the PR is +878/-47 against main (925 changed lines, 6 files).
  6. Docstrings: fixed. Five barriers (moe_fused_route_quant_scatter.py:182-183); whole slots past numel skip their load (:131-134); route order here, atomic order in moe_route_psum_fused (:144-146, :178-180).
  7. kernel_bench_callable "quant_a1" passes tp_route_params: fixed, grouped_moe_gfx1250.py:1681.
  8. row_to_token refusal: fixed, moe_kernels.py:3847. The compact pad-row case runs at the default switch (test_flydsl_grouped_gemm.py:1003, case (1, 0)), and test_public_moe_fused_route_bitwise has dim-1024 cases that assert the token-multidest route build ran (test_moe_tp_route_quant_fused.py:151, :188).

On #7547: the two are route modes of the same builder and are exclusive there. This PR's tp_route_slots serves the batches the single-workgroup route serves today (up to 2048 routes); #7547 serves the larger ones. #7547 is stacked on this PR, so this one goes first.

What ran at this head, on one idle MI455X (gfx1250), 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 (the file is 215 lines and 8 cases, was 278 and 51).
  • test_public_moe_fused_route_bitwise[80-1024-a4w4] alone: 1 passed; with AITER_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 device time against main's route launch + quant, MiniMax-M3 routing: -43% to -49% at 1-128 tokens, -26% (dim 1024) and -9% (dim 6144) at 409 tokens.
  • On CPU, compile-only: the 81 kernels without the route are byte-identical to main.

Still open:

  • Timing on wider shapes near the 2048-route cap (E = 256 top-8, E = 96 / E = 256 top-6) is not measured; will post when measured. The whole-layer fused_moe time is not re-measured at this head either.
  • One earlier run of the PR test, on a GPU shared with another job, aborted (native SIGABRT) in test_public_moe_fused_route_bitwise[80-1024-a4w4]. The cause is not identified; it did not recur in three runs on an idle card.
  • CI has no gfx1250 runner, and op_tests/flydsl_tests/test_flydsl_grouped_gemm.py gives no regression signal in that image (main fails there in the same way), so the results above are the only device evidence.

@lirui927

lirui927 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Reply to the review-pr scan of 2026-12-02 at 5689d664 (zhuyuhua-v), which supersedes the scan at 2616ddb6. Thanks.

  1. Undeclared AOT sources (earlier scan): fixed, as the new scan confirms. kernels/dpp_utils.py and kernels/topk/topk_common.py are in _GFX1250_MOE_ROUTE_QUANT (aiter/aot/flydsl/sources.py). python -m aiter.aot.flydsl.moe and --check pass on CPU (12 jobs). tests/test_flydsl_aot_launch.py itself has not run: it does not import on the CPU host used.
  2. Timing of wider shapes near the 2048-route cap: not done, still open. Only MiniMax-M3 routing (E = 129, top-5) is timed, at dim 1024 and 6144. python3 op_tests/test_moe_tp_route_quant_fused.py -p wide -t 128 256 -d 4096 7168 times E = 256 top-8 up to the cap but has not been run on a device; the benchmark has no pattern for the top-6 shapes. Will post when measured. Until then the default-on range is wider than the timed range, as the scan says.
  3. gfx1250 results at this head: done. On one idle MI455X the PR test gives 8 passed and the benchmark script exits 0 (48 rows, err 0). On the P0 manifest question: this PR changes nothing under .github; whether the 8 cases join a gfx1250 manifest is the maintainers' call.
  4. SIGABRT in test_public_moe_fused_route_bitwise[80-1024-a4w4]: not explained, still open. At this head on an idle card the case alone gives 1 passed with the fold on and 1 passed with AITER_TDM_FUSE_TP_ROUTE_QUANT=0, and it passes in the full 8-case run. The abort happened once, on a GPU shared with another job; no HIP error was captured, and the repeat loop under GPU contention that the scan asks for has not been run.
  5. Size: 925 changed lines in 6 files against main. The refactor the earlier design gate wanted split out is gone from the diff ([FlyDSL] refactor(flydsl): share the token-multidest quant context #8496 is closed).

@valarLip valarLip left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ 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:

  1. 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, no a1_scale and 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.

  2. 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 the col_base/buffer_load (:654-671). Its result then replaces that load. The comment at :617-620 warns 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.

  3. 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_ctx refactor.
    • 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.

  4. Run the new default-on path in a gfx1250 CI lane. op_tests/test_moe_tp_route_quant_fused.py skips off gfx1250, and it is in neither .github/test-manifests/mi450-gfx1250-p0.txt nor -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).

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants