[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
@@ -255,4 +255,55 @@ theorem poolInsert_nodup (cap : Nat) (pool : List Nat) (seq : Nat)
|
||||
pinned. The engine relies on this combinatorial fact (cap ≥ 3) when evicting;
|
||||
`poolInsert_preserves_pinned` proves the I3 invariant that anchors survive. -/
|
||||
|
||||
/-! ## Part D: SWA-window checkpoint observational equivalence
|
||||
|
||||
A SWA layer attends only to the causal window `[winStart w p, p]` at position
|
||||
`p` (kernel `k_flash_swa_gqa_*`: `k_start = p - w + 1` when `p ≥ w`, else 0).
|
||||
So a checkpoint at `seq_len = n` need only store KV from `kvStart w n = n - min n w`
|
||||
(the last `min n w` positions); the older KV is never read again. Restoring that
|
||||
window and prefilling the suffix is observationally exact for every future
|
||||
position `p ≥ n`. Full-attention layers keep the whole prefix (`kvStart = 0`).
|
||||
This is the memory refinement that shrinks SWA checkpoints (~40 of 48 pools). -/
|
||||
|
||||
/-- First KV position a SWA checkpoint at `n` must retain (window `w`). -/
|
||||
def kvStart (w n : Nat) : Nat := n - min n w
|
||||
|
||||
/-- First KV position the SWA attention reads at position `p` (window `w`). -/
|
||||
def winStart (w p : Nat) : Nat := if p ≥ w then p - w + 1 else 0
|
||||
|
||||
/-- CONTAINMENT: for any future position `p ≥ n`, the attention window starts at
|
||||
or after the checkpoint's retained start, so the window is fully covered by
|
||||
`[kvStart w n, p]` (restored window `[kvStart, n)` + suffix `[n, p]`). -/
|
||||
theorem kvStart_le_winStart (w n p : Nat) (h : n ≤ p) :
|
||||
kvStart w n ≤ winStart w p := by
|
||||
unfold kvStart winStart
|
||||
split
|
||||
· omega
|
||||
· have hn : n ≤ w := by omega
|
||||
have : min n w = n := Nat.min_eq_left hn
|
||||
omega
|
||||
|
||||
/-- Observational equivalence of windowed restore. `read kv p` is the SWA attention
|
||||
output, which by the kernel contract depends only on KV in `[winStart w p, p]`
|
||||
(`hwin`). If the restored+suffix cache `kv'` agrees with the cold cache `kv` on
|
||||
`[kvStart w n, p]` (`hagree` — restored window + causal suffix), then the read
|
||||
at `p` is identical. The containment lemma lifts the agreement to the window. -/
|
||||
theorem swa_window_restore_equiv {V R : Type} (w n p : Nat)
|
||||
(read : (Nat → V) → Nat → R)
|
||||
(hwin : ∀ kv kv' : Nat → V,
|
||||
(∀ i, winStart w p ≤ i → i ≤ p → kv i = kv' i) → read kv p = read kv' p)
|
||||
(kv kv' : Nat → V) (hn : n ≤ p)
|
||||
(hagree : ∀ i, kvStart w n ≤ i → i ≤ p → kv i = kv' i) :
|
||||
read kv p = read kv' p := by
|
||||
apply hwin
|
||||
intro i hi1 hi2
|
||||
exact hagree i (Nat.le_trans (kvStart_le_winStart w n p hn) hi1) hi2
|
||||
|
||||
/-- A full-attention checkpoint retains everything: its window covers the whole
|
||||
prefix (`n ≤ w`), so `kvStart w n = 0` and no KV is discarded. -/
|
||||
theorem kvStart_full (n w : Nat) (h : n ≤ w) : kvStart w n = 0 := by
|
||||
unfold kvStart
|
||||
have : min n w = n := Nat.min_eq_left h
|
||||
omega
|
||||
|
||||
end VettaPrefixCache
|
||||
Reference in new issue
Block a user