Repository navigation
Conversation
|
Please undo all the reformatting, leave only actual changes. Stick to repo code style. |
Done. |
|
Which models benefit from this? |
|
Such as Kimi K3 (top-16) directly hits the Bitonic Sort fast path. Non-power-of-2 models benefit from the regular Top-K path via: · Unrolled static indices: prevent compiler register spilling to Local Memory. |
|
Codex tells me that Kimi K3 would not hit the new fast paths. Can you help explain the discrepancy? Any idea why the Radeon timings are so slow? I've never seen this op be very expensive in any of the models I've run. |
|
"AMD Radeon Graphics" suggests a weaker iGPU, but doesn't say which one. @Winamin What CPU is it? |
|
Currently, Kimi K3 is rejected at the code level because K != N (896 experts). The current check is K == N. However, the current acceleration is supported, it's just that there is a problem in the code I wrote. Also, the current AMD test is on a 9600X iGPU. |
|
Yeah, that's a 2-CU minimal desktop iGPU, so slow is expected. |
What I mean is the sorting algorithm can do this. The current guard blocks this case outright. Supporting models like Kimi K3 requires a top-K variant. This variant adds no extra cost and may even be lighter than the current path, as we avoid the repeated O(K·N) reduction loop. @jeffbolznv |
When selecting a small number of experts with a 32-lane subgroup and a power-of-two expert field of up to 1024, find the Kth score with a subgroup sort, shortlist the candidates that can reach it, then sort the shortlist to pick the winners. Fall back to the reduction loop when scores are non-finite or the shortlist does not fit in a subgroup. For the remaining cases, pick the next expert first, then fetch its weight once instead of carrying it through every reduction step.
@jeffbolznv Updated.The 896/16 config (Kimi K3's routing shape) now hits the fast path: −94% vs upstream 9d6f04f — synthetic shader benchmark, not a Kimi K3 model run. The measurement method is a single call to the Vulkan topk_moe shader time, 512 tokens/dispatch, RTX 5060 Ti, 32-lane. 82 configs, 0 mismatches. Negative = faster. |
| // The Kth lane maximum is a lower bound for the Kth expert. | ||
| best = sort_expert_lanes(best, lane); | ||
| const uvec3 cutoff = subgroupShuffle(best, n_expert_used - 1); |
There was a problem hiding this comment.
The Kth lane maximum is a lower bound for the Kth expert. Sorting the 32 lane maxima gives a cutoff; candidates below it are dropped before the final sort. This replaces the O(K·N) repeated reduction with one O(N) scan plus two subgroup sorts.
|
On RDNA3 with RADV the new fast path never runs. The pipeline gets WARP_SIZE = device->subgroup_size, which is 64 there, so the llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp Line 4536 in 918fe46 RDNA has subgroup size control with minSubgroupSize 32, and the backend already asks for wave32 on AMD in a few places, for example for the coopmat1 flash attention: llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp Lines 2028 to 2032 in 918fe46 Creating the topk_moe pipeline with a required subgroup size of 32 when that is available might be enough to get the fast path on AMD. I can test it on the XTX if you push it. Written with Claude Opus 5.5 |
|
Thanks for the quick update. Tested 3d525e2 merged onto master 779b413 on the 7900 XTX (RADV, Mesa 26.2.5). The wave32 pipeline is used now and the sort path runs on RDNA3. Results are correct:
No speed change on this card. On Gemma 4, decode topk_moe is 3.55 us per call before and after. At pp512 the call goes from 3.8 to 4.4 us, and with the sort path disabled it is back at 3.8 us. End to end it's flat. Gemma 4 tg128 is 135.8 vs 135.3 t/s. K2-Horizon is 103.4 vs 103.8 tg128 and 2679 vs 2679 pp512. Qwen3.8-Flash-Next, with half of its experts offloaded to the iGPU, is 20.2 vs 20.25 tg64, and its topk_moe call is 22.1 vs 22.2 us. Written with Claude Opus 5.5 |
|
You put benchmark numbers for cases > 512 which will never currently fuse in the Vulkan backend, because |
|
In the current PR commit, the expert count ( n_experts_spec ) is fetched directly from the backend via constant_id , ensuring full compatibility with the existing pipeline limits. This benchmark measures the maximum capability of the operator, targeting the scenario where 16 experts are selected at a time. For models within the backend pipeline limits and with fewer than 512 experts, the actual gains per operator remain substantial. |
|
Baseline commit 01d5774 This table reports a standalone Vulkan microbenchmark of the fused Top-K MoE shader. Both versions receive identical synthetic FP32 router logits and, where applicable, identical routing biases. Each case uses the model's expert count, K, gating function, and normalization settings. The token count is the number of rows processed in one dispatch. Each pair runs both shaders, with the order alternating between pairs. Vulkan GPU timestamps bracket a block of repeated dispatches, including the compute barrier after each dispatch. The elapsed time is divided by the number of dispatches. Both GPUs used 64 dispatches per block, with 16 warmup pairs and 49 measured pairs. For this benchmark, both commits were run with Wave32 on both GPUs. Each cell shows the median dispatch time as RTX 5060 Ti
Ryzen 5 9600X Radeon iGPU, Wave32 for both commits
Written with Codex |
|
This seems like a lot of complexity for a fusion that I haven't seen cost a lot in absolute time on real models. Do you have any real end-to-end perf data? |
|
I did already test end-to-end too and saw no difference. |
|
I can only say that this part does not have much impact on end-to-end, but the improvement is steady. |
|
In the OLMoE-1B-7B-0924-Instruct, it's more about improving low-end devices
Each card alternated running 6 sets of A/B |
|
I believe this tradeoff is acceptable. We get consistent routing kernel speedups, at the cost of substantial additional complexity. |
|
This PR optimizes fused Vulkan Top-K MoE dispatch while keeping the pipeline limit at 10. Shader microbenchmarks show substantial gains for large-expert prefill, while full-model gains remain small. Model: Qwen3.5-35B-A3B Q3_K_S, 256 experts, K=8, all 41 layers GPU-offloaded. Each case used a separate The small end-to-end changes are consistent with the measured Top-K share of GPU time: 0.04–0.05% at prefill and 0.36–0.42% at TG128 on RTX 5060 Ti. On the Radeon iGPU, the baseline uses Wave64 and the PR uses Wave32, reducing the share from 0.14–0.21% to 0.01–0.02% at prefill and from 0.72% to 0.19% at TG128.
Written with Codex So the small end-to-end improvement is expected. Top-k routing accounts for only a small portion of total inference time, and we are close to the practical limit with this optimization. |
Replace the top8, shortlist sort and full sort paths and the uvec3 bitonic helpers with one integer key shortlist path plus a single argmax loop for the cases it rejects. The shortlist is sorted with a lane bitonic sort on packed keys. Output is unchanged (TOPK_MOE passes on RTX 5060 Ti and on a Radeon 9600X iGPU) and timings are equal or better for most expert/k setups.
|
Mechanism: build order-preserving integer keys from the (optionally biased) scores, take the k-th largest lane maximum as a cutoff, compact experts at or above it into shared memory, then sort the shortlist with a lane bitonic sort on packed key + inverted slot so equal scores keep the lower expert ID. One argmax loop remains for k < 4, k > 32, rows where the shortlist does not fit in one warp, and cases where adjacent packed score keys collide. Padding experts get key 0 and never pass the cutoff. The shortlist path needs wave32, which the pipeline already requests for this shader. vs master: +125 / -92 across the two files. vs the previous commit: 496 → 252 lines. The top8 path, separate shortlist sort, full sort, and uvec3 bitonic helpers are gone. Performance at 512 experts vs the previous commit: 26% faster across k = 1 to 64. k=8 is the only cell that goes the other way (+9%), where the old top8 shortcut was cheaper. Performance at 512 experts vs master: 27% faster across the same k range. Only k=1 is slower (+14%), and that cell is dispatch bound. TOPK_MOE passes on both an RTX 5060 Ti and a 9600X iGPU. |
|
Could you both take another look at the latest version when you have time? thanks! @jeffbolznv @0cc4m |

Overview
When selecting a small number of experts, the Kth score is found with a subgroup bitonic sort. Candidates that can reach it are shortlisted, and the shortlist is then sorted to pick the winners. Ties are broken by ascending expert ID. Weights follow their experts through the shortlist; ranking uses score + bias when a bias is present, while the emitted weight is the unbiased score.
Fast path conditions:
The sort loops are unrolled over the 32-lane subgroup. Inputs that do not meet these conditions fall back to the existing round-robin selection path. The regular Top-K path is unchanged: each round first selects the winning expert by score and ID, then reads its weight once within the subgroup.
Additional information
The measurement method is the time cost of a single call of the independent Vulkan topk_moe shader.
Requirements