[PR #630] GLM decode route in one pass an expert - three block barriers instead of nine (stacked on #486; route -24%, decode -0.35% on top of #451 + #486) #637

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

📋 Pull Request Information

Original PR: https://github.com/mw00/project-maya/pull/630
Author: @merbanan
Created: 10/28/2026
Status: 🔄 Open

Base: main ← Head: pr/route-one


📝 Commits (3)

  • 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)
  • 8340e0e GLM decode route: the route kernel's reads and writes reordered - a micro-optimization (MAYA_GLM_ROUTE_FAST, on by default; MAYA_GLM_ROUTE_PROF measures it)
  • 958e696 GLM decode route in one pass an expert (MAYA_GLM_ROUTE_ONE, on by default; =0: moe_route_kernel): each thread its own expert's score, rank and entries, one warp the plan a lane an entry and the request - three block barriers instead of nine, no single-thread loop; the same plan and request bit for bit (glm_route_one_test)

📊 Changes

7 files changed (+835 additions, -27 deletions)

View changed files

📝 CMakeLists.txt (+7 -0)
📝 docs/SETTINGS.md (+4 -0)
📝 include/strata/kernels/glm_fast.hpp (+2 -0)
📝 src/core/glm_fast_path.cu (+5 -0)
📝 src/core/glm_model.cu (+1 -0)
📝 src/kernels/cuda/glm_fast.cu (+524 -27)
➕ tests/glm_route_one_test.cu (+292 -0)

📄 Description

Stacked on #486, which is stacked on #451. The first two commits belong to those PRs; this PR adds the third.

This PR computes the decode's route in one pass per expert:

  • Phase 0: each of the 320 threads scores its own expert. Affinity reuses the thread's own table load. Non-finite scores are caught with one __syncthreads_or.
  • Phase 1: each thread ranks its own expert with route_rank, the same rule topk_rank uses (now a shared helper). The k owners write their own plan entries: id, weight, table entry, home, count.
  • Then one warp builds the plan and the request, one lane per entry:
    • the weight sum, in route order, so the bits match;
    • the CPU lane's picks, as in #486;
    • each entry classified as resident, left out, CPU, skip or fetch;
    • spares handed out by ballot;
    • the request head assembled in shared memory and written in 8-byte words, then published.

The result is three block barriers instead of nine, and no single-thread loop. The shared expert's swiglu/q8_1 stays in the other blocks, as before.

The same plan and request, bit for bit. MAYA_GLM_ROUTE_ONE is on by default; =0 gives moe_route_kernel. It applies to the plain decode route (with ROUTE_RANK and ROUTE_FAST, no predictor, no look-ahead, no near ids). The other cases keep moe_route_kernel. The setting has a row in docs/SETTINGS.md. tools/check_hip_compat.py passes.

Exactness

tests/glm_route_one_test.cu (CUDA tests, glm_route_one_test) runs 20000 random routes under both kernels. It covers:

  • E=256, k of 8, 6, 4 and 1;
  • ties, NaN and Inf;
  • sticky errors, the helper, the CPU plan;
  • skips, promotions, disk reads and misses;
  • affinity off and at 0.02.

It hashes the plan, the sequences, the whole table, the counters, sh_hq, the error flag and the ring slot. All 40000 routes match on a V100 (this branch) and on an RTX 2060. The deterministic decode (CPU lane off, MAYA_GLM_RAM_GB=50) gives the same tokens at 2K, 8K and 27K.

Speed

Measured on a Tesla V100-PCIE-32GB at 250 W with a Ryzen 9 7950X and turboderp's EXL3 2.05bpw, on v1.1.35 with this stack.

The route kernel (MAYA_GLM_ROUTE_PROF, µs per route at ~1.38 GHz):

main #451 + #486 this PR
43.3 30.7 23.4 (-24 %)

The test's own timing of back-to-back routes shows 20.0 -> 16.4 µs (affinity 0) and 17.8 -> 16.0 µs (affinity 0.02).

Decode, deterministic (CPU lane off, MAYA_GLM_RAM_GB=50, mean of 4 interleaved rounds, ms per token):

prompt main #451 + #486 this PR
2K 67.25 66.76 66.54
8K 57.46 57.03 56.82
27K 64.87 64.46 64.24

That is -1.0 to -1.1 % against main, and -0.35 % against #451 + #486.

Decode, served settings (CPU lane on, DEFER_CPU=1, ROUTE_AFFINITY=0.02, default RAM tier, mean of 3 rounds × 3 prompts):

main #451 + #486 this PR
28.82 27.85 (-3.4 %) 27.78 (-3.6 %)

All 9 runs of this stack beat all 9 runs of main. On its own, this commit's gain over #486 is inside the noise in the served setting. Its case is the route kernel: a quarter less serial time at every MoE layer, which counts for more on a faster GPU where the route is a bigger share of a token. It has been running on our served V100 since 2026-10-28.


🔄 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/630 **Author:** [@merbanan](https://github.com/merbanan) **Created:** 10/28/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `pr/route-one` --- ### 📝 Commits (3) - [`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) - [`8340e0e`](https://github.com/mw00/project-maya/commit/8340e0ec9e085db1b7d1bb0ad1aa6534c8fca59d) GLM decode route: the route kernel's reads and writes reordered - a micro-optimization (MAYA_GLM_ROUTE_FAST, on by default; MAYA_GLM_ROUTE_PROF measures it) - [`958e696`](https://github.com/mw00/project-maya/commit/958e696234b7e6420e8139f8644c36360d279a46) GLM decode route in one pass an expert (MAYA_GLM_ROUTE_ONE, on by default; =0: moe_route_kernel): each thread its own expert's score, rank and entries, one warp the plan a lane an entry and the request - three block barriers instead of nine, no single-thread loop; the same plan and request bit for bit (glm_route_one_test) ### 📊 Changes **7 files changed** (+835 additions, -27 deletions) <details> <summary>View changed files</summary> 📝 `CMakeLists.txt` (+7 -0) 📝 `docs/SETTINGS.md` (+4 -0) 📝 `include/strata/kernels/glm_fast.hpp` (+2 -0) 📝 `src/core/glm_fast_path.cu` (+5 -0) 📝 `src/core/glm_model.cu` (+1 -0) 📝 `src/kernels/cuda/glm_fast.cu` (+524 -27) ➕ `tests/glm_route_one_test.cu` (+292 -0) </details> ### 📄 Description **Stacked on #486, which is stacked on #451.** The first two commits belong to those PRs; this PR adds the third. This PR computes the decode's route in one pass per expert: - **Phase 0:** each of the 320 threads scores its own expert. Affinity reuses the thread's own table load. Non-finite scores are caught with one `__syncthreads_or`. - **Phase 1:** each thread ranks its own expert with `route_rank`, the same rule `topk_rank` uses (now a shared helper). The k owners write their own plan entries: id, weight, table entry, home, count. - **Then one warp builds the plan and the request, one lane per entry:** - the weight sum, in route order, so the bits match; - the CPU lane's picks, as in #486; - each entry classified as resident, left out, CPU, skip or fetch; - spares handed out by ballot; - the request head assembled in shared memory and written in 8-byte words, then published. The result is three block barriers instead of nine, and no single-thread loop. The shared expert's swiglu/q8_1 stays in the other blocks, as before. **The same plan and request, bit for bit.** `MAYA_GLM_ROUTE_ONE` is on by default; `=0` gives `moe_route_kernel`. It applies to the plain decode route (with `ROUTE_RANK` and `ROUTE_FAST`, no predictor, no look-ahead, no near ids). The other cases keep `moe_route_kernel`. The setting has a row in `docs/SETTINGS.md`. `tools/check_hip_compat.py` passes. ## Exactness `tests/glm_route_one_test.cu` (CUDA tests, `glm_route_one_test`) runs 20000 random routes under both kernels. It covers: - E=256, k of 8, 6, 4 and 1; - ties, NaN and Inf; - sticky errors, the helper, the CPU plan; - skips, promotions, disk reads and misses; - affinity off and at 0.02. It hashes the plan, the sequences, the whole table, the counters, `sh_hq`, the error flag and the ring slot. All 40000 routes match on a V100 (this branch) and on an RTX 2060. The deterministic decode (CPU lane off, `MAYA_GLM_RAM_GB=50`) gives the same tokens at 2K, 8K and 27K. ## Speed Measured on a Tesla V100-PCIE-32GB at 250 W with a Ryzen 9 7950X and turboderp's EXL3 2.05bpw, on v1.1.35 with this stack. **The route kernel** (`MAYA_GLM_ROUTE_PROF`, µs per route at ~1.38 GHz): | main | #451 + #486 | this PR | |---:|---:|---:| | 43.3 | 30.7 | 23.4 (-24 %) | The test's own timing of back-to-back routes shows 20.0 -> 16.4 µs (affinity 0) and 17.8 -> 16.0 µs (affinity 0.02). **Decode, deterministic** (CPU lane off, `MAYA_GLM_RAM_GB=50`, mean of 4 interleaved rounds, ms per token): | prompt | main | #451 + #486 | this PR | |---|---:|---:|---:| | 2K | 67.25 | 66.76 | 66.54 | | 8K | 57.46 | 57.03 | 56.82 | | 27K | 64.87 | 64.46 | 64.24 | That is -1.0 to -1.1 % against main, and -0.35 % against #451 + #486. **Decode, served settings** (CPU lane on, `DEFER_CPU=1`, `ROUTE_AFFINITY=0.02`, default RAM tier, mean of 3 rounds × 3 prompts): | main | #451 + #486 | this PR | |---:|---:|---:| | 28.82 | 27.85 (-3.4 %) | 27.78 (-3.6 %) | All 9 runs of this stack beat all 9 runs of main. On its own, this commit's gain over #486 is inside the noise in the served setting. Its case is the route kernel: a quarter less serial time at every MoE layer, which counts for more on a faster GPU where the route is a bigger share of a token. It has been running on our served V100 since 2026-10-28. --- <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#637
No description provided.