[Proto] reuse persistent KV cap on long->short session switch (reviewer tip)

serve_gen passed the per-request 4096 bucket (cap) to restore/forward, so after a
long session (states_cap=32768) a shorter request (bucket 20480) failed the
st.max_seq == max_seq reuse check and reallocated the persistent KV from scratch
(free 6.4GB, alloc 4GB) -> the VRAM drop+regrow Federico observed (31.7->25.9GB).
Now cap = eng.states_cap.get() after the grow-check: restore/forward use the actual
persistent stride, so returning to a shorter context reuses the KV (no realloc).
Measured: GEN1 5000tok then 100tok -> vram_end 23.56GB both (no drop). gw GPU 4/4.
This commit is contained in:
adm committed 2026-12-02 04:22:02 +01:00
1 parent 755626fae2
commit 99cd458d3b
1 file changed
+8 -4
+8 -4
View File
@@ -293,12 +293,16 @@ pub fn serve_gen(
} }
// Persistent KV states: size once to a 4096-bucket cap >= max_seq and reuse // Persistent KV states: size once to a 4096-bucket cap >= max_seq and reuse
// across requests (no per-request ~3.75 GB alloc/free sawtooth). Grow only when // across requests (no per-request ~3.75 GB alloc/free sawtooth). Grow only when
// a request crosses the bucket. `cap` is the KV stride passed to the forwards. // a request crosses the bucket.
let cap = max_seq.next_multiple_of(4096).min(MAX_CONTEXT); let need = max_seq.next_multiple_of(4096).min(MAX_CONTEXT);
if eng.states_cap.get() < cap || eng.states.borrow().len() != eng.n_layers { if eng.states_cap.get() < need || eng.states.borrow().len() != eng.n_layers {
*eng.states.borrow_mut() = (0..eng.n_layers).map(|_| None).collect(); *eng.states.borrow_mut() = (0..eng.n_layers).map(|_| None).collect();
eng.states_cap.set(cap); eng.states_cap.set(need);
} }
// KV stride = the PERSISTENT cap, which may exceed this request's bucket after
// a longer session. Reuse it (restore/forward take this, not `need`) so returning
// from a long context to a short one does NOT reallocate the KV from scratch.
let cap = eng.states_cap.get();
let mut states = eng.states.borrow_mut(); let mut states = eng.states.borrow_mut();
// Prefix checkpoint reuse (proofs/prefix_cache.lean restore_equiv): restore // Prefix checkpoint reuse (proofs/prefix_cache.lean restore_equiv): restore