[PR #486] GLM decode route: the route kernel's reads and writes reordered - a micro-optimization (stacked on #451; route -14%, decode 0.1-0.3%) #559

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

📋 Pull Request Information

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

Base: main ← Head: pr/route-fast


📝 Commits (2)

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

📊 Changes

5 files changed (+262 additions, -26 deletions)

View changed files

📝 docs/SETTINGS.md (+3 -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 (+251 -26)

📄 Description

Stacked on #451. The first commit is #451's; this PR is the second one.

A micro-optimization of the decode's route kernel, like #451. It changes the order the kernel reads and writes in, without changing what it computes:

  • The route error flag is read from a device copy. Every route started with a host-mapped read, a PCIe round trip, and both copies are written on an error.
  • The CPU lane's request input (x) goes out at the kernel's start, not after the plan.
  • The plan's global reads go into shared memory in parallel: the route experts' counts and helper flags, this layer's and the next layer's spares, and the predicted experts' table entries. Thread 0's plan read them one dependent load after another.
  • The request's head is written a thread a field, not by thread 0 alone.
  • The weights' sum and the CPU lane's picks run in warp 0:
    • Each lane adds the k weights in route order, so the sum is the same bits.
    • An expert goes to the CPU when its rank in the coldest-first order (ties: the later in route order) is below the plan's to_cpu. That is the same set thread 0's greedy loop picked.

The same plan and request. MAYA_GLM_ROUTE_FAST is on by default; =0 gives the old order (A/B). It applies with at most 8 experts per token.

MAYA_GLM_ROUTE_PROF=1 (measurement, off by default) prints the kernel's phases and thread 0's plan phases in cycles at exit. It's how the numbers below were measured.

Both settings have rows in docs/SETTINGS.md. tools/check_hip_compat.py passes.

Exactness

The deterministic run has the CPU lane off and a fixed RAM tier. With MAYA_GLM_ROUTE_FAST on and off, the tokens are identical at 2K and 8K: 1a056479 / d765e0a5.

Speed

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

The route kernel (MAYA_GLM_ROUTE_PROF, default settings, 2K prompt + 256 tokens, cycles per route at ~1.38 GHz):

=0 this PR
the plan (thread 0) done 32,019 29,049
the request head written 42,682 31,623
the sequence published (kernel end) 50,793 43,537 (-14 %)
plan phases: the sum / the lane picks 1,594 / 6,459 528 / 525

Decode, deterministic (CPU lane off, MAYA_GLM_RAM_GB=50, 512 tokens, alternating rounds, ms per token):

prompt =0 this PR
2K 70.59 70.54 -0.1 %
8K 83.29 83.00 -0.3 %
27K 70.63 70.47 -0.2 %

3 rounds on upstream main v1.1.27 + #451; every round with the change was faster at 8K and 27K.

That is small, as #451's was. The route is serial work at every MoE layer of every token, and the CPU lane's request reaches the host sooner, so the gain is larger with the lane on. In the served configuration it measured about +1-3 % over 3 rounds, inside that setting's ±2 % run-to-run spread.


🔄 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/486 **Author:** [@merbanan](https://github.com/merbanan) **Created:** 10/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `pr/route-fast` --- ### 📝 Commits (2) - [`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) ### 📊 Changes **5 files changed** (+262 additions, -26 deletions) <details> <summary>View changed files</summary> 📝 `docs/SETTINGS.md` (+3 -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` (+251 -26) </details> ### 📄 Description **Stacked on #451.** The first commit is #451's; this PR is the second one. A micro-optimization of the decode's route kernel, like #451. It changes the order the kernel reads and writes in, without changing what it computes: - **The route error flag** is read from a device copy. Every route started with a host-mapped read, a PCIe round trip, and both copies are written on an error. - **The CPU lane's request input** (`x`) goes out at the kernel's start, not after the plan. - **The plan's global reads** go into shared memory in parallel: the route experts' counts and helper flags, this layer's and the next layer's spares, and the predicted experts' table entries. Thread 0's plan read them one dependent load after another. - **The request's head** is written a thread a field, not by thread 0 alone. - **The weights' sum and the CPU lane's picks** run in warp 0: - Each lane adds the k weights in route order, so the sum is the same bits. - An expert goes to the CPU when its rank in the coldest-first order (ties: the later in route order) is below the plan's `to_cpu`. That is the same set thread 0's greedy loop picked. **The same plan and request.** `MAYA_GLM_ROUTE_FAST` is on by default; `=0` gives the old order (A/B). It applies with at most 8 experts per token. `MAYA_GLM_ROUTE_PROF=1` (measurement, off by default) prints the kernel's phases and thread 0's plan phases in cycles at exit. It's how the numbers below were measured. Both settings have rows in `docs/SETTINGS.md`. `tools/check_hip_compat.py` passes. ## Exactness The deterministic run has the CPU lane off and a fixed RAM tier. With `MAYA_GLM_ROUTE_FAST` on and off, the tokens are identical at 2K and 8K: 1a056479 / d765e0a5. ## Speed Measured on a Tesla V100-PCIE-32GB at 250 W with a Ryzen 9 7950X and turboderp's EXL3 2.05bpw. **The route kernel** (`MAYA_GLM_ROUTE_PROF`, default settings, 2K prompt + 256 tokens, cycles per route at ~1.38 GHz): | | `=0` | this PR | |---|---:|---:| | the plan (thread 0) done | 32,019 | 29,049 | | the request head written | 42,682 | 31,623 | | the sequence published (kernel end) | 50,793 | 43,537 (-14 %) | | plan phases: the sum / the lane picks | 1,594 / 6,459 | 528 / 525 | **Decode, deterministic** (CPU lane off, `MAYA_GLM_RAM_GB=50`, 512 tokens, alternating rounds, ms per token): | prompt | `=0` | this PR | | |---|---:|---:|---:| | 2K | 70.59 | 70.54 | -0.1 % | | 8K | 83.29 | 83.00 | -0.3 % | | 27K | 70.63 | 70.47 | -0.2 % | 3 rounds on upstream main v1.1.27 + #451; every round with the change was faster at 8K and 27K. That is small, as #451's was. The route is serial work at every MoE layer of every token, and the CPU lane's request reaches the host sooner, so the gain is larger with the lane on. In the served configuration it measured about +1-3 % over 3 rounds, inside that setting's ±2 % run-to-run spread. --- <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#559
No description provided.