[PR #3536] [Engine] metal: the session no longer holds unused QSA K/V pools (6.4 GiB resident at 262K context) #4673

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

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/3536
Author: @oceanio
Created: 10/21/2026
Status: 🔄 Open

Base: main ← Head: pr/metal-session-kv-solo


📝 Commits (2)

  • 98efff3 metal: the session's QSA states carry no K/V pools or pooled indexer keys (the decode window keeps its own)
  • 036d07a docs (Mac): the engine's own memory grows with --max-context; what it was before the session K/V change

📊 Changes

2 files changed (+21 additions, -0 deletions)

View changed files

📝 docs/MAC.md (+1 -0)
📝 src/core/layer.cpp (+20 -0)

📄 Description

Problem and result

On the Mac (Metal) engine the decode window (metal/src/engine/window.cpp) keeps its own K/V and pooled indexer keys for the 12 QSA layers, sized by --max-context. It never reads or writes the session's QSA K/V pools, but kv_plan still sizes them for the whole context. On Metal a session buffer is one shared MTLBuffer in the residency set, so those pools are resident in full whether anything touches them or not: about 24.6 KB per context token.

Measured first on main 0.2.1.4 (phys_footprint of the engine 5 s after it is loaded, Qwen3.8-Flash-Next IQ3_S, M5 Max):

--max-context main this PR saved
262144 16.99 / 16.98 GiB 10.62 / 10.61 GiB 6.37 GiB
65536 9.27 / 9.27 GiB 7.68 / 7.68 GiB 1.59 GiB

The same 6.4 GiB is still saved after 9 requests (24.91 → 18.53 GiB at 262K). Output and speed do not change (Validation).

Since v0.2.9 each Metal batch slot has its own session, and the saving applies to each: with 2 slots at 262K the engine holds 18.15 GiB instead of 37.28. On v0.2.10 the numbers are the same (Validation).

None. This is a focused fix. Context: since v0.2.10 the Metal engine parks conversations, through session QSA states that the window binds to its own K/V (Window::bind_session / bind_draft), so they need no pools of their own. Parking works the same with this PR. Session files fail on v0.2.10 on a Mac for two other reasons, with or without this PR; #3541 fixes those, and with it on top they work with this PR too (Validation).

Approach

  • src/core/layer.cpp, kv_plan: under #if defined(STRATA_GPU_METAL) a QSA state gets 0 K/V slots and 0 pooled indexer rows. qsa_state_init then leaves the pool pointers and idx_pooled null, so they do not alias the page table or block_pos that follow. The page table, the indexer tail and dead rows, block_pos and the RoPE tables are carved as before.
  • The window points the session's QSA states at its own K/V when it binds them (Window::bind_session / bind_draft, v0.2.10), so parking and session files read the window's K/V. Until a state is bound, n_slots == 0 makes a conversation snapshot refuse it (conversation_snapshot.cpp valid) instead of reading pools that are not there.
  • docs/MAC.md: one line saying that the engine's own memory grows with --max-context, with the numbers before and after.
  • Non-Metal builds: unchanged. unifdef -USTRATA_GPU_METAL src/core/layer.cpp gives the same file before and after.
  • No flag or setting: the pools were never used on Metal, so there is nothing to opt into.

Validation

Re-run on v0.2.10 (2026-11-03). Rebased onto main ffa80d73 (v0.2.10). The code lines are the same; the kv_plan comment now names the window's binding (Approach). At the new tip 036d07ac, one round per setting against v0.2.10's own build, the same procedure as below:

--max-context v0.2.10 this PR
262144 16.99 GiB 10.61 GiB
65536 9.28 GiB 7.68 GiB
262144, 2 batch slots ("parallel": 2) 37.28 GiB 18.15 GiB
  • Greedy output: 8 of 8 requests identical at both contexts (the 9th carries a per-run tag); with 2 slots, 10 of 10.
  • Decode of the 4 short prompts (tok/s): at 262K, v0.2.10 97.6 93.0 91.2 94.5 and this PR 97.5 96.4 96.2 99.3; at 64K, 98.4 98.6 96.4 99.4 and 98.3 98.5 96.1 99.4; with 2 slots, 98.4 98.4 95.9 99.3 and 98.2 98.3 96.3 99.4. (One round.)
  • Parking (tools/parking_test.py with #3541's log-path fix, 28,692 tokens, with MTP): the follow-up after A, B, A is identical to the one that never parked (95/95), as on v0.2.10.
  • Session files: with #3541's two commits on top, session_restart_test.py at 3,785 and 28,785 tokens, with and without MTP: restored in a new engine, follow-up identical, the whole conversation reused. Without them they fail as on v0.2.10.
  • Metal build 0.1.56-metal: 0 errors, the same 16 warnings as main.
  • unifdef -USTRATA_GPU_METAL src/core/layer.cpp: identical to v0.2.10's.

Re-run on v0.2.9 (2026-11-01). Rebased onto main c62d5e3a (v0.2.9) with no conflicts; the commits are the same lines. v0.2.9 gives the Metal engine batch slots, each with its own session, and kv_plan sizes their QSA pools as it does the main session's, so this PR now drops theirs too. At the new tip 30ec0885, one round per setting against v0.2.9's own build, the same procedure as below (the batch round adds two requests sent 1 s apart, which run in the two slots):

--max-context v0.2.9 this PR
262144 16.99 GiB 10.61 GiB
65536 9.28 GiB 7.68 GiB
262144, 2 batch slots ("parallel": 2) 37.28 GiB 18.15 GiB
  • With 2 slots the saving is 19.1 GiB: 6.4 GiB for the main session and for each slot's.
  • Greedy output: 8 of 8 requests identical at both contexts (the 9th carries a per-run tag); with 2 slots, also the
    two requests that ran side by side (10 of 10).
  • Decode of the 4 short prompts (tok/s): at 262K, v0.2.9 93.6 94.6 91.6 94.1 and this PR 92.6 94.2 92.0 95.4; at 64K,
    93.6 94.0 92.1 95.4 and 93.6 94.0 92.1 95.2; with 2 slots, 93.7 94.4 92.1 95.3 and 92.5 94.1 92.4 95.9. (One round.)
  • Metal build 0.1.55-metal: 0 errors, the same 16 warnings as main.
  • unifdef -USTRATA_GPU_METAL src/core/layer.cpp: identical to v0.2.9's.

The rest of this section is the first run, on 9de04cd4.

Environment: Apple M5 Max (128 GB), macOS 26.7.1, Apple clang 21.0.0. Both builds from the same checkout procedure (cmake -S metal -B build-metal -G Ninja -DCMAKE_BUILD_TYPE=Release, target strata): main 9de04cd4 and this branch (9de04cd4 + 2 commits). Model: Qwen3.8-Flash-Next GSQ-RCO IQ3_S (SC117's abliterated build, two-file layout), resident, --expert-cache 64 --prefill auto --spec 2 --mtp <rt> --mmap-experts, through each tree's own serve/server.py. Runs alternate main / PR / main / PR at each context, 30 s apart.

Passed:

  • Metal build: both build with the same 11 warnings, none in layer.cpp.
  • unifdef -USTRATA_GPU_METAL src/core/layer.cpp: identical to main's (cmp).
  • Memory: the table above (2 runs per build per context).
  • Greedy output (temperature 0, up to 128 tokens): 8 requests per run. They are 4 short prompts, the same ~1.2K-token system prompt with two questions, a live follow-up turn (1,577 tokens reused), and a second follow-up rewound to the same point (1,577 reused). All 8 are identical between main and this PR, in both rounds, at 262K and at 65K, and identical across rounds within each build. A 9th request (a ~15K-token prompt carrying a per-run tag) read and answered in every run, with prompt reuse and checkpoints logged the same in both builds.
  • Decode speed of the 4 short prompts (tok/s, round 1 / round 2):
    • 262K: main 93.1 94.5 91.9 94.6 / 93.3 93.6 91.7 95.1; PR 93.2 94.7 92.0 95.1 / 93.6 94.5 91.5 94.4
    • 65K: main 93.0 94.4 91.8 95.2 / 93.3 94.2 91.7 95.0; PR 93.2 94.1 91.6 94.5 / 92.9 93.6 92.1 95.3

Failed:
None observed.

Not run:

  • CUDA, HIP and SYCL builds: no such toolchains here. layer.cpp is compiled by every backend, so this stays a draft. Could someone build it on one of them? The change is entirely inside #if defined(STRATA_GPU_METAL), and the unifdef check above is my only evidence for the other backends.
  • Other Macs, the SSD-streamed experts (Macs under ~96 GB), vision, --max-context above 262K.
  • Long-context quality checks (needles, KL). The window's K/V is untouched, so I expect no change, but I did not run them for this PR.

Limits

  • The saving applies to the Metal engine only. It is per session, so the batch slots (since v0.2.9) each save the same.
  • Anything that reads a session QSA state's K/V on Metal must go through the window's binding, as v0.2.10's parking does; an unbound state has no pools.

🤖 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/3536 **Author:** [@oceanio](https://github.com/oceanio) **Created:** 10/21/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `pr/metal-session-kv-solo` --- ### 📝 Commits (2) - [`98efff3`](https://github.com/Niko1221/Strata/commit/98efff31adb2ba5bb9505c3c0228f85427d5147e) metal: the session's QSA states carry no K/V pools or pooled indexer keys (the decode window keeps its own) - [`036d07a`](https://github.com/Niko1221/Strata/commit/036d07ac7c79dc28900bf5e873b46caa98aa772a) docs (Mac): the engine's own memory grows with --max-context; what it was before the session K/V change ### 📊 Changes **2 files changed** (+21 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `docs/MAC.md` (+1 -0) 📝 `src/core/layer.cpp` (+20 -0) </details> ### 📄 Description ## Problem and result On the Mac (Metal) engine the decode window (`metal/src/engine/window.cpp`) keeps its own K/V and pooled indexer keys for the 12 QSA layers, sized by `--max-context`. It never reads or writes the session's QSA K/V pools, but `kv_plan` still sizes them for the whole context. On Metal a session buffer is one shared `MTLBuffer` in the residency set, so those pools are resident in full whether anything touches them or not: about 24.6 KB per context token. Measured first on main 0.2.1.4 (phys_footprint of the engine 5 s after it is loaded, Qwen3.8-Flash-Next IQ3_S, M5 Max): | `--max-context` | main | this PR | saved | |---|---:|---:|---:| | 262144 | 16.99 / 16.98 GiB | 10.62 / 10.61 GiB | 6.37 GiB | | 65536 | 9.27 / 9.27 GiB | 7.68 / 7.68 GiB | 1.59 GiB | The same 6.4 GiB is still saved after 9 requests (24.91 → 18.53 GiB at 262K). Output and speed do not change (Validation). Since v0.2.9 each Metal batch slot has its own session, and the saving applies to each: with 2 slots at 262K the engine holds 18.15 GiB instead of 37.28. On v0.2.10 the numbers are the same (Validation). ## Related discussion None. This is a focused fix. Context: since v0.2.10 the Metal engine parks conversations, through session QSA states that the window binds to its own K/V (`Window::bind_session` / `bind_draft`), so they need no pools of their own. Parking works the same with this PR. Session files fail on v0.2.10 on a Mac for two other reasons, with or without this PR; #3541 fixes those, and with it on top they work with this PR too (Validation). ## Approach - `src/core/layer.cpp`, `kv_plan`: under `#if defined(STRATA_GPU_METAL)` a QSA state gets 0 K/V slots and 0 pooled indexer rows. `qsa_state_init` then leaves the pool pointers and `idx_pooled` null, so they do not alias the page table or `block_pos` that follow. The page table, the indexer tail and dead rows, `block_pos` and the RoPE tables are carved as before. - The window points the session's QSA states at its own K/V when it binds them (`Window::bind_session` / `bind_draft`, v0.2.10), so parking and session files read the window's K/V. Until a state is bound, `n_slots == 0` makes a conversation snapshot refuse it (`conversation_snapshot.cpp` `valid`) instead of reading pools that are not there. - `docs/MAC.md`: one line saying that the engine's own memory grows with `--max-context`, with the numbers before and after. - Non-Metal builds: unchanged. `unifdef -USTRATA_GPU_METAL src/core/layer.cpp` gives the same file before and after. - No flag or setting: the pools were never used on Metal, so there is nothing to opt into. ## Validation **Re-run on v0.2.10 (2026-11-03).** Rebased onto main `ffa80d73` (v0.2.10). The code lines are the same; the `kv_plan` comment now names the window's binding (Approach). At the new tip `036d07ac`, one round per setting against v0.2.10's own build, the same procedure as below: | `--max-context` | v0.2.10 | this PR | |---|---:|---:| | 262144 | 16.99 GiB | 10.61 GiB | | 65536 | 9.28 GiB | 7.68 GiB | | 262144, 2 batch slots (`"parallel": 2`) | 37.28 GiB | 18.15 GiB | - Greedy output: 8 of 8 requests identical at both contexts (the 9th carries a per-run tag); with 2 slots, 10 of 10. - Decode of the 4 short prompts (tok/s): at 262K, v0.2.10 97.6 93.0 91.2 94.5 and this PR 97.5 96.4 96.2 99.3; at 64K, 98.4 98.6 96.4 99.4 and 98.3 98.5 96.1 99.4; with 2 slots, 98.4 98.4 95.9 99.3 and 98.2 98.3 96.3 99.4. (One round.) - Parking (`tools/parking_test.py` with #3541's log-path fix, 28,692 tokens, with MTP): the follow-up after A, B, A is identical to the one that never parked (95/95), as on v0.2.10. - Session files: with #3541's two commits on top, `session_restart_test.py` at 3,785 and 28,785 tokens, with and without MTP: restored in a new engine, follow-up identical, the whole conversation reused. Without them they fail as on v0.2.10. - Metal build 0.1.56-metal: 0 errors, the same 16 warnings as main. - `unifdef -USTRATA_GPU_METAL src/core/layer.cpp`: identical to v0.2.10's. **Re-run on v0.2.9 (2026-11-01).** Rebased onto main `c62d5e3a` (v0.2.9) with no conflicts; the commits are the same lines. v0.2.9 gives the Metal engine batch slots, each with its own session, and `kv_plan` sizes their QSA pools as it does the main session's, so this PR now drops theirs too. At the new tip `30ec0885`, one round per setting against v0.2.9's own build, the same procedure as below (the batch round adds two requests sent 1 s apart, which run in the two slots): | `--max-context` | v0.2.9 | this PR | |---|---:|---:| | 262144 | 16.99 GiB | 10.61 GiB | | 65536 | 9.28 GiB | 7.68 GiB | | 262144, 2 batch slots (`"parallel": 2`) | 37.28 GiB | 18.15 GiB | - With 2 slots the saving is 19.1 GiB: 6.4 GiB for the main session and for each slot's. - Greedy output: 8 of 8 requests identical at both contexts (the 9th carries a per-run tag); with 2 slots, also the two requests that ran side by side (10 of 10). - Decode of the 4 short prompts (tok/s): at 262K, v0.2.9 93.6 94.6 91.6 94.1 and this PR 92.6 94.2 92.0 95.4; at 64K, 93.6 94.0 92.1 95.4 and 93.6 94.0 92.1 95.2; with 2 slots, 93.7 94.4 92.1 95.3 and 92.5 94.1 92.4 95.9. (One round.) - Metal build 0.1.55-metal: 0 errors, the same 16 warnings as main. - `unifdef -USTRATA_GPU_METAL src/core/layer.cpp`: identical to v0.2.9's. The rest of this section is the first run, on `9de04cd4`. Environment: Apple M5 Max (128 GB), macOS 26.7.1, Apple clang 21.0.0. Both builds from the same checkout procedure (`cmake -S metal -B build-metal -G Ninja -DCMAKE_BUILD_TYPE=Release`, target `strata`): main `9de04cd4` and this branch (`9de04cd4` + 2 commits). Model: Qwen3.8-Flash-Next GSQ-RCO IQ3_S (SC117's abliterated build, two-file layout), resident, `--expert-cache 64 --prefill auto --spec 2 --mtp <rt> --mmap-experts`, through each tree's own `serve/server.py`. Runs alternate main / PR / main / PR at each context, 30 s apart. **Passed:** - Metal build: both build with the same 11 warnings, none in `layer.cpp`. - `unifdef -USTRATA_GPU_METAL src/core/layer.cpp`: identical to main's (`cmp`). - Memory: the table above (2 runs per build per context). - Greedy output (temperature 0, up to 128 tokens): 8 requests per run. They are 4 short prompts, the same ~1.2K-token system prompt with two questions, a live follow-up turn (1,577 tokens reused), and a second follow-up rewound to the same point (1,577 reused). All 8 are identical between main and this PR, in both rounds, at 262K and at 65K, and identical across rounds within each build. A 9th request (a ~15K-token prompt carrying a per-run tag) read and answered in every run, with prompt reuse and checkpoints logged the same in both builds. - Decode speed of the 4 short prompts (tok/s, round 1 / round 2): - 262K: main 93.1 94.5 91.9 94.6 / 93.3 93.6 91.7 95.1; PR 93.2 94.7 92.0 95.1 / 93.6 94.5 91.5 94.4 - 65K: main 93.0 94.4 91.8 95.2 / 93.3 94.2 91.7 95.0; PR 93.2 94.1 91.6 94.5 / 92.9 93.6 92.1 95.3 **Failed:** None observed. **Not run:** - CUDA, HIP and SYCL builds: no such toolchains here. `layer.cpp` is compiled by every backend, so this stays a draft. Could someone build it on one of them? The change is entirely inside `#if defined(STRATA_GPU_METAL)`, and the unifdef check above is my only evidence for the other backends. - Other Macs, the SSD-streamed experts (Macs under ~96 GB), vision, `--max-context` above 262K. - Long-context quality checks (needles, KL). The window's K/V is untouched, so I expect no change, but I did not run them for this PR. ## Limits - The saving applies to the Metal engine only. It is per session, so the batch slots (since v0.2.9) each save the same. - Anything that reads a session QSA state's K/V on Metal must go through the window's binding, as v0.2.10's parking does; an unbound state has no pools. 🤖 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#4673
No description provided.