# VRAM budgeting: why the expert cache must hold back the KV growth Status: patch written, compile verification running (`kv_unmapped()` in `src/poly/engine.cpp`). ## The bug `strata-poly` sized its expert cache from the VRAM that was free **at load**: ``` arena = min(all_experts, free_at_load - reserve) ``` The attention (KV) caches, however, are mapped **later**, as a conversation grows (`Engine::cover()`); with VMM support each cache is reserved at `--max-context` and mapped in `kKvGrow`-row pieces. Those rows come out of the same free VRAM the expert cache had just filled with experts. `cover()` therefore calls `shrink_cache()` on every prompt to take the rows' VRAM back. Two consequences, both seen in production: 1. **Steady churn.** Every prompt cuts the expert cache, and `mind_vram()` grows it back afterwards. The cache oscillates instead of being sized once. 2. **Hard failure.** Once the cache is at zero there is nothing left to cut, and the request dies with `engine: no VRAM for the attention cache (2 MiB more)`. Case 2 is what kompute's GTX 1060 6 GB hit: Qwen3.6-35B-A3B, `--max-context 131072`, `--kv q8_0`. At load the cache was `expert_cache_mib=1138`, `expert_slots=357` of 10496. Under real traffic it then oscillated — every one of these lines is one request cutting the cache and `mind_vram()` growing it back: ``` strata-poly: the attention caches gave back 0.64 GiB ...; the expert cache 0 -> 167 experts strata-poly: the attention caches gave back 0.80 GiB ...; the expert cache 0 -> 216 experts strata-poly: the expert cache 0 -> 0 experts (0.00 GiB) for the VRAM free now, 2 MiB left E srv handle_line: task 800157: engine: no VRAM for the attention cache (2 MiB more) ``` The measured row size is `strata-poly: q8_0 KV cache: 544 bytes a row`, so one attention cache wants `544 * 131072` = 68 MiB at a full 131072-token context. How many of those caches a model has depends on how many of its layers are attention rather than GDN; the total is what `kv_unmapped()` now holds back. Note `2 MiB left`, not "out of VRAM": the reserve was still nominally there, but the experts had taken everything beyond it and the rows needed more than the reserve. ## Why "just set --vram-reserve-mib 512" does not fix it The reserve is a **fixed** number; the rows grow to a **computed** one. Any fixed reserve only moves the failure to a longer prompt. What has to be held back is: ``` kv_rest = sum over every chunked Kv of (reserved - mapped) ``` i.e. exactly the rows the context will still map before it is full. ## The fix `Engine::kv_unmapped()` returns that figure. It is then used in three places: | Place | What it does with `kv_rest` | |---|---| | `fill_expert_cache()` | `beyond = free - reserve - kv_rest`; the expert cache is capped at `beyond`, so the rows fit by construction | | `mind_vram()` | `grow_cache()` is only asked for `free - keep - kv_rest`: the cache does not grow back into rows that are still to come | | `held_is_budget()` | `budget_` counts `kv_unmapped()` as the rows' VRAM, so `cover()` does not cut the cache for VRAM the sizing already reserved | When `kv_rest >= free - reserve`, no expert fits at all and the engine says so plainly instead of starting with a cache it will immediately have to destroy: ``` strata-poly: the attention caches still need X GiB to reach N tokens and this card has Y GiB free beyond the R MiB reserve: no expert fits in VRAM, so every expert runs on the CPU. A smaller --max-context, --kv q8_0, or a smaller --vram-reserve-mib makes room ``` A single-allocation cache (`--kv-whole`, or a card without VMM) reserves nothing and holds nothing back — it was whole from the start. ## Why not shrink the context instead Shrinking `--max-context` treats the symptom and costs the thing the deployment exists for. Qwen3.6-35B-A3B is a mixture-of-experts model: 35B total, ~3B active per token, 10496 routed experts. Its expert weights are the **elastic** block (the expert cache, `vram_elastic=1`), while the attention/KV cache is the **fixed** one. The correct move is to size the elastic block to what is left after the fixed one has its full allowance — which is what this patch does. Context is not the knob; the expert count is. ## Verifying it Watch for these two lines in the engine log after a prompt of any length: - `expert cache auto: ... -> N slots` — N must be non-zero and stable across prompts of different lengths - no `expert cache 0 -> 0 experts` and no `no VRAM for the attention cache` On the GTX 1060 the expected outcome is a much smaller but **stable** cache: the rows' 131072-token allowance is held back once, so the cache is sized to what is really left instead of the 1138 MiB it took while the rows were still unmapped. The exact slot count follows from the card's free VRAM at load and must be read from the log, not predicted.