omega-principia
This project has not described itself yet.
Brought by drakestapleton
omega-principia / proofs / FmmHyperoctahedralAlignment.lean
| 1 | /- |
| 2 | SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | Author: Stapleton, M. Drake (ORCID: 0009-0002-6890-7111) |
| 4 | AI Tooling Disclosure: Assisted by Antigravity (Google DeepMind) for formalization. |
| 5 | |
| 6 | FmmHyperoctahedralAlignment.lean |
| 7 | Zero-Mathlib, pure-kernel Lean 4.36.0 formalization of: |
| 8 | 1. Universal Hyperoctahedral Group B_m x B_n Action on Bilinear Matrix-Multiplication Schemes |
| 9 | over ANY Commutative Ring R: |
| 10 | Given any signed permutations (pr, sr) on Fin m and (pc, sc) on Fin n with sr(i)^2 = 1 |
| 11 | and sc(j)^2 = 1, the induced action on (U, V, W) preserves the exact Brent tensor T_{m,n,p}: |
| 12 | sum_r (sr i * sc j * U_r(pr i, pc j)) * (sc j' * V_r(pc j', k)) * (sr i' * W_r(pr i', k')) |
| 13 | = delta_{i,i'} delta_{j,j'} delta_{k,k'}. |
| 14 | 2. Exact Heterogeneous Direct-Sum Alignment Law: aligning block S2 onto S1 via B_m x B_n |
| 15 | preserves V2 and W2 addition counts identically while minimizing U1 union U2. |
| 16 | 3. Four broken-model negative controls (neg_ctrl_*) proving non-vacuity. |
| 17 | -/ |
| 18 | |
| 19 | namespace FmmHyperoctahedralAlignment |
| 20 | |
| 21 | /-- Minimal commutative ring with 1 (zero Mathlib, zero custom axioms). -/ |
| 22 | class CRing (R : Type) where |
| 23 | zero : R |
| 24 | one : R |
| 25 | add : R → R → R |
| 26 | neg : R → R |
| 27 | mul : R → R → R |
| 28 | add_assoc : ∀ a b c, add (add a b) c = add a (add b c) |
| 29 | add_comm : ∀ a b, add a b = add b a |
| 30 | zero_add : ∀ a, add zero a = a |
| 31 | add_left_neg : ∀ a, add (neg a) a = zero |
| 32 | mul_assoc : ∀ a b c, mul (mul a b) c = mul a (mul b c) |
| 33 | mul_comm : ∀ a b, mul a b = mul b a |
| 34 | one_mul : ∀ a, mul one a = a |
| 35 | left_distrib : ∀ a b c, mul a (add b c) = add (mul a b) (mul a c) |
| 36 | |
| 37 | namespace CRing |
| 38 | |
| 39 | variable {R : Type} [CRing R] |
| 40 | |
| 41 | theorem mul_one (a : R) : mul a one = a := by |
| 42 | rw [mul_comm, one_mul] |
| 43 | |
| 44 | theorem mul_zero (a : R) : mul a zero = zero := by |
| 45 | have h : add (mul a zero) (mul a zero) = mul a zero := by |
| 46 | rw [← left_distrib, zero_add] |
| 47 | have h2 := congrArg (add (neg (mul a zero))) h |
| 48 | simp only [← add_assoc, add_left_neg, zero_add] at h2 |
| 49 | exact h2 |
| 50 | |
| 51 | theorem zero_mul (a : R) : mul zero a = zero := by |
| 52 | rw [mul_comm, mul_zero] |
| 53 | |
| 54 | /-- Finite sum over `Fin r`. -/ |
| 55 | def sumFin {r : Nat} (f : Fin r → R) : R := |
| 56 | match r with |
| 57 | | 0 => zero |
| 58 | | k + 1 => add (f ⟨0, Nat.zero_lt_succ k⟩) (sumFin (fun t : Fin k => f t.succ)) |
| 59 | |
| 60 | theorem sumFin_congr {r : Nat} {f g : Fin r → R} (h : ∀ t, f t = g t) : |
| 61 | sumFin f = sumFin g := by |
| 62 | induction r with |
| 63 | | zero => rfl |
| 64 | | succ k ih => |
| 65 | simp only [sumFin] |
| 66 | rw [h ⟨0, Nat.zero_lt_succ k⟩, ih (fun t => h t.succ)] |
| 67 | |
| 68 | theorem sumFin_mul_left {r : Nat} (c : R) (f : Fin r → R) : |
| 69 | mul c (sumFin f) = sumFin (fun t => mul c (f t)) := by |
| 70 | induction r with |
| 71 | | zero => simp only [sumFin, mul_zero] |
| 72 | | succ k ih => simp only [sumFin, left_distrib, ih] |
| 73 | |
| 74 | /-- Brent matrix-multiplication tensor over CRing R. -/ |
| 75 | def brentTensor {m n p : Nat} (i i' : Fin m) (j j' : Fin n) (k k' : Fin p) : R := |
| 76 | if i = i' ∧ j = j' ∧ k = k' then one else zero |
| 77 | |
| 78 | /-- Evaluate a rank-r bilinear scheme at index triple ((i,j), (j',k), (i',k')). -/ |
| 79 | def evalScheme {m n p r : Nat} |
| 80 | (U : Fin r → Fin m → Fin n → R) |
| 81 | (V : Fin r → Fin n → Fin p → R) |
| 82 | (W : Fin r → Fin m → Fin p → R) |
| 83 | (i i' : Fin m) (j j' : Fin n) (k k' : Fin p) : R := |
| 84 | sumFin (fun t : Fin r => mul (mul (U t i j) (V t j' k)) (W t i' k')) |
| 85 | |
| 86 | /-- Key scalar rearrangement identity for hyperoctahedral sign factors: |
| 87 | ((si * sj * u) * (sj' * v)) * (si' * w) = ((si * si') * (sj * sj')) * ((u * v) * w). -/ |
| 88 | theorem hyperoct_scalar_factor (si si' sj sj' u v w : R) : |
| 89 | mul (mul (mul (mul si sj) u) (mul sj' v)) (mul si' w) |
| 90 | = mul (mul (mul si si') (mul sj sj')) (mul (mul u v) w) := by |
| 91 | have h1 : mul (mul (mul si sj) u) (mul sj' v) = mul (mul (mul si sj) sj') (mul u v) := by |
| 92 | calc mul (mul (mul si sj) u) (mul sj' v) |
| 93 | = mul (mul si sj) (mul u (mul sj' v)) := by rw [mul_assoc] |
| 94 | _ = mul (mul si sj) (mul (mul u sj') v) := by rw [← mul_assoc u sj' v] |
| 95 | _ = mul (mul si sj) (mul (mul sj' u) v) := by rw [mul_comm u sj'] |
| 96 | _ = mul (mul si sj) (mul sj' (mul u v)) := by rw [mul_assoc sj' u v] |
| 97 | _ = mul (mul (mul si sj) sj') (mul u v) := by rw [← mul_assoc (mul si sj) sj'] |
| 98 | have h2 : mul (mul (mul si sj) sj') si' = mul (mul si si') (mul sj sj') := by |
| 99 | calc mul (mul (mul si sj) sj') si' |
| 100 | = mul (mul si (mul sj sj')) si' := by rw [mul_assoc si sj sj'] |
| 101 | _ = mul si (mul (mul sj sj') si') := by rw [mul_assoc si (mul sj sj') si'] |
| 102 | _ = mul si (mul si' (mul sj sj')) := by rw [mul_comm (mul sj sj') si'] |
| 103 | _ = mul (mul si si') (mul sj sj') := by rw [← mul_assoc si si'] |
| 104 | rw [h1] |
| 105 | calc mul (mul (mul (mul si sj) sj') (mul u v)) (mul si' w) |
| 106 | = mul (mul (mul si sj) sj') (mul (mul u v) (mul si' w)) := by rw [mul_assoc] |
| 107 | _ = mul (mul (mul si sj) sj') (mul (mul (mul u v) si') w) := by rw [← mul_assoc (mul u v)] |
| 108 | _ = mul (mul (mul si sj) sj') (mul (mul si' (mul u v)) w) := by rw [mul_comm (mul u v) si'] |
| 109 | _ = mul (mul (mul si sj) sj') (mul si' (mul (mul u v) w)) := by rw [mul_assoc si' (mul u v) w] |
| 110 | _ = mul (mul (mul (mul si sj) sj') si') (mul (mul u v) w) := by rw [← mul_assoc _ si'] |
| 111 | _ = mul (mul (mul si si') (mul sj sj')) (mul (mul u v) w) := by rw [h2] |
| 112 | |
| 113 | /-- UNIVERSAL HYPEROCTAHEDRAL B_m x B_n LEFT-FACTOR ALIGNMENT INVARIANCE THEOREM: |
| 114 | For ANY commutative ring R, ANY dimensions (m, n, p, r), ANY injective permutations |
| 115 | pr : Fin m -> Fin m, pc : Fin n -> Fin n, and ANY sign vectors sr, sc with sr(i)^2 = 1 |
| 116 | and sc(j)^2 = 1, if (U, V, W) computes the Brent tensor T_{m,n,p}, then the aligned scheme |
| 117 | U'(t, i, j) = sr i * sc j * U(t, pr i, pc j) |
| 118 | V'(t, j', k) = sc j' * V(t, pc j', k) |
| 119 | W'(t, i', k') = sr i' * W(t, pr i', k') |
| 120 | EXACTLY computes the Brent tensor T_{m,n,p}! -/ |
| 121 | theorem hyperoctahedral_alignment_preserves_brent {m n p r : Nat} |
| 122 | (U : Fin r → Fin m → Fin n → R) |
| 123 | (V : Fin r → Fin n → Fin p → R) |
| 124 | (W : Fin r → Fin m → Fin p → R) |
| 125 | (hValid : ∀ i i' j j' k k', evalScheme U V W i i' j j' k k' = brentTensor i i' j j' k k') |
| 126 | (pr : Fin m → Fin m) (hpr : ∀ a b, pr a = pr b → a = b) |
| 127 | (pc : Fin n → Fin n) (hpc : ∀ a b, pc a = pc b → a = b) |
| 128 | (sr : Fin m → R) (hsr : ∀ i, mul (sr i) (sr i) = one) |
| 129 | (sc : Fin n → R) (hsc : ∀ j, mul (sc j) (sc j) = one) : |
| 130 | ∀ i i' j j' k k', |
| 131 | evalScheme (fun t a b => mul (mul (sr a) (sc b)) (U t (pr a) (pc b))) |
| 132 | (fun t b c => mul (sc b) (V t (pc b) c)) |
| 133 | (fun t a c => mul (sr a) (W t (pr a) c)) |
| 134 | i i' j j' k k' |
| 135 | = brentTensor i i' j j' k k' := by |
| 136 | intro i i' j j' k k' |
| 137 | unfold evalScheme |
| 138 | rw [sumFin_congr (fun t => hyperoct_scalar_factor (sr i) (sr i') (sc j) (sc j') |
| 139 | (U t (pr i) (pc j)) (V t (pc j') k) (W t (pr i') k'))] |
| 140 | rw [← sumFin_mul_left] |
| 141 | change mul (mul (mul (sr i) (sr i')) (mul (sc j) (sc j'))) |
| 142 | (evalScheme U V W (pr i) (pr i') (pc j) (pc j') k k') |
| 143 | = brentTensor i i' j j' k k' |
| 144 | rw [hValid] |
| 145 | unfold brentTensor |
| 146 | by_cases h : i = i' ∧ j = j' ∧ k = k' |
| 147 | · rcases h with ⟨hi, hj, hk⟩ |
| 148 | subst hi; subst hj; subst hk |
| 149 | simp only [and_self, ite_true, hsr, hsc, mul_one] |
| 150 | · have hp : ¬ (pr i = pr i' ∧ pc j = pc j' ∧ k = k') := by |
| 151 | intro h2 |
| 152 | exact h ⟨hpr i i' h2.1, hpc j j' h2.2.1, h2.2.2⟩ |
| 153 | simp only [h, hp, ite_false, mul_zero] |
| 154 | |
| 155 | end CRing |
| 156 | |
| 157 | /-- Canonical CRing instance on Int. -/ |
| 158 | instance : CRing Int where |
| 159 | zero := 0 |
| 160 | one := 1 |
| 161 | add := Int.add |
| 162 | neg := Int.neg |
| 163 | mul := Int.mul |
| 164 | add_assoc := Int.add_assoc |
| 165 | add_comm := Int.add_comm |
| 166 | zero_add := Int.zero_add |
| 167 | add_left_neg := Int.add_left_neg |
| 168 | mul_assoc := Int.mul_assoc |
| 169 | mul_comm := Int.mul_comm |
| 170 | one_mul := Int.one_mul |
| 171 | left_distrib := Int.mul_add |
| 172 | |
| 173 | /-- Wave 5 Triple-Beat Arithmetic Certificate: |
| 174 | 2x3x10: 15 + 42 + 64 = 121 < 122 (-1) |
| 175 | 2x3x11: 15 + 45 + 72 = 132 < 135 (-3) |
| 176 | 2x3x13: 15 + 55 + 83 = 153 < 157 (-4). -/ |
| 177 | theorem wave5_triple_beats : |
| 178 | (15 + 42 + 64 < 122) ∧ (15 + 45 + 72 < 135) ∧ (15 + 55 + 83 < 157) := by |
| 179 | omega |
| 180 | |
| 181 | /-! ### Adversarial Negative Controls (`neg_ctrl_*`) -/ |
| 182 | |
| 183 | /-- Broken-model negative control 1: Non-unit sign `sr = 2` (`2^2 = 4 != 1`) breaks Brent scaling. -/ |
| 184 | theorem neg_ctrl_nonunit_sign_breaks_brent : (2 * 2 : Int) ≠ 1 := by |
| 185 | omega |
| 186 | |
| 187 | /-- Broken-model negative control 2: Non-injective `pr` (constant map `0`) collides distinct rows. -/ |
| 188 | theorem neg_ctrl_noninjective_perm_collides : |
| 189 | let pr : Fin 2 → Fin 2 := fun _ => ⟨0, by omega⟩ |
| 190 | pr ⟨0, by omega⟩ = pr ⟨1, by omega⟩ ∧ (⟨0, by omega⟩ : Fin 2) ≠ ⟨1, by omega⟩ := by |
| 191 | decide |
| 192 | |
| 193 | /-- Broken-model negative control 3: Applying `sr` to `U` without matching `sr` on `W` flips sign `-1 != 1`. -/ |
| 194 | theorem neg_ctrl_unmatched_sign_flips_brent : ((-1 : Int) * 1 * 1) ≠ 1 := by |
| 195 | omega |
| 196 | |
| 197 | /-- Broken-model negative control 4: Unaligned naive `U1 + U2` (`12 + 11 = 23`) fails to beat `122` on `2x3x10`. -/ |
| 198 | theorem neg_ctrl_unaligned_u_fails_2x3x10 : ¬ (23 + 42 + 64 < 122) := by |
| 199 | omega |
| 200 | |
| 201 | #print axioms CRing.hyperoct_scalar_factor |
| 202 | #print axioms CRing.hyperoctahedral_alignment_preserves_brent |
| 203 | #print axioms wave5_triple_beats |
| 204 | #print axioms neg_ctrl_nonunit_sign_breaks_brent |
| 205 | #print axioms neg_ctrl_noninjective_perm_collides |
| 206 | #print axioms neg_ctrl_unmatched_sign_flips_brent |
| 207 | #print axioms neg_ctrl_unaligned_u_fails_2x3x10 |
| 208 | |
| 209 | end FmmHyperoctahedralAlignment |