[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:
1 parent
f12281ec61
commit
736c541887
6 files changed
+220
-10
No files matched your search
@@ -207,6 +207,64 @@ pub fn new_state(d: &Dims, max_seq: usize, n_passes: usize) -> Result<LayerState
|
||||
})
|
||||
}
|
||||
|
||||
/// A checkpoint of one layer's decode state at a prefix boundary `seq_len`: the
|
||||
/// KV prefix `[n_kv, seq_len, hd]` per pass + the ShortConv history rings. Sized
|
||||
/// to seq_len (KV unpooled), held in the prefix cache across requests. Restoring
|
||||
/// it and prefilling the suffix is exact (proofs/prefix_cache.lean restore_equiv).
|
||||
pub struct LayerSnapshot {
|
||||
pub seq_len: usize,
|
||||
pub k_cache: Vec<DeviceBuf>, pub v_cache: Vec<DeviceBuf>,
|
||||
pub hist_k: [DeviceBuf; 3], pub hist_v: [DeviceBuf; 3],
|
||||
pub hist_a: [DeviceBuf; 3], pub hist_m: [DeviceBuf; 3],
|
||||
}
|
||||
|
||||
impl LayerState {
|
||||
/// Snapshot the KV prefix `[0, seq_len)` (pitched copy out of the
|
||||
/// `[n_kv, max_seq, hd]` cache) plus the current ShortConv history rings.
|
||||
pub fn snapshot(&self, d: &Dims, seq_len: usize) -> Result<LayerSnapshot, String> {
|
||||
let es = if f16_kv() { 2 } else { 4 };
|
||||
let row = seq_len * d.hd * es;
|
||||
let snap_kv = |src: &DeviceBuf| -> Result<DeviceBuf, String> {
|
||||
let b = DeviceBuf::alloc_unpooled(d.n_kv * row)?;
|
||||
b.copy2d_from(src, row, self.max_seq * d.hd * es, row, d.n_kv)?;
|
||||
Ok(b)
|
||||
};
|
||||
let k_cache = self.k_cache.iter().map(snap_kv).collect::<Result<Vec<_>, _>>()?;
|
||||
let v_cache = self.v_cache.iter().map(snap_kv).collect::<Result<Vec<_>, _>>()?;
|
||||
let cp = |src: &DeviceBuf| -> Result<DeviceBuf, String> {
|
||||
let b = DeviceBuf::alloc(src.bytes)?; b.copy_from(src, src.bytes)?; Ok(b)
|
||||
};
|
||||
Ok(LayerSnapshot {
|
||||
seq_len, k_cache, v_cache,
|
||||
hist_k: [cp(&self.hist_k[0])?, cp(&self.hist_k[1])?, cp(&self.hist_k[2])?],
|
||||
hist_v: [cp(&self.hist_v[0])?, cp(&self.hist_v[1])?, cp(&self.hist_v[2])?],
|
||||
hist_a: [cp(&self.hist_a[0])?, cp(&self.hist_a[1])?, cp(&self.hist_a[2])?],
|
||||
hist_m: [cp(&self.hist_m[0])?, cp(&self.hist_m[1])?, cp(&self.hist_m[2])?],
|
||||
})
|
||||
}
|
||||
|
||||
/// Restore the KV prefix `[0, snap.seq_len)` + history into `self` (whose
|
||||
/// max_seq must be >= snap.seq_len). Positions >= seq_len stay zero for the
|
||||
/// suffix prefill to fill; attention at pos >= seq_len reads only [0, pos].
|
||||
pub fn restore(&mut self, d: &Dims, snap: &LayerSnapshot) -> Result<(), String> {
|
||||
let es = if f16_kv() { 2 } else { 4 };
|
||||
let row = snap.seq_len * d.hd * es;
|
||||
for (dst, src) in self.k_cache.iter().zip(&snap.k_cache) {
|
||||
dst.copy2d_from(src, self.max_seq * d.hd * es, row, row, d.n_kv)?;
|
||||
}
|
||||
for (dst, src) in self.v_cache.iter().zip(&snap.v_cache) {
|
||||
dst.copy2d_from(src, self.max_seq * d.hd * es, row, row, d.n_kv)?;
|
||||
}
|
||||
for i in 0..3 {
|
||||
self.hist_k[i].copy_from(&snap.hist_k[i], snap.hist_k[i].bytes)?;
|
||||
self.hist_v[i].copy_from(&snap.hist_v[i], snap.hist_v[i].bytes)?;
|
||||
self.hist_a[i].copy_from(&snap.hist_a[i], snap.hist_a[i].bytes)?;
|
||||
self.hist_m[i].copy_from(&snap.hist_m[i], snap.hist_m[i].bytes)?;
|
||||
}
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user