Repository navigation
[HIP] [Perf] MoE routing top-k: readlane/DPP instead of ds_bpermute - #7338
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
ce45343 to
c582e7f
Compare
samremes
left a comment
There was a problem hiding this comment.
Benchmark vs Existing Kernels & Review Summary
We verified bit-identity and op_tests (test_moeTopkSoftmax.py, test_moe_topk_gating.py) on MI325X (gfx942, 304 CUs) and MI355X (gfx950, 256 CUs), and benchmarked this PR against both main (c1096fa40a) and the other MoE routing kernels already available in AITER / vLLM (CUDA Graph replay: 64 calls/graph, 8 replays, 25 samples across 5 alternating rounds, median per-call µs).
1. Comparison with Existing Kernels on MI325X (gfx942)
A. Softmax Routing — Qwen3-MoE (E=128, k=8, bf16, renorm=True) & Neighboring Shapes
| Shape | M | Base topk_gating |
PR #7338 topk_gating |
PR Speedup | grouped_topk (is_softmax=True) |
topk_softmax (HIP) |
topk_softmax_asm (existing .co) |
|---|---|---|---|---|---|---|---|
E=128 k=8 bf16 |
1 | 3.210 | 2.755 | 1.17× | 2.989 | 5.949 | 2.118 (1.30× vs PR) |
E=128 k=8 bf16 |
4 | 3.412 | 2.881 | 1.18× | 3.030 | 5.967 | 2.071 (1.39× vs PR) |
E=128 k=8 bf16 |
16 | 3.441 | 2.897 | 1.19× | 3.214 | 5.994 | 2.171 (1.33× vs PR) |
E=128 k=8 bf16 |
64 | 3.470 | 2.966 | 1.17× | 3.302 | 7.384 | 2.263 (1.31× vs PR) |
E=128 k=8 bf16 |
128 | 3.503 | 2.975 | 1.18× | 3.304 | 7.339 | 2.281 (1.30× vs PR) |
E=128 k=8 bf16 |
1024 | 6.277 | 6.161 | 1.02× | 3.973 | 7.641 | 2.390 (2.58× vs PR) |
E=128 k=8 bf16 |
4096 | 6.448 | 6.303 | 1.02× | 6.485 | 7.712 | 3.236 (1.95× vs PR) |
E=256 k=8 bf16 |
1 / 64 / 1024 | 3.705 / 3.924 / 6.493 | 3.154 / 3.344 / 6.302 | 1.17× / 1.17× / 1.03× | 3.299 / 3.718 / 3.977 | 6.223 / 7.793 / 7.946 | 2.338 / 2.543 / 2.655 |
E=512 k=10 bf16 |
1 / 64 / 1024 | 5.308 / 5.501 / 6.514 | 4.502 / 4.677 / 5.879 | 1.18× / 1.18× / 1.11× | 4.316 / 4.561 / 5.231 | 9.679 / 10.012 / 10.208 | N/A |
E=128 k=8 shared=1 |
1 / 64 / 1024 | 3.568 / 3.832 / 6.661 | 3.118 / 3.338 / 6.509 | 1.14× / 1.15× / 1.02× | N/A | 6.753 / 8.481 / 8.849 | N/A |
- AITER already ships precompiled ASM kernels (
topk_softmax_asminhsa/{gfx942,gfx950}/topksoftmax/topksoftmax.csv, covering(E, k) ∈ {(128, 4/6/8), (256, 6/8), (384, 8), (640, 8)}forbf16andfp32), which are 1.30–1.39× faster than PR #7338 atM=1..128(2.05–2.28 µs) and 2.58× faster atM=1024(and1.97–2.26 µsvs2.58–2.76 µsongfx950), buttopk_gating/topk_softmaxdo not currently route totopk_softmax_asm.
B. Ungrouped Biased Sigmoid — GLM-5.2 (E=256, k=8, fp32, strided [M, 9][:, :8], renorm=True, scale=2.5)
| M | Base biased_grouped_topk_hip |
PR #7338 biased_grouped_topk_hip |
PR Speedup | topk_gating(sigmoid) |
vLLM Triton _sigmoid_bias_flat |
|---|---|---|---|---|---|
| 1 | 2.916 | 2.731 | 1.07× | 3.917 | 2.463 (contig: 2.395) |
| 4 | 3.017 | 2.847 | 1.06× | 3.954 | 2.472 |
| 16 | 3.071 | 2.943 | 1.04× | 4.008 | 2.520 |
| 64 | 3.133 | 2.988 | 1.05× | 4.062 | 2.556 |
| 128 | 3.169 | 3.034 | 1.04× | 4.099 | 2.602 |
| 1024 | 3.450 | 3.315 | 1.04× | 4.337 | — |
| 4096 | 6.654 | 6.028 | 1.10× | 8.297 | — |
- Within AITER's C++/HIP ops,
biased_grouped_topk_hip(topk_reg_kernel) is the fastest path (1.3–2.2×ahead oftopk_gating(..., score_func="sigmoid")acrossE=128..512), and this PR consistently improves it on bothgfx942andgfx950. - For vLLM decode (
M <= 128,E <= 512,k <= 8), vLLM's 0-LDS register Triton kernel (_sigmoid_bias_flat_topk_kernel, currently gated to_MAX_TOKENS = 1) runs at 2.42–2.60 µs ongfx942(2.14–2.47 µsongfx950), 1.12–1.17× faster than PR #7338. - One neighboring shape to check on
gfx942(ROCm 10.1):E=128, k=32, fp32improved atM=1..1024(4.19 → 3.95 µsatM=1) but measured slower atM=4096(7.84 → 9.43 µs).
2. Recommendation
- Follow-up dispatch opportunities (outside this PR): For maximum Qwen3-MoE and GLM-5.2 decode performance, separate follow-ups should (a) route eligible unbiased contiguous
softmaxcalls (num_shared_experts == 0,num_valid_rows == None,routed_scaling_factor == 1.0) totopk_softmax_asm, and (b) raise_MAX_TOKENSon vLLM's_sigmoid_bias_flat_topk_kernelforM <= 128. - This PR itself is clean, bit-identical, and worth merging for the general
topk_reg_kernelandtopk_gating_kernel_prefillpaths — which remain active whenevernum_valid_rows(CUDA-graph / DP padding),num_shared_experts > 0(3.57 → 3.12 µs),correction_biason softmax,M > 128/topk > 8, or bit-identical legacy tie ordering are needed. - Small inline improvements on the current diff: All cross-lane encodings (
row_ror:8for XOR 8,ds_swizzle0x401ffor XOR 16,warp_rank_desc_uptokey == 0dead-lane invariant, and wave32subwarpArgmaxLane) checked out cleanly. Left a few focused inline comments where small tweaks to the touched lines drop remainingds_bpermute/ds_swizzleops, extra DPP moves, or compiler-generated branch/EXECblocks while preserving 0-ULP bit-identity. - Nit (
aiter-bot): Please bumpReviewed revisionin the PR description fromce453431700cto71283d53aa8c(or the next head SHA).
…entical Covers the register top-k behind biased_grouped_topk and the topk_gating softmax decode path. Signed-off-by: Pham Binh <[email protected]>
The existing thirty_two_way_tie case only reached the non-unrolled warp_rank_desc fallback with the unrolled (topk<=8) renorm. Two combinations this PR's rank/renorm unrolling introduces were still untested: topk>8 together with the n_cand<=16 unrolled rank tier, and EXPERTS_PER_LANE==8 (E=512), the edge of the BALLOT_SLOTS<=8 compile gate.
c8ca617 to
22f02c9
Compare
…ble tests - lane_xor<4>: start from mov_dpp with bound_ctrl, dropping a v_mov and s_nop. - Full-wave softmax max: fused DPP max plus readlane, halving the LDS ops. - lane_serial_sum: drop the per-lane guard; unused lanes already hold +0.0f. - Ballot slots: compute mbcnt outside the ternary, avoiding EXEC save/restore. - Stable tests: topk=4, E=192 and E=200; pad the legacy reference to the LDS path. Outputs are bit-identical to the previous commit.
|
@samremes all five inline suggestions are in 0d5bd9b; Per call, CUDA graph, median of 5 alternating rounds, MI325X, ROCm 10.0 image:
I can't reproduce the E128 k32 M=4096 slowdown; which image did you use? At M=16384, E128 k8 bf16, E384 k8 bf16 and grouped softmax E256 are 3–4% slower than before the fixes, still 1.03–1.06× over main; I haven't looked into it yet. Agreed that |
samremes
left a comment
There was a problem hiding this comment.
Approving 0d5bd9b4a. All five suggestions are in and correct (lane_xor<4> mov_dpp + bound_ctrl, local_max readlane limited to the full-wave row, lane_serial_sum<8>, the mbcnt hoist, and the new stable_cases). Thanks for the quick turnaround.
Validation: head 0d5bd9b4a vs main f70ac7f90, both JIT-built from scratch on each arch.
MI325X (gfx942) |
MI355X (gfx950) |
|
|---|---|---|
Bit-identical outputs (biased_grouped_topk_hip, grouped_topk, topk_gating; E=64–2048, k=4–32, M=1–8192; random, 16/32-way ties, all-equal, NaN, ±inf, masked) |
26,148 / 26,148 configs | 26,148 / 26,148 configs |
test_moeTopkSoftmax.py, test_moe_topk_gating.py |
pass; 32/32 reg_stable rows err 0, incl. the new topk=4, E=192, E=200 rows |
same |
| PR vs main, 23 shapes (CUDA graph, 6 alternating rounds) | 1.004–1.216× faster | 1.02–1.21× faster |
biased_grouped_topk_hip E128 k32 fp32, M=4096 |
10.177 → 9.543 µs | 6.436 → 6.205 µs |
Control: topk_gating sigmoid E128 k32 fp32, M=4096 (code path unchanged) |
16.436 vs 16.443 µs | 9.956 vs 9.958 µs |
Image / E128 k32 M=4096: round 1 used rocm/pytorch:rocm10.1.0_ubuntu24.04_py3.12_pytorch_release_2.14.0, and so did this round. I can't reproduce the slowdown either: your 10.217 → 9.598 µs matches our 10.177 → 9.543 µs. Looking back at the round-1 data, the PR arm was fine; it is within 1.2% of today's numbers. The base arm (main c1096fa40a) was 23% faster than today's main at this shape, and 28% faster on the unchanged topk_gating sigmoid control. So the "regression" came from an anomalous baseline, not this PR. Sorry for the noise.
M=16384: we didn't measure against the pre-fix commit. Against main, the PR is 1.03–1.07× faster on gfx942 and 1.13–1.18× faster on gfx950 for E128 k8 bf16, E384 k8 bf16 and grouped softmax E256, so this is not blocking.
Non-blocking:
- The padded legacy reference in
test_moeTopkSoftmax.pyhard-codes(2, 3, 4, 6, 8, 14, 16, 32), which mirrorsAITER_TOPK_REG_LAUNCH_EPLS. If the C++ list grows, the reference silently lands on the register path and the stable cases compare the kernel with itself. A comment pointing at the macro would be enough. - Agreed on the unseeded multi-group
test_grouped_topkcases: we saw the same intermittenttopk_ids ... failed!lines on MI325X on main too. I'm opening a separate PR to seed that test. - CI: MI35X
Standard Testsshards 0, 4 and 5 failed with "The self-hosted runner lost communication with the server", not a test failure. GitHub won't re-run jobs while shard 6 is still running, so I'll re-run the failed jobs as soon as the run finishes.
…pk-mw Conflicts with #7338 (readlane/DPP top-k) in two files: - csrc/kernels/topk_softmax_kernels_group.cu: both sides' helpers stay (warp_rank_desc_slots; warp_rank_desc_upto, lane_serial_sum). emit_legacy_ordered_topk is main's template <bool UNROLLED> with this branch's slot split: the one-slot arm (wave64) takes main's unrolled rank tiers, the two-slot arm (wave32) is unchanged, and the renorm tail is main's. main's ballot compaction and readlane pivot merge without conflict and now also run at wave32 for EPL 4, 6 and 8. - op_tests/test_moeTopkSoftmax.py: thirty_two_way_tie keeps pattern id 7, forty_way_tie takes 8 (the id only seeds the rows). main's padding loop gives 260 for the two ref260 cases, so ref_experts goes and the cases move into stable_cases. Device code of module_moe_asm: gfx942 and gfx950 match main for every kernel main has; on gfx1250 only topk_reg_kernel EPL 4, 6 and 8 change against aa3a0de. Not run on a GPU. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Summary
Replaces
ds_bpermuteround trips withreadlane, DPP,ds_swizzleandballot/mbcntin the two decode-size MoE routing kernels on wave64: the register top-k behindbiased_grouped_topk/ single-groupgrouped_topk(GLM-5.2) andtopk_gatingsoftmax (Qwen3-MoE). Outputs are bit-identical to main. At M = 1–128 on MI325X, GLM-5.2 routing is 1.11–1.19× faster and Qwen3 routing 1.22–1.24× faster.Motivation
At decode sizes each row is one wave, so the cost is the launch plus the wave's dependent cross-lane chain. On gfx942 every
__shfl/__shfl_xoris ads_bpermute, the slowest step in that chain.Changes
Data movement only. Dispatch, launch configuration, API, tie rules and arithmetic order are unchanged.
topk_reg_kernel(≤ 8 scores a lane, E ≤ 512): the pivot is broadcast withreadlane; candidate slots come from oneballot+mbcntinstead of twowarp_cumsumscans; the rank and the renorm sum are unrolled over constant lanes whenn_cand ≤ 8 / 16andtopk ≤ 8. Wider instantiations keep the original code.topk_gating_kernel_prefill: when a row is the whole wave, the argmax broadcasts and the softmax max usereadlaneand the fused DPP max; the softmax sum keeps the__shfl_xorpairing order, exchanging through DPP andds_swizzleon gfx9.op_tests/test_moeTopkSoftmax.py: newstable_casesfor the added boundaries; the legacy reference is padded until it takes the LDS kernel.Performance (if applicable)
Main
f70ac7f90vs this PR0d5bd9b4a, both JIT-built invllm/vllm-openai-rocm:nightly-edad8b03c5285a1c12e7e4f83c1af709b319a01b(PyTorch 2.13, ROCm 10.0) on one idle GPU. CUDA graph of 64 calls, per-call median of 5 alternating rounds; round-to-round spread ≤ 0.011 µs on the rows below.biased_grouped_topk, GLM-5.2 E=256 top-8 fp32, strided[M,9]out, M=1topk_gatingsoftmax, Qwen3 E=128 top-8 bf16, renorm, M=1Speedup (main / this PR) on the other shapes that share the changed code
Spreads above 0.1 µs: E128 k32 at M ≤ 64 (≤ 0.51), E512 k10 at M=1 (0.44), grouped softmax at M=64 (0.42), E896 at M=8 (0.57), and E256 / E384 / E2048 at M=16384 (≤ 0.32). The sigmoid row has no source change; its
opt_nkernel compiles one instruction longer becausesubwarpArgmax's two__shflno longer share an address.Bandwidth and roofline: latency-bound at M ≤ 128. A GLM-5.2 row reads 1 KB and writes 64 B, so HBM traffic is negligible.
Per-wave
rocprofv3counters at M=1, main → this PR:Resources:
topk_reg_kernelinstantiations with ≤ 8 scores a lane use ≤ 52 VGPRs and 0 scratch (GLM: 41 VGPRs, 46 SGPRs).topk_gating_kernel_prefill<bf16,128,TPW=1>: 19 VGPRs, 0 scratch.Testing
Documentation
No API, dispatch or output change.
Dependencies
Breaking Changes
None.
PR Self-Review
Reviewed revision
0d5bd9b4aBehavior and intent
Same outputs, bit for bit, from fewer and cheaper cross-lane operations in
topk_reg_kernelandtopk_gating_kernel_prefill. The existing code is correct, but at decode sizes most of the wave's time goes tods_bpermuteround trips in the scans, rank, renorm and butterflies.Validation and unverified scope
f70ac7f90vs0d5bd9b4a: 960 configurations throughbiased_grouped_topk,grouped_topkandtopk_gating(E = 64–896, top-k 4–32, M = 1–16384, random, ties, NaN and ±inf, renorm on and off), weights compared as bit patterns: 0 differences.0d5bd9b4a, rebuilt from scratch:python3 op_tests/test_moeTopkSoftmax.pypassed with 0failed!lines and all 32stable_casesrows aterr0.op_tests/test_moe_topk_gating.py,op_tests/test_topk_softmax.pyandpytest tests/test_moe_dispatch_snapshot.pypassed.test_grouped_topkcases print intermittentfailed!lines on main too; they run the LDS kernel, which this PR does not change.PR-specific question
LAUNCHER_TOPK_REGpasses: wave64 (reg_lanes == 64), no group filter,num_experts % 4 == 0,4 <= topk <= 32. The new slots, rank tiers and renorm unroll are compile-time gated toEXPERTS_PER_LANE <= 8; the 14/16/32-a-lane instantiations keep the original code.pivot > -inf,topk <= n_cand <= 64) sees the samen_cand. Rank tiers checkn_cand <= 8 / 16and the renorm unroll checkstopk <= 8, each falling back to the original loop.topk_gating: thereadlanepaths compile only when a row is the whole wave, and the DPP /ds_swizzleexchanges only under__GFX9__; other targets keep__shfl_xor.n_candhold key 0 and lanes pasttopkhold weight+0.0f, so slot order and the unrolled loops cannot change the output.Changed op tests
op_tests/test_moeTopkSoftmax.pyaddsstable_casesfor boundaries no existing case reached:thirty_two_way_tie(n_cand = 32, back to thewarp_rank_descloop), top-16 withn_cand = 16, E=512 (theEXPERTS_PER_LANE == 8gate), top-4 (dead lanes in the rank and renorm), E=192 (odd experts per lane) and E=200 (masked last round). The padded legacy reference previously stayed on the register path for E = 128/192/200; it now pads until it takes the LDS kernel, so each case compares two different kernels.