Attention.lean
Lean 4 source, lean/DataMiningAsObservation/Attention.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
Attention.lean. Equation 0.23 and chapter 11 section 11.1, attention.
An attention head weights each earlier token's value by the softmax of its query-key score
and sums. For scalar values the output lies between the smallest and largest value, since
the weights are a convex combination. The head reads the keys only through their scores
against the query, so two sets of keys with the same scores give the same output, whatever
else differs about them. That is the read subspace of the head, a few query-weighted
directions of each key, and the reason a reconstruction that preserves the keys' cosine can
still move the output.
-/
import Mathlib
import DataMiningAsObservation.Softmax
namespace DMO.Attention
open Finset DMO.Softmax
variable {ι : Type*} [Fintype ι] [Nonempty ι] {d : ℕ}
/-- The scores of a query against the keys, scaled by the square root of the dimension. -/
noncomputable def scores (q : Fin d → ℝ) (k : ι → Fin d → ℝ) (i : ι) : ℝ :=
(q ⬝ᵥ k i) / Real.sqrt d
/-- **Equation 0.23.** The head's output for scalar values. -/
noncomputable def output (q : Fin d → ℝ) (k : ι → Fin d → ℝ) (v : ι → ℝ) : ℝ :=
∑ i, softmax (scores q k) i * v i
/-- **The output never exceeds the largest value.** -/
theorem output_le_max (q : Fin d → ℝ) (k : ι → Fin d → ℝ) (v : ι → ℝ) :
output q k v ≤ Finset.univ.sup' Finset.univ_nonempty v := by
unfold output
calc ∑ i, softmax (scores q k) i * v i
≤ ∑ i, softmax (scores q k) i * Finset.univ.sup' Finset.univ_nonempty v := by
apply Finset.sum_le_sum
intro i _
exact mul_le_mul_of_nonneg_left (Finset.le_sup' v (Finset.mem_univ i)) (softmax_pos _ i).le
_ = Finset.univ.sup' Finset.univ_nonempty v := by
rw [← Finset.sum_mul, softmax_sum, one_mul]
/-- **The output is never below the smallest value.** -/
theorem min_le_output (q : Fin d → ℝ) (k : ι → Fin d → ℝ) (v : ι → ℝ) :
Finset.univ.inf' Finset.univ_nonempty v ≤ output q k v := by
unfold output
calc Finset.univ.inf' Finset.univ_nonempty v
= ∑ i, softmax (scores q k) i * Finset.univ.inf' Finset.univ_nonempty v := by
rw [← Finset.sum_mul, softmax_sum, one_mul]
_ ≤ ∑ i, softmax (scores q k) i * v i := by
apply Finset.sum_le_sum
intro i _
exact mul_le_mul_of_nonneg_left (Finset.inf'_le v (Finset.mem_univ i)) (softmax_pos _ i).le
omit [Nonempty ι] in
/-- **The head reads the keys only through their scores.** Two sets of keys with the same
scores against the query give the same output. -/
theorem output_congr (q : Fin d → ℝ) (k k' : ι → Fin d → ℝ) (v : ι → ℝ)
(h : ∀ i, q ⬝ᵥ k i = q ⬝ᵥ k' i) : output q k v = output q k' v := by
unfold output
have : scores q k = scores q k' := by
funext i; unfold scores; rw [h i]
rw [this]
omit [Nonempty ι] in
/-- **A key change orthogonal to the query is unread.** Adding to every key a vector the query
does not read leaves the output unchanged, however large the change. -/
theorem output_nuisance (q : Fin d → ℝ) (k : ι → Fin d → ℝ) (v : ι → ℝ) (w : ι → Fin d → ℝ)
(hw : ∀ i, q ⬝ᵥ w i = 0) : output q (fun i => k i + w i) v = output q k v := by
apply output_congr
intro i
rw [dotProduct_add, hw i, add_zero]
end DMO.Attention