Eviction.lean
Lean 4 source, lean/DataMiningAsObservation/Eviction.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
Eviction.lean. Chapter 13 section 13.6, eviction.
Evicting tokens from a cache replaces attention over all tokens with attention over the kept
subset. The output after eviction is a convex combination of the kept values, so it lies
between their smallest and largest, and when the kept values agree it equals them whatever
was evicted. Evicting a token whose value equals the current output leaves the output
where it is. The chapter's serving-stack numbers, ninety-seven percent evicted at a five
percent keep against an oracle, are the ledger's measurements and are not re-derived here.
-/
import Mathlib
import DataMiningAsObservation.Softmax
namespace DMO.Eviction
open Finset DMO.Softmax
variable {ι : Type*} [Fintype ι] [DecidableEq ι]
/-- Attention over a kept subset `K` of tokens with scores `z` and values `v`. -/
noncomputable def keptOutput (K : Finset ι) (z v : ι → ℝ) : ℝ :=
∑ i ∈ K, (Real.exp (z i) / ∑ j ∈ K, Real.exp (z j)) * v i
omit [Fintype ι] [DecidableEq ι] in
/-- The kept weights sum to one. -/
theorem kept_weights_sum (K : Finset ι) (hK : K.Nonempty) (z : ι → ℝ) :
∑ i ∈ K, Real.exp (z i) / ∑ j ∈ K, Real.exp (z j) = 1 := by
rw [← Finset.sum_div, div_self]
exact (Finset.sum_pos (fun j _ => Real.exp_pos (z j)) hK).ne'
omit [Fintype ι] [DecidableEq ι] in
/-- **The output after eviction lies between the kept values.** -/
theorem keptOutput_le_max (K : Finset ι) (hK : K.Nonempty) (z v : ι → ℝ) :
keptOutput K z v ≤ K.sup' hK v := by
unfold keptOutput
have hpos : 0 < ∑ j ∈ K, Real.exp (z j) := Finset.sum_pos (fun j _ => Real.exp_pos (z j)) hK
calc ∑ i ∈ K, (Real.exp (z i) / ∑ j ∈ K, Real.exp (z j)) * v i
≤ ∑ i ∈ K, (Real.exp (z i) / ∑ j ∈ K, Real.exp (z j)) * K.sup' hK v := by
apply Finset.sum_le_sum
intro i hi
exact mul_le_mul_of_nonneg_left (Finset.le_sup' v hi) (by positivity)
_ = K.sup' hK v := by rw [← Finset.sum_mul, kept_weights_sum K hK z, one_mul]
omit [Fintype ι] [DecidableEq ι] in
theorem min_le_keptOutput (K : Finset ι) (hK : K.Nonempty) (z v : ι → ℝ) :
K.inf' hK v ≤ keptOutput K z v := by
unfold keptOutput
have hpos : 0 < ∑ j ∈ K, Real.exp (z j) := Finset.sum_pos (fun j _ => Real.exp_pos (z j)) hK
calc K.inf' hK v
= ∑ i ∈ K, (Real.exp (z i) / ∑ j ∈ K, Real.exp (z j)) * K.inf' hK v := by
rw [← Finset.sum_mul, kept_weights_sum K hK z, one_mul]
_ ≤ ∑ i ∈ K, (Real.exp (z i) / ∑ j ∈ K, Real.exp (z j)) * v i := by
apply Finset.sum_le_sum
intro i hi
exact mul_le_mul_of_nonneg_left (Finset.inf'_le v hi) (by positivity)
omit [Fintype ι] [DecidableEq ι] in
/-- **Agreeing kept values fix the output**, whatever was evicted. -/
theorem keptOutput_const (K : Finset ι) (hK : K.Nonempty) (z : ι → ℝ) (c : ℝ) :
keptOutput K z (fun _ => c) = c := by
unfold keptOutput
rw [← Finset.sum_mul, kept_weights_sum K hK z, one_mul]
omit [DecidableEq ι] in
/-- **Eviction is a choice of subset**, and the whole cache is the case of no eviction. -/
theorem keptOutput_univ (z v : ι → ℝ) [Nonempty ι] :
keptOutput Finset.univ z v = ∑ i, softmax z i * v i := by
unfold keptOutput softmax
rfl
end DMO.Eviction