Repository navigation
[HIP] [FlyDSL] [JIT] [Perf] gfx1250 MoE router GEMM + top-k in one launch: aiter.router_topk_gating - #8431
[HIP] [FlyDSL] [JIT] [Perf] gfx1250 MoE router GEMM + top-k in one launch: aiter.router_topk_gating#8431XiaobingSuper wants to merge 11 commits into
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
…-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]>
624f007 to
d3afecb
Compare
With an aiter that has flydsl_moe_router_topk (ROCm/aiter#8431), MiniMax-M3's routed MoE hands the gate weight to FusedMoE, which runs the router GEMM and the biased sigmoid top-4 as one launch and passes the routing to forward_impl: bitwise the same topk_weights / topk_ids, and the same MoE output. aiter's router_split_k decides per call (gfx1250, 128 experts, tuned shapes); other calls keep the gate GEMM + topk_gating. Co-Authored-By: Claude Opus 5.5 <[email protected]>
With an aiter that has flydsl_moe_router_topk (ROCm/aiter#8431), MiniMax-M3's routed MoE hands the gate weight to FusedMoE, which runs the router GEMM and the biased sigmoid top-4 as one launch and passes the routing to forward_impl: bitwise the same topk_weights / topk_ids, and the same MoE output. aiter's router_split_k decides per call (gfx1250, 128 experts, tuned shapes); other calls keep the gate GEMM + topk_gating. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
GPU check of a28269c on 4x MI455X (gfx1250, one GPU used): this PR's own tree (main 9f3cba2 + the two commits), every module JIT-built from it, flydsl 0.5.0.
Router time, rand inputs, unfused -> fused (us): T=1 4.46 -> 4.00, 64 5.14 -> 4.94, 128 5.45 -> 4.85, 192 6.39 -> 4.95, 256 6.24 -> 4.96. At T=16, 320 and 512 the fused launch is slower: 4.74 -> 4.82, 6.86 -> 7.84, 6.62 -> 7.20. |
WAITING ON AUTHORPR Self-ReviewRevision Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision. Questions to answer in the PR description
Edit the Missing or invalid answers
Shadow mode reports what would block but keeps the status successful. Set |
Same shape as ROCm#8431's simplified op test, on this branch's router_split_k API. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Rebased onto current main as three linear commits: the op with its kernel, tuned table and op test (
@yadaish could you review this one? (CONTACTS.md lists you for FlyDSL MoE kernels.) |
With an aiter that has flydsl_moe_router_topk (ROCm/aiter#8431), MiniMax-M3's routed MoE hands the gate weight to FusedMoE.forward_fused_router, which runs the router GEMM and the biased sigmoid top-k as one launch and passes the routing to forward_impl. FusedMoE.supports_fused_router checks that the layer routes with the plain biased-sigmoid top-k (topk_gating) the op reproduces. aiter's flydsl_moe_router_topk_supported then decides each call (arch, expert count, top-k, layouts, a tuned row for the shape); every other call keeps tgemm.mm + topk_gating. The op sums the logits in fp32 in its own order, so near ties may rank otherwise than after the bf16 GEMM. The aiter import covers only the two router names, so an older aiter turns the fusion off and nothing else does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
64aac3b to
9d8d224
Compare
|
review-pr scan (aiter's Fixed in
review-pr cardaiter PR #8431 — [HIP] [FlyDSL] [JIT] [Perf] gfx1250 MoE router GEMM + biased sigmoid top-k in one launchAdds a gfx1250-only FlyDSL op that fuses the MoE router GEMM and the biased-sigmoid top-k gating into one cluster-launched kernel, selected per shape by a new tuned table, with a tuner and an AOT module. Review (advisory):
|
… launch flydsl_moe_router_topk(x, w, topk, correction_bias=None, need_renorm=True, routed_scaling_factor=1.0, score_func="sigmoid", *, num_valid_rows=None, topk_weights=None, topk_ids=None) computes tgemm.mm(x, w) + topk_gating(...) with topk_gating's routing parameters in one gfx1250 FlyDSL launch. flydsl_moe_router_topk_supported(<same arguments>) is True where the call is implemented (sigmoid, 128 experts, top-1 to top-32, bf16 x / w, gfx1250) and aiter/configs/moe_router_topk_tuned.csv (AITER_CONFIG_MOE_ROUTER_TOPK, padded M) has a fused row; otherwise callers keep tgemm.mm + topk_gating and the op raises ValueError with the reason. split_k / prefetch come from the tuned row. The logits are summed in the kernel's own split-K order, so experts whose score + bias are within 2e-3 may rank in either order. The op test checks the fused launch at every split_k and tgemm.mm + topk_gating against an fp32 reference, on random, tied, saturating and padded rows, and that each tuned row is supported exactly where it is fused. Rows: E 128, K 6144, top-4, M 1-512 on MI455X (256 CUs). Co-Authored-By: Claude Opus 5.5 <[email protected]>
csrc/flydsl_moe_router_topk/moe_router_topk_tune.py (TunerCommon): per shape of moe_router_topk_untuned.csv, the tuned router GEMM (tuned into --gemm_tuned_file first if missing) + topk_gating against the fused launch at each split_k in (1, 2, 4, 8, 16) that divides K into 32-wide steps and each prefetch depth, in interleaved CUDA graphs with an A/A band. Every candidate must pass the op test's fp32 route check; fused = 1 where the fastest beats the unfused pair by more than the band. Co-Authored-By: Claude Opus 5.5 <[email protected]>
aiter/aot/flydsl/moe_router_topk.py replays flydsl_moe_router_topk for each fused row of the tuned router table, with and without expert bias and renorm (both compiled in), and is listed in AOT_MODULES. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…/renorm/scale builds The launch runs under `with torch.cuda.device(x.device)` on that device's current stream, and the cached handle is keyed by the device index, so a call on a non-current GPU, or the same shape on two GPUs, launches on the right one. The op test gains a "no_bias" case (no expert bias, scale 2.5) and a "no_renorm" case (renorm off, scale 2.5); the torch reference and the unfused topk_gating apply the same renorm and scale. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The launcher takes ldo, the row stride of both outputs, so the routed top-k can be written into the first columns of a wider buffer. The wrapper passes topk_weights.stride(0) and accepts outputs with a unit column stride and rows at least topk apart (both sharing one stride); the op test adds a strided_out case that also checks the other columns stay untouched. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A workgroup's waves split its K chunk instead of its experts: each wave covers all of the group's experts and the tile's rows, so every weight and activation fragment is loaded once per workgroup (the activation was loaded by every wave before), and the waves sum through LDS in wave order. A cluster covers tile_m (16 to 128) tokens, each weight fragment feeding tile_m / 16 WMMAs, which halves the weight re-reads at M 512 and lets M 1024 fuse. Only rows below m store partials. The logits are now summed in (split, wave, K step) order. The launch takes waves and tile_m (tuned table columns, wrapper and op-test arguments); the tuner sweeps (split_k, waves, prefetch) x tile_m and passes the renorm / scale its op-test helpers now take. The table rows keep their old split_k / prefetch with waves 1 and tile_m 16 until retuned. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The selection values become order-preserving int keys (they are never NaN or -0.0 after the NaN -> -inf map): the sorting network compares ints and each round's wave max is an integer butterfly, without the canonicalizing v_max that maxnumf adds per step. The winner lane is the ballot's lowest lane, which shifts its sorted list (an exhausted lane shifts in -inf / 0 / 0) instead of a cursor select chain, and reads the weight from that lane directly. Outputs are bitwise those of the cursor rounds (864 cases: ties, saturation, NaN logits, +-inf bias, padding, top-1/4/8/32, renorm on and off); 0.05-0.15 us less per call. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Retuned on MI455X (256 CUs) for MiniMax-M3.1's router (E 128, K 6144, top-4, bias, renorm, scale 1), with the prefill sizes 1024-8192 added. Per call in CUDA graphs, fused vs tgemm.mm + topk_gating: 3.04-3.70 us vs 4.34-6.63 us for M 1-256, 4.25-4.40 vs 6.60-8.03 for M 320-512, 5.95 vs 7.38 at M 1024 and 8.65 vs 9.01 at M 2048 (A/A 0.32); M 4096 and 8192 stay unfused (16.2 vs 12.7, 31.5 vs 15.0). Co-Authored-By: Claude Opus 5.5 <[email protected]>
The tuner imports it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ery model `router_topk_gating(topk_weights, topk_indices, x, router_weight, ...)` is `topk_gating` over `tgemm.mm(x, router_weight, otype=logits_dtype)`, with topk_gating's arguments and defaults. It runs the gfx1250 fused launch where that takes the call and the GEMM and `topk_gating` elsewhere, so callers no longer check support themselves. The wrapper checks once (`try_flydsl_moe_router_topk`) and shares the launch with `flydsl_moe_router_topk`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
9d8d224 to
edab03f
Compare
…op-k in one aiter launch `FusedMoE.forward(hidden_states, router_weight=...)` takes an unquantized [E, K] router weight instead of its logits. `select_experts` then runs the `topk_gating` routings (sigmoid / sqrtsoftplus, not grouped, no custom routing) through aiter's `router_topk_gating`: the router GEMM and the top-k in one launch where aiter fuses them (gfx1250), its GEMM and `topk_gating` elsewhere. Every other routing, SP MegaMoE and fake EPLB take the GEMM's logits first (`router_gemm`, as the gate's `ReplicatedLinear` gives them). MiniMax-M3 hands its gate weight over. Needs ROCm/aiter#8431. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s logits check_router_topk_gating compared aiter.router_topk_gating's fallback (tgemm.mm + topk_gating) with a second tgemm.mm + topk_gating, bit for bit. That also asserts that two launches of the GEMM agree bit for bit, which a tuned split-K GEMM does not promise: on gfx950, x[33, 6144] @ w[128, 6144]^T with bf16 logits pads M to 48 and takes that tuned row, a FlyDSL hgemm with ks8 whose partial sums land in the bf16 output by packed atomic adds (BufferAtomicPkAdd), in launch order. MI35X shard 3 failed there (E=128, bf16) on both runs; E=96 has no tuned row and fp32 logits take another one. The fallback is now checked on one launch's logits, handed to both sides through tgemm.mm, and the GEMM call itself is checked: its operands and its logits dtype. topk_gating reads its logits only (const input), so both sides see the same values. The fused gfx1250 path is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Cause. On gfx950,
Change. The fallback is now checked on one launch's logits, handed to both sides through a patched
Validation. black and ruff 0.16.0 are clean. Not run on a GPU here: this push's MI35X shard 3 is the check. Side note, outside this PR: on gfx950, any bf16 router or other GEMM of this shape at M 33-48 therefore takes that bf16-atomic split-K row. Its output is run-to-run nondeterministic, and its accumulation rounds through bf16. |
What
aiter.router_topk_gating(topk_weights, topk_indices, x, router_weight, correction_bias=None, need_renorm=True, routed_scaling_factor=1.0, score_func="sqrtsoftplus", num_shared_experts=0, num_valid_rows=None, *, logits_dtype=None)is an MoE layer's router GEMM and top-k gating:topk_gating(topk_weights, topk_indices, tgemm.mm(x, router_weight, otype=logits_dtype), ...), withtopk_gating's arguments and defaults. Any model can call it on any GPU: where a fused kernel takes the call it is one launch, elsewhere it is the GEMM andtopk_gating, so callers check nothing themselves. Other score functions, expert counts or GEMM options become kernel branches and keyword arguments; existing calls do not change.The fused kernel (gfx1250, FlyDSL,
aiter/ops/flydsl/kernels/moe_router_topk_gfx1250.py) implements sigmoid scores with an optional fp32 correction bias, 128 experts, top-1 to top-32, bf16 activations, router weight and logits, and outputs whose rows may be further apart than top-k (the routed top-k inside a buffer that also holds fused shared-expert columns). It runs whereaiter/configs/moe_router_topk_tuned.csvhas a fused row for the shape.Per tile of
tile_mtokens, one cluster ofsplit_kx expert-group workgroups; a workgroup's waves split K and sum through LDS, so each weight fragment is read once and feedstile_m / 16WMMAs. The valid rows' partials go to a workspace; after the cluster barrier one wave per row sums them, adds the bias, selects with order-preserving integer keys (topk_gating's order and ties), renormalizes and scales.Why
At decode the router GEMM and the top-k are two small launches. MI455X, 256 CUs, E 128, K 6144, top-4, CUDA graph, us per call:
tgemm.mm+topk_gatingM 4096 and 8192 keep the unfused pair (12.6 / 15.2 us against 16.2 / 31.5 fused). An empty kernel costs 1.29 us in the same graph.
The logits are summed in fp32 in the kernel's own order (K split, wave, K step) and rounded to bf16, so results are not bitwise those of
tgemm.mm: experts whose score + bias are within 2e-3 may rank in either order.PR Self-Review
Reviewed revision
edab03f
Behavior and intent
aiter.router_topk_gating(aiter/ops/topk.py); no existing call changes. It takes the fused launch whennum_shared_expertsis 0, the logits arex.dtype, the GPU is gfx1250 andflydsl_moe_router_topk_supportedholds; otherwisetgemm.mm+topk_gating.split_k/prefetch/waves/tile_mcome from the tuned table (keyed on gfx, cu_num, padded M, N, K, topk): rows M 1 to 2048 fused, 4096 and 8192 unfused, for N 128, K 6144, top-4.csrc/flydsl_moe_router_topk/moe_router_topk_tune.py: tries the launch candidates per M against the unfused pair in interleaved CUDA graphs and marks a row fused only when it is faster by more than the A/A band. AOT:aiter/aot/flydsl/moe_router_topk.py.Validation and unverified scope
python3 op_tests/test_flydsl_moe_front.pypassed every case (330 rows: M 1 / 33 / 256 x top-1 / 4 / 32 x every launch x random, tied, saturating, padded and strided-output rows; each id within 2e-3 of its rank's selection value, distinct ids, weights within rtol 1e-2, padding rows exact, the buffer's other columns untouched), andaiter.router_topk_gatingequals the fused launch bit for bit where it takes the call andtgemm.mm+topk_gatingbit for bit where it does not (96 experts, fp32 logits). The integer-key selection equals the previous selection bit for bit on 864 edge cases (ties, saturation, NaN, +-inf bias, padding, top-1 / 4 / 8 / 32, with and without renorm). Prefill sizes M 1024 / 2048 / 4096 at top-4 / 8: 56 / 56 rows.--check: 72 / 72 replays.test_config_shape_collision.py -k moe_router_topk,test_csv_validation.pyandtests/test_flydsl_aot_launch.pypassed.PR-specific question
Which architecture, dtype and shape boundaries select the changed kernel path, and what invariant prevents unsupported inputs from reaching it?
get_gfx_runtime() == "gfx1250"plus a fused row for this gfx / cu_num / padded M / N / K / topk. The table has gfx1250, 256-CU rows only.logits_dtypeNone orx.dtype);correction_biasNone or contiguous fp32 [E];num_valid_rowsint32; outputs fp32 / int32 with unit column stride and a shared row stride of at least top-k.num_shared_experts0, x with unit column stride and a row stride divisible by 8, router_weight contiguous [E, K],score_func"sigmoid".router_topk_gatinglaunches the kernel only throughtry_flydsl_moe_router_topk, which runsflydsl_moe_router_topk_supported's checks first; the directflydsl_moe_router_topkraisesValueErroron the same checks; the kernel builder also rejects an unknown score function, expert count, topk,tile_mor a K not divisible by 32 x split_k x waves.Changed op tests
op_tests/test_flydsl_moe_front.py(new)tgemm.mm+topk_gatingagainst an fp32 reference at every launch candidate, top-1 / 4 / 32, tied, saturating, padded and strided-output rows;aiter.router_topk_gating's dispatch, bit for bit; each tuned row supported exactly where it is fused, and softmax or 96 experts unsupported and raising. The tuner reuses its reference and check.op_tests/test_moe_topk_gating.pycovers the unfused top-k only.op_tests/tuning_tests/test_config_shape_collision.py(+4 lines)AITER_CONFIG_MOE_ROUTER_TOPKfamily so its rows are checked for duplicate shape keys.🤖 Generated with Claude Code