[HIP][LD][GW] Bound VRAM across requests: unpooled KV + FP16 serve KV + bucketed flash scratch

Federico saw VRAM grow ~7 GiB per agent step (44.9/48 GiB, near OOM). Root cause:
the KV cache scales with max_seq=prompt+max_new (varies per request) and the
exact-size DeviceBuf pool retained every distinct KV size, so a multi-turn chat
accumulated KV without bound. The full-attn flash scores scratch (n_win grows per
token) leaked similarly.

- DeviceBuf gains a 'pooled' flag + alloc_unpooled(): always hipFree on drop,
  never pooled. KV (k_cache/v_cache) uses it -> freed at request end, no
  accumulation. Per-token fixed-size intermediates stay pooled (decode speed).
- flash_scores_scratch buckets the allocation to a power of two so the pool
  reuses it as cur_pos grows (kernel still strides by actual n_win <= bucket).
- KV dtype: FP16 for the resident serve (exact strata KvDtype::FP16 default,
  parity-verified EXACT, half the VRAM of F32). Safe process-wide switch
  set_kv_f16(); InklingEngine::new opts in. The ld CPU-oracle parity tests keep
  the F32 default (VETTA_F32_KV=1 also forces F32).

Measured (new binary, 3 requests of 200/400/600 prompt tokens): VRAM 7.66 ->
8.07 -> 8.33 GiB (small bounded expert lazy-load growth, NOT +7 GiB/step);
clean release to 0.34 GiB on QUIT. Parity EXACT (16 tok); 18 ld + 18 hip + 5 gw
GPU green; workspace -D warnings green; 5 Lean 0 sorryAx.
This commit is contained in:
adm committed 2026-12-01 20:02:33 +01:00
1 parent b1f65942c8
commit c9713c8bc1
3 files changed
+68 -22

No files matched your search

+20 -4
View File
@@ -185,11 +185,14 @@ pub fn new_state(d: &Dims, max_seq: usize, n_passes: usize) -> Result<LayerState
// VETTA_F16_KV=1 matches the strata default (KvDtype::FP16) and its
// length-dependent rounding (the F32 cache diverges on near-ties as it grows).
let kv_n = d.n_kv * max_seq * d.hd;
// KV is unpooled: its size scales with max_seq (prompt+max_new), which varies
// per request. Pooling by exact size would retain every distinct KV size and
// grow VRAM without bound across a multi-turn chat; free it on request end.
let kv_buf = || -> Result<DeviceBuf, String> {
if f16_kv() {
let b = DeviceBuf::alloc(kv_n * 2)?; b.h2d_bytes(&vec![0u8; kv_n * 2])?; Ok(b)
let b = DeviceBuf::alloc_unpooled(kv_n * 2)?; b.h2d_bytes(&vec![0u8; kv_n * 2])?; Ok(b)
} else {
let b = DeviceBuf::alloc(kv_n * 4)?; b.h2d(&vec![0f32; kv_n])?; Ok(b)
let b = DeviceBuf::alloc_unpooled(kv_n * 4)?; b.h2d(&vec![0f32; kv_n])?; Ok(b)
}
};
let kv_vec = || -> Result<Vec<DeviceBuf>, String> { (0..n_passes.max(1)).map(|_| kv_buf()).collect() };
@@ -204,8 +207,21 @@ pub fn new_state(d: &Dims, max_seq: usize, n_passes: usize) -> Result<LayerState
})
}
/// FP16 KV cache opt-in (strata default KV dtype is FP16).
fn f16_kv() -> bool { std::env::var("VETTA_F16_KV").is_ok() }
/// KV cache dtype switch. The CPU-oracle parity baseline (and the ld packer
/// tests) use F32; the resident serve opts into FP16 — the exact strata default
/// (KvDtype::FP16, "default lossless FP16"), parity-verified EXACT (16/40 greedy
/// tokens) and half the VRAM. Attention math stays f32; only stored K/V round to
/// FP16. Process-wide safe switch (no env mutation); serve calls set_kv_f16(true).
static KV_F16: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
/// Select FP16 KV (true) or the F32 baseline (false). The resident serve sets
/// true at startup; tests keep the F32 default for CPU-oracle parity.
pub fn set_kv_f16(on: bool) { KV_F16.store(on, std::sync::atomic::Ordering::Relaxed); }
fn f16_kv() -> bool {
if std::env::var("VETTA_F32_KV").is_ok() { return false; }
KV_F16.load(std::sync::atomic::Ordering::Relaxed) || std::env::var("VETTA_F16_KV").is_ok()
}
/// Run one SWA layer step on `cur` (in place), appending a depth-block snapshot
/// to `blocks` when `ip % block_size == 0`. `pos` is the decode position.