[CUDA] Optimize QMoE SoftmaxTopK router for small-batch decode - #28980
Merged
Merged
Conversation
Tianlei Wu (tianleiwu)
requested review from
Akshay Sonawane (apsonawane) and
kunal-vaishnavi
June 10, 2026 16:32
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes the CUDA QMoE router softmax+top-k path by introducing block-per-row kernels that parallelize the work across threads (targeting the small-batch / autoregressive decode case) and adding a dispatcher to select the best kernel based on k and num_experts.
Changes:
- Added
SoftmaxTopKMergeKernel(CUB block sort) fork <= 64andnum_experts <= 1024. - Added
SoftmaxTopKKernelBlock(block-parallel reductions + k rounds of argmax) as fallback fork <= 64andnum_experts > 1024. - Replaced direct
SoftmaxTopKKernel<<<...>>>()launches withDispatchSoftmaxTopK(...)in allLaunchSoftmaxTopKoverloads.
Tianlei Wu (tianleiwu)
marked this pull request as draft
June 11, 2026 15:55
The QMoE router top-k kernel (SoftmaxTopKKernel) used one thread per row. In autoregressive decode (num_rows == 1) this leaves all but one thread of the block idle while it serially scans every expert's logit with uncoalesced, latency-bound global reads. For Qwen3 MoE (256 experts, top-8, 40 MoE layers) this kernel alone accounted for ~50% of decode GPU time (~139 us/call). Add two block-per-row kernels and dispatch to them for the common case: - SoftmaxTopKMergeKernel: each block sorts one row's logits descending with cub::BlockMergeSort and reads the first k. A full block sort of <=1024 logits is the fastest option on these sizes (benchmarked), matching the CUB block-merge recommendation already used elsewhere for top-k. - SoftmaxTopKKernelBlock: block-parallel max/sum reduction plus k rounds of block argmax; used as the fallback for num_experts > 1024. DispatchSoftmaxTopK selects the merge kernel by num_experts (capacity = block_size * items_per_thread) for k <= 64, falls back to the block-argmax kernel for larger expert counts, and keeps the original one-thread-per-row kernel only for k > 64. Tie-breaking (lower expert index wins on equal logits) and the softmax normalization semantics are preserved. On an H200 this cuts the router from ~5.56 ms/token to ~0.17 ms/token and raises end-to-end Qwen3.6-35B-A3B INT4 decode from ~80 to ~113 tok/s. All QMoE CUDA parity tests pass.
- Sort a composite (logit, index) key so equal-logit ties deterministically prefer the lower expert index instead of relying on cub::BlockMergeSort stability (it is not a stable sort). - Use non-templated ::cuda::maximum() / ::cuda::std::plus() to match the other CUDA softmax kernels in the repo. - Drop redundant __syncthreads() barriers (the single barrier after each shared write already publishes the value and separates the shared-storage reuse; Sort() leaves results in registers and temp.merge is not reused).
Tianlei Wu (tianleiwu)
force-pushed
the
tlwu/qmoe_router_topk_perf
branch
from
June 11, 2026 23:04
939da79 to
f36a84e
Compare
Tianlei Wu (tianleiwu)
marked this pull request as ready for review
June 11, 2026 23:04
Tianlei Wu (tianleiwu)
requested review from
Akshay Sonawane (apsonawane) and
Copilot
June 12, 2026 00:19
Akshay Sonawane (apsonawane)
approved these changes
Jun 12, 2026
Tianlei Wu (tianleiwu)
added a commit
that referenced
this pull request
Jun 12, 2026
This cherry-picks #28980 to 1.27 release
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Optimizes the CUDA QMoE router top-k (
LaunchSoftmaxTopK) for small-batch / autoregressive decode by replacing the old one-thread-per-row hot path with parallel CUB and warp-level top-k kernels. The dispatch now uses the fastest specialized path for common MoE expert counts while preserving the existing softmax normalization and deterministic lower-index tie-breaking semantics.This PR also factors the warp-level top-k sorting code into a reusable CUDA helper header and adds direct CUDA-internal tests so the new routing paths are covered independently of higher-level QMoE tests.
Motivation and Context
The previous router path launched a 256-thread block per row but did all top-k work in a single thread. In decode scenarios such as
num_rows == 1, that made the router latency-bound on a serial scan of all expert logits and turnedSoftmaxTopKKernelinto a major MoE decode bottleneck.For a Qwen3-style MoE workload with 256 experts, top-8 routing, and 40 MoE layers, the original router accounted for roughly 50% of decode GPU time. Moving the work to block/warp-parallel kernels removes that bottleneck while keeping the same output ordering and scaling behavior.
Key Changes
DispatchSoftmaxTopKrouting fork <= 64andnum_experts <= 1024, with a fallback to the original scalar kernel for larger or uncommon shapes.SoftmaxTopKWarpBitonicKernelfornum_experts <= 32, using one warp per row and in-register bitonic sorting via warp shuffles.SoftmaxTopKWarpMergeKernelfor32 < num_experts <= 64, using a single warp and CUB warp merge sort.SoftmaxTopKMergeKernelwith CUB block merge sort fornum_experts <= 128,256,512, and1024.onnxruntime/core/providers/cuda/cu_inc/topk_warp_sort.cuhwith reusable warp bitonic and warp merge sort helpers.(score, index)into auint64_tstable sort key for the CUB merge paths, matching onnxruntime-genai's lower-index tie-breaking and avoiding compound comparators.SoftmaxTopK_*tests covering warp bitonic, warp merge, block merge, stable ties, normalization,float,half, andbfloat16.Performance
H200 measurements for the target QMoE decode scenario showed the router cost dropping from roughly
5.56 ms/tokento0.17 ms/token, improving end-to-end Qwen3.6-35B-A3B INT4 decode throughput from about80 tok/sto113 tok/s.Additional profiling of the
32 < num_experts <= 64warp merge path showed the packeduint64_tstable sort key is consistently faster than a{float, int}struct comparator on H200:Testing
lintrunner -aninja onnxruntime_providers_cuda_utninja onnxruntime_provider_testGTEST_FILTER='CUDA_EP_Unittest.SoftmaxTopK_*' ./onnxruntime_provider_test --gtest_filter='CUDA_EP_Unittest.All'onnxruntime/test/python/transformers/test_qmoe_cuda.py -k parity(44 passed)