mirror of
https://github.com/mw00/project-maya.git
synced 2026-12-03 21:08:26 +07:00
[PR #451] GLM decode route: the top k by rank, every thread at once - a micro-optimization (-5 us a route, decode 0.15-0.33% faster on a V100) #528
Labels
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
mw00/project-maya#528
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/mw00/project-maya/pull/451
Author: @merbanan
Created: 10/24/2026
Status: 🔄 Open
Base:
main← Head:pr/route-rank📝 Commits (1)
aecbbc9GLM decode route: the top k by rank, every thread at once - a micro-optimization (MAYA_GLM_ROUTE_RANK, on by default; =0: topk_warp)📊 Changes
2 files changed (+65 additions, -7 deletions)
View changed files
📝
docs/SETTINGS.md(+1 -0)📝
src/kernels/cuda/glm_fast.cu(+64 -7)📄 Description
What
The decode's
moe_route_kernelpicks the route's top k withtopk_warp: one warp runs k serial rounds, each a full rescan plus a warp arg-max, while the other nine warps of the block wait. On a V100 that is about 6.5 µs of a route kernel that averages about 40 µs, once per MoE layer and token, on the decode's critical path.topk_rankgets the same picks by rank, with every thread working at once. Thread e counts the scores that beat its own: larger, or equal at a lower id. The experts of rank < k write their id at their rank. To make it cheap, a warp's 32 ids are one span:>=;>;The same pass gives the near misses (ranks k .. k + near_n, for
MAYA_GLM_ROUTE_LOG). The next-layer prediction (MAYA_GLM_PREFETCH_N) uses it too.topk_warp: the largest first, ties to the lower id.topk_warp/topk_argmaxrun as before. A route with a non-finite score is refused as before; its picks are never used.MAYA_GLM_ROUTE_RANK=0brings back the serial rounds. There is a row indocs/SETTINGS.md.route_kernel(glm_batch.cu) is not touched: it is 0.02 % of a prompt's GPU time.Exactness
topk_warp's and to a CPU sort by (score desc, id asc) in every route.MAYA_GLM_RAM_GB=50), Maya-S-v2 IQ2_XXS, fresh 2K / 8K / 27K prompts plus 512 greedy tokens, 4 rounds each way. The tokens were identical in all 8 runs at every length, and so were the VRAM hit rates (69.01 / 70.61 / 69.13 %).Speed
Top-k alone (one block per launch, as in the decode; clock64 around the selection; V100 at 1380 MHz):
topk_warp(main)topk_rank(this PR)The route kernel (nvprof): 40 → 35 µs on average, 31 → 26 µs at the minimum (live config, 2K prompt + 512 tokens).
Decode, ms a token (deterministic as above, 4 alternating rounds, mean with the range):
MAYA_GLM_ROUTE_RANK=0At 8K and 27K, every round with the change was faster than every round without it.
That saving (0.15–0.28 ms a token) matches about 5 µs × the route kernels a token runs. With the CPU lane on (the normal setting, about 40 ms a token here), the same saving would be about 0.5 %. That is inside the ±2 % run-to-run spread, and 4 rounds each way showed no difference either way. Prompt speed is unchanged.
Hardware: Tesla V100-PCIE-32GB at 250 W, Ryzen 9 7950X, Linux, CUDA build
native(sm_70). Not run on other cards or on AMD (tools/check_hip_compat.pypasses; the kernel uses onlyfloat4shared loads and__syncthreads).The idea comes from recursive-oss/rec, whose decode megakernel has each expert team find its own routing rank. Taken as written there (warp r runs r + 1 rounds), it was 5.4 µs here. The rank count above is our own code.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.