[GW][LD][HIP][Proofs] Wire prefix checkpoint cache into serve: restore longest prefix, prefill only new suffix

Concept ported from strata --auto-ckpt, adapted to vetta's contiguous per-pass
KV + ShortConv history (proofs/prefix_cache.lean, restore_equiv). serve_gen now
restores the longest cached STRICT prefix of a prompt and prefills only the new
suffix tokens; the final logits still come from a real forward of the last prompt
token. Checkpoints save at pos (prompt + generated non-EOS tokens) so the next
turn reuses the full prior context. Pinned anchors {512,1024} never evicted (I3),
LRU at cap 6; KV snapshots unpooled (freed on eviction). VETTA_NO_PREFIX_CACHE
forces cold prefill for A/B.

Device-to-device copy FFI (vetta_buf_copy / vetta_buf_copy2d) + DeviceBuf
copy_from/copy2d_from; LayerState snapshot/restore (pitched KV prefix + hist).

Verified: CPU 91 + gw GPU 5 + hip GPU 18 + ld GPU 18 green; Lean 0 sorryAx.
Multi-turn parity EXACT (restore == cold re-prefill), both prewarm- and
decode-state checkpoints. Measured (gfx1150, Inkling-2-Mini, FP16 KV, greedy):
605-token prompt, 600-token cached prefix -> prefill 132.5s cold vs 2.29s warm
(57.8x), end-to-end 133.2s vs 3.2s (41.8x).
This commit is contained in:
adm committed 2026-12-01 20:30:13 +01:00
1 parent f12281ec61
commit 736c541887
6 files changed
+220 -10

No files matched your search

+16
View File
@@ -29,6 +29,8 @@ mod ffi {
pub fn vetta_buf_free(p: *mut c_void) -> c_int;
pub fn vetta_buf_h2d(dst: *mut c_void, src: *const c_void, bytes: usize) -> c_int;
pub fn vetta_buf_d2h(dst: *mut c_void, src: *const c_void, bytes: usize) -> c_int;
pub fn vetta_buf_copy(dst: *mut c_void, src: *const c_void, bytes: usize) -> c_int;
pub fn vetta_buf_copy2d(dst: *mut c_void, dpitch: usize, src: *const c_void, spitch: usize, width: usize, height: usize) -> c_int;
pub fn vetta_rms_norm_f32(
x: *const c_float, w: *const c_float, y: *mut c_float,
rows: c_int, d: c_int, eps: c_float, stream: *mut c_void,
@@ -242,6 +244,20 @@ impl DeviceBuf {
if rc != 0 { return Err("d2h failed".into()); }
Ok(())
}
/// Device-to-device copy of `bytes` from `src` into `self`.
pub fn copy_from(&self, src: &DeviceBuf, bytes: usize) -> Result<(), String> {
if bytes > self.bytes || bytes > src.bytes { return Err("copy overflow".into()); }
let rc = unsafe { ffi::vetta_buf_copy(self.ptr, src.ptr, bytes) };
if rc != 0 { return Err("d2d copy failed".into()); }
Ok(())
}
/// Pitched device-to-device copy: `height` rows of `width` bytes, source row
/// stride `spitch`, destination row stride `dpitch` (KV prefix sub-block).
pub fn copy2d_from(&self, src: &DeviceBuf, dpitch: usize, spitch: usize, width: usize, height: usize) -> Result<(), String> {
let rc = unsafe { ffi::vetta_buf_copy2d(self.ptr, dpitch, src.ptr, spitch, width, height) };
if rc != 0 { return Err("d2d copy2d failed".into()); }
Ok(())
}
}
#[cfg(feature = "hip")]