[Core] Flat VRAM: persistent KV states + SWA-window prefix checkpoints

a6 (persistent states): serve_gen allocated+freed ~3.75GB KV every request
(the +/-3.75GB sawtooth, max_new=16384). InklingEngine now holds persistent
per-layer states sized to a 4096-bucket cap >= prompt+max_new, reused across
requests and grown only on a bucket crossing. restore reuses a correctly-sized
state; a cold request resets the ShortConv hist rings (reset_history, async)
since the KV prefix is overwritten by prefill before any causal read. Verified:
two identical GEN16384 requests -> vram 23.46/23.49GB flat (no sawtooth),
identical reference output; twice-paris cold-reuse output identical.

a8 (SWA-window checkpoints): 40 of 48 KV pools are SWA (swa_win=1024) but the
snapshot stored the full seq_len on all of them (~510MB/checkpoint at seq2600).
Now SWA layers checkpoint only the last min(seq,1024) positions (kv_start offset
via new DeviceBuf::copy2d_at); full-attn layers keep the whole prefix. Checkpoint
~210MB. proofs/prefix_cache.lean Part D proves kvStart_le_winStart containment +
swa_window_restore_equiv (windowed restore observationally exact). Verified:
cold vs restored(1536, window truncated to [512,1536)) emit identical tokens.

Measured 5-turn VRAM climb +2.6GB -> +1.16GB (bounded by cap 8), KV flat.
Suite green: ld23 hip33 gw4 GPU serial, CPU -D warnings, Lean 0 sorryAx.
This commit is contained in:
adm committed 2026-12-02 02:52:32 +01:00
1 parent a6ca73105c
commit 47f8b52070
4 files changed
+159 -42

No files matched your search

+16
View File
@@ -329,6 +329,22 @@ impl DeviceBuf {
if rc != 0 { return Err("d2d copy2d failed".into()); }
Ok(())
}
/// Pitched device-to-device copy with byte offsets into BOTH buffers: copies
/// `height` rows of `width` bytes from `src[src_off..]` (row stride `spitch`)
/// into `self[dst_off..]` (row stride `dpitch`). Used by the SWA-window prefix
/// snapshot/restore to copy only the last `swa_win` KV positions of each head.
pub fn copy2d_at(&self, dst_off: usize, src: &DeviceBuf, src_off: usize, dpitch: usize, spitch: usize, width: usize, height: usize) -> Result<(), String> {
if height > 0 {
let dlast = dst_off + (height - 1) * dpitch + width;
let slast = src_off + (height - 1) * spitch + width;
if dlast > self.bytes || slast > src.bytes { return Err("copy2d_at overflow".into()); }
}
let dp = unsafe { (self.ptr as *mut u8).add(dst_off) as *mut std::os::raw::c_void };
let sp = unsafe { (src.ptr as *const u8).add(src_off) as *const std::os::raw::c_void };
let rc = unsafe { ffi::vetta_buf_copy2d(dp, dpitch, sp, spitch, width, height) };
if rc != 0 { return Err("d2d copy2d_at failed".into()); }
Ok(())
}
/// Async zero-fill of the whole buffer on the default stream (hipMemsetAsync):
/// ordered before later kernels with NO blocking host round-trip. Replaces a
/// sync `h2d(&vec![0; n])` that would drain the GPU queue every layer visit.