[Proofs] speculative_draft.lean: lossless greedy spec equivalence + rollback=truncate
Brick 1 of DSpark Step 1 (proof-first per AGENTS invariant 6): - spec_lossless: one speculative round (draft, batched causal verify, accept longest matching prefix, emit correction/bonus) emits EXACTLY the plain-greedy prefix of length match_len+1 for ANY draft => speculation changes throughput, never the token sequence. - kept_rejected_disjoint: with ring R >= window+chunk, kept window and rejected draft tail occupy disjoint ring slots => rollback = truncate history length, no KV data restore needed. Core tactics only; 0 sorryAx; all 7 proofs green.
This commit is contained in:
1 parent
ca3a6b0e15
commit
afce5cbbfe
1 file changed
+108
@@ -0,0 +1,108 @@
|
||||
/-
|
||||
vetta-infer — lossless greedy speculative decoding (DSpark Step 1).
|
||||
|
||||
Models the accept/rollback control flow of block speculative decoding and
|
||||
proves the two invariants that make it safe to ship:
|
||||
|
||||
(A) LOSSLESS GREEDY: one speculative round (draft ds, batched causal verify,
|
||||
accept longest matching prefix, emit correction/bonus) emits EXACTLY the
|
||||
plain-greedy prefix of length (match_len + 1). The draft therefore only
|
||||
changes HOW MANY tokens are produced per round, never WHICH tokens.
|
||||
Iterating rounds reproduces plain greedy token-for-token.
|
||||
|
||||
(B) ROLLBACK = TRUNCATE: with the SWA ring capacity R >= window + chunk
|
||||
(proofs/prefix_cache.lean Part E), the rejected draft tail occupies ring
|
||||
slots disjoint from the kept window, so rolling back to the accepted
|
||||
length needs NO data restore — truncating the history length suffices.
|
||||
|
||||
Core prover tactics only; no Mathlib. `#print axioms` must show no sorryAx.
|
||||
-/
|
||||
|
||||
namespace Vetta.Spec
|
||||
|
||||
/-- Longest prefix of the draft `ds` that equals plain greedy from `p`. -/
|
||||
def match_len (g : List Nat → Nat) (p : List Nat) : List Nat → Nat
|
||||
| [] => 0
|
||||
| d :: rest => if g p = d then 1 + match_len g (p ++ [d]) rest else 0
|
||||
|
||||
/-- Plain greedy: `greedy_seq g p n` = the next n greedy tokens after p. -/
|
||||
def greedy_seq (g : List Nat → Nat) (p : List Nat) : Nat → List Nat
|
||||
| 0 => []
|
||||
| n + 1 => greedy_seq g p n ++ [g (p ++ greedy_seq g p n)]
|
||||
|
||||
/-- One speculative round as the accept/correction loop: walk the draft while it
|
||||
matches greedy; on the first mismatch (or end) emit one greedy token. -/
|
||||
def spec_out (g : List Nat → Nat) (p : List Nat) : List Nat → List Nat
|
||||
| [] => p ++ [g p]
|
||||
| d :: rest => if g p = d then spec_out g (p ++ [d]) rest else p ++ [g p]
|
||||
|
||||
/-- Greedy shift: starting one token later (at the greedy token) drops the head. -/
|
||||
theorem greedy_shift (g : List Nat → Nat) (p : List Nat) (n : Nat) :
|
||||
greedy_seq g p (n + 1) = [g p] ++ greedy_seq g (p ++ [g p]) n := by
|
||||
induction n with
|
||||
| zero => simp [greedy_seq]
|
||||
| succ n ih =>
|
||||
show greedy_seq g p (n + 1) ++ [g (p ++ greedy_seq g p (n + 1))]
|
||||
= [g p] ++ (greedy_seq g (p ++ [g p]) n
|
||||
++ [g ((p ++ [g p]) ++ greedy_seq g (p ++ [g p]) n)])
|
||||
rw [ih]
|
||||
simp [List.append_assoc]
|
||||
|
||||
/-- (A) LOSSLESS GREEDY: a speculative round emits exactly the plain-greedy
|
||||
prefix of length match_len+1, for ANY draft (even an adversarial one). -/
|
||||
theorem spec_lossless (g : List Nat → Nat) (p : List Nat) (ds : List Nat) :
|
||||
spec_out g p ds = p ++ greedy_seq g p (match_len g p ds + 1) := by
|
||||
induction ds generalizing p with
|
||||
| nil => simp [spec_out, match_len, greedy_seq]
|
||||
| cons d rest ih =>
|
||||
simp only [spec_out, match_len]
|
||||
split
|
||||
· -- g p = d : accept head, recurse on (p ++ [d])
|
||||
rename_i h
|
||||
rw [ih (p ++ [d])]
|
||||
rw [greedy_shift (p := p)]
|
||||
simp [h, List.append_assoc, Nat.add_comm]
|
||||
· -- g p != d : reject immediately, emit the greedy correction
|
||||
simp [greedy_seq]
|
||||
|
||||
/-- Local ring injectivity (same statement as prefix_cache.lean Part E, kept
|
||||
here so this file verifies standalone with `lean`). -/
|
||||
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).symm
|
||||
have hy := (Nat.mod_add_div y R).symm
|
||||
rw [h] at hx
|
||||
have hq : x / R ≤ y / R := Nat.div_le_div_right hxy
|
||||
have hlt' : R * (y / R - x / R) < R := by
|
||||
have h1 : R * (y / R - x / R) = R * (y / R) - R * (x / R) := by
|
||||
rw [Nat.mul_sub_left_distrib]
|
||||
have h2 : R * (y / R) - R * (x / R) = y - x := by omega
|
||||
omega
|
||||
have h0 : y / R - x / R = 0 := by
|
||||
cases Nat.eq_zero_or_pos (y / R - x / R) with
|
||||
| inl hz => exact hz
|
||||
| inr hp =>
|
||||
have hge : R ≤ R * (y / R - x / R) := by
|
||||
simpa [Nat.mul_one] using Nat.mul_le_mul_left R hp
|
||||
exfalso
|
||||
exact Nat.not_le_of_gt hlt' hge
|
||||
have hqeq : x / R = y / R := by omega
|
||||
rw [hqeq] at hx
|
||||
omega
|
||||
|
||||
/-- (B) kept window and rejected tail occupy disjoint ring slots when the span
|
||||
fits the ring: positions [a, k) kept vs [k, b) rejected, b - a < R. Hence
|
||||
rollback can truncate the history length without restoring any KV data. -/
|
||||
theorem kept_rejected_disjoint (R a k b i j : Nat) (hR : 0 < R)
|
||||
(hi : a ≤ i) (hi2 : i < k) (hj : k ≤ j) (hj2 : j < b) (hspan : b - a < R) :
|
||||
i % R ≠ j % R := by
|
||||
intro heq
|
||||
have hle : i ≤ j := Nat.le_trans (Nat.le_of_lt hi2) hj
|
||||
have hlt : j - i < R := by omega
|
||||
have := slot_inj R i j hR hle hlt heq
|
||||
omega
|
||||
|
||||
end Vetta.Spec
|
||||
|
||||
#print axioms Vetta.Spec.spec_lossless
|
||||
#print axioms Vetta.Spec.kept_rejected_disjoint
|
||||
Reference in new issue
Block a user