[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

Open
opened 2026-10-29 21:26:27 +07:00 by bsakaguchi · 0 comments
Owner

📋 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)

  • aecbbc9 GLM 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

This is a micro-optimization. It saves about 5 µs per decode route, which is 0.15–0.33 % faster decode on a V100. That's within run-to-run noise in normal use, where the CPU lane is on. It is exact (the same experts, the same tokens) and small (one kernel function), which is why it seemed worth sending. Please weigh it as a micro-optimization, not as a speed-up anyone will notice.

What

The decode's moe_route_kernel picks the route's top k with topk_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_rank gets 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:

  • below the span, a score beats e when it is >=;
  • above the span, when it is >;
  • only the span's own 32 ids need the id compare.

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.

  • The same order and ties as topk_warp: the largest first, ties to the lower id.
  • Where it applies: the experts must be a multiple of 32 and at most the block's 320 threads (GLM's 256); otherwise topk_warp / topk_argmax run as before. A route with a non-finite score is refused as before; its picks are never used.
  • A/B: MAYA_GLM_ROUTE_RANK=0 brings back the serial rounds. There is a row in docs/SETTINGS.md.
  • The prompt path's route_kernel (glm_batch.cu) is not touched: it is 0.02 % of a prompt's GPU time.

Exactness

  • Microbenchmark (standalone, not in this PR): 60,000 routes of 256 scores, a quarter of them with many ties and some fully tied. The picks were identical to topk_warp's and to a CPU sort by (score desc, id asc) in every route.
  • In the engine: deterministic runs (CPU lane off, 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):

selection cycles µs
topk_warp (main) 8,993 6.5
one warp per rank, each running its own r + 1 rounds 7,429 5.4
rank count, every compare with the id test 5,684 4.1
topk_rank (this PR) 3,207 2.3

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):

prompt MAYA_GLM_ROUTE_RANK=0 this PR
2K 97.02 (96.89–97.09) 96.87 (96.73–96.93) −0.15 %
8K 84.61 (84.49–84.68) 84.35 (84.31–84.41) −0.31 %
27K 85.65 (85.51–85.74) 85.37 (85.34–85.44) −0.33 %

At 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.py passes; the kernel uses only float4 shared 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.

## 📋 Pull Request Information **Original PR:** https://github.com/mw00/project-maya/pull/451 **Author:** [@merbanan](https://github.com/merbanan) **Created:** 10/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `pr/route-rank` --- ### 📝 Commits (1) - [`aecbbc9`](https://github.com/mw00/project-maya/commit/aecbbc9ab4a0fe072c1399383fc5b009510e6a8e) GLM 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) <details> <summary>View changed files</summary> 📝 `docs/SETTINGS.md` (+1 -0) 📝 `src/kernels/cuda/glm_fast.cu` (+64 -7) </details> ### 📄 Description > **This is a micro-optimization.** It saves about 5 µs per decode route, which is **0.15–0.33 % faster decode** on a V100. That's within run-to-run noise in normal use, where the CPU lane is on. It is exact (the same experts, the same tokens) and small (one kernel function), which is why it seemed worth sending. Please weigh it as a micro-optimization, not as a speed-up anyone will notice. ## What The decode's `moe_route_kernel` picks the route's top k with `topk_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_rank` gets 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: - below the span, a score beats e when it is `>=`; - above the span, when it is `>`; - only the span's own 32 ids need the id compare. 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. - **The same order and ties as `topk_warp`:** the largest first, ties to the lower id. - **Where it applies:** the experts must be a multiple of 32 and at most the block's 320 threads (GLM's 256); otherwise `topk_warp` / `topk_argmax` run as before. A route with a non-finite score is refused as before; its picks are never used. - **A/B:** `MAYA_GLM_ROUTE_RANK=0` brings back the serial rounds. There is a row in `docs/SETTINGS.md`. - The prompt path's `route_kernel` (glm_batch.cu) is not touched: it is 0.02 % of a prompt's GPU time. ## Exactness - **Microbenchmark** (standalone, not in this PR): 60,000 routes of 256 scores, a quarter of them with many ties and some fully tied. The picks were identical to `topk_warp`'s and to a CPU sort by (score desc, id asc) in every route. - **In the engine:** deterministic runs (CPU lane off, `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): | selection | cycles | µs | |---|---:|---:| | `topk_warp` (main) | 8,993 | 6.5 | | one warp per rank, each running its own r + 1 rounds | 7,429 | 5.4 | | rank count, every compare with the id test | 5,684 | 4.1 | | **`topk_rank` (this PR)** | **3,207** | **2.3** | **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): | prompt | `MAYA_GLM_ROUTE_RANK=0` | this PR | | |---|---:|---:|---:| | 2K | 97.02 (96.89–97.09) | 96.87 (96.73–96.93) | −0.15 % | | 8K | 84.61 (84.49–84.68) | 84.35 (84.31–84.41) | −0.31 % | | 27K | 85.65 (85.51–85.74) | 85.37 (85.34–85.44) | −0.33 % | At 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.py` passes; the kernel uses only `float4` shared 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
mw00/project-maya#528
No description provided.