Prove2Me
Navigate
DiscoverCollectionsFormalpediaBlogsUsersMomentumMy Missions+
Prove2Me
⌕
Log in
← Formalpedia

Chapter AttentionMixture

Definition
ChapterAttentionMixture

by leonardopedro · Oct 6, 2026 · Mathlib 0df444a (Lean v4.33.1)

definitionsformalizationtimepiece

Formal definitions for the timepiece Lean 4 formalization (source chapter BookProof/ChapterAttentionMixture.lean): generated def bundle for ChapterAttentionMixture. See BookProof/ChapterAttentionMixture.lean for full context.

Definition code
import Definitions.Def_ChapterAttentionOutput
import Definitions.Def_ChapterAttentionEntropy
import Definitions.Def_ChapterSoftmaxOrder
import Definitions.Def_ChapterSoftmaxSharpness
import Mathlib


/-!
# Chapter "The Coherent State of Attention": many heads are one mixture

A transformer layer runs several attention heads in parallel and combines them.
Probabilistically, a bank of heads read by a common set of values is a **mixture**
of the individual attention distributions, and this module proves the three facts
that make the mixture picture work.

* `mixture_isProb` — a convex combination of attention distributions is again an
  attention distribution (a probability vector over the keys).
* `observableExpectation_mixture` — **the ensemble output is the weighted mean of
  the head outputs**: combining the heads' distributions first and reading the
  values once is the same as reading the values head by head and averaging.  This
  is why the "concatenate then project" construction of multi-head attention is
  legitimate.
* `le_shannonEntropy_mixture` — **mixing heads never destroys information.**  By
  concavity of `x ↦ -x log x`, the entropy of the mixture is at least the mean of
  the head entropies: a bank of confident but disagreeing heads produces an
  uncertain consensus, never the reverse.

The multi-head specializations `multiHead_output_eq_mean` and
`le_shannonEntropy_multiHead` apply all three to the Softmax heads of
`ChapterSoftmaxSharpness` — heads may differ both in their scores and in their
temperatures.

Everything here is `sorry`-free and `axiom`-free (only `propext`,
`Classical.choice`, `Quot.sound`).
-/

open scoped BigOperators

noncomputable section

namespace BookProof.ChapterAttentionMixture

open BookProof.ChapterSoftmaxSharpness BookProof.ChapterSoftmaxOrder
  BookProof.ChapterObservableExpectation BookProof.ChapterAttentionOutput
  BookProof.ChapterAttentionEntropy

variable {m H : ℕ} {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E]

/-! ## The mixture of a bank of attention distributions -/

/-- The mixture of the distributions `p h` with mixing weights `w h`. -/
def mixture (w : Fin H → ℝ) (p : Fin H → Fin m → ℝ) (j : Fin m) : ℝ := ∑ h, w h * p h j







/-! ## The output of the ensemble -/



/-! ## Mixing heads never destroys information -/







/-! ## Multi-head attention -/

/-- The attention distribution of a bank of Softmax heads: head `h` has its own
scores `s h` and its own inverse temperature `beta h`. -/
def multiHead (w : Fin H → ℝ) (beta : Fin H → ℝ) (s : Fin H → Fin m → ℝ) :
    Fin m → ℝ :=
  mixture w (fun h => scoreSoftmax (beta h) (s h))







end BookProof.ChapterAttentionMixture

end
Source
https://github.com/leonardopedro/timepiece/blob/61595bc/BookProof/ChapterAttentionMixture.lean

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paper
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me