Repository navigation
[Triton/Gluon] [FlyDSL] [MoE] Kimi-K3 HERD on gfx1250: K+1 sigmoid top-k and one route+psum kernel - #7494
Conversation
…e+psum kernel The gfx1250 exact sigmoid top-k produces the Top-(K+1) candidates fused_moe(herd_topk=K) takes for Kimi-K3: it admits the K+1 widths 5, 9 and 17, padding its slots to a power of two and masking the stores, and Kimi-K3's FP32 correction bias, which the wave32 HIP kernel rejects with BF16 logits. fused_moe(herd_topk) on gfx1250 dropped HERD candidates in the grouped MoE route with moe_route_psum_herd only up to 512 experts; Kimi-K3's 896 were finalized up front and then took the counter fill, route and psum remap launches. moe_herd_route_psum replaces moe_route_psum_herd at every expert count. One workgroup does the HERD finalize, route, tile-aligned psum and row remap for up to HERD_ROUTE_BLOCK (1024) experts and rows, with the routes and weights of herd_finalize_candidates bit for bit. It picks each row's drop once, where moe_route_psum_herd picked it again for each of the row's K routes: it copies the candidates into LDS with coalesced loads and stages its outputs there, orders the kept routes with a sorting network over id * 32 + candidate keys, and splits the popularity and route counters into four copies by wave so a hot expert does not serialize their atomics. fused_moe(herd_topk) no longer finalizes the candidates ahead of the MoE at any expert count. It takes at most 1024 tokens, and AITER_FLYDSL_HERD_MAX_M, which bounds the steps ATOM routes with HERD on gfx1250, now defaults to it, so larger batches route without HERD. Kernel alone on gfx1250, uniform routing, moe_route_psum_herd -> moe_herd_route_psum, us: E, K M=16 M=128 M=512 M=1024 128, 4 3.5 -> 3.0 3.8 -> 3.1 6.7 -> 3.7 10.6 -> 4.9 256, 6 3.7 -> 3.3 5.0 -> 3.5 10.1 -> 4.4 17.5 -> 6.0 512, 8 4.1 -> 4.0 6.2 -> 4.2 15.9 -> 5.3 28.5 -> 7.1 896, 16 - -> 4.5 - -> 5.0 - -> 7.0 - -> 10.1 op_tests/test_flydsl_herd_route_psum.py checks fused_moe(herd_topk) against the K-route MoE over the kept routes, with moe_herd_route_psum the only route kernel launched. op_tests/test_flydsl_herd_k3_onoff.py benchmarks HERD off/on for Kimi-K3 per stage and checks the on path's kernels and outputs.
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
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 |
Resolve grouped_moe_gfx1250.py: keep the removal of herd_route_psum_remap, which herd_route_psum replaces at every expert count, and carry #7271's dispatch onto it: herd_route_psum launches through _run_compiled, so warmed calls take the cached CompiledFunction, and passes current_stream_arg().
Remove op_tests/test_flydsl_herd_k3_onoff.py and op_tests/test_flydsl_herd_route_psum.py, which do not follow .claude/skills/aiter-op-test, and the tests this branch added to test_topk_gating.py and test_flydsl_herd.py. The tests of the removed herd_route_psum_remap / moe_route_psum_herd go as well: test_grouped_route_psum_reuses_kernel_across_batches and test_grouped_route_psum_skips_pad_rows in test_flydsl_herd.py, and test_route_psum_herd with its run_torch_herd_drop reference in test_flydsl_moe_aux.py. test_herd_topk_is_only_for_gfx1250 now expects the grouped MoE to drop the candidates at any expert count.
There was a problem hiding this comment.
🟡 Changes recommended
Temporary normalized tensors can lose their last owner before the kernel launch, allowing storage reuse, while key routing, padding, and FP32-bias cases lack regression coverage.
5 open issues
What changed in this PR
This PR enables Kimi-K3 HERD routing on gfx1250 and replaces the grouped MoE HERD route with one FlyDSL kernel that finalizes candidates, routes tokens and computes prefix sums.
Changes:
- Extends exact sigmoid top-k to K=5, 9 and 17 and FP32 correction bias.
- Adds a fused HERD route for up to 1024 experts and rows, with cached launch dispatch.
- Removes the previous HERD route implementation and up-front finalization for wide MoEs.
| File | Description |
|---|---|
op_tests/test_flydsl_moe_aux.py |
Removes the old HERD route benchmark and reference helper. |
op_tests/test_flydsl_herd.py |
Updates grouped-route expectations and removes old route-specific tests. |
aiter/ops/triton/moe/moe_routing/topk_gating.py |
Passes the padded top-k width to the kernel. |
aiter/ops/triton/_triton_kernels/moe/moe_routing/topk_gating.py |
Pads selection slots and masks output stores. |
aiter/ops/topk.py |
Expands sigmoid dispatch eligibility and the default HERD token window. |
aiter/ops/flydsl/kernels/moe_herd_route_psum.py |
Adds the single-workgroup HERD finalize, route and prefix-sum kernel. |
aiter/ops/flydsl/kernels/moe_contiguous_psum.py |
Removes the previous fused HERD route kernel. |
aiter/ops/flydsl/herd_topk.py |
Adds the herd_route_psum allocation and launch wrapper. |
aiter/ops/flydsl/grouped_moe_gfx1250.py |
Uses the new wrapper and removes the old launch helpers. |
aiter/fused_moe.py |
Enforces the 1024-token limit and removes wide-MoE up-front finalization. |
Review effort: Balanced
…he launch herd_route_psum gave ptr_arg the int32 / FP32 copies of the candidates as temporaries. ptr_arg keeps only the address, so a copy was freed before the launch, and the next copy could take its memory: with int64 ids and BF16 values the kernel read the values as ids. The copies now stay in local variables until the launch is enqueued.
…oute kernels The finalize kernel, _kept_routes (moe_sorting_herd) and moe_herd_route_psum each had a copy of the drop selection and the kept-weight scale. _herd_drop in kernels/herd_topk.py now holds the tie rules and the scale, and the three kernels call it, so their routes and weights stay equal bit for bit. The sorting network of moe_herd_route_psum stays local.
test_flydsl_herd.py checks herd_route_psum against the HERD finalize (bit for bit) and the torch drop: kept ids, weights, per-expert counts, the tile-aligned psum and that the rows fill each expert's band once. It covers 128 to 1024 experts, 1 to 1024 rows, tied values and a hot expert set, one compiled kernel across batches, num_valid_rows changes under CUDA graph replay, and int64 ids with BF16 values. test_herd_topk_is_only_for_gfx1250 checks that fused_moe(herd_topk) rejects 1025 tokens on gfx1250. test_flydsl_moe_aux.py benchmarks herd_route_psum on gfx1250, eager and graph replay, with a Kimi-K3 decode table (E=896, K=16, M=16..1024).
test_exact_sigmoid_topk_herd_widths runs K = 5, 9, 16 and 17 with BF16 and FP32 correction bias, with and without renormalization, against the torch reference. Rows past num_valid_rows must get ids -1 and weights 0, and sentinels after each row's K slots must stay unchanged, so the padded slots are not stored. BF16 bias also checks against the HIP kernel; FP32 bias does not, because the wave32 HIP kernel aborts on a mixed bias dtype.
- Keep this PR's all-input output overlap check; drop main's hidden_states-only check and the unused bytes_overlap import - Drop the HERD finalize block that main (ROCm#7494) moved out of _fused_moe_impl - Hand the new caller-presort path kernel_output (16-byte rule) instead of its own 4-byte alignment check - Keep both output-table rows: adaptive-aux sort now in place, caller presort zeroed unless the caller did - test_presorted_moe_hook: 2- and 4-byte offsets now copy back; a 16-byte offset is used in place
- Restores the import of DROPPED_ROUTE_ROW from moe_route_maps, which the route remap uses for routes with no grouped row. #7494 dropped the import on a branch where only comments still named the constant; #7365, merged in between, had added a use in code, so the merged file raised NameError and test_flydsl_moe_aux.py failed on main.
- Restores the import of DROPPED_ROUTE_ROW from moe_route_maps, which the route remap uses for routes with no grouped row. #7494 dropped the import on a branch where only comments still named the constant; #7365, merged in between, had added a use in code, so the merged file raised NameError and test_flydsl_moe_aux.py failed on main.
…imi-K3 main (#2893) dropped FusedMoE.select_experts' mask_pad_rows, so the HERD branch raised TypeError on its first decode step. Without it the Top-(K+1) candidates route pad rows to -1 like _select_logical, and apply() passes the same pass row count to fused_moe as num_local_tokens (#2905), which aiter's herd_route_psum takes as num_valid_rows: the pad rows of a partly filled graph bucket no longer vote in HERD's popularity. The experts must run their pass's own rows. A DP-attention or PCP gather, and simulated DP's repeat, interleave every rank's -1 pad rows, pass no count and can exceed the 1024 rows fused_moe(herd_topk) takes, so use_flydsl_herd stays off there. HERD now serves DeepSeek-V4's sqrtsoftplus top-k and Kimi-K3's sigmoid grouped top-k over one group only; both fold the routed scale in, which the drop applies. aiter's HERD constants import under a guard, and use_flydsl_herd needs aiter v0.1.29 (ROCm/aiter#7494, probed by herd_route_psum): older aiter aborts on Kimi-K3's Top-17 candidates with an FP32 bias. test_flydsl_herd_routing binds the HERD candidate call to the real FusedMoE.select_experts signature, and covers the routing set, the gather guard, the aiter probe and the num_local_tokens hand-off. test_flydsl_herd_graph_padding_gpu replays a bs=32 graph at 17, 1, 31 and 32 real rows against eager HERD over the real rows alone (gfx1250 only).
…2713) * feat(moe): route FlyDSL MXFP4 decode through aiter HERD on gfx1250 With AITER_FLYDSL_USE_HERD=1, Mxfp4MoEMethod.apply() on gfx1250 picks the Top-(K+1) sqrtsoftplus candidates with topk_gating and passes them to fused_moe(herd_topk=K), which drops each token's least batch-popular candidate, renormalizes and applies the routed scale (fused into the route/prefix-sum kernel by default, AITER_HERD_FUSED_DROP=1). Only decode steps whose batch is inside aiter's HERD window (16..128 tokens) take this path, and only for TP (no EP or EPLB), GU-interleaved weights, sqrtsoftplus routing with renormalization, no grouped top-k, no custom or hash routing, no fused shared experts and no expert mask. Everything else keeps select_experts. * style(moe): sort the HERD import and use functools.cache Fixes ruff I001 (aiter.ops.topk sorts after aiter.ops.shuffle) and UP033 (@cache instead of @lru_cache(maxsize=None)) on the FlyDSL HERD change. * [MoE] Adapt FlyDSL HERD to aiter valid-row routing * [MoE] Route FlyDSL HERD candidates through the layer's own top-k The HERD branch of Mxfp4MoEMethod.select_experts forced a non-grouped top-k, and _flydsl_herd_step admitted sqrtsoftplus only, so DeepSeek-V4 was the only model HERD could serve. Candidates now take the layer's own routing at top_k + 1: topk_gating for sqrtsoftplus, fused_topk for softmax (GPT-OSS) and biased_grouped_topk for a one-group grouped top-k (Kimi-K3). Grouped top-k with more than one group keeps plain routes. The drop applies the routed scale the layer's own routing folds in. FusedMoE.routing_scale holds that rule and select_experts shares it: grouped top-k and sqrtsoftplus fold the scale in, sigmoid only with a fused shared column, softmax never. Also sets use_flydsl_herd on the Mxfp4MoEMethod objects that test_shared_expert_dispatch and test_mxfp4_triton_moe_decode build by hand; the HERD hooks broke three of those tests. * [MoE] Trim the FlyDSL HERD routing hooks Cuts the HERD additions to atom/model_ops/moe.py from 134 to 71 lines without changing behaviour: - Drop the one-shot HERD batch log and FusedMoE.routing_scale; apply() takes the routed scale select_experts folds in (grouped top-k and sqrtsoftplus) inline, and select_experts is back to main. - The HERD branch of select_experts leaves default arguments out. - _flydsl_herd_step folds its checks into one condition and drops the expert_mask test, which use_flydsl_herd's not-EP check already covers. - Shorter comments. * [MoE] Route FlyDSL HERD pad rows like the plain routing Since #2776, pure TP routes pad rows like real rows (mask_pad_rows= layer.use_ep) and passes fused_moe no scheduled_tokens_gpu. The HERD branch of select_experts kept the default mask, so its Top-(K+1) candidates still carried -1 pad rows while fused_moe got no num_local_tokens. aiter's HERD drop leaves -1 rows out only with that count, so pure-TP HERD decode broke. The HERD branch now takes the same mask_pad_rows as _select_logical: pure TP routes its pad rows as real candidates, and a later change to that policy applies to HERD as well. * [MoE] Keep graph pad rows out of FlyDSL HERD; serve DeepSeek-V4 and Kimi-K3 main (#2893) dropped FusedMoE.select_experts' mask_pad_rows, so the HERD branch raised TypeError on its first decode step. Without it the Top-(K+1) candidates route pad rows to -1 like _select_logical, and apply() passes the same pass row count to fused_moe as num_local_tokens (#2905), which aiter's herd_route_psum takes as num_valid_rows: the pad rows of a partly filled graph bucket no longer vote in HERD's popularity. The experts must run their pass's own rows. A DP-attention or PCP gather, and simulated DP's repeat, interleave every rank's -1 pad rows, pass no count and can exceed the 1024 rows fused_moe(herd_topk) takes, so use_flydsl_herd stays off there. HERD now serves DeepSeek-V4's sqrtsoftplus top-k and Kimi-K3's sigmoid grouped top-k over one group only; both fold the routed scale in, which the drop applies. aiter's HERD constants import under a guard, and use_flydsl_herd needs aiter v0.1.29 (ROCm/aiter#7494, probed by herd_route_psum): older aiter aborts on Kimi-K3's Top-17 candidates with an FP32 bias. test_flydsl_herd_routing binds the HERD candidate call to the real FusedMoE.select_experts signature, and covers the routing set, the gather guard, the aiter probe and the num_local_tokens hand-off. test_flydsl_herd_graph_padding_gpu replays a bs=32 graph at 17, 1, 31 and 32 real rows against eager HERD over the real rows alone (gfx1250 only). * [MoE] Decide FlyDSL HERD in apply() as select_experts did, not by width apply() told HERD's raw Top-(K+1) candidates from final routes by the ids' width, which also had to rule out a fused shared column. It now asks the same _flydsl_herd_step select_experts asked, over the same rows: HERD never runs on gathered rows, so their count is the one routing saw. Nothing per step is kept on the method. No try around aiter: the HERD constants import directly, and the v0.1.29 check looks herd_route_psum up on aiter.ops.flydsl.herd_topk, which aiter has had since those constants (v0.1.26). test_dp_gather_routed_rows stubs aiter.ops.topk with the other aiter modules moe.py imports.



Summary
Runs Kimi-K3 (E=896, K=16) HERD through
fused_moe(herd_topk=K)on gfx1250, the way ATOM routes it (ROCm/ATOM#2713), and replaces the HERD route kernel at every expert count with one that finalizes, routes and prefix-sums in a single workgroup launch.Motivation
e_score_correction_biaswith BF16 logits. Both the K3 decode routing (Top-16) and the HERD candidate routing (Top-17) take the HIPbiased_grouped_topkon wave32, which aborts withMixed bias dtype requires the register top-k path. The exact sigmoid top-k also admitted only K in {4, 8, 16}, not the K+1 widths HERD needs.fused_moe(herd_topk)dropped HERD candidates inside the grouped MoE route (moe_route_psum_herd) only up to 512 experts. K3's 896 experts were finalized up front (herd_finalize) and then took the counter fill,moe_routeandpsum_remaplaunches.Changes
aiter/ops/topk.pyAITER_FLYDSL_HERD_MAX_Mdefaults to 1024, the most tokensfused_moe(herd_topk)takes.aiter/ops/triton/.../topk_gating.pyaiter/ops/flydsl/kernels/moe_herd_route_psum.py(new)herd_finalize_candidatesbit for bit.aiter/ops/flydsl/herd_topk.pyherd_route_psum()wrapper.aiter/ops/flydsl/grouped_moe_gfx1250.pyherd_route_psumat every expert count.herd_route_psum_remapand_get_compiled_route_psum_herd.aiter/ops/flydsl/kernels/moe_contiguous_psum.pybuild_moe_route_psum_herd_module.aiter/fused_moe.pyherd_topkrejects more than 1024 tokens.Behavior changes
All of these are on the opt-in HERD path (
herd_topk/AITER_FLYDSL_USE_HERD=1), except the first.gfx1250
biased_grouped_topkwith K in {5, 9, 17} or an FP32 bias now takes the Triton exact sigmoid top-k. Previously it took HIP, which aborted for FP32 bias with BF16 logits.HERD with E ≤ 512 (GPT-OSS, MiniMax-M3, DeepSeek-V4-Flash) now uses
moe_herd_route_psum.fused_moe(herd_topk=6)(E=256, K=6, a8w4) gives bitwise-identical outputs to main at M = 16..1024.fused_moe(herd_topk)on gfx1250 raises in two cases:The second case already raised for E ≤ 512. For wider MoEs it previously finalized up front and fell back to the generic path.
AITER_FLYDSL_HERD_MAX_Mnow defaults to 1024, so ATOM's HERD decode window widens from 16..128 to 16..1024 for every model. SetAITER_FLYDSL_HERD_MAX_M=128to keep the previous window.gfx950: no behavior change.
_HERD_MAX_TOKENS).fused_moe(herd_topk)is still rejected off gfx1250.Performance (gfx1250)
Route kernel alone, uniform routing,
moe_route_psum_herd→moe_herd_route_psum, us:Kimi-K3 TP8 MoE layer, HERD off → on. This is the sum of top-k, route, GEMM and gather/quant kernel time, median of 3 × 200 iterations, us (
op_tests/test_flydsl_herd_k3_onoff.py):The route stage goes from three kernels to one and is faster up to M=512 (for example 9.9 → 5.8 us at M=16).
Known limitation: at M=1024 the single-workgroup route is 1.5–2.4 us slower than HERD-off's multi-block route. The total MoE layer is still faster at M=1024 because of the GEMM saving.
Compatibility with ROCm/ATOM#2713
No ATOM change is needed:
biased_grouped_topkand callsfused_moe(herd_topk=16).topk_gatingand callsfused_moe(herd_topk=6).Checked with ATOM's call sequence: FP32 bias,
renormalize=False, scale 1.0, nonum_valid_rows/num_local_tokens, Situv2, a4w4 and a8w4, M = 16..1024, including CUDA graph replay. HERD-on outputs equal the K-route MoE over the HERD-kept routes.Test
gfx1250, torch 2.11.0+rocm10.1.0a20260811, FlyDSL 0.4.1:
op_tests/test_flydsl_herd.py: 78 passed, 1 skipped (the gfx950 router-drop test)op_tests/test_flydsl_herd_route_psum.py: 42 passedop_tests/test_flydsl_herd_k3_onoff.py: 7 passedop_tests/triton_tests/moe/moe_routing/test_topk_gating.py: 14 passedop_tests/test_flydsl_moe_aux.pyHERD route benchmark (gfx1250): checks passpython op_tests/test_flydsl_herd_k3_onoff.py --kernels