Punto 2: the 40 SWA pools now store a circular ring of swa_win+SWA_CHUNK_CAP(512)
=1536 positions (slot=pos%seq) instead of the whole 32768 context (~5GiB wasted);
the 8 global pools keep a linear max_context cache allocated ONCE. KV capacities
are fixed per layer kind => no drop+realloc when a session crosses a 4096 multiple.
kv_store{,_f16,_f16_batched} write pos%seq; flash decode/prefill/prefill_wmma read
p%seq (global seq>pos => identity). snapshot/restore map the window onto ring slots
with 1 or 2 pitched copies (wrap). serve clamps prefill chunk to SWA_CHUNK_CAP so
ring>=swa_win+chunk holds. proofs/prefix_cache.lean Part E (0 sorryAx): slot_inj,
window_slots_distinct, ring_no_clobber, global_slot_id. New GPU swa_ring_kv_parity
(R=1536, P=2000) vs an independent host LINEAR oracle: 1.48e-5.
Punto 1: BUF_POOL buckets pooled allocs in (4KiB,64MiB] to a power of two so variable
batch tails / future speculative B converge to a closed size set (no hipMalloc/Free
churn); larger allocs stay exact. Weight uploads (load_raw/load_bf16_as_f32/expert
matrix/arena) switched to alloc_unpooled: resident, exact, never pooled (rounding
them had added ~11GiB).
MEASURED gfx1150 FP16 KV chunk256: READY ~21.0GB; multi-turn 2048->13000->2048->5000
vram flat ~23.1-23.7GB (only bounded cap-8 checkpoint turnover, no KV resize);
prefill 2048 ~12.2s, 13000 ~78.6s (~6ms/tok). Suite GREEN hip37 ld23 gw4, CPU23, Lean6.
387 lines
17 KiB
Lean4
387 lines
17 KiB
Lean4
/-
|
|
vetta-infer — Prefix Checkpoint Cache formal verification (Lean 4 core)
|
|
|
|
Models the prefix-reuse cache adopted in concept from strata's `--auto-ckpt`
|
|
(reference `proofs/auto_ckpt_pool.lean`), adapted to vetta's contiguous per-pass
|
|
KV + ShortConv history in a single conversation slot. Because vetta's KV is a
|
|
contiguous `[n_kv, max_seq, hd]` cache (not a paged SWA ring), the reference's
|
|
ring-page arithmetic is unnecessary. The core correctness invariant is the
|
|
observational equivalence of restore+suffix-prefill vs cold prefill, which
|
|
follows from causality of the forward (KV attention and ShortConv are causal:
|
|
the state after position p depends only on tokens [0..p)).
|
|
|
|
Part A — Causal prefill fold & restore equivalence (the correctness core).
|
|
Part B — Checkpoint validity is intrinsic and preserved by pool operations.
|
|
Part C — Auto-pool eviction: pinned anchors {512,1024} never evicted (I3),
|
|
size cap, no duplicate seq_len, an unpinned victim exists when cap ≥ 3.
|
|
|
|
0 sorryAx; core tactics only (no Mathlib).
|
|
-/
|
|
|
|
namespace VettaPrefixCache
|
|
|
|
/-- Token ids are natural numbers. -/
|
|
abbrev Tok := Nat
|
|
|
|
/-! ## Part A: causal prefill fold & restore equivalence -/
|
|
|
|
variable {S : Type}
|
|
|
|
/-- Prefill folds the causal step over a token list starting from state `s`.
|
|
The state encapsulates all history `[0..pos)`, so each step is causal. -/
|
|
def prefill (step : S → Tok → S) (s : S) : List Tok → S
|
|
| [] => s
|
|
| t :: ts => prefill step (step s t) ts
|
|
|
|
/-- Cold prefill from the initial state. -/
|
|
def cold (init : S) (step : S → Tok → S) (toks : List Tok) : S :=
|
|
prefill step init toks
|
|
|
|
theorem prefill_nil (step : S → Tok → S) (s : S) : prefill step s [] = s := rfl
|
|
|
|
theorem prefill_cons (step : S → Tok → S) (s : S) (t : Tok) (ts : List Tok) :
|
|
prefill step s (t :: ts) = prefill step (step s t) ts := rfl
|
|
|
|
/-- Folding over a concatenation composes: prefill (prefill s a) b = prefill s (a ++ b). -/
|
|
theorem prefill_append (step : S → Tok → S) (s : S) (a b : List Tok) :
|
|
prefill step (prefill step s a) b = prefill step s (a ++ b) := by
|
|
induction a generalizing s with
|
|
| nil => simp [prefill]
|
|
| cons t ts ih => simp [prefill, ih]
|
|
|
|
/-- A checkpoint stores the state reached after cold-prefilling its token prefix. -/
|
|
structure Ckpt (S : Type) where
|
|
toks : List Tok
|
|
state : S
|
|
|
|
/-- Validity: the stored state equals cold prefill of the stored prefix. -/
|
|
def Valid (init : S) (step : S → Tok → S) (ck : Ckpt S) : Prop :=
|
|
ck.state = cold init step ck.toks
|
|
|
|
/-- CORE correctness: restoring a valid checkpoint and prefilling the suffix is
|
|
observationally identical to cold-prefilling the whole `prefix ++ suffix`.
|
|
This is what makes partial prefill (only the new tokens) exact. -/
|
|
theorem restore_equiv (init : S) (step : S → Tok → S) (ck : Ckpt S) (suffix : List Tok)
|
|
(h : Valid init step ck) :
|
|
prefill step ck.state suffix = cold init step (ck.toks ++ suffix) := by
|
|
unfold Valid cold at h
|
|
rw [h]
|
|
exact prefill_append step init ck.toks suffix
|
|
|
|
/-- Restoring with an empty suffix reproduces the checkpoint state exactly. -/
|
|
theorem restore_nil (init : S) (step : S → Tok → S) (ck : Ckpt S)
|
|
(_h : Valid init step ck) : prefill step ck.state [] = ck.state := by
|
|
simp [prefill]
|
|
|
|
/-! ## Part B: validity is intrinsic and preserved by pool operations -/
|
|
|
|
/-- A pool is a list of checkpoints; the slot invariant is that every stored
|
|
checkpoint is valid (its state is the cold prefill of its own prefix). -/
|
|
def PoolValid (init : S) (step : S → Tok → S) (pool : List (Ckpt S)) : Prop :=
|
|
∀ ck ∈ pool, Valid init step ck
|
|
|
|
theorem poolValid_nil (init : S) (step : S → Tok → S) :
|
|
PoolValid init step [] := by
|
|
intro ck h; cases h
|
|
|
|
/-- Filtering a pool (invalidation) preserves validity of the survivors. -/
|
|
theorem poolValid_filter (init : S) (step : S → Tok → S) (p : Ckpt S → Bool)
|
|
(pool : List (Ckpt S)) (h : PoolValid init step pool) :
|
|
PoolValid init step (pool.filter p) := by
|
|
intro ck hck
|
|
exact h ck (List.mem_filter.mp hck).1
|
|
|
|
/-- Appending a checkpoint preserves validity when the new checkpoint is valid. -/
|
|
theorem poolValid_append (init : S) (step : S → Tok → S) (pool : List (Ckpt S))
|
|
(ck : Ckpt S) (h : PoolValid init step pool) (hck : Valid init step ck) :
|
|
PoolValid init step (pool ++ [ck]) := by
|
|
intro x hx
|
|
rcases List.mem_append.mp hx with hx | hx
|
|
· exact h x hx
|
|
· simp at hx; subst hx; exact hck
|
|
|
|
/-- Invalidation above a limit keeps only checkpoints whose prefix length ≤ limit;
|
|
validity is preserved (a corollary of filter preservation). -/
|
|
theorem invalidate_above_valid (init : S) (step : S → Tok → S) (pool : List (Ckpt S))
|
|
(limit : Nat) (h : PoolValid init step pool) :
|
|
PoolValid init step (pool.filter (fun ck => ck.toks.length ≤ limit)) :=
|
|
poolValid_filter init step (fun ck => ck.toks.length ≤ limit) pool h
|
|
|
|
/-! ## Part C: auto-pool eviction invariants (I3 pinned anchors, cap, nodup) -/
|
|
|
|
/-- Pinned static anchors are never evicted (reference invariant I3). -/
|
|
def pinned (seq : Nat) : Bool := seq == 512 || seq == 1024
|
|
|
|
theorem pinned_512 : pinned 512 = true := by simp [pinned]
|
|
theorem pinned_1024 : pinned 1024 = true := by simp [pinned]
|
|
|
|
/-- Membership of a `find?` result (self-contained, core only). -/
|
|
theorem mem_of_find_some {p : Nat → Bool} {l : List Nat} {a : Nat}
|
|
(h : l.find? p = some a) : a ∈ l := by
|
|
induction l with
|
|
| nil => simp [List.find?] at h
|
|
| cons x xs ih =>
|
|
simp only [List.find?] at h
|
|
split at h
|
|
· injection h with hx; subst hx; simp
|
|
· exact List.mem_cons.mpr (Or.inr (ih h))
|
|
|
|
/-- The predicate of a `find?` result holds. -/
|
|
theorem pred_of_find_some {p : Nat → Bool} {l : List Nat} {a : Nat}
|
|
(h : l.find? p = some a) : p a = true := by
|
|
induction l with
|
|
| nil => simp [List.find?] at h
|
|
| cons x xs ih =>
|
|
simp only [List.find?] at h
|
|
split at h
|
|
· injection h with hx; subst hx; assumption
|
|
· exact ih h
|
|
|
|
/-- Filtering out a present element strictly shrinks the list (no Nodup needed). -/
|
|
theorem length_filter_ne_lt {l : List Nat} {a : Nat} (hmem : a ∈ l) :
|
|
(l.filter (· != a)).length < l.length := by
|
|
induction l with
|
|
| nil => cases hmem
|
|
| cons x xs ih =>
|
|
by_cases hxa : x = a
|
|
· have hf : (x :: xs).filter (· != a) = xs.filter (· != a) := by
|
|
simp only [List.filter]; split <;> simp_all
|
|
rw [hf]
|
|
have := List.length_filter_le (· != a) xs
|
|
simp; omega
|
|
· have hf : (x :: xs).filter (· != a) = x :: xs.filter (· != a) := by
|
|
simp only [List.filter]; split <;> simp_all
|
|
rw [hf]
|
|
have hmem' : a ∈ xs := by
|
|
rcases List.mem_cons.mp hmem with h | h
|
|
· exact absurd h.symm hxa
|
|
· exact h
|
|
have := ih hmem'
|
|
simp; omega
|
|
|
|
/-- Filtering preserves distinctness (self-contained, core only). -/
|
|
theorem nodup_filter {l : List Nat} (p : Nat → Bool) (h : l.Nodup) :
|
|
(l.filter p).Nodup := by
|
|
induction l with
|
|
| nil => simp [List.filter]
|
|
| cons x xs ih =>
|
|
have hc := List.nodup_cons.mp h
|
|
simp only [List.filter]
|
|
split
|
|
· apply List.nodup_cons.mpr
|
|
constructor
|
|
· intro hh; exact hc.1 (List.mem_filter.mp hh).1
|
|
· exact ih hc.2
|
|
· exact ih hc.2
|
|
|
|
/-- Pool insert models `save_auto_checkpoint`: keep existing seq, else grow below
|
|
cap, else evict the first unpinned victim and append. If every entry is pinned
|
|
the pool is left unchanged (no unpinned victim). -/
|
|
def poolInsert (cap : Nat) (pool : List Nat) (seq : Nat) : List Nat :=
|
|
if seq ∈ pool then pool
|
|
else if pool.length < cap then pool ++ [seq]
|
|
else match pool.find? (fun s => !pinned s) with
|
|
| some v => pool.filter (· != v) ++ [seq]
|
|
| none => pool
|
|
|
|
/-- Inserting an already-present seq is a no-op. -/
|
|
theorem poolInsert_mem (cap : Nat) (pool : List Nat) (seq : Nat) (h : seq ∈ pool) :
|
|
poolInsert cap pool seq = pool := by
|
|
simp [poolInsert, h]
|
|
|
|
/-- The pool size never exceeds the cap (assuming it started ≤ cap). -/
|
|
theorem poolInsert_length_le (cap : Nat) (pool : List Nat) (seq : Nat)
|
|
(h : pool.length ≤ cap) : (poolInsert cap pool seq).length ≤ cap := by
|
|
unfold poolInsert
|
|
split
|
|
· exact h
|
|
· split
|
|
· rename_i hlt
|
|
simp only [List.length_append, List.length_singleton]
|
|
omega
|
|
· split
|
|
· rename_i v hv
|
|
have hmem : v ∈ pool := mem_of_find_some hv
|
|
have hfull : pool.length = cap := by omega
|
|
have hf : (pool.filter (· != v)).length < pool.length := length_filter_ne_lt hmem
|
|
simp only [List.length_append, List.length_singleton]
|
|
omega
|
|
· exact h
|
|
|
|
/-- I3: a pinned anchor present in the pool survives insertion. -/
|
|
theorem poolInsert_preserves_pinned (cap : Nat) (pool : List Nat) (seq s : Nat)
|
|
(hs : pinned s = true) (hmem : s ∈ pool) : s ∈ poolInsert cap pool seq := by
|
|
unfold poolInsert
|
|
split
|
|
· exact hmem
|
|
· split
|
|
· exact List.mem_append_left _ hmem
|
|
· split
|
|
· rename_i v hv
|
|
have hvun : pinned v = false := by
|
|
have := pred_of_find_some hv
|
|
simp at this
|
|
exact this
|
|
have hne : s ≠ v := by
|
|
intro heq; subst heq; rw [hs] at hvun; simp at hvun
|
|
have hf : s ∈ pool.filter (· != v) := by
|
|
apply List.mem_filter.mpr; exact ⟨hmem, by simp [hne]⟩
|
|
exact List.mem_append_left _ hf
|
|
· exact hmem
|
|
|
|
/-- Insertion preserves distinctness of seq_len entries. -/
|
|
theorem poolInsert_nodup (cap : Nat) (pool : List Nat) (seq : Nat)
|
|
(hnd : pool.Nodup) : (poolInsert cap pool seq).Nodup := by
|
|
unfold poolInsert
|
|
split
|
|
· exact hnd
|
|
· rename_i hnotmem
|
|
split
|
|
· exact List.nodup_append.mpr ⟨hnd, (by simp : [seq].Nodup), by
|
|
intro a ha b hb; simp only [List.mem_singleton] at hb; subst hb
|
|
intro heq; subst heq; exact hnotmem ha⟩
|
|
· split
|
|
· rename_i v hv
|
|
have hfnd : (pool.filter (· != v)).Nodup := nodup_filter _ hnd
|
|
have hseqnot' : seq ∉ pool.filter (· != v) := by
|
|
intro hh; exact hnotmem (List.mem_filter.mp hh).1
|
|
exact List.nodup_append.mpr ⟨hfnd, (by simp : [seq].Nodup), by
|
|
intro a ha b hb; simp only [List.mem_singleton] at hb; subst hb
|
|
intro heq; subst heq; exact hseqnot' ha⟩
|
|
· exact hnd
|
|
|
|
/- With cap ≥ 3 the pool always holds an unpinned victim: the only pinned anchors
|
|
are {512, 1024} (two values), so a Nodup pool of length ≥ 3 cannot be all
|
|
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
|
|
|
|
/-!
|
|
Part E — SWA KV ring (2026-12-02). The 40 SWA pools store a circular ring of
|
|
`R = swa_win + chunk_cap` positions (`slot R p = p % R`); the 8 global pools keep
|
|
a linear cache of `MAX_CONTEXT` positions, where `p < R` makes `%` the identity
|
|
(`global_slot_id`). Correctness of the ring rests on three facts, proved here
|
|
BEFORE the kernel/`LayerState` change (AGENTS.md invariant 6):
|
|
E1 `slot_inj` / `window_slots_distinct`: a causal window spans ≤ R positions, so
|
|
its slots are pairwise distinct (a read never aliases two logical positions).
|
|
E2 `ring_no_clobber`: with `R ≥ swa_win + chunk`, the writes of one batched chunk
|
|
(`[c, c+chunk)`) cannot overwrite a slot still needed by an earlier token's
|
|
window read (`[c-swa_win, c+chunk)`), except the same logical position.
|
|
E3 `global_slot_id`: below the ring length the modulo is the identity, so the
|
|
global (linear) pools and all small-seq parity tests are unaffected.
|
|
-/
|
|
|
|
/-- Ring slot of logical position `p` in a ring of `R` positions. -/
|
|
def slot (R p : Nat) : Nat := p % R
|
|
|
|
/-- E1 core: two positions at distance < R with the same slot are equal. -/
|
|
theorem slot_inj (R x y : Nat) (_hR : 0 < R) (hxy : x ≤ y) (hlt : y - x < R)
|
|
(h : x % R = y % R) : x = y := by
|
|
have hx := Nat.mod_add_div x R
|
|
have hy := Nat.mod_add_div y R
|
|
rw [h] at hx
|
|
have hq : x / R ≤ y / R := Nat.div_le_div_right hxy
|
|
have hAB : R * (x / R) ≤ R * (y / R) := Nat.mul_le_mul_left R hq
|
|
have hyx : y - x = R * (y / R) - R * (x / R) := by omega
|
|
have h2 : y - x = R * (y / R - x / R) := by
|
|
rw [← Nat.mul_sub_left_distrib] at hyx
|
|
exact hyx
|
|
have hd : y / R - x / R = 0 := by
|
|
cases Nat.eq_zero_or_pos (y / R - x / R) with
|
|
| inl h0 => exact h0
|
|
| inr hpos =>
|
|
have h1d : 1 ≤ y / R - x / R := by omega
|
|
have hge : R ≤ R * (y / R - x / R) := by
|
|
simpa [Nat.mul_one] using Nat.mul_le_mul_left R h1d
|
|
omega
|
|
rw [hd, Nat.mul_zero] at h2
|
|
omega
|
|
|
|
/-- E1: within any half-open interval of length ≤ R the ring slot is injective,
|
|
so a causal window (span ≤ swa_win < R) never reads one slot as two positions. -/
|
|
theorem window_slots_distinct (R a b p q : Nat) (hR : 0 < R) (hb : b ≤ R)
|
|
(hp : a ≤ p) (hp' : p < a + b) (hq : a ≤ q) (hq' : q < a + b)
|
|
(h : slot R p = slot R q) : p = q := by
|
|
unfold slot at h
|
|
have hlt : p - q < R := by omega
|
|
have hlt' : q - p < R := by omega
|
|
cases Nat.le_total p q with
|
|
| inl hle => exact slot_inj R p q hR hle (by omega) h
|
|
| inr hle => exact (slot_inj R q p hR hle (by omega) h.symm).symm
|
|
|
|
/-- E2: a batched chunk's KV writes cannot clobber a slot an earlier token of the
|
|
same chunk still reads, provided `R ≥ swa_win + chunk`. `w` ranges over the
|
|
chunk's writes `[c, c+chunk)`, `r` over the union of window reads
|
|
`[c-swa_win, c+chunk)`; equal slots force `w = r` (the causal same-position
|
|
write-then-read, which is ordered by the kernel). -/
|
|
theorem ring_no_clobber (R win chunk c w r : Nat) (hR : 0 < R)
|
|
(hcap : win + chunk ≤ R)
|
|
(hw : c ≤ w) (hw' : w < c + chunk)
|
|
(hr : c - win ≤ r) (hr' : r < c + chunk)
|
|
(h : slot R w = slot R r) : w = r := by
|
|
unfold slot at h
|
|
cases Nat.le_total w r with
|
|
| inl hle =>
|
|
have hlt : r - w < R := by omega
|
|
exact slot_inj R w r hR hle hlt h
|
|
| inr hle =>
|
|
have hlt : w - r < R := by omega
|
|
exact (slot_inj R r w hR hle hlt h.symm).symm
|
|
|
|
/-- E3: below the ring length the modulo is the identity — global (linear) pools
|
|
and every small-`seq` parity test behave exactly as before the ring. -/
|
|
theorem global_slot_id (R p : Nat) (h : p < R) : slot R p = p := by
|
|
unfold slot
|
|
exact Nat.mod_eq_of_lt h
|
|
|
|
end VettaPrefixCache |