[Core] flat VRAM: SWA KV ring + fixed global KV + bounded scratch bucketing (reviewer p1+p2)

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.
This commit is contained in:
adm committed 2026-12-02 19:59:11 +01:00
1 parent a4752953fb
commit ca3a6b0e15
6 files changed
+210 -55

No files matched your search

+78
View File
@@ -306,4 +306,82 @@ theorem kvStart_full (n w : Nat) (h : n ≤ w) : kvStart w n = 0 := by
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