Aether Catalog definitions — MachineLearning_TransformerUniversality_PermutationDichotomy
DefinitionMachineLearning_TransformerUniversality_PermutationDichotomyaether-catalogmachinelearning
Definition bundle for the Aether Catalog module MachineLearning.TransformerUniversality.PermutationDichotomy, transplanted by skeleton subtraction; supplies the types and constants the catalog's theorems import.
Definition code
-- Def bundle generated from MachineLearning/TransformerUniversality/PermutationDichotomy.lean by skeleton subtraction
import Mathlib
/-!
# The symmetry dichotomy for attention architectures
`Catalog/MachineLearning/TransformerUniversality/ContinuousUniversality.lean` proves that the
subalgebra generated by linear-attention readouts is uniformly dense in the continuous
functionals of a sequence, and
`Catalog/MachineLearning/TransformerUniversality/PositionalSymmetry.lean` shows that additive
positional encodings destroy permutation symmetry. Neither file answers the question those two
results pose together:
> *exactly which* sequence functionals can a symmetric (positional-encoding-free) attention
> architecture approximate, and does positional encoding really restore everything?
This file answers both, and does so at the natural level of generality: an arbitrary **finite
group of token symmetries**, presented as a homomorphism `act : Γ →* Equiv.Perm ι`. Taking
`Γ = Equiv.Perm ι` with `act = id` recovers the fully permutation-symmetric (absolute-position
free) transformer; taking `Γ` cyclic recovers relative/shift-equivariant attention.
Write `Seq ι κ = ι → κ → ℝ` for length-`ι` sequences of `κ`-dimensional features and let
`permAct σ x = x ∘ σ` be the token-permutation action. The architecture class is the
`ℝ`-subalgebra `attentionAlgebra` generated by the linear attention readouts (a readout with
uniform softmax weights is literally a uniform-attention head, see
`softmaxHeadRead_zero_scores`), and the *symmetric* class is its subset of `Γ`-invariant
elements.
Main results.
* `group_uniform_universal` — **positive half**: on every compact `Γ`-saturated set of
sequences, every `Γ`-invariant continuous functional is uniformly approximated by a
`Γ`-invariant element of `attentionAlgebra`. The proof runs Stone–Weierstrass and then
averages the approximant over `Γ`, which needs the algebra to be stable under the action
(`attentionAlgebra_compPerm_mem`). `symmetric_uniform_universal` and
`shift_uniform_universal` are the full-symmetric and cyclic-shift specializations.
* `invariant_defect_lower_bound`, `symmetric_class_closed_under_uniform_limits`,
`orbit_barrier_lower`, `orbit_barrier_attained` — **negative half**: the invariant class is
uniformly closed and the optimal invariant error on an orbit is *exactly* half the orbit
oscillation. `symmetry_barrier_position_read` makes this concrete: no model invariant under
a permutation that moves position `i₀` reads position `i₀` to accuracy better than `1/2`.
* `positional_restores_universality`, `positional_seq2seq_universality` — **the repair**: with
the one-hot positional encoding `posEnc`, *fully permutation-invariant* models on the
augmented feature space `κ ⊕ ι` approximate every continuous functional, and every continuous
sequence-to-sequence map, uniformly on compacta. The mechanism is an explicit invariant
continuous decoder `decodeSeq` inverting `posEnc`.
* `equivariant_uniform_universal` — **exact converse**: through the marked-token encoding,
`Γ`-invariant attention models uniformly approximate every continuous `Γ`-equivariant
sequence-to-sequence map, and the resulting head family is itself exactly equivariant. So
the expressive power of symmetric attention is *precisely* the equivariant continuous maps.
-/
open scoped BigOperators
namespace PermutationDichotomy
/-- Length-`ι` sequences of `κ`-dimensional real feature vectors. -/
abbrev Seq (ι κ : Type*) := ι → κ → ℝ
section Action
variable {ι κ : Type*}
/-- The token-permutation action on sequences: `permAct σ x` reads position `i` of the
permuted sequence off position `σ i` of the original. -/
def permAct (σ : Equiv.Perm ι) (x : Seq ι κ) : Seq ι κ := fun i => x (σ i)
theorem continuous_permAct (σ : Equiv.Perm ι) : Continuous (permAct σ (κ := κ)) :=
continuous_pi fun i => continuous_apply (σ i)
/-- The permutation action as a continuous map. -/
def permCM (σ : Equiv.Perm ι) : C(Seq ι κ, Seq ι κ) := ⟨permAct σ, continuous_permAct σ⟩
variable {Γ : Type*} [Group Γ]
/-- A functional of a sequence is *`Γ`-invariant* when the token symmetries in `Γ` do not
change its value. -/
def InvariantUnder (act : Γ →* Equiv.Perm ι) (f : Seq ι κ → ℝ) : Prop :=
∀ (g : Γ) (x : Seq ι κ), f (permAct (act g) x) = f x
/-- A set of sequences is *`Γ`-saturated* when it is closed under the token symmetries. -/
def SaturatedUnder (act : Γ →* Equiv.Perm ι) (K : Set (Seq ι κ)) : Prop :=
∀ g : Γ, ∀ x ∈ K, permAct (act g) x ∈ K
/-- Invariance under the *full* symmetric group of the token positions. -/
abbrev Invariant (f : Seq ι κ → ℝ) : Prop :=
InvariantUnder (MonoidHom.id (Equiv.Perm ι)) f
/-- Saturation under the *full* symmetric group of the token positions. -/
abbrev Saturated (K : Set (Seq ι κ)) : Prop :=
SaturatedUnder (MonoidHom.id (Equiv.Perm ι)) K
end Action
section Architecture
variable {ι κ : Type*} [Fintype ι] [Fintype κ]
/-- A linear attention readout with fixed weights, as a continuous functional. -/
noncomputable def readoutCM (w : Seq ι κ) : C(Seq ι κ, ℝ) where
toFun x := ∑ i, ∑ a, w i a * x i a
continuous_toFun := by
apply continuous_finset_sum
intro i _
apply continuous_finset_sum
intro a _
exact continuous_const.mul ((continuous_apply a).comp (continuous_apply i))
/-- A genuine softmax attention read: value `∑ a, w j a * x j a` at token `j`, weighted by the
softmax of the scores `s`. -/
noncomputable def softmaxHeadRead (s : ι → ℝ) (w : Seq ι κ) (x : Seq ι κ) : ℝ :=
∑ j, (Real.exp (s j) / ∑ k, Real.exp (s k)) * (∑ a, w j a * x j a)
/-- The architecture class: the `ℝ`-subalgebra of continuous functionals generated by the
linear attention readouts (attention heads followed by multiplicative mixing). -/
noncomputable def attentionAlgebra (ι κ : Type*) [Fintype ι] [Fintype κ] :
Subalgebra ℝ C(Seq ι κ, ℝ) :=
Algebra.adjoin ℝ (Set.range (readoutCM (ι := ι) (κ := κ)))
end Architecture
section GroupStability
variable {ι κ : Type*} [Fintype ι] [Fintype κ]
end GroupStability
section Symmetrization
variable {ι κ : Type*} [Fintype ι] [Fintype κ] {Γ : Type*} [Group Γ] [Fintype Γ]
/-- Group averaging of a model over the symmetry group `Γ`. -/
noncomputable def symmetrize (act : Γ →* Equiv.Perm ι) (q : C(Seq ι κ, ℝ)) : C(Seq ι κ, ℝ) :=
(Fintype.card Γ : ℝ)⁻¹ •
∑ g : Γ, (ContinuousMap.compRightAlgHom ℝ ℝ (permCM (act g))) q
end Symmetrization
section PositiveHalf
variable {ι κ : Type*} [Fintype ι] [Fintype κ] {Γ : Type*} [Group Γ] [Fintype Γ]
variable [DecidableEq ι]
end PositiveHalf
section NegativeHalf
variable {ι κ : Type*}
end NegativeHalf
section PositionalRepair
variable {ι κ : Type*} [Fintype ι] [DecidableEq ι] [Fintype κ]
/-- One-hot positional encoding: each token keeps its features and acquires an extra block of
`ι` coordinates holding the indicator of its own position. -/
def posEnc (x : Seq ι κ) : Seq ι (κ ⊕ ι) :=
fun i => Sum.elim (x i) (fun j => if j = i then (1:ℝ) else 0)
/-- Positionally tagged sequences can be decoded *invariantly*: attending from the tag block to
the feature block reconstructs the ordered sequence, and the reconstruction is a sum over
tokens, hence permutation invariant. -/
def decodeSeq (z : Seq ι (κ ⊕ ι)) : Seq ι κ :=
fun i a => ∑ j, z j (Sum.inr i) * z j (Sum.inl a)
/-- The saturation of the positionally encoded image of a set. -/
def encSaturation (K : Set (Seq ι κ)) : Set (Seq ι (κ ⊕ ι)) :=
⋃ σ : Equiv.Perm ι, (permAct σ) '' (posEnc '' K)
end PositionalRepair
section EquivariantHeads
variable {ι κ : Type*} [DecidableEq ι]
/-- The marked-query encoding: the queried position `i` is flagged by an extra feature. -/
def markAt (i : ι) (x : Seq ι κ) : Seq ι (κ ⊕ Unit) :=
fun j => Sum.elim (x j) (fun _ => if j = i then (1:ℝ) else 0)
end EquivariantHeads
section EquivariantUniversality
variable {ι κ : Type*} [Fintype ι] [DecidableEq ι] [Fintype κ]
variable {Γ : Type*} [Group Γ] [Fintype Γ]
/-- The feature block of an augmented (marked) sequence. -/
def feat (z : Seq ι (κ ⊕ Unit)) : Seq ι κ := fun i a => z i (Sum.inl a)
/-- The mark coordinate of an augmented sequence. -/
def markCoord (z : Seq ι (κ ⊕ Unit)) (i : ι) : ℝ := z i (Sum.inr ())
/-- A sequence-to-sequence map is *`Γ`-equivariant* when a token symmetry of the input permutes
the output tokens the same way. -/
def EquivariantUnder (act : Γ →* Equiv.Perm ι) (F : Seq ι κ → Seq ι κ) : Prop :=
∀ (g : Γ) (x : Seq ι κ) (i : ι) (a : κ), F (permAct (act g) x) i a = F x (act g i) a
/-- The *marked functional* of an equivariant map: contract the output of `F` on the feature
block against the mark coordinates. This is the key construction: it converts an equivariant
sequence-to-sequence map into a genuinely **invariant** scalar functional of the marked
sequence, with no loss of information. -/
def markedFunctional (F : Seq ι κ → Seq ι κ) (a : κ) (z : Seq ι (κ ⊕ Unit)) : ℝ :=
∑ i, F (feat z) i a * markCoord z i
/-- The symmetry saturation of the marked image of a set of sequences. -/
def markSaturation (act : Γ →* Equiv.Perm ι) (K : Set (Seq ι κ)) : Set (Seq ι (κ ⊕ Unit)) :=
⋃ g : Γ, ⋃ i : ι, (permAct (act g)) '' (markAt i '' K)
end EquivariantUniversality
end PermutationDichotomy