[PR #4302] [Engine] implement cache card logic for DeepSeek-V4.1, up to 30% faster decode #4992

Open
opened 2026-11-07 16:31:52 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/4302
Author: @Licenser
Created: 10/27/2026
Status: 🔄 Open

Base: main ← Head: cache-cards


📝 Commits (10+)

  • e67468a dsv41: decide a decode pick's lane in one function
  • 56a8127 dsv41: --expert-cache-device1 fills CUDA1 with the CPU lane's experts
  • 624a40f dsv41: CUDA1 computes its experts' part of a decode token
  • 005f7d7 dsv41: refill a CUDA1 slot whose expert the main card took
  • 31d6098 dsv41: CUDA1 swaps its coldest expert for a hotter CPU-lane one
  • 8837407 dsv41: an expert the main card evicts goes to CUDA1 when hotter
  • e228fd7 dsv41: an evicted expert takes the CUDA1 slot its swap freed
  • dbec795 dsv41: CUDA1's margin swaps every 16 tokens past a bar of its own
  • 18728c0 dsv41: count the CUDA1 serve line per request
  • d441ea0 dsv41: CUDA1 swaps at every window by default

📊 Changes

4 files changed (+482 additions, -20 deletions)

View changed files

📝 docs/DEEPSEEK41.md (+38 -0)
📝 src/dsv41/ds4_main.cpp (+12 -1)
📝 src/dsv41/ds4_model.cu (+427 -18)
📝 src/dsv41/ds4_model.hpp (+5 -1)

📄 Description

Problem and result

With two cards of different speed, strata-dsv41 can only split the layers between them. The faster card then waits for
the slower one's layers, and prompt reading gets slower. On an A40 with an RTX 2080 Ti (no peer access), the best split
(4 of 40 layers on the 2080 Ti) decoded 1.7% faster than the A40 alone and read 4K-16K prompts at 0.64-0.87x.

This PR adds --expert-cache-device1 N|auto to strata-dsv41, the Qwen engine's flag name for its helper cache. CUDA1
takes no layers. It holds the experts CUDA0 leaves to the CPU lane, the most-routed first, and computes the picks it
holds while the model writes, beside the CPU lane. On the same pair with the stock IQ2_XXS 2.00 bpw set: 7.6% faster
decode than the A40 alone, prompts read at the same speed. The flag is off by default; without it the scored output is
byte-identical to main's.

Arm (the A40 is CUDA0) Decode speed vs A40 alone Chat decode tok/s 16K prefill tok/s 4 clients tok/s
A40 alone - 36.36 859.0 23.84
A40 + 2080 Ti cache card (default) +7.6% 38.50 858.9 26.27
cache card, swaps every 16 tokens +6.7% 38.49 856.6 24.94
cache card, CUDA1's experts as loaded +2.7% 36.74 856.9 24.49
layer split, 4 layers on the 2080 Ti +1.7% 37.02 548.2 23.91
A40 alone, run again -0.3% 35.82 859.1 23.62

Adamyno tested it in this thread on 2x RTX 2080 Ti 22 GB (i9-7980XE, the same IQ2_XXS set, --max-context 32768 --dspark auto --cpu-threads 15), five 512-token runs each:

Arm (their measurement) Decode tok/s, mean vs one card CUDA1 holds
one 2080 Ti alone 16.98 - -
+ the second 2080 Ti as cache card 22.12 +30% 2,518 experts, 21.7 GB
the same, DS4_CACHE_CARD=0 (card computes none) 17.06 +0.5% the same

Prompt reading was unchanged there too. The gain grows with the second card's memory (21.7 GB of experts there
against 10.3 GB here) and with the share of experts the first card leaves to the CPU lane (about 32% on one 2080 Ti,
against an A40 that holds most of the model).

Decode speed: the 52 of 58 requests with at least 20 generated tokens in both runs, paired by order; the A40 alone's
time for the arm's tokens, sum(g_arm * t_ref / g_ref), over the arm's time, sum(t_arm), from the engine's
G generated in T ms lines. The cache card reads prompts at 0.99-1.00x of the A40 alone. Over the whole workload, where
prompt reading is 68% of the time, it took 0.989x the A40 alone's time and the split 1.133x.

None yet.

Approach

  • One place decides a decode pick's lane (lane_of, the first commit, no change in behaviour): CUDA0's VRAM, the
    RAM store's zero-copy, CUDA1, skipped, or the CPU lane, in that order. CUDA1 only takes picks that would go to the
    CPU lane.
  • Load: CUDA1 is left out of the layer placement. After CUDA0's experts are placed, the CPU lane's experts are
    ranked across all layers by their share of their layer's routing in the expert profile and copied to CUDA1 while
    they fit (--reserve-mib and 256 MiB for its buffers kept free). On the pair above with the shipped profile: 1,197
    experts, 10.27 GB, 8.2% of the routed picks by the profile, loaded in 0.9 s.
  • Decode, windows of one token: the service thread that computes the CPU lane hands CUDA1 the layer's picks it holds
    (the input copied over, a plan kernel, the existing GGUF expert kernels gg::moe_gate_up / gg::moe_down, the
    result copied back), computes the CPU's picks meanwhile, and adds CUDA1's weighted sum into the layer's response
    before publishing it. Prompt reading and windows of more than one token run as before.
  • Moves between windows, with CUDA0's expert cache:
    • a CUDA1 slot whose expert CUDA0 took is refilled with the hottest CPU-lane expert of its layer;
    • an expert CUDA0 evicts takes the CUDA1 slot that freed, or CUDA1's coldest of its layer when it is hotter;
    • at every window, a layer's coldest CUDA1 expert gives way to the layer's hottest CPU-lane expert when that one's
      heat is above the coldest's heat x 1.25 + 1.
      Copies are read from the model files into pinned memory on their own thread and copied on their own stream; at most
      8 swaps are in flight.
  • A CUDA error on CUDA1 stops the engine with a message, since its picks would otherwise be missing from the answer.
  • Logs: a start line (experts, GB, share of the picks, load time) and, after each served request, CUDA1's share of
    the answer's experts, the waits for it and the moves in the request.
  • Compatibility: opt-in. GGUF packs only, CUDA1 must not be in --gpus; the engine says so at the start. Switches
    for tests: DS4_CACHE_CARD=0 (CUDA1 loaded, unused), DS4_CACHE_CARD_FILL=0 (no moves), DS4_CARD_EVERY,
    DS4_CARD_MARGIN, DS4_CARD_BONUS (the swaps' pace and bar). docs/DEEPSEEK41.md has a section on it.

Validation

Environment: A40 (sm_86, 48 GB, PCIe 4.0 x16) + RTX 2080 Ti (sm_75, 11 GB, PCIe 3.0 x16), no peer access;
Threadripper 3960X, 184 GiB RAM; Linux, GCC 15.2, CUDA 13.4, packaging/build_dsv41_cuda.sh with ARCH="75;86-real"
(portable). Model: ISTA-DASLab/DeepSeek-V4.1-Flash-1017-GSQ-RCO-GGUF IQ2_XXS 2.00 bpw @ cb02422c (setup's pin),
--experts 5, a fresh copy of the shipped data/dsv41/profile-2.00.bin for each run. Cards set with
CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=1,0. The first line under Passed is this branch (64437ebf)
against main 46c152a1; the other lines were measured with this branch on main 5e8a4cca, against 5e8a4cca.

Passed:

  • On main 46c152a1, the A40 alone, without the flag: --score over the same 4,995 positions with
    --no-expert-cache and the CPU lane's experts read in place (--mmap-experts on), main against this branch, in
    windows of 16 and of 1: byte-identical score files.
  • Without the flag: --score over 5 x 1,000-token segments (4,995 positions) with --no-expert-cache, main against
    this branch, in windows of 16 and of 1: byte-identical score files.
  • --expert-cache-device1 auto with DS4_CACHE_CARD=0: byte-identical to the A40 alone (windows of 1).
  • CUDA1 computing, fixed placement, two runs: byte-identical to each other.
  • Against the A40 alone (tools/quant_quality.py compare --segments, fixed placement): KL 0.0169, top-1 95.30%. For
    scale, the A40 alone with its cache adapting against fixed: KL 0.0135.
  • Bench, through serve/server.py on 127.0.0.1, a fixed workload of 58 requests (chat, 4K and 16K prompts, agent turns,
    an 8K needle, four clients at once), 3 runs per test, medians, the arms in the table's order: the table above. Each
    arm's start line was checked (the A40 runs layers 0-39, CUDA1 the experts; the split 0-3 and 4-39); no failed
    request, no out-of-memory, the needle found 3/3 in every arm.
  • The cache card with the default pace: 10.3% of the answers' experts (weighted by generated tokens), 2,085 ms waited
    for it over the run, 11,033 moves: 0.51 ms waited a generated token.
  • Builds: CUDA, sm_75 + sm_86, Linux, on main 46c152a1 and on 5e8a4cca.
  • Build, on main 5e8a4cca: HIP for gfx1200 + gfx1201 (tools/dsv41/build_hip_linux.sh, targets strata-dsv41 and
    ds4_iq_test) with ROCm 7.10.0a20251120 from AMD's TheRock wheels, setup's pin. Compiled and linked. The warnings
    main's HIP build lacks are 21 of a kind it has many of, a runtime call's hipError_t left unchecked
    (-Wunused-value), all at calls this branch adds.

Failed:

  • None observed.

Not run:

  • HIP on an AMD card (built only; there is no AMD card here) and a Windows (MSVC) build. The card code uses CUDA
    streams, events, pinned memory and a copy thread.
  • Other card pairs than the two above (mine and Adamyno's), cards with peer access, more than two cards (only CUDA1
    becomes a cache card).
  • Other packs: the MXFP4 set (UD-Q8_K_XL) and the IQ1_M and IQ2_XS sets.
  • --dspark on, with and without the card.
  • On main 46c152a1: the lines with CUDA1, the bench and a HIP build.

Limits

  • Setup and the server do not know the DeepSeek cache card yet. A "gpu" list in a config is a layer split, and with
    "layer_split": "auto" the server puts the faster card last, which would make it CUDA1. Setup's
    recommend_remote_expert_opt adds --remote-expert-opt beside --expert-cache-device1 on two-GPU configs, an option
    strata-dsv41 refuses. The docs set the cards through the config's env and leave out "gpu".
  • The card speeds up decoding only: prompts are read on CUDA0 alone, at 0.99-1.00x of the A40 alone.
  • With DSpark loaded (--dspark on, or auto where the GPU holds enough of the experts), CUDA1 sits out its verify
    rounds. Not measured.
  • Measured on two machines: mine, and Adamyno's 2x 2080 Ti 22 GB in this thread.
  • No unit tests.

Used Claude.


🔄 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/4302 **Author:** [@Licenser](https://github.com/Licenser) **Created:** 10/27/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `cache-cards` --- ### 📝 Commits (10+) - [`e67468a`](https://github.com/Niko1221/Strata/commit/e67468ae72d68dc2f66571a605051c9f59bbae80) dsv41: decide a decode pick's lane in one function - [`56a8127`](https://github.com/Niko1221/Strata/commit/56a8127544067d8de4e6df068c8e8a149b37b9f8) dsv41: --expert-cache-device1 fills CUDA1 with the CPU lane's experts - [`624a40f`](https://github.com/Niko1221/Strata/commit/624a40fdb0c84ad04ee92de142e7c4343a6d08ed) dsv41: CUDA1 computes its experts' part of a decode token - [`005f7d7`](https://github.com/Niko1221/Strata/commit/005f7d7da0f8678dc240fe88b9a041099997ceb1) dsv41: refill a CUDA1 slot whose expert the main card took - [`31d6098`](https://github.com/Niko1221/Strata/commit/31d6098c1a2855da8d109c59870b3019f3dc9bb2) dsv41: CUDA1 swaps its coldest expert for a hotter CPU-lane one - [`8837407`](https://github.com/Niko1221/Strata/commit/883740701b37cf505ab35f7471fe7cf9245d1e6c) dsv41: an expert the main card evicts goes to CUDA1 when hotter - [`e228fd7`](https://github.com/Niko1221/Strata/commit/e228fd7de081ae1032c0b4d843cce65d394a0cfe) dsv41: an evicted expert takes the CUDA1 slot its swap freed - [`dbec795`](https://github.com/Niko1221/Strata/commit/dbec79546d3ea7a40d4bf43b6041f7ec844cf380) dsv41: CUDA1's margin swaps every 16 tokens past a bar of its own - [`18728c0`](https://github.com/Niko1221/Strata/commit/18728c0667caa736058d494f061e127481f09c40) dsv41: count the CUDA1 serve line per request - [`d441ea0`](https://github.com/Niko1221/Strata/commit/d441ea0b198456ad31455aa59d88f3bf59cc778d) dsv41: CUDA1 swaps at every window by default ### 📊 Changes **4 files changed** (+482 additions, -20 deletions) <details> <summary>View changed files</summary> 📝 `docs/DEEPSEEK41.md` (+38 -0) 📝 `src/dsv41/ds4_main.cpp` (+12 -1) 📝 `src/dsv41/ds4_model.cu` (+427 -18) 📝 `src/dsv41/ds4_model.hpp` (+5 -1) </details> ### 📄 Description ## Problem and result With two cards of different speed, strata-dsv41 can only split the layers between them. The faster card then waits for the slower one's layers, and prompt reading gets slower. On an A40 with an RTX 2080 Ti (no peer access), the best split (4 of 40 layers on the 2080 Ti) decoded 1.7% faster than the A40 alone and read 4K-16K prompts at 0.64-0.87x. This PR adds `--expert-cache-device1 N|auto` to strata-dsv41, the Qwen engine's flag name for its helper cache. CUDA1 takes no layers. It holds the experts CUDA0 leaves to the CPU lane, the most-routed first, and computes the picks it holds while the model writes, beside the CPU lane. On the same pair with the stock IQ2_XXS 2.00 bpw set: 7.6% faster decode than the A40 alone, prompts read at the same speed. The flag is off by default; without it the scored output is byte-identical to main's. | Arm (the A40 is CUDA0) | Decode speed vs A40 alone | Chat decode tok/s | 16K prefill tok/s | 4 clients tok/s | | ----------------------------------------- | ------------------------: | ----------------: | ----------------: | --------------: | | A40 alone | - | 36.36 | 859.0 | 23.84 | | A40 + 2080 Ti cache card (default) | +7.6% | 38.50 | 858.9 | 26.27 | | cache card, swaps every 16 tokens | +6.7% | 38.49 | 856.6 | 24.94 | | cache card, CUDA1's experts as loaded | +2.7% | 36.74 | 856.9 | 24.49 | | layer split, 4 layers on the 2080 Ti | +1.7% | 37.02 | 548.2 | 23.91 | | A40 alone, run again | -0.3% | 35.82 | 859.1 | 23.62 | Adamyno tested it in this thread on 2x RTX 2080 Ti 22 GB (i9-7980XE, the same IQ2_XXS set, `--max-context 32768 --dspark auto --cpu-threads 15`), five 512-token runs each: | Arm (their measurement) | Decode tok/s, mean | vs one card | CUDA1 holds | | ------------------------------------------------- | -----------------: | ----------: | ---------------------- | | one 2080 Ti alone | 16.98 | - | - | | + the second 2080 Ti as cache card | 22.12 | **+30%** | 2,518 experts, 21.7 GB | | the same, `DS4_CACHE_CARD=0` (card computes none) | 17.06 | +0.5% | the same | Prompt reading was unchanged there too. The gain grows with the second card's memory (21.7 GB of experts there against 10.3 GB here) and with the share of experts the first card leaves to the CPU lane (about 32% on one 2080 Ti, against an A40 that holds most of the model). Decode speed: the 52 of 58 requests with at least 20 generated tokens in both runs, paired by order; the A40 alone's time for the arm's tokens, sum(g_arm * t_ref / g_ref), over the arm's time, sum(t_arm), from the engine's `G generated in T ms` lines. The cache card reads prompts at 0.99-1.00x of the A40 alone. Over the whole workload, where prompt reading is 68% of the time, it took 0.989x the A40 alone's time and the split 1.133x. ## Related discussion None yet. ## Approach - **One place decides a decode pick's lane** (`lane_of`, the first commit, no change in behaviour): CUDA0's VRAM, the RAM store's zero-copy, CUDA1, skipped, or the CPU lane, in that order. CUDA1 only takes picks that would go to the CPU lane. - **Load:** CUDA1 is left out of the layer placement. After CUDA0's experts are placed, the CPU lane's experts are ranked across all layers by their share of their layer's routing in the expert profile and copied to CUDA1 while they fit (`--reserve-mib` and 256 MiB for its buffers kept free). On the pair above with the shipped profile: 1,197 experts, 10.27 GB, 8.2% of the routed picks by the profile, loaded in 0.9 s. - **Decode, windows of one token:** the service thread that computes the CPU lane hands CUDA1 the layer's picks it holds (the input copied over, a plan kernel, the existing GGUF expert kernels `gg::moe_gate_up` / `gg::moe_down`, the result copied back), computes the CPU's picks meanwhile, and adds CUDA1's weighted sum into the layer's response before publishing it. Prompt reading and windows of more than one token run as before. - **Moves between windows**, with CUDA0's expert cache: - a CUDA1 slot whose expert CUDA0 took is refilled with the hottest CPU-lane expert of its layer; - an expert CUDA0 evicts takes the CUDA1 slot that freed, or CUDA1's coldest of its layer when it is hotter; - at every window, a layer's coldest CUDA1 expert gives way to the layer's hottest CPU-lane expert when that one's heat is above the coldest's heat x 1.25 + 1. Copies are read from the model files into pinned memory on their own thread and copied on their own stream; at most 8 swaps are in flight. - **A CUDA error on CUDA1 stops the engine** with a message, since its picks would otherwise be missing from the answer. - **Logs:** a start line (experts, GB, share of the picks, load time) and, after each served request, CUDA1's share of the answer's experts, the waits for it and the moves in the request. - **Compatibility:** opt-in. GGUF packs only, CUDA1 must not be in `--gpus`; the engine says so at the start. Switches for tests: `DS4_CACHE_CARD=0` (CUDA1 loaded, unused), `DS4_CACHE_CARD_FILL=0` (no moves), `DS4_CARD_EVERY`, `DS4_CARD_MARGIN`, `DS4_CARD_BONUS` (the swaps' pace and bar). `docs/DEEPSEEK41.md` has a section on it. ## Validation Environment: A40 (sm_86, 48 GB, PCIe 4.0 x16) + RTX 2080 Ti (sm_75, 11 GB, PCIe 3.0 x16), no peer access; Threadripper 3960X, 184 GiB RAM; Linux, GCC 15.2, CUDA 13.4, `packaging/build_dsv41_cuda.sh` with `ARCH="75;86-real"` (portable). Model: `ISTA-DASLab/DeepSeek-V4.1-Flash-1017-GSQ-RCO-GGUF` IQ2_XXS 2.00 bpw @ cb02422c (setup's pin), `--experts 5`, a fresh copy of the shipped `data/dsv41/profile-2.00.bin` for each run. Cards set with `CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=1,0`. The first line under Passed is this branch (64437ebf) against main 46c152a1; the other lines were measured with this branch on main 5e8a4cca, against 5e8a4cca. **Passed:** - On main 46c152a1, the A40 alone, without the flag: `--score` over the same 4,995 positions with `--no-expert-cache` and the CPU lane's experts read in place (`--mmap-experts on`), main against this branch, in windows of 16 and of 1: byte-identical score files. - Without the flag: `--score` over 5 x 1,000-token segments (4,995 positions) with `--no-expert-cache`, main against this branch, in windows of 16 and of 1: byte-identical score files. - `--expert-cache-device1 auto` with `DS4_CACHE_CARD=0`: byte-identical to the A40 alone (windows of 1). - CUDA1 computing, fixed placement, two runs: byte-identical to each other. - Against the A40 alone (`tools/quant_quality.py compare --segments`, fixed placement): KL 0.0169, top-1 95.30%. For scale, the A40 alone with its cache adapting against fixed: KL 0.0135. - Bench, through `serve/server.py` on 127.0.0.1, a fixed workload of 58 requests (chat, 4K and 16K prompts, agent turns, an 8K needle, four clients at once), 3 runs per test, medians, the arms in the table's order: the table above. Each arm's start line was checked (the A40 runs layers 0-39, CUDA1 the experts; the split 0-3 and 4-39); no failed request, no out-of-memory, the needle found 3/3 in every arm. - The cache card with the default pace: 10.3% of the answers' experts (weighted by generated tokens), 2,085 ms waited for it over the run, 11,033 moves: 0.51 ms waited a generated token. - Builds: CUDA, sm_75 + sm_86, Linux, on main 46c152a1 and on 5e8a4cca. - Build, on main 5e8a4cca: HIP for gfx1200 + gfx1201 (`tools/dsv41/build_hip_linux.sh`, targets `strata-dsv41` and `ds4_iq_test`) with ROCm 7.10.0a20251120 from AMD's TheRock wheels, setup's pin. Compiled and linked. The warnings main's HIP build lacks are 21 of a kind it has many of, a runtime call's `hipError_t` left unchecked (`-Wunused-value`), all at calls this branch adds. **Failed:** - None observed. **Not run:** - HIP on an AMD card (built only; there is no AMD card here) and a Windows (MSVC) build. The card code uses CUDA streams, events, pinned memory and a copy thread. - Other card pairs than the two above (mine and Adamyno's), cards with peer access, more than two cards (only CUDA1 becomes a cache card). - Other packs: the MXFP4 set (UD-Q8_K_XL) and the IQ1_M and IQ2_XS sets. - `--dspark on`, with and without the card. - On main 46c152a1: the lines with CUDA1, the bench and a HIP build. ## Limits - Setup and the server do not know the DeepSeek cache card yet. A `"gpu"` list in a config is a layer split, and with `"layer_split": "auto"` the server puts the faster card last, which would make it CUDA1. Setup's `recommend_remote_expert_opt` adds `--remote-expert-opt` beside `--expert-cache-device1` on two-GPU configs, an option strata-dsv41 refuses. The docs set the cards through the config's `env` and leave out `"gpu"`. - The card speeds up decoding only: prompts are read on CUDA0 alone, at 0.99-1.00x of the A40 alone. - With DSpark loaded (`--dspark on`, or `auto` where the GPU holds enough of the experts), CUDA1 sits out its verify rounds. Not measured. - Measured on two machines: mine, and Adamyno's 2x 2080 Ti 22 GB in this thread. - No unit tests. Used Claude. --- <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 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#4992
No description provided.