Skip to content

[HIP] [FlyDSL] [JIT] [Perf] gfx1250 MoE router GEMM + top-k in one launch: aiter.router_topk_gating - #8431

Open
XiaobingSuper wants to merge 11 commits into
ROCm:mainfrom
XiaobingSuper:xiaobingsuper/gfx1250-moe-router-topk-fused
Open

XiaobingSuper wants to merge 11 commits into
ROCm:mainfrom
XiaobingSuper:xiaobingsuper/gfx1250-moe-router-topk-fused

Conversation

@XiaobingSuper

@XiaobingSuper XiaobingSuper commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

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), ...), with topk_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 and topk_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 where aiter/configs/moe_router_topk_tuned.csv has a fused row for the shape.

Per tile of tile_m tokens, one cluster of split_k x expert-group workgroups; a workgroup's waves split K and sum through LDS, so each weight fragment is read once and feeds tile_m / 16 WMMAs. 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:

M 1 8 16 64 128 256 320 512 1024 2048
fused 3.10 3.27 3.51 3.57 3.72 3.71 4.29 4.41 5.94 8.62
tgemm.mm + topk_gating 4.35 4.69 4.71 5.12 5.41 6.21 6.99 6.51 7.16 8.81

M 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

  • New public op aiter.router_topk_gating (aiter/ops/topk.py); no existing call changes. It takes the fused launch when num_shared_experts is 0, the logits are x.dtype, the GPU is gfx1250 and flydsl_moe_router_topk_supported holds; otherwise tgemm.mm + topk_gating.
  • split_k / prefetch / waves / tile_m come 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.
  • Tuner 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

  • MI455X, one GPU, this head: python3 op_tests/test_flydsl_moe_front.py passed 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), and aiter.router_topk_gating equals the fused launch bit for bit where it takes the call and tgemm.mm + topk_gating bit 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.
  • AOT build and --check: 72 / 72 replays. test_config_shape_collision.py -k moe_router_topk, test_csv_validation.py and tests/test_flydsl_aot_launch.py passed.
  • Unverified: CI execution of the fused path (no gfx1250 runner; elsewhere the op test runs the dispatch check only); other CU counts; end-to-end model accuracy (MiniMax-M3's integration is perf(moe): route from the router weight; MiniMax-M3 router GEMM + top-k in one aiter launch ATOM#3682). M 2048's margin is small (8.65 vs 9.01 us with an A/A band of 0.32 us at tuning).

PR-specific question

Which architecture, dtype and shape boundaries select the changed kernel path, and what invariant prevents unsupported inputs from reaching it?

  • Architecture: 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.
  • Dtype: x and router_weight bf16, logits bf16 (logits_dtype None or x.dtype); correction_bias None or contiguous fp32 [E]; num_valid_rows int32; outputs fp32 / int32 with unit column stride and a shared row stride of at least top-k.
  • Shape: E 128, topk 1 to 32, num_shared_experts 0, x with unit column stride and a row stride divisible by 8, router_weight contiguous [E, K], score_func "sigmoid".
  • Invariant: router_topk_gating launches the kernel only through try_flydsl_moe_router_topk, which runs flydsl_moe_router_topk_supported's checks first; the direct flydsl_moe_router_topk raises ValueError on the same checks; the kernel builder also rejects an unknown score function, expert count, topk, tile_m or a K not divisible by 32 x split_k x waves.

Changed op tests

  • op_tests/test_flydsl_moe_front.py (new)
    • Covers: the fused launch and tgemm.mm + topk_gating against 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.
    • Not redundant: it is the only test of this op; op_tests/test_moe_topk_gating.py covers the unfused top-k only.
  • op_tests/tuning_tests/test_config_shape_collision.py (+4 lines)
    • Covers: registers the AITER_CONFIG_MOE_ROUTER_TOPK family so its rows are checked for duplicate shape keys.
    • Not redundant: the collision test only checks the families it lists.

🤖 Generated with Claude Code

@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter standard tests on MI35X
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:mi300x Run Aiter standard tests on MI300X in PRs; main branch always runs them
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the Radeon R9700 ROCm runner
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 8431 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

Reviews:
A reviewer is usually assigned within one working day after the PR is ready for review (see Code Review Process). If nobody has responded for about a week, mention the area contact listed in CONTACTS.md. Want to help review? See REVIEWERS.md.

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
XiaobingSuper force-pushed the xiaobingsuper/gfx1250-moe-router-topk-fused branch from 624f007 to d3afecb Compare November 23, 2026 08:31
XiaobingSuper added a commit to XiaobingSuper/aiter that referenced this pull request Nov 23, 2026
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/ATOM that referenced this pull request Nov 23, 2026
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]>
XiaobingSuper added a commit to XiaobingSuper/ATOM that referenced this pull request Nov 23, 2026
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]>
@XiaobingSuper

Copy link
Copy Markdown
Contributor Author

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.

python3 op_tests/test_flydsl_moe_front.py --num-tokens 1,16,33,64,128,192,256,320,512 passes: 36/36 rows (9 token counts x rand / ties / saturate / pad_rows) bit_exact and counts_exact against tgemm.mm + topk_gating.

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.

@XiaobingSuper
XiaobingSuper marked this pull request as ready for review November 24, 2026 06:04
@XiaobingSuper
XiaobingSuper requested review from a team and a balanced review from Copilot November 24, 2026 06:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@aiter-bot

aiter-bot commented Nov 24, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision bf5bd983e43c · mode shadow

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

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. Which architecture, dtype and shape boundaries select the changed kernel path, and what invariant prevents unsupported inputs from reaching it?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA

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

Copilot AI balanced review requested due to automatic review settings November 24, 2026 08:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions github-actions Bot changed the title [FlyDSL] [Perf] gfx1250 MoE router GEMM + biased sigmoid top-k in one launch [HIP] [FlyDSL] [JIT] [Perf] gfx1250 MoE router GEMM + biased sigmoid top-k in one launch Nov 24, 2026
Copilot AI balanced review requested due to automatic review settings November 25, 2026 07:01
XiaobingSuper added a commit to XiaobingSuper/aiter that referenced this pull request Nov 25, 2026
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]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 30, 2026 08:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions github-actions Bot changed the title [FlyDSL] [JIT] [Perf] gfx1250 MoE router GEMM + biased sigmoid top-k in one launch [HIP] [FlyDSL] [JIT] [Perf] gfx1250 MoE router GEMM + biased sigmoid top-k in one launch Nov 30, 2026
@XiaobingSuper

Copy link
Copy Markdown
Contributor Author

Rebased onto current main as three linear commits: the op with its kernel, tuned table and op test (8b56250c9); the tuner (878d3186a); and the AOT module (64aac3b1f). The merge of main is gone, and the diff against main is unchanged (11 files, +1069). On one MI455X at the new head:

  • test_flydsl_moe_front.py passes 180/180.
  • The AOT build and --check pass 64/64 replays.
  • The config collision and CSV validation tests pass.
  • black 26.10.1 and ruff 0.16.0 are clean.

@yadaish could you review this one? (CONTACTS.md lists you for FlyDSL MoE kernels.)

XiaobingSuper added a commit to XiaobingSuper/ATOM that referenced this pull request Nov 30, 2026
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]>
@XiaobingSuper
XiaobingSuper force-pushed the xiaobingsuper/gfx1250-moe-router-topk-fused branch from 64aac3b to 9d8d224 Compare December 1, 2026 12:44
Copilot AI balanced review requested due to automatic review settings December 1, 2026 12:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@XiaobingSuper

Copy link
Copy Markdown
Contributor Author

review-pr scan (aiter's .claude/skills/review-pr, at 64aac3b1f, auto-validation off, no GPU).

Fixed in 9d8d22475, with the branch rebased onto main (the AOT_MODULES conflict with main's unified_attention keeps both entries):

  • The launch in moe_front_gfx1250.py:227 now runs under with torch.cuda.device(x.device) on that device's current stream, with specialization_key=(x.device.index,).
  • test_flydsl_moe_front.py adds no_bias (no correction bias, factor 2.5) and no_renorm (need_renorm=False, factor 2.5) cases over every m / topk / split_k. The torch reference and the unfused topk_gating apply the same renorm and scale.
  • On one MI455X, before the rebase, all 270 cases pass, and fused outputs are byte-identical to the previous head. The rebase only adds main's own changes.
review-pr card

aiter PR #8431 — [HIP] [FlyDSL] [JIT] [Perf] gfx1250 MoE router GEMM + biased sigmoid top-k in one launch

Adds 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): ⚠️ NEEDS WORK
Validation (deterministic): NOT RUN — auto-validation disabled (REVIEW_AUTO_VALIDATE=0, no GPU used); the target op_tests/test_flydsl_moe_front.py exists and is gfx1250-only (it skips on other GPUs, and no CI runner is gfx1250)
Perf (advisory): NOT RUN — auto-validation disabled, no GPU used; the PR adds the target, so base has nothing to time; the PR's fused vs tgemm.mm + topk_gating table (MI455X, CUDA graph) matches the us / unfused_us columns of its tuned CSV but was not re-run here
Size: 1069 lines / 11 files — over the review bar
Design (advisory): SPLIT — along the PR's own commits: (1) the op, kernel, tuned/untuned CSVs, the jit/core.py config property and the op test; (2) the tuner; (3) the AOT module with its README/common.py registration
Human: recommend valarLip review whether aiter#8431 should ship the router fusion as a separate op flydsl_moe_router_topk gated by its own tuned table (moe_router_topk_tuned.csv via AITER_CONFIG_MOE_ROUTER_TOPK in aiter/jit/core.py) rather than as a backend chosen inside the existing tgemm.mm + topk_gating router path

⚠️ [inferred] moe_front_gfx1250.py:227 calls _run_compiled with current_stream_arg() (line 239, the current device's stream), with no with torch.cuda.device(x.device) guard and no device in the cached handle's key (the launcher from _router_launcher keeps one _cf) — in one process, a call with x on cuda:1 while cuda:0 is current, or the same shape on two GPUs, may launch on the wrong device or reuse a handle compiled for the other GPU. Author must wrap the launch in with torch.cuda.device(x.device): and pass specialization_key=(x.device.index,).
⚠️ [verified] test_flydsl_moe_front.py always passes a correction bias and never sets need_renorm or routed_scaling_factor (and check_routes always renormalises), so the has_bias=False and need_renorm=False builds (which flydsl_moe_router_topk_supported accepts for every tuned row and aiter/aot/flydsl/moe_router_topk.py pre-builds) and any scale != 1.0 build are never launched by any test, and a bug in them would ship unseen. Author must add rand cases with correction_bias=None, need_renorm=False and a non-1.0 routed_scaling_factor, with a reference that applies the same renorm/scale.
📝 [verified] The diff no longer applies to main b2bca5d: the aiter/aot/flydsl/common.py AOT_MODULES hunk conflicts with main's new unified_attention entry (the README.md hunk only fails a plain git apply and merges cleanly), so any CI result on this head describes an older tree. Author must rebase and keep both AOT entries.

XiaobingSuper and others added 10 commits December 2, 2026 08:11
… 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]>
@XiaobingSuper
XiaobingSuper force-pushed the xiaobingsuper/gfx1250-moe-router-topk-fused branch from 9d8d224 to edab03f Compare December 2, 2026 08:15
Copilot AI balanced review requested due to automatic review settings December 2, 2026 08:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@XiaobingSuper XiaobingSuper changed the title [HIP] [FlyDSL] [JIT] [Perf] gfx1250 MoE router GEMM + biased sigmoid top-k in one launch [FlyDSL] [Perf] gfx1250 MoE router GEMM + top-k in one launch: aiter.router_topk_gating Dec 2, 2026
XiaobingSuper added a commit to XiaobingSuper/ATOM that referenced this pull request Dec 2, 2026
…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]>
Copilot AI balanced review requested due to automatic review settings December 4, 2026 03:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@XiaobingSuper

Copy link
Copy Markdown
Contributor Author

bf5bd983e4 fixes the MI35X shard 3 failure in check_router_topk_gating. It was (128, None), the same on both runs. The change is test only.

Cause. On gfx950, aiter.router_topk_gating is tgemm.mm + topk_gating. The check compared it bit for bit with a second tgemm.mm + topk_gating, which also asserts that two launches of the GEMM agree bit for bit. This shape does not promise that:

  • x[33, 6144] @ w[128, 6144]^T with bf16 logits pads M to 48 (getPaddedM, gl=0).
  • That takes the gfx950 row M=48 N=128 K=6144 in model_configs/glm5_bf16_tuned_gemm.csv: flydsl_hgemm_abf16_wbf16_bf16_t16x32x128x6_ks8_..._pft_gfx950.
  • With split_k > 1 and a bf16 output, that kernel adds its 8 partial sums into the output with packed bf16 atomics (BufferAtomicPkAdd, kernels/gemm_a16w16_gfx950.py write_cshuffle_vec_to_global), in launch order.
  • So the low bits of the logits, and with them the sigmoid weights, can differ between launches.
  • The other two cases take other GEMMs: E=96 has no tuned row, and fp32 logits pad to the M=64 fp32 row, an asm kernel.

Change. The fallback is now checked on one launch's logits, handed to both sides through a patched tgemm.mm.

  • The GEMM call itself is still checked: its operands are x and w, and its otype is the logits dtype.
  • topk_gating only reads its logits (a const input), so both sides see the same values.
  • The fused gfx1250 path is unchanged.

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.

@github-actions github-actions Bot changed the title [FlyDSL] [Perf] gfx1250 MoE router GEMM + top-k in one launch: aiter.router_topk_gating [HIP] [FlyDSL] [JIT] [Perf] gfx1250 MoE router GEMM + top-k in one launch: aiter.router_topk_gating Dec 4, 2026

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants