[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:
1 parent
a6ca73105c
commit
47f8b52070
4 files changed
+159
-42
No files matched your search
@@ -277,20 +277,31 @@ pub fn new_state(d: &Dims, max_seq: usize, n_passes: usize) -> Result<LayerState
|
||||
/// it and prefilling the suffix is exact (proofs/prefix_cache.lean restore_equiv).
|
||||
pub struct LayerSnapshot {
|
||||
pub seq_len: usize,
|
||||
/// First KV position stored in this snapshot. For full-attn layers it is 0
|
||||
/// (whole prefix); for SWA layers it is seq_len - min(seq_len, swa_win), so
|
||||
/// only the causal window is checkpointed (the older KV is never read again).
|
||||
pub kv_start: 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> {
|
||||
/// Snapshot the KV prefix plus the current ShortConv history rings. For a
|
||||
/// SWA layer (`swa`) only the last `min(seq_len, swa_win)` KV positions are
|
||||
/// stored (the causal window): attention at any future pos >= seq_len reads
|
||||
/// [pos-swa_win+1..pos] ⊆ [kv_start..pos], so the discarded older KV is never
|
||||
/// observed (proofs/prefix_cache.lean swa_window_restore_equiv). Full-attn
|
||||
/// layers attend globally and keep the whole `[0, seq_len)` prefix.
|
||||
pub fn snapshot(&self, d: &Dims, seq_len: usize, swa: bool) -> Result<LayerSnapshot, String> {
|
||||
let es = if f16_kv() { 2 } else { 4 };
|
||||
let row = seq_len * d.hd * es;
|
||||
let win = if swa { seq_len.min(d.swa_win.max(0) as usize) } else { seq_len };
|
||||
let kv_start = seq_len - win;
|
||||
let row = win * d.hd * es;
|
||||
let src_off = kv_start * 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)?;
|
||||
b.copy2d_at(0, src, src_off, 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<_>, _>>()?;
|
||||
@@ -299,7 +310,7 @@ impl LayerState {
|
||||
let b = DeviceBuf::alloc(src.bytes)?; b.copy_from(src, src.bytes)?; Ok(b)
|
||||
};
|
||||
Ok(LayerSnapshot {
|
||||
seq_len, k_cache, v_cache,
|
||||
seq_len, kv_start, 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])?],
|
||||
@@ -307,17 +318,21 @@ impl LayerState {
|
||||
})
|
||||
}
|
||||
|
||||
/// 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].
|
||||
/// Restore the KV prefix `[kv_start, seq_len)` + history into `self` (whose
|
||||
/// max_seq must be >= snap.seq_len). For SWA snapshots kv_start > 0: positions
|
||||
/// below it are left untouched (never read by the causal window); the suffix
|
||||
/// prefill fills `[seq_len, pos)`. Attention at pos >= seq_len reads only the
|
||||
/// restored window + suffix, so this is observationally exact (restore_equiv).
|
||||
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;
|
||||
let win = snap.seq_len - snap.kv_start;
|
||||
let row = win * d.hd * es;
|
||||
let dst_off = snap.kv_start * 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)?;
|
||||
dst.copy2d_at(dst_off, src, 0, 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)?;
|
||||
dst.copy2d_at(dst_off, src, 0, 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)?;
|
||||
@@ -327,6 +342,20 @@ impl LayerState {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Zero the ShortConv history rings (async device memset). Used when reusing
|
||||
/// persistent states for a COLD request (no prefix restore): the KV prefix is
|
||||
/// overwritten by prefill before any causal read, but the hist rings seed the
|
||||
/// first 3 shortconv steps and must start empty for a fresh conversation.
|
||||
pub fn reset_history(&mut self) -> Result<(), String> {
|
||||
for i in 0..3 {
|
||||
self.hist_k[i].zero_async()?;
|
||||
self.hist_v[i].zero_async()?;
|
||||
self.hist_a[i].zero_async()?;
|
||||
self.hist_m[i].zero_async()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// KV cache dtype switch. The CPU-oracle parity baseline (and the ld packer
|
||||
|
||||
Reference in new issue
Block a user