poly: hold the attention caches' growth back from the expert cache (kv_unmapped)
The expert cache was sized from the VRAM free at load, but the attention caches map their rows later, as the conversation grows (cover()). A card filled with experts then cut the cache on every prompt, and once it was at zero a long prompt died with "no VRAM for the attention cache (2 MiB more)" (GTX 1060 6 GB, Qwen3.6-35B-A3B, --max-context 131072, --kv q8_0). kv_unmapped() is what the chunked caches will still map to reach --max-context; fill_expert_cache(), mind_card_vram() and held_is_budget() leave it to the rows. If it does not fit, every expert runs on the CPU and the message says what makes room. docs/VRAM_BUDGET.md has the analysis. Not yet verified: docs/VRAM_BUDGET.md says "compile verification running" -- build and run it before relying on it.
This commit is contained in:
1 parent
7813dc8b53
commit
5cd37fa703
3 files changed
+147
-4
No files matched your search
@@ -0,0 +1,94 @@
|
||||
# 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.
|
||||
+49
-4
@@ -1172,6 +1172,24 @@ void Engine::fill_expert_cache() {
|
||||
const size_t reserve = size_t(reserve_mib_) << 20;
|
||||
if (free_b <= reserve) return;
|
||||
|
||||
// What the attention caches will still take before the context is full is held back from the expert cache
|
||||
// (kv_unmapped()). The rows are mapped later, as the conversation grows (cover()), so sizing the experts on
|
||||
// the whole free figure fills the card with them and leaves the rows nothing: a 6 GB card (GTX 1060,
|
||||
// Qwen3.6-35B-A3B, --max-context 131072, --kv q8_0, 544 bytes a row) ended a 93k-token prompt with "no VRAM
|
||||
// for the attention cache (2 MiB more)". A cache sized with the rows held back cannot run into that.
|
||||
const size_t spare = free_b - reserve; // beyond the reserve: what the rows and the experts share
|
||||
const size_t kv_rest = kv_unmapped();
|
||||
if (kv_rest >= spare) {
|
||||
std::fprintf(stderr, "strata-poly: the attention caches still need %.2f GiB to reach %lld tokens and this "
|
||||
"card has %.2f GiB free beyond the %d 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\n",
|
||||
(double) kv_rest / 1073741824.0, (long long) opt_.max_context,
|
||||
(double) spare / 1073741824.0, reserve_mib_);
|
||||
return;
|
||||
}
|
||||
const size_t beyond = spare - kv_rest; // the most the expert cache may take on this card
|
||||
|
||||
struct Cand {
|
||||
int il, e;
|
||||
size_t bytes;
|
||||
@@ -1209,7 +1227,7 @@ void Engine::fill_expert_cache() {
|
||||
n_trade_ = (int) sizes.size();
|
||||
// sized again (resize_cache()), it is no larger than load() made it, or than its addresses with VMM
|
||||
const size_t most = vmm_ ? std::min(all, total_b) : full_bytes_ > 0 ? full_bytes_ : all;
|
||||
size_t arena = std::min({all, free_b - reserve, arena_.base ? arena_.reserved - 256 : most});
|
||||
size_t arena = std::min({all, beyond, arena_.base ? arena_.reserved - 256 : most});
|
||||
if (arena == 0) return;
|
||||
if (vmm_) { // in pieces, so that it can give some up to the attention caches (shrink_cache()) and grow
|
||||
if (!arena_.base) vmm_reserve(arena_, most + 256); // addresses only: grow_cache() maps more of them
|
||||
@@ -1220,7 +1238,7 @@ void Engine::fill_expert_cache() {
|
||||
if (arena == 0) return;
|
||||
}
|
||||
exp_ = (uint8_t*) arena_.base;
|
||||
budget_ = free_b - reserve + other_rows(); // when it holds every expert, the rest goes to the rows first
|
||||
budget_ = arena_.mapped + other_rows() + kv_unmapped(); // the rows to come are the rows' (held_is_budget())
|
||||
for (const Kv& k : kv_) budget_ += k.mapped;
|
||||
} else {
|
||||
exp_ = (uint8_t*) dmalloc(arena + 256, "the expert cache");
|
||||
@@ -1343,12 +1361,18 @@ bool Engine::mind_card_vram() {
|
||||
held_is_budget();
|
||||
return true;
|
||||
}
|
||||
return free >= keep + (int64_t) (kArenaPiece >> 20) && grow_cache(size_t(free - keep) << 20) > 0;
|
||||
// The rows that the context will still map are not free to take (kv_unmapped(), as fill_expert_cache()):
|
||||
// growing the experts into them is what left a long prompt without VRAM for its own attention cache.
|
||||
const int64_t kv_rest = (int64_t) (kv_unmapped() >> 20);
|
||||
return free >= keep + kv_rest + (int64_t) (kArenaPiece >> 20) && grow_cache(size_t(free - keep - kv_rest) << 20) > 0;
|
||||
}
|
||||
|
||||
// The cache's VRAM and the attention caches' as they are now become the budget cover() keeps them to.
|
||||
// The rows still to be mapped (kv_unmapped()) count as the rows' - they are what the context will take, and the
|
||||
// cache was sized to leave them room. Without them cover() would cut the cache for VRAM that the reserve
|
||||
// already holds back, and cut it again with every prompt.
|
||||
void Engine::held_is_budget() {
|
||||
budget_ = arena_.mapped + other_rows();
|
||||
budget_ = arena_.mapped + other_rows() + kv_unmapped();
|
||||
for (const Kv& k : kv_) budget_ += k.mapped;
|
||||
}
|
||||
|
||||
@@ -1378,6 +1402,27 @@ size_t Engine::other_rows() const {
|
||||
return b;
|
||||
}
|
||||
|
||||
// The VRAM this card's attention caches will still take to reach --max-context: every chunked cache's reservation
|
||||
// less what is mapped. A cache in one allocation (--kv-whole, or a card without VMM) is whole from the start and
|
||||
// so holds nothing back; the same for a sequence whose rows are all mapped already.
|
||||
//
|
||||
// Why fill_expert_cache() needs this: the experts are sized from what is free at load, but the rows are mapped
|
||||
// later, as the conversation grows (cover()). Without this figure a card is filled with experts to its last MiB
|
||||
// and the rows have to take that VRAM back one cut at a time - and when the cache is down to zero there is nothing
|
||||
// left to cut, so a long prompt ends with "no VRAM for the attention cache". A 6 GB card with a 131072-token
|
||||
// context reached exactly that (Qwen3.6-35B-A3B, --kv q8_0: 544 MiB of KV at 93k tokens). Held back here, the
|
||||
// cards' rows fit by construction and the cache is sized once, on what is really left.
|
||||
size_t Engine::kv_unmapped() const {
|
||||
size_t b = 0;
|
||||
const auto of = [&b](const std::vector<Kv>& kv) {
|
||||
for (const Kv& k : kv)
|
||||
if (k.reserved > k.mapped) b += k.reserved - k.mapped;
|
||||
};
|
||||
of(kv_);
|
||||
for (const Seq& q : seqs_) of(q.kv);
|
||||
return b;
|
||||
}
|
||||
|
||||
// `bytes` more VRAM at the cache's end, for the experts it lacks that are used most (use per byte, as
|
||||
// fill_expert_cache() chooses), side by side by layer. VRAM the card does not give is not an error: the cache
|
||||
// keeps what was mapped. Returns the experts it took.
|
||||
|
||||
@@ -623,6 +623,10 @@ protected:
|
||||
void take_bound(int s);
|
||||
void hold(int s); ///< bind() on this card alone
|
||||
size_t other_rows() const;
|
||||
/// The VRAM this card's attention caches will still take to reach --max-context: every sequence's chunked
|
||||
/// cache, its reservation less what is mapped. fill_expert_cache() holds it back from the expert cache, so a
|
||||
/// card is not filled with experts that a long prompt then has to evict row by row (and be unable to).
|
||||
size_t kv_unmapped() const;
|
||||
size_t budget_ = 0; ///< their VRAM and the expert cache's together, as the cache was filled
|
||||
int asked_reserve_mib_ = -1; ///< Options::vram_reserve_mib as load() got it
|
||||
int reserve_mib_ = 0; ///< what the cache was sized to leave free
|
||||
|
||||
Reference in new issue
Block a user