[PR #3185] [Engine] arena: hand back the block for layers a GPU holds whole (opt-in, saves 21.7 GiB of RAM) #4494

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

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/3185
Author: @weituotian
Created: 10/19/2026
Status: 🔄 Open

Base: main ← Head: pr/arena-release


📝 Commits (8)

  • a5550e3 docs: upstream v0.2.2 measured - first token -28.4% (10/10), but its auto layer split picks K=31 and costs 5.8% decode
  • 035d13a arena-skip 1/3: load_experts_ranges takes a dst_bias
  • 34dd151 arena-skip 2/3a: the tail block's plumbing (members, set_split_layer, release_tail) - inert until open honours it
  • 0f744f3 arena-skip 2/3b+3/3: two-block load, blob_at() branch, and the release decision after host_res
  • fe7542a arena-skip: works on the GGUF path - the V100 layer block (21.71 GiB) is handed back
  • 5a97722 docs: design note for the arena release
  • b26e4d5 arena-skip: hand the block back only when the tail stage lends nothing, and make the own-buffer margin per-stage
  • 4fc7884 arena-skip: do not create the tail block in modes that move residency at run time

📊 Changes

7 files changed (+799 additions, -24 deletions)

View changed files

➕ docs/ARCH-EXPERT-MEMORY-2026-10-19.md (+477 -0)
➕ docs/UPSTREAM-0.2.2.md (+68 -0)
📝 include/strata/core/expert_source.hpp (+23 -1)
📝 include/strata/core/pinned.hpp (+4 -2)
📝 src/core/expert_source.cpp (+118 -16)
📝 src/core/pinned.cu (+6 -4)
📝 src/program/generate.cpp (+103 -1)

📄 Description

Problem and result

On a layer split, the layers a GPU stage holds whole — every one of its experts resident in that card's cache — are never read from the CPU expert arena. They are still copied into it and paid for in RAM/commit for the whole run. On this rig that is 21.71 GiB of a 46.84 GiB arena, and it is the difference between "the Windows commit limit is 117 GiB and 0.5 GiB of it is left" and 21 GiB of headroom.

After (opt-in, STRATA_ARENA_RELEASE_BLOCK=1):

strata generate: arena-skip: layers 27-47 are fully resident; the block goes back once the prompt partition is known
strata generate: arena-skip: layers 27-47 are fully resident: 21.71 GiB of the expert arena handed back to the OS
before after
experts loaded 46.84 GiB 46.84 GiB (unchanged)
Windows commit free once loaded 0.5 GiB 21.4 GiB
process commit ~64.5 GiB ~43.4 GiB
decode tok/s (10 replayed real agent requests, paired) — −1.2 %, 5 up / 5 down
file_blobs / file_mb (per request) — 0 / 0

Default off: one block, byte-identical to before.

No issue filed — this grew out of a private investigation on a 3090 + V100 rig. A related data point appeared in the thread from @ruibeikaa: on four GP100 dies where every layer is resident he frees the whole arena (STRATA_ARENA_FREE=1), engine private memory 51.86 → 5.02 GiB, 15 replies and state hashes identical, decode 56.38 → 56.34 tok/s, start-up 276 → 277 s. That is the all-resident case (one allocation, nothing to partition); this PR is the per-stage case. His guard list is what the guards below were checked against.

Approach

A second allocation at the split point, released whole. A large-page region cannot be freed in part (MEM_DECOMMIT on MEM_LARGE_PAGES fails), and the default arena is anonymous 2 MiB pages — so a released page would read back as zeros, not as a fault to be re-read. release() already exists and already acts on every VRAM-resident pair, but only for the file-backed arena (STRATA_ARENA_MMAP), where MADV_DONTNEED just means a minor fault later. The only way to keep the large pages is to make the releasable layers their own allocation: the split point is known ~1,600 lines before the arena is built (stage_of at generate.cpp:4339, arena_src.open at 4995), and the tail stage's layers are a contiguous layer range.

Released only on proof, and only after the prompt partition. The block goes back when every pair of those layers has host_res != kNotResident: a layer with all 512 experts resident has no spare slot to admit() another, so the adaptive tier can never evict one into a read. That is true when the table is built — but the serve setup may then hand that stage's prompt path a loan of its own cache slots, which evicts experts for the whole prompt and refills them after it. So the release waits for the partition and happens only when the tail stage lent nothing:

arena-skip: the tail block stays - CUDA1 lends 1027 of its cache slots to the prompt buffers, which evicts experts the block still holds

Guards. The block is not created at all when residency can move at run time: --vram-elastic, --peer-device, --adapt-async, STRATA_RAM_ADAPT / STRATA_RAM_ADAPT2, and the Foresight swap space (STRATA_FS_SLOTS). Not touched: --mmap-experts / --resident-* (own partial-residency machinery), the padded STRATA_PIN_BY_NEED layout, --shared-expert-arena, --expert-cache-device1..3 — open() falls back to one block for all of them.

Both expert sources. The pack here has no experts.bin (native_experts.txt → the GGUF shards), so open() takes the from_gguf path. That needed two changes to load_experts_gguf: a dst_bias (it writes at dst + lay.blob_offset(...), which a second allocation cannot use) and a layer range (it walked all lay.n_layers, so a block would have filled all 48 layers). load_experts_ranges got the same dst_bias. Both default to the old behaviour.

Files. include/strata/core/pinned.hpp, src/core/pinned.cu (dst_bias), include/strata/core/expert_source.hpp, src/core/expert_source.cpp (the second PinnedArena, blob_at() branch, release_tail(), close()), src/program/generate.cpp (set_split_layer behind the switch, the residency proof, the deferred release, the guards, STRATA_SPLIT_OWN_MARGIN_MIB).

Validation

Passed:

Build — revision 4fc78847 (branch pr/arena-release, cut from 46c152a1 = v0.2.7.1), Windows 11, MSVC 19.4x, CUDA 12.x, -DCMAKE_CUDA_ARCHITECTURES="70;86" (sm_70 for the V100, sm_86 for the 3090):

D:\strata-blackbox\build_v100_cuda12.cmd      ->  === build exit=0 ===
build-cuda12\strata.exe                       ->  231.4 MB, sm_70 + sm_86 cubins (152 at the cubdump)

Start, 3090 + V100, --layer-split 27, --max-context 262144, --kv int8 --kv-resident 32768:

strata generate: layer split: CUDA1 runs layers 27-47, expert cache 10752 slots (21.70 GiB), 10752 of its 10752 profiled pairs
strata generate: session is up (engine 0.1.53)
strata generate: arena-skip: layers 27-47 are fully resident: 21.71 GiB of the expert arena handed back to the OS
-> FreeVirtualMemory 0.5 -> 21.4 GiB; `experts loaded` still 46.84 GiB

Ten replayed real agent requests (replay.py --suite 56:6,0:4, recorded traffic, ~200K-token contexts, paired per request against the same 10 without the switch): decode median −1.2 % (5 up / 5 down), VRAM hit 96.70 % vs 96.15 %, CPU pool 3.10 % vs 3.55 %, file_blobs/file_mb 0 on every request, zero could not produce a blob, engine log free of errors.

The borrow case — K=26 with --vram-reserve-later-mib 0, where the V100 lends 1,027 slots:

arena-skip: layers 26-47 are fully resident; the block goes back once the prompt partition is known
arena-skip: the tail block stays - CUDA1 lends 1027 of its cache slots to the prompt buffers, ...
-> a request is served normally (before this guard it was `prefill: expert source has no blob`, exit 1)

The runtime-residency guard — same rig with --vram-elastic:

arena-skip: one block - the residency can move at run time (--vram-elastic, a peer expert cache, --adapt-async, the Foresight swap space or STRATA_RAM_ADAPT), so a released layer could still be read
-> one arena, no release, normal start

Failed:

prefill: expert source has no blob / engine exit 1 / restart loop — observed in an earlier revision of this branch when the release ran before the prompt partition (K=26). Fixed by deferring the release; the case above is the regression test. No other failure observed.

Not run:

Linux, HIP, SYCL and Metal builds (this tree has no pinned.dp.cpp); the AMD/Linux path of load_experts_ranges with a non-zero dst_bias; a single-GPU run; STRATA_ARENA_MMAP and --shared-expert-arena (both keep one block, unexercised with the switch on); the router look-ahead with a released block — in this tree it warms CUDA0's arena and the block is a later stage, but I could not prove it never reaches a later stage, so it is the one guard I did not add.

Limits

  • Opt-in and off by default; it saves RAM/commit, not time. It cannot help unless some stage holds every expert of its layers, and that depends on the split: on this rig --layer-split auto picks K=31 (V100 gets 17 layers, 8,704 slots) where the condition still holds for the V100 but the split is 5.8 % slower than K=27 for unrelated reasons; at K=26 the V100 has 22 layers, still 100 % covered, but decode is flat and first token 32 % worse. K=27 is the measured optimum here.
  • The set is a snapshot (host_res at start plus the prompt partition); the guards cover the modes I know can move residency afterwards. A future mode that evicts outside that list would make a released block readable — the failure is loud (nullptr → the expert source could not produce a blob), not silent, which is the property this design relies on.
  • STRATA_SPLIT_OWN_MARGIN_MIB (default 1,536 MiB, the pre-existing slack) is only there so one stage can keep its prompt buffers when it is just short of that margin; --no-prefill-borrow does the same for every stage and costs the other card's cache (measured: 8,709 → 7,819 slots, decode −9 %).

🔄 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/3185 **Author:** [@weituotian](https://github.com/weituotian) **Created:** 10/19/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `pr/arena-release` --- ### 📝 Commits (8) - [`a5550e3`](https://github.com/Niko1221/Strata/commit/a5550e3dbb30542ce788e4b789a46b61dae8c6cb) docs: upstream v0.2.2 measured - first token -28.4% (10/10), but its auto layer split picks K=31 and costs 5.8% decode - [`035d13a`](https://github.com/Niko1221/Strata/commit/035d13ab76a16eadedabf4e9db474cbd5c0e6ae3) arena-skip 1/3: load_experts_ranges takes a dst_bias - [`34dd151`](https://github.com/Niko1221/Strata/commit/34dd1513ed28e1c16501241222faf9cd62e9f82b) arena-skip 2/3a: the tail block's plumbing (members, set_split_layer, release_tail) - inert until open honours it - [`0f744f3`](https://github.com/Niko1221/Strata/commit/0f744f301fde883e0dc06f253b0a35c409924fc9) arena-skip 2/3b+3/3: two-block load, blob_at() branch, and the release decision after host_res - [`fe7542a`](https://github.com/Niko1221/Strata/commit/fe7542ae486740f32b650f9396fdc952342606ce) arena-skip: works on the GGUF path - the V100 layer block (21.71 GiB) is handed back - [`5a97722`](https://github.com/Niko1221/Strata/commit/5a977220ffbe1ef51aaf525244cdf4c853dfe84a) docs: design note for the arena release - [`b26e4d5`](https://github.com/Niko1221/Strata/commit/b26e4d575edf63461191203c355442fa0c72fc77) arena-skip: hand the block back only when the tail stage lends nothing, and make the own-buffer margin per-stage - [`4fc7884`](https://github.com/Niko1221/Strata/commit/4fc788474c4fb33cb136e761a6bbd0fba2bb078f) arena-skip: do not create the tail block in modes that move residency at run time ### 📊 Changes **7 files changed** (+799 additions, -24 deletions) <details> <summary>View changed files</summary> ➕ `docs/ARCH-EXPERT-MEMORY-2026-10-19.md` (+477 -0) ➕ `docs/UPSTREAM-0.2.2.md` (+68 -0) 📝 `include/strata/core/expert_source.hpp` (+23 -1) 📝 `include/strata/core/pinned.hpp` (+4 -2) 📝 `src/core/expert_source.cpp` (+118 -16) 📝 `src/core/pinned.cu` (+6 -4) 📝 `src/program/generate.cpp` (+103 -1) </details> ### 📄 Description ## Problem and result On a **layer split**, the layers a GPU stage holds *whole* — every one of its experts resident in that card's cache — are never read from the CPU expert arena. They are still copied into it and paid for in RAM/commit for the whole run. On this rig that is 21.71 GiB of a 46.84 GiB arena, and it is the difference between "the Windows commit limit is 117 GiB and 0.5 GiB of it is left" and 21 GiB of headroom. After (opt-in, `STRATA_ARENA_RELEASE_BLOCK=1`): ``` strata generate: arena-skip: layers 27-47 are fully resident; the block goes back once the prompt partition is known strata generate: arena-skip: layers 27-47 are fully resident: 21.71 GiB of the expert arena handed back to the OS ``` | | before | after | |---|---:|---:| | `experts loaded` | 46.84 GiB | 46.84 GiB (unchanged) | | **Windows commit free once loaded** | **0.5 GiB** | **21.4 GiB** | | process commit | ~64.5 GiB | **~43.4 GiB** | | decode tok/s (10 replayed real agent requests, paired) | — | **−1.2 %, 5 up / 5 down** | | `file_blobs` / `file_mb` (per request) | — | **0 / 0** | Default off: one block, byte-identical to before. ## Related discussion No issue filed — this grew out of a private investigation on a 3090 + V100 rig. A related data point appeared in the thread from @ruibeikaa: on four GP100 dies where *every* layer is resident he frees the whole arena (`STRATA_ARENA_FREE=1`), engine private memory 51.86 → 5.02 GiB, 15 replies and state hashes identical, decode 56.38 → 56.34 tok/s, start-up 276 → 277 s. That is the all-resident case (one allocation, nothing to partition); this PR is the per-stage case. His guard list is what the guards below were checked against. ## Approach **A second allocation at the split point, released whole.** A large-page region cannot be freed in part (`MEM_DECOMMIT` on `MEM_LARGE_PAGES` fails), and the default arena is anonymous 2 MiB pages — so a released page would read back as **zeros**, not as a fault to be re-read. `release()` already exists and already acts on every VRAM-resident pair, but only for the *file-backed* arena (`STRATA_ARENA_MMAP`), where `MADV_DONTNEED` just means a minor fault later. The only way to keep the large pages is to make the releasable layers their own allocation: the split point is known ~1,600 lines before the arena is built (`stage_of` at `generate.cpp:4339`, `arena_src.open` at 4995), and the tail stage's layers are a contiguous layer range. **Released only on proof, and only after the prompt partition.** The block goes back when every pair of those layers has `host_res != kNotResident`: a layer with all 512 experts resident has no spare slot to `admit()` another, so the adaptive tier can never evict one into a read. That is true when the table is built — but the serve setup may then hand that stage's prompt path a **loan** of its own cache slots, which evicts experts for the whole prompt and refills them after it. So the release waits for the partition and happens only when the tail stage lent nothing: ``` arena-skip: the tail block stays - CUDA1 lends 1027 of its cache slots to the prompt buffers, which evicts experts the block still holds ``` **Guards.** The block is not created at all when residency can move at run time: `--vram-elastic`, `--peer-device`, `--adapt-async`, `STRATA_RAM_ADAPT` / `STRATA_RAM_ADAPT2`, and the Foresight swap space (`STRATA_FS_SLOTS`). Not touched: `--mmap-experts` / `--resident-*` (own partial-residency machinery), the padded `STRATA_PIN_BY_NEED` layout, `--shared-expert-arena`, `--expert-cache-device1..3` — `open()` falls back to one block for all of them. **Both expert sources.** The pack here has no `experts.bin` (`native_experts.txt` → the GGUF shards), so `open()` takes the `from_gguf` path. That needed two changes to `load_experts_gguf`: a `dst_bias` (it writes at `dst + lay.blob_offset(...)`, which a second allocation cannot use) and a **layer range** (it walked all `lay.n_layers`, so a block would have filled all 48 layers). `load_experts_ranges` got the same `dst_bias`. Both default to the old behaviour. **Files.** `include/strata/core/pinned.hpp`, `src/core/pinned.cu` (dst_bias), `include/strata/core/expert_source.hpp`, `src/core/expert_source.cpp` (the second `PinnedArena`, `blob_at()` branch, `release_tail()`, `close()`), `src/program/generate.cpp` (`set_split_layer` behind the switch, the residency proof, the deferred release, the guards, `STRATA_SPLIT_OWN_MARGIN_MIB`). ## Validation **Passed:** *Build* — revision `4fc78847` (branch `pr/arena-release`, cut from `46c152a1` = v0.2.7.1), Windows 11, MSVC 19.4x, CUDA 12.x, `-DCMAKE_CUDA_ARCHITECTURES="70;86"` (sm_70 for the V100, sm_86 for the 3090): ``` D:\strata-blackbox\build_v100_cuda12.cmd -> === build exit=0 === build-cuda12\strata.exe -> 231.4 MB, sm_70 + sm_86 cubins (152 at the cubdump) ``` *Start, 3090 + V100, `--layer-split 27`, `--max-context 262144`, `--kv int8 --kv-resident 32768`*: ``` strata generate: layer split: CUDA1 runs layers 27-47, expert cache 10752 slots (21.70 GiB), 10752 of its 10752 profiled pairs strata generate: session is up (engine 0.1.53) strata generate: arena-skip: layers 27-47 are fully resident: 21.71 GiB of the expert arena handed back to the OS -> FreeVirtualMemory 0.5 -> 21.4 GiB; `experts loaded` still 46.84 GiB ``` *Ten replayed real agent requests* (`replay.py --suite 56:6,0:4`, recorded traffic, ~200K-token contexts, paired per request against the same 10 without the switch): decode median **−1.2 % (5 up / 5 down)**, VRAM hit 96.70 % vs 96.15 %, CPU pool 3.10 % vs 3.55 %, `file_blobs`/`file_mb` **0** on every request, zero `could not produce a blob`, engine log free of errors. *The borrow case* — K=26 with `--vram-reserve-later-mib 0`, where the V100 lends 1,027 slots: ``` arena-skip: layers 26-47 are fully resident; the block goes back once the prompt partition is known arena-skip: the tail block stays - CUDA1 lends 1027 of its cache slots to the prompt buffers, ... -> a request is served normally (before this guard it was `prefill: expert source has no blob`, exit 1) ``` *The runtime-residency guard* — same rig with `--vram-elastic`: ``` arena-skip: one block - the residency can move at run time (--vram-elastic, a peer expert cache, --adapt-async, the Foresight swap space or STRATA_RAM_ADAPT), so a released layer could still be read -> one arena, no release, normal start ``` **Failed:** `prefill: expert source has no blob` / engine exit 1 / restart loop — observed in an earlier revision of this branch when the release ran before the prompt partition (K=26). Fixed by deferring the release; the case above is the regression test. No other failure observed. **Not run:** Linux, HIP, SYCL and Metal builds (this tree has no `pinned.dp.cpp`); the AMD/Linux path of `load_experts_ranges` with a non-zero `dst_bias`; a single-GPU run; `STRATA_ARENA_MMAP` and `--shared-expert-arena` (both keep one block, unexercised with the switch on); the router look-ahead with a released block — in this tree it warms CUDA0's arena and the block is a later stage, but I could not prove it never reaches a later stage, so it is the one guard I did not add. ## Limits - Opt-in and off by default; it saves RAM/commit, not time. It cannot help unless some stage holds **every** expert of its layers, and that depends on the split: on this rig `--layer-split auto` picks K=31 (V100 gets 17 layers, 8,704 slots) where the condition still holds for the V100 but the split is 5.8 % slower than K=27 for unrelated reasons; at K=26 the V100 has 22 layers, still 100 % covered, but decode is flat and first token 32 % worse. K=27 is the measured optimum here. - The set is a snapshot (`host_res` at start plus the prompt partition); the guards cover the modes I know can move residency afterwards. A future mode that evicts outside that list would make a released block readable — the failure is loud (nullptr → `the expert source could not produce a blob`), not silent, which is the property this design relies on. - `STRATA_SPLIT_OWN_MARGIN_MIB` (default 1,536 MiB, the pre-existing slack) is only there so one stage can keep its prompt buffers when it is just short of that margin; `--no-prefill-borrow` does the same for every stage and costs the other card's cache (measured: 8,709 → 7,819 slots, decode −9 %). --- <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#4494
No description provided.