Buckets:
cfahlgren1/math / lean /OAI /LinearAlgebra /MatrixMultiplication /Tensor /ComplexTensorBatching.lean
| import OAI.LinearAlgebra.MatrixMultiplication.Tensor.ComplexTensor | |
| namespace OAI | |
| /-! Finite coefficient tensors and their algebraic transformations. -/ | |
| open scoped BigOperators | |
| namespace MatrixMultiplication.Foundation | |
| namespace Tensor | |
| variable {K X Y Z U V W : Type*} [CommSemiring K] | |
| def diagonal (ι : Type*) [DecidableEq ι] : Tensor K ι ι ι := | |
| fun i j k => if i = j ∧ i = k then 1 else 0 | |
| theorem diagonal_rankAtMost (ι : Type*) [Fintype ι] [DecidableEq ι] : | |
| RankAtMost (diagonal (K := K) ι) (Fintype.card ι) := by | |
| let a : ι → ι → K := fun i j => if i = j then 1 else 0 | |
| have heq : diagonal (K := K) ι = | |
| fun x y z => ∑ i, rankOne (a i) (a i) (a i) x y z := by | |
| funext x y z | |
| simp [diagonal, rankOne, a, mul_ite, ite_and] | |
| split_ifs <;> simp_all | |
| rw [heq] | |
| exact rankAtMost_sum_rankOne a a a | |
| theorem RankAtMost.repeat_batch {S : Tensor K U V W} {k r R B : ℕ} | |
| (hbatch : RankAtMost (directSum (fun _ : Fin k => S)) r) (hfit : R ≤ B * k) : | |
| RankAtMost (directSum (fun _ : Fin R => S)) (B * r) := by | |
| classical | |
| let f : Fin R → Fin B × Fin k := fun i => finProdFinEquiv.symm (Fin.castLE hfit i) | |
| have hf : Function.Injective f := finProdFinEquiv.symm.injective.comp (Fin.castLE_injective hfit) | |
| have hbig := (diagonal_rankAtMost (K := K) (Fin B)).product hbatch | |
| simp only [Fintype.card_fin] at hbig | |
| rcases hbig with ⟨a, b, c, heq⟩ | |
| let liftU : (Fin R × U) → Fin B × (Fin k × U) := fun x => ((f x.1).1, (f x.1).2, x.2) | |
| let liftV : (Fin R × V) → Fin B × (Fin k × V) := fun y => ((f y.1).1, (f y.1).2, y.2) | |
| let liftW : (Fin R × W) → Fin B × (Fin k × W) := fun z => ((f z.1).1, (f z.1).2, z.2) | |
| refine ⟨fun i x => a i (liftU x), fun i y => b i (liftV y), | |
| fun i z => c i (liftW z), ?_⟩ | |
| funext x y z | |
| change directSum (fun _ : Fin R => S) x y z = | |
| ∑ i, rankOne (a i) (b i) (c i) (liftU x) (liftV y) (liftW z) | |
| have hentry := congrFun (congrFun (congrFun heq (liftU x)) (liftV y)) (liftW z) | |
| rw [← hentry] | |
| by_cases hxy : x.1 = y.1 | |
| · by_cases hxz : x.1 = z.1 | |
| · have hyz : y.1 = z.1 := hxy.symm.trans hxz | |
| simp [directSum, Tensor.product, diagonal, liftU, liftV, liftW, hxy, hyz] | |
| · have hn : f x.1 ≠ f z.1 := fun h => hxz (hf h) | |
| have hs : (f x.1).1 ≠ (f z.1).1 ∨ (f x.1).2 ≠ (f z.1).2 := by | |
| by_cases hfirst : (f x.1).1 = (f z.1).1 | |
| · exact Or.inr (fun hsecond => hn (Prod.ext hfirst hsecond)) | |
| · exact Or.inl hfirst | |
| rcases hs with hs | hs <;> | |
| simp [directSum, Tensor.product, diagonal, liftU, liftV, liftW, hxz, hs] | |
| · have hn : f x.1 ≠ f y.1 := fun h => hxy (hf h) | |
| have hs : (f x.1).1 ≠ (f y.1).1 ∨ (f x.1).2 ≠ (f y.1).2 := by | |
| by_cases hfirst : (f x.1).1 = (f y.1).1 | |
| · exact Or.inr (fun hsecond => hn (Prod.ext hfirst hsecond)) | |
| · exact Or.inl hfirst | |
| rcases hs with hs | hs <;> | |
| simp [directSum, Tensor.product, diagonal, liftU, liftV, liftW, hxy, hs] | |
| theorem RankAtMost.batch_substitute [Fintype U] [Fintype V] [Fintype W] | |
| {T : Tensor K X Y Z} {S : Tensor K U V W} {r b : ℕ} | |
| (hT : RankAtMost T r) | |
| (hbatch : RankAtMost (directSum (fun _ : Fin r => S)) b) : | |
| RankAtMost (Tensor.product T S) b := by | |
| classical | |
| rcases hT with ⟨a, c, e, rfl⟩ | |
| let A : (X × U) → (Fin r × U) → K := | |
| fun x i => if x.2 = i.2 then a i.1 x.1 else 0 | |
| let B : (Y × V) → (Fin r × V) → K := | |
| fun y i => if y.2 = i.2 then c i.1 y.1 else 0 | |
| let C : (Z × W) → (Fin r × W) → K := | |
| fun z i => if z.2 = i.2 then e i.1 z.1 else 0 | |
| have heq : Tensor.restrict A B C (directSum (fun _ : Fin r => S)) = | |
| Tensor.product (fun x y z => ∑ i, rankOne (a i) (c i) (e i) x y z) S := by | |
| funext x y z | |
| simp [Tensor.restrict, directSum, Tensor.product, rankOne, A, B, C, | |
| Fintype.sum_prod_type, ite_and, ite_mul, mul_ite, Finset.sum_mul] | |
| rw [← heq] | |
| exact hbatch.restrict A B C | |
| theorem RankAtMost.batch_product [Fintype U] [Fintype V] [Fintype W] | |
| {T : Tensor K X Y Z} {S : Tensor K U V W} {R k r B : ℕ} | |
| (hT : RankAtMost T R) | |
| (hbatch : RankAtMost (directSum (fun _ : Fin k => S)) r) | |
| (hfit : R ≤ B * k) : RankAtMost (Tensor.product T S) (B * r) := | |
| hT.batch_substitute (hbatch.repeat_batch hfit) | |
| end Tensor | |
| end MatrixMultiplication.Foundation | |
| end OAI | |
Xet Storage Details
- Size:
- 4.33 kB
- Xet hash:
- ae8acb13f81593e8b5959d60fb231be523d07852a0e4258d46e69f320a4284f9
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.