[PR #1737] [CLOSED] route-resident: one warp per token in the swap kernel #3640

Closed
opened 2026-11-07 15:29:45 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/1737
Author: @aly8246
Created: 10/9/2026
Status: ❌ Closed

Base: main ← Head: route-resident-parallel-scan


📝 Commits (2)

  • fc54d84 route-resident: one warp per token in the swap kernel
  • 488eecb route-resident parity: the swap against a scalar reference

📊 Changes

3 files changed (+309 additions, -22 deletions)

View changed files

📝 CMakeLists.txt (+8 -0)
📝 src/kernels/cuda/native_router.cu (+46 -22)
➕ src/kernels/route_resident_parity.cu (+255 -0)

📄 Description

route-resident: one warp per token in the swap kernel

STRATA_ROUTE_RESIDENT swaps a routed expert for one that is already resident, and the swap kernel is
the only work it adds to a window. That kernel was written one thread per token: a verify window is 1
to 8 rows, so a block had up to 8 threads doing it, each scanning 512 experts per rank with a ten-way
"already picked" check inside - call it 4 x 512 x 12 scalar operations, on the critical stream, once
per layer. It cost about as much as the waiting it removed, which is why the option measured as a wash.

This commit makes it one warp per token. Lane j reads experts j, j+32, ..., the warp reduces to the
best logit in a shuffle tree, and the ranks are still visited in order, because a swap changes the set
that is already picked. The answer is the same bits: a strictly greater logit wins, the smallest expert
index breaks ties, the already-picked test runs against the ids as they are at that rank, and the
softmax is the same expression.

What it buys on our machine, with the option at margin 0.25:

decode        +6.6% median over 12 matched pairs, both orders   (104.7 -> 112.2 tok/s)
ms/window     -4.4% median over the same pairs                  (18.47 -> 17.27 ms)
at 1M context no slowdown: 112.8 tok/s with 543k tokens in the window, against about 112 tok/s
              for the 512K configuration, which holds 8% more experts in its cache

The rest of this is how those numbers were taken and where the limits of them are.

Three legs in one session (STRATA_VERIFY_PROFILE), same prompts, same 8083-slot cache, medians of the
per-window stage table:

configuration                        hc-read1+router   waitB    ms/window   pcie_e
option off                                2.25          9.30      21.85      1.87
option on, one thread per token           5.00          5.01      21.97      1.00
option on, this commit                    2.58          4.87      19.75      0.95

The option takes about 4.3 ms of PCIe waiting out of a window; the serial swap handed 2.75 ms of that
back, the warp swap hands back 0.33 ms.

Twelve matched pairs of engine restarts, nine decode samples a leg, three greedy 150-token prompts, the
learned profile copied back before every leg so both arms start from the same cache. Six pairs ran the
unpatched build first and six the patched one:

decode      104.7 -> 112.2   median, +6.6%   faster in 11 of 12   p = 0.006
ms/window   18.47 -> 17.27   median, -4.4%   shorter in 11 of 12  p = 0.006
first six pairs +2.7% decode / -4.7% window, last six +7.0% / -4.3%
pcie_e is the same in both arms (0.63 against 0.64), so the same experts are routed.
All twelve pairs
pair  order   decode unpatched   patched    delta    ms/window unpatched   patched    delta
 1    A->B         104.1          115.7     +11.1%         18.64          17.01      -8.7%
 2    A->B         103.5          107.4      +3.8%         18.47          18.12      -1.9%
 3    A->B         111.4          107.7      -3.3%         17.71          18.26      +3.1%
 4    A->B         101.4          113.0     +11.4%         19.11          17.03     -10.9%
 5    A->B         105.8          107.6      +1.7%         19.05          18.05      -5.2%
 6    A->B         104.8          105.6      +0.8%         18.60          17.83      -4.1%
 7    B->A         106.6          113.4      +6.4%         18.70          17.16      -8.2%
 8    B->A         103.1          110.2      +6.9%         18.14          17.29      -4.7%
 9    B->A         106.9          111.5      +4.3%         17.91          17.26      -3.6%
10    B->A         104.3          114.7     +10.0%         17.81          17.11      -3.9%
11    B->A         108.3          115.9      +7.0%         18.47          16.61     -10.1%
12    B->A         104.5          113.6      +8.7%         18.43          17.76      -3.6%

The gain follows how many tail experts the option actually redirects, so a smaller cache gains more.
Four pairs with a 4000-slot cache instead of the automatic 8083 (pcie_e 1.83 against 1.78, about three
times the traffic), two pairs each way: decode +8.8% median, ms/window -7.8% median, 4 of 4.

Where it also shows up: three real agent sessions on this machine - many tool calls, 27, 49 and 102
requests - whose engine configurations differ only in --max-context. A longer context costs VRAM before
--expert-cache auto sizes the cache (the RoPE tables alone are 0.26 KB per token), so the three ran
with three different caches:

--max-context   cache slots   cache VRAM   K/V in pinned RAM   output/wall   PCIe share
   262144           8548       16.21 GiB         3.09 GiB          92.7          8.7%
   524288           8308       15.76 GiB         6.19 GiB          91.6          8.1%
  1048576           7857       14.87 GiB        12.38 GiB          91.5          9.6%

output/wall is the plugin's own tok/s (output divided by duration, so it carries the prefill) - these
sessions reuse their parked conversations heavily, so read it as "did anything move", not as a decode
rate. 8% fewer experts and about 10% more traffic across PCIe, and nothing moved: the medians sit
inside 1.3%. The same knob on the unpatched build, with the option off, is worth -9.5% of the window
for +224 slots (reserve 700 -> 250 MiB), so the sensitivity was there to lose.

Those are three single runs of a real workload, not matched pairs, and no further testing was done. The
speed improvement above is what this PR claims; the 1M figures are what one session did on this
machine.

The option's price is the margin's, not the kernel's, and this commit does not touch it. Teacher-forced
log-probabilities over 7677 positions of one 7.6k-token text (technical prose, source code and
Chinese), top-64, against the same text with the option off:

margin   argmax same   KL (top-64)   teacher-forced loss   perplexity
off      -             0.000         3.2033                -
0.25     84.9%         0.235         3.2238                +0.6%
0.50     83.0%         0.293         3.4360                +7.3%
1.00     63.5%         1.001         4.7751                +49%

Two runs with the option off are bit-identical, so that spread is the option and not the machine. 0.25
keeps nearly all of the waitB saving for a fraction of the distortion; the default 1.0 is not worth
its price.

Parity: 400 random cases - half built with many equal logits, random lo/hi, margins from -0.5 to 1.5, 1
to 8 tokens, about 60% of the experts resident, routed ids with repeats - compared bit for bit on the
ids, the weights and the four counters, both kernels side by side. 0 mismatches.

Tests: src/kernels/route_resident_parity.cu, registered in ctest next to the other router parity tests
(TIMEOUT 120). Same case generator, against a scalar reference of the semantics - a serial walk with
the same comparisons, not a copy of the kernel this branch replaced - so it tests the code in the tree.
ids and the four counters must match bit for bit; the weights are a softmax ratio and the reference is
a second compilation of it, so they are held to 8 ulp (worst seen: 6). A case that swaps nothing has
to leave the weights untouched, which the sentinel they start from checks, and the test fails if no
case swapped at all.

route_resident_parity: 240 cases, 205 with swaps (2004 swaps), 0 mismatches, weights within 6 ulp

🔄 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/Niko1221/Strata/pull/1737 **Author:** [@aly8246](https://github.com/aly8246) **Created:** 10/9/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `route-resident-parallel-scan` --- ### 📝 Commits (2) - [`fc54d84`](https://github.com/Niko1221/Strata/commit/fc54d849be946e55f8b0bed69030a65ff0e31fad) route-resident: one warp per token in the swap kernel - [`488eecb`](https://github.com/Niko1221/Strata/commit/488eecba80019d07179fa7f7bad7d2c147ea4fdb) route-resident parity: the swap against a scalar reference ### 📊 Changes **3 files changed** (+309 additions, -22 deletions) <details> <summary>View changed files</summary> 📝 `CMakeLists.txt` (+8 -0) 📝 `src/kernels/cuda/native_router.cu` (+46 -22) ➕ `src/kernels/route_resident_parity.cu` (+255 -0) </details> ### 📄 Description route-resident: one warp per token in the swap kernel STRATA_ROUTE_RESIDENT swaps a routed expert for one that is already resident, and the swap kernel is the only work it adds to a window. That kernel was written one thread per token: a verify window is 1 to 8 rows, so a block had up to 8 threads doing it, each scanning 512 experts per rank with a ten-way "already picked" check inside - call it 4 x 512 x 12 scalar operations, on the critical stream, once per layer. It cost about as much as the waiting it removed, which is why the option measured as a wash. This commit makes it one warp per token. Lane j reads experts j, j+32, ..., the warp reduces to the best logit in a shuffle tree, and the ranks are still visited in order, because a swap changes the set that is already picked. The answer is the same bits: a strictly greater logit wins, the smallest expert index breaks ties, the already-picked test runs against the ids as they are at that rank, and the softmax is the same expression. What it buys on our machine, with the option at margin 0.25: decode +6.6% median over 12 matched pairs, both orders (104.7 -> 112.2 tok/s) ms/window -4.4% median over the same pairs (18.47 -> 17.27 ms) at 1M context no slowdown: 112.8 tok/s with 543k tokens in the window, against about 112 tok/s for the 512K configuration, which holds 8% more experts in its cache The rest of this is how those numbers were taken and where the limits of them are. Three legs in one session (STRATA_VERIFY_PROFILE), same prompts, same 8083-slot cache, medians of the per-window stage table: configuration hc-read1+router waitB ms/window pcie_e option off 2.25 9.30 21.85 1.87 option on, one thread per token 5.00 5.01 21.97 1.00 option on, this commit 2.58 4.87 19.75 0.95 The option takes about 4.3 ms of PCIe waiting out of a window; the serial swap handed 2.75 ms of that back, the warp swap hands back 0.33 ms. Twelve matched pairs of engine restarts, nine decode samples a leg, three greedy 150-token prompts, the learned profile copied back before every leg so both arms start from the same cache. Six pairs ran the unpatched build first and six the patched one: decode 104.7 -> 112.2 median, +6.6% faster in 11 of 12 p = 0.006 ms/window 18.47 -> 17.27 median, -4.4% shorter in 11 of 12 p = 0.006 first six pairs +2.7% decode / -4.7% window, last six +7.0% / -4.3% pcie_e is the same in both arms (0.63 against 0.64), so the same experts are routed. <details><summary>All twelve pairs</summary> pair order decode unpatched patched delta ms/window unpatched patched delta 1 A->B 104.1 115.7 +11.1% 18.64 17.01 -8.7% 2 A->B 103.5 107.4 +3.8% 18.47 18.12 -1.9% 3 A->B 111.4 107.7 -3.3% 17.71 18.26 +3.1% 4 A->B 101.4 113.0 +11.4% 19.11 17.03 -10.9% 5 A->B 105.8 107.6 +1.7% 19.05 18.05 -5.2% 6 A->B 104.8 105.6 +0.8% 18.60 17.83 -4.1% 7 B->A 106.6 113.4 +6.4% 18.70 17.16 -8.2% 8 B->A 103.1 110.2 +6.9% 18.14 17.29 -4.7% 9 B->A 106.9 111.5 +4.3% 17.91 17.26 -3.6% 10 B->A 104.3 114.7 +10.0% 17.81 17.11 -3.9% 11 B->A 108.3 115.9 +7.0% 18.47 16.61 -10.1% 12 B->A 104.5 113.6 +8.7% 18.43 17.76 -3.6% </details> The gain follows how many tail experts the option actually redirects, so a smaller cache gains more. Four pairs with a 4000-slot cache instead of the automatic 8083 (pcie_e 1.83 against 1.78, about three times the traffic), two pairs each way: decode +8.8% median, ms/window -7.8% median, 4 of 4. Where it also shows up: three real agent sessions on this machine - many tool calls, 27, 49 and 102 requests - whose engine configurations differ only in --max-context. A longer context costs VRAM before --expert-cache auto sizes the cache (the RoPE tables alone are 0.26 KB per token), so the three ran with three different caches: --max-context cache slots cache VRAM K/V in pinned RAM output/wall PCIe share 262144 8548 16.21 GiB 3.09 GiB 92.7 8.7% 524288 8308 15.76 GiB 6.19 GiB 91.6 8.1% 1048576 7857 14.87 GiB 12.38 GiB 91.5 9.6% output/wall is the plugin's own tok/s (output divided by duration, so it carries the prefill) - these sessions reuse their parked conversations heavily, so read it as "did anything move", not as a decode rate. 8% fewer experts and about 10% more traffic across PCIe, and nothing moved: the medians sit inside 1.3%. The same knob on the unpatched build, with the option off, is worth -9.5% of the window for +224 slots (reserve 700 -> 250 MiB), so the sensitivity was there to lose. Those are three single runs of a real workload, not matched pairs, and no further testing was done. The speed improvement above is what this PR claims; the 1M figures are what one session did on this machine. The option's price is the margin's, not the kernel's, and this commit does not touch it. Teacher-forced log-probabilities over 7677 positions of one 7.6k-token text (technical prose, source code and Chinese), top-64, against the same text with the option off: margin argmax same KL (top-64) teacher-forced loss perplexity off - 0.000 3.2033 - 0.25 84.9% 0.235 3.2238 +0.6% 0.50 83.0% 0.293 3.4360 +7.3% 1.00 63.5% 1.001 4.7751 +49% Two runs with the option off are bit-identical, so that spread is the option and not the machine. 0.25 keeps nearly all of the waitB saving for a fraction of the distortion; the default 1.0 is not worth its price. Parity: 400 random cases - half built with many equal logits, random lo/hi, margins from -0.5 to 1.5, 1 to 8 tokens, about 60% of the experts resident, routed ids with repeats - compared bit for bit on the ids, the weights and the four counters, both kernels side by side. 0 mismatches. Tests: src/kernels/route_resident_parity.cu, registered in ctest next to the other router parity tests (TIMEOUT 120). Same case generator, against a scalar reference of the semantics - a serial walk with the same comparisons, not a copy of the kernel this branch replaced - so it tests the code in the tree. ids and the four counters must match bit for bit; the weights are a softmax ratio and the reference is a second compilation of it, so they are held to 8 ulp (worst seen: 6). A case that swaps nothing has to leave the weights untouched, which the sentinel they start from checks, and the test fails if no case swapped at all. route_resident_parity: 240 cases, 205 with swaps (2004 swaps), 0 mismatches, weights within 6 ulp --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 15:29:45 +07:00
Sign in to join this conversation.
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
Niko1221/Strata#3640
No description provided.