[Core] flat VRAM: SWA KV ring + fixed global KV + bounded scratch bucketing (reviewer p1+p2)

Punto 2: the 40 SWA pools now store a circular ring of swa_win+SWA_CHUNK_CAP(512)
=1536 positions (slot=pos%seq) instead of the whole 32768 context (~5GiB wasted);
the 8 global pools keep a linear max_context cache allocated ONCE. KV capacities
are fixed per layer kind => no drop+realloc when a session crosses a 4096 multiple.
kv_store{,_f16,_f16_batched} write pos%seq; flash decode/prefill/prefill_wmma read
p%seq (global seq>pos => identity). snapshot/restore map the window onto ring slots
with 1 or 2 pitched copies (wrap). serve clamps prefill chunk to SWA_CHUNK_CAP so
ring>=swa_win+chunk holds. proofs/prefix_cache.lean Part E (0 sorryAx): slot_inj,
window_slots_distinct, ring_no_clobber, global_slot_id. New GPU swa_ring_kv_parity
(R=1536, P=2000) vs an independent host LINEAR oracle: 1.48e-5.
Punto 1: BUF_POOL buckets pooled allocs in (4KiB,64MiB] to a power of two so variable
batch tails / future speculative B converge to a closed size set (no hipMalloc/Free
churn); larger allocs stay exact. Weight uploads (load_raw/load_bf16_as_f32/expert
matrix/arena) switched to alloc_unpooled: resident, exact, never pooled (rounding
them had added ~11GiB).
MEASURED gfx1150 FP16 KV chunk256: READY ~21.0GB; multi-turn 2048->13000->2048->5000
vram flat ~23.1-23.7GB (only bounded cap-8 checkpoint turnover, no KV resize);
prefill 2048 ~12.2s, 13000 ~78.6s (~6ms/tok). Suite GREEN hip37 ld23 gw4, CPU23, Lean6.
This commit is contained in:
adm committed 2026-12-02 19:59:11 +01:00
1 parent a4752953fb
commit ca3a6b0e15
6 files changed
+210 -55

No files matched your search

+10 -17
View File
@@ -16,7 +16,7 @@
//! is callable from an integration test without spawning the binary.
use std::io::{self, BufRead, Write};
use std::cell::{Cell, RefCell};
use std::cell::RefCell;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{mpsc, Arc};
use std::time::Instant;
@@ -123,10 +123,11 @@ impl PrefixCache {
// request (same cap + pass count); restore overwrites its KV
// prefix + hist rings. The stale KV tail beyond seq_len is never
// read (causal attention), so reuse is exact (restore_equiv).
let want = if is_swa(ip) { vetta_infer_ld::layer::swa_ring(d) } else { max_seq };
let reuse = states[ip].as_ref()
.map_or(false, |st| st.max_seq == max_seq && st.k_cache.len() == s.k_cache.len());
.map_or(false, |st| st.max_seq == want && st.k_cache.len() == s.k_cache.len());
if !reuse {
states[ip] = Some(new_state(d, max_seq, s.k_cache.len())?);
states[ip] = Some(new_state(d, want, s.k_cache.len())?);
}
states[ip].as_mut().unwrap().restore(d, s)?;
}
@@ -178,7 +179,6 @@ pub struct InklingEngine {
/// `states_cap` (a 4096-bucket >= prompt+max_new). Eliminates the per-request
/// ~3.75 GB alloc/free VRAM sawtooth; grown only when a request crosses a bucket.
states: RefCell<Vec<Option<LayerState>>>,
states_cap: Cell<usize>,
/// Resident context window (advertised in READY). Defaults to MAX_CONTEXT;
/// VETTA_MAX_CONTEXT / `--max-context` raise it (like strata) for longer agents.
max_context: usize,
@@ -237,7 +237,6 @@ impl InklingEngine {
cap: t.final_logit_softcapping as f32,
prefix: RefCell::new(PrefixCache::new()),
states: RefCell::new(Vec::new()),
states_cap: Cell::new(0),
max_context,
})
}
@@ -315,19 +314,13 @@ pub fn serve_gen(
emit_all(emit, sess.on_error("prompt exceeds context window"));
return;
};
let max_seq = (n_prompt + effective_max_new + 2).min(max_context);
// 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
// a request crosses the bucket.
let need = max_seq.next_multiple_of(4096).min(max_context);
if eng.states_cap.get() < need || eng.states.borrow().len() != eng.n_layers {
// KV capacities are FIXED per layer kind (SWA ring = swa_win+chunk_cap, global
// = max_context), allocated once and never resized across requests: no
// drop+realloc when a session crosses a 4096 multiple (reviewer punto 2).
if eng.states.borrow().len() != eng.n_layers {
*eng.states.borrow_mut() = (0..eng.n_layers).map(|_| None).collect();
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 cap = max_context;
let mut states = eng.states.borrow_mut();
// Prefix checkpoint reuse (proofs/prefix_cache.lean restore_equiv): restore
@@ -385,7 +378,7 @@ pub fn serve_gen(
let mut history: Vec<i32> = g.ids[..start].to_vec();
history.reserve(n_prompt - start + effective_max_new);
let mut logits: Option<DeviceBuf> = None;
let chunk = prefill_chunk_size();
let chunk = prefill_chunk_size().min(vetta_infer_ld::layer::SWA_CHUNK_CAP);
let suffix = &g.ids[start..];
let mut done = 0usize;
// Monotonic 512-grid threshold: split a chunk exactly on the next grid boundary