[PR #2963] [Engine] resident RAM mode: the RAM copy is page-locked in slices when the one page-locked allocation is refused (32K prompt 10.5 -> 6.6 s, decode -13% a round) #4364

Open
opened 2026-11-07 15:35:11 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/2963
Author: @sergqwer
Created: 10/17/2026
Status: 🔄 Open

Base: main ← Head: pr/resident-pin-slices


📝 Commits (1)

  • df2c2a9 resident RAM mode: the RAM copy is page-locked in slices when the one page-locked allocation is refused

📊 Changes

5 files changed (+266 additions, -42 deletions)

View changed files

📝 docs/DETAILS.md (+7 -0)
📝 include/strata/core/expert_source.hpp (+12 -3)
📝 include/strata/core/pinned.hpp (+6 -0)
📝 src/core/expert_source.cpp (+211 -26)
📝 src/core/pinned.cu (+30 -13)

📄 Description

Problem and result

With --resident-experts (and --resident-budget-gib, which setup writes for the large Unsloth quants), the experts the GPU does not hold are copied into RAM, page-locked by one cudaHostAlloc. When the driver refuses that one allocation, the copy is malloc'd and only locked in the working set. A Q8_0 pack's ~100 GiB copy on a 128 GB Windows PC is such a case: WDDM caps page-locked memory at the GPU's shared-memory budget. What the refusal costs today, in pin_cache_complement:

  • pinned() is false for every expert and pcie_layer() is false, so the PCIe share (--pcie-frac) fetches nothing and every decode miss goes to the CPU;
  • the prompt path copies every expert through the stager's host buffers instead of by DMA from the copy.

After: the same bytes are registered slice by slice, and the experts in them are DMA'd and read over PCIe again. Forced on this PC: a fresh 32K prompt 10.5 -> 6.6 s, decode -13% a round (18.8 -> 16.4 ms), start 22.5 -> 16.9 s; that is path 1's speed. Nothing changes when the one allocation is granted.

None beyond this PR. It was #2963 before the 0.2.0-beta tracker reset, unchanged except for the rebase. No default changes where the allocation is granted (the identity runs below); where it is refused, the new path is the fallback.

Approach

Three paths, each named in the log (page-locking path 1/2/3 ... X of Y GiB pinned):

  • Path 1: the one cudaHostAlloc, as before.
  • Path 2 (new): the allocation was refused, or has no device alias. The copy is VirtualAlloc'd (posix_memalign elsewhere) and registered with cudaHostRegister(Portable | Mapped) slice by slice, before anything writes it. Each slice is about one layer of the pack, cut at expert starts (page-aligned where possible), with its own device alias. Registration stops at the first refusal. pinned() and device_alias() look the slice up; an expert no slice holds whole is the CPU's. pcie_layer() is true. By default the slices stop below the GPU adapter's shared-memory budget, less what the process uses there and a margin (on a layer split, the smallest room of the devices): pinned until the driver refused a slice, the copy filled that WDDM segment and every GPU phase of the prompt path got slower (numbers in the code comment). STRATA_COMPLEMENT_PIN_GIB: all = slices until the driver refuses one, N = at most N GiB, 0 = path 3.
  • Path 3: no slice pinned. Pageable and locked, as before. The STRATA_PARTIAL_PIN=1 budget prefix keeps its own path.
  • pinned.cu: #243's private sliced_pin_limit becomes the public shared_pin_limit_for(dev); the arena's STRATA_ARENA_PIN_GIB=auto calls it for the current device, as before. STRATA_PIN_MARGIN_GIB sets the margin (default 4 GiB, #243's value).
  • STRATA_TEST_COMPLEMENT_REFUSE=1 takes the one allocation as refused, so paths 2 and 3 can be tested on a PC whose driver grants it.
  • docs/DETAILS.md: one bullet in the resident-mode section.

Validation

Revision: this branch, one commit df2c2a92 on main e88ee39d (v0.2.0-beta + 12 commits), against a build of main e88ee39d with the same configure. RTX 5090, Ryzen 9 9950X3D, 128 GB, Windows 11; MSVC 2022 17.14, CUDA 13.3, release configure (-DSTRATA_PORTABLE=ON, 75-real;86-real;89-real;120-real, ggml at the pinned 3cf03257). Model: the NVFP4 GPTQ pack (Maximilian228/Qwen3.8-Flash-Next-NVFP4-GPTQ-Strata, upstream setup's --family nvfp4 --model NVFP4 files).

Passed:

  • Identity. strata --serve with setup's model files plus --expert-cache 3000 --prefill auto --spec 6 --spec-min-p 0.7 --max-context 262144 --kv int8 --gpu 0 --pcie-frac 0 --adapt-every 1000000, env STRATA_PREFILL_CPU_SHARE=0 STRATA_PCIE_BALANCE=0 STRATA_IQ_MT_MIN=1 STRATA_DUMP_FIRST_LOGITS=<file>; a 2,100-token and a 32,037-token prompt, 32 greedy tokens each. First-token logits and tokens:

    run 2K 32K
    main, default mode 1e9c612c f5b2872c
    this branch, default mode identical identical
    main, --resident-experts (58.97 GiB copy, one cudaHostAlloc) identical to main default identical
    this branch, --resident-experts, path 1 (58.97 of 58.97 GiB pinned) identical identical
    this branch, path 2 (STRATA_TEST_COMPLEMENT_REFUSE=1: 45 of 45 slices, 58.97 GiB, 22,903 of 22,903 experts, in 4.4 s) identical identical
    this branch, path 3 (also STRATA_COMPLEMENT_PIN_GIB=0: nothing pinned, 60,389 MiB locked) identical identical

    (resident runs with STRATA_RESIDENT_HEADROOM_GIB=10)

  • Speed. --resident-experts --expert-cache auto --vram-reserve-mib 1500 --prefill auto --spec 6 --spec-min-p 0.7 --kv int8 --max-context 262144, STRATA_RESIDENT_HEADROOM_GIB=10. This PC grants the allocation, so paths 2 and 3 are forced with STRATA_TEST_COMPLEMENT_REFUSE=1. 3 starts per path, interleaved (p2 p3 p1 p3 p1 p2 p1 p2 p3). Each start: 3 decodes of up to 600 tokens after a 45-token prompt, then two fresh 32K prompts. Mean ± sd over the starts:

    path 3 (today's fallback) path 2 (this PR) path 1 (allocation granted)
    32K prompt 10.51 ± 0.68 s 6.56 ± 0.13 s 6.56 ± 0.10 s
    decode, ms a round 18.8 ± 0.4 16.4 ± 0.8 16.6 ± 0.5
    decode, tok/s 129.9 ± 2.4 146.9 ± 4.8 145.7 ± 3.3
    ready (process start to engine ready) 22.5 ± 0.3 s 16.9 ± 0.2 s 17.1 ± 0.4 s

    The cache got 7,136-7,210 slots and the RAM copy was 48.13-48.26 GiB. Path 2 registered 37 of 37 slices (every expert of the copy) in 3.3 s. Path 3's ready time includes locking 48 GiB in the working set. "ms a round" = decode time / (tokens - accepted drafts).

Failed: none observed.

Not run:

  • A real refusal on this revision (no Q8_0 pack on this PC now). Earlier, on our fork's code with a Q8_0 pack where the driver really refuses the allocation (a ~100 GiB copy, same PC): decode 48.8 -> 53.3-55.0 tok/s, 32K prompt 9.9 -> 8.1-8.5 s.
  • Linux: path 2 registers the same way there (posix_memalign); not run.
  • HIP and Metal builds: both compile expert_source.cpp and pinned.cu; no ROCm or Mac here. SYCL keeps its own expert_source (unchanged).
  • A layer split with the resident mode.

Limits

  • Path 2 registers with cudaHostRegister directly, as path 1's cudaHostAlloc does; 0.2.0's pinned-memory test knob (STRATA_TEST_PIN_BUDGET_MIB) does not count either of them.
  • A full card makes any path slow: on 477ac13c (0.1.45) a batch without --vram-reserve-mib (the cache filled the card, ~100 MiB free) had one path-2 run at 22 s for the 32K prompt, with the engine's own "only N MiB of VRAM free" warning.

🤖 Generated with Claude 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/Niko1221/Strata/pull/2963 **Author:** [@sergqwer](https://github.com/sergqwer) **Created:** 10/17/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `pr/resident-pin-slices` --- ### 📝 Commits (1) - [`df2c2a9`](https://github.com/Niko1221/Strata/commit/df2c2a920c223ca246d4ceb95673893f5cef75d4) resident RAM mode: the RAM copy is page-locked in slices when the one page-locked allocation is refused ### 📊 Changes **5 files changed** (+266 additions, -42 deletions) <details> <summary>View changed files</summary> 📝 `docs/DETAILS.md` (+7 -0) 📝 `include/strata/core/expert_source.hpp` (+12 -3) 📝 `include/strata/core/pinned.hpp` (+6 -0) 📝 `src/core/expert_source.cpp` (+211 -26) 📝 `src/core/pinned.cu` (+30 -13) </details> ### 📄 Description ## Problem and result With `--resident-experts` (and `--resident-budget-gib`, which setup writes for the large Unsloth quants), the experts the GPU does not hold are copied into RAM, page-locked by one `cudaHostAlloc`. When the driver refuses that one allocation, the copy is malloc'd and only locked in the working set. A Q8_0 pack's ~100 GiB copy on a 128 GB Windows PC is such a case: WDDM caps page-locked memory at the GPU's shared-memory budget. What the refusal costs today, in `pin_cache_complement`: - `pinned()` is false for every expert and `pcie_layer()` is false, so the PCIe share (`--pcie-frac`) fetches nothing and every decode miss goes to the CPU; - the prompt path copies every expert through the stager's host buffers instead of by DMA from the copy. After: the same bytes are registered slice by slice, and the experts in them are DMA'd and read over PCIe again. Forced on this PC: a fresh 32K prompt 10.5 -> 6.6 s, decode -13% a round (18.8 -> 16.4 ms), start 22.5 -> 16.9 s; that is path 1's speed. Nothing changes when the one allocation is granted. ## Related discussion None beyond this PR. It was #2963 before the 0.2.0-beta tracker reset, unchanged except for the rebase. No default changes where the allocation is granted (the identity runs below); where it is refused, the new path is the fallback. ## Approach Three paths, each named in the log (`page-locking path 1/2/3 ... X of Y GiB pinned`): - **Path 1:** the one `cudaHostAlloc`, as before. - **Path 2** (new): the allocation was refused, or has no device alias. The copy is `VirtualAlloc`'d (`posix_memalign` elsewhere) and registered with `cudaHostRegister(Portable | Mapped)` slice by slice, before anything writes it. Each slice is about one layer of the pack, cut at expert starts (page-aligned where possible), with its own device alias. Registration stops at the first refusal. `pinned()` and `device_alias()` look the slice up; an expert no slice holds whole is the CPU's. `pcie_layer()` is true. By default the slices stop below the GPU adapter's shared-memory budget, less what the process uses there and a margin (on a layer split, the smallest room of the devices): pinned until the driver refused a slice, the copy filled that WDDM segment and every GPU phase of the prompt path got slower (numbers in the code comment). `STRATA_COMPLEMENT_PIN_GIB`: `all` = slices until the driver refuses one, `N` = at most N GiB, `0` = path 3. - **Path 3:** no slice pinned. Pageable and locked, as before. The `STRATA_PARTIAL_PIN=1` budget prefix keeps its own path. - `pinned.cu`: #243's private `sliced_pin_limit` becomes the public `shared_pin_limit_for(dev)`; the arena's `STRATA_ARENA_PIN_GIB=auto` calls it for the current device, as before. `STRATA_PIN_MARGIN_GIB` sets the margin (default 4 GiB, #243's value). - `STRATA_TEST_COMPLEMENT_REFUSE=1` takes the one allocation as refused, so paths 2 and 3 can be tested on a PC whose driver grants it. - `docs/DETAILS.md`: one bullet in the resident-mode section. ## Validation Revision: this branch, one commit df2c2a92 on `main` **e88ee39d** (v0.2.0-beta + 12 commits), against a build of `main` e88ee39d with the same configure. RTX 5090, Ryzen 9 9950X3D, 128 GB, Windows 11; MSVC 2022 17.14, CUDA 13.3, release configure (`-DSTRATA_PORTABLE=ON`, `75-real;86-real;89-real;120-real`, ggml at the pinned 3cf03257). Model: the NVFP4 GPTQ pack (`Maximilian228/Qwen3.8-Flash-Next-NVFP4-GPTQ-Strata`, upstream setup's `--family nvfp4 --model NVFP4` files). **Passed:** - **Identity.** `strata --serve` with setup's model files plus `--expert-cache 3000 --prefill auto --spec 6 --spec-min-p 0.7 --max-context 262144 --kv int8 --gpu 0 --pcie-frac 0 --adapt-every 1000000`, env `STRATA_PREFILL_CPU_SHARE=0 STRATA_PCIE_BALANCE=0 STRATA_IQ_MT_MIN=1 STRATA_DUMP_FIRST_LOGITS=<file>`; a 2,100-token and a 32,037-token prompt, 32 greedy tokens each. First-token logits and tokens: | run | 2K | 32K | |---|---|---| | `main`, default mode | `1e9c612c` | `f5b2872c` | | this branch, default mode | identical | identical | | `main`, `--resident-experts` (58.97 GiB copy, one `cudaHostAlloc`) | identical to `main` default | identical | | this branch, `--resident-experts`, path 1 (58.97 of 58.97 GiB pinned) | identical | identical | | this branch, path 2 (`STRATA_TEST_COMPLEMENT_REFUSE=1`: 45 of 45 slices, 58.97 GiB, 22,903 of 22,903 experts, in 4.4 s) | identical | identical | | this branch, path 3 (also `STRATA_COMPLEMENT_PIN_GIB=0`: nothing pinned, 60,389 MiB locked) | identical | identical | (resident runs with `STRATA_RESIDENT_HEADROOM_GIB=10`) - **Speed.** `--resident-experts --expert-cache auto --vram-reserve-mib 1500 --prefill auto --spec 6 --spec-min-p 0.7 --kv int8 --max-context 262144`, `STRATA_RESIDENT_HEADROOM_GIB=10`. This PC grants the allocation, so paths 2 and 3 are forced with `STRATA_TEST_COMPLEMENT_REFUSE=1`. 3 starts per path, interleaved (p2 p3 p1 p3 p1 p2 p1 p2 p3). Each start: 3 decodes of up to 600 tokens after a 45-token prompt, then two fresh 32K prompts. Mean ± sd over the starts: | | path 3 (today's fallback) | path 2 (this PR) | path 1 (allocation granted) | |---|---|---|---| | 32K prompt | 10.51 ± 0.68 s | **6.56 ± 0.13 s** | 6.56 ± 0.10 s | | decode, ms a round | 18.8 ± 0.4 | **16.4 ± 0.8** | 16.6 ± 0.5 | | decode, tok/s | 129.9 ± 2.4 | **146.9 ± 4.8** | 145.7 ± 3.3 | | ready (process start to engine ready) | 22.5 ± 0.3 s | **16.9 ± 0.2 s** | 17.1 ± 0.4 s | The cache got 7,136-7,210 slots and the RAM copy was 48.13-48.26 GiB. Path 2 registered 37 of 37 slices (every expert of the copy) in 3.3 s. Path 3's ready time includes locking 48 GiB in the working set. "ms a round" = decode time / (tokens - accepted drafts). **Failed:** none observed. **Not run:** - A real refusal on this revision (no Q8_0 pack on this PC now). Earlier, on our fork's code with a Q8_0 pack where the driver really refuses the allocation (a ~100 GiB copy, same PC): decode 48.8 -> 53.3-55.0 tok/s, 32K prompt 9.9 -> 8.1-8.5 s. - Linux: path 2 registers the same way there (`posix_memalign`); not run. - HIP and Metal builds: both compile `expert_source.cpp` and `pinned.cu`; no ROCm or Mac here. SYCL keeps its own `expert_source` (unchanged). - A layer split with the resident mode. ## Limits - Path 2 registers with `cudaHostRegister` directly, as path 1's `cudaHostAlloc` does; 0.2.0's pinned-memory test knob (`STRATA_TEST_PIN_BUDGET_MIB`) does not count either of them. - A full card makes any path slow: on 477ac13c (0.1.45) a batch without `--vram-reserve-mib` (the cache filled the card, ~100 MiB free) had one path-2 run at 22 s for the 32K prompt, with the engine's own "only N MiB of VRAM free" warning. 🤖 Generated with [Claude Code](https://claude.com/claude-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 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#4364
No description provided.