Download data/lean/OAI/LinearAlgebra/MatrixMultiplication/JointExtraction/CoarseHashing.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 10.6 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/LinearAlgebra/MatrixMultiplication/JointExtraction/CoarseHashing.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/LinearAlgebra/MatrixMultiplication/JointExtraction/CoarseHashing.lean
-
curl -L -o CoarseHashing.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/LinearAlgebra/MatrixMultiplication/JointExtraction/CoarseHashing.lean
10.6 kB
| import OAI.LinearAlgebra.MatrixMultiplication.JointExtraction.Hashing | |
| import OAI.LinearAlgebra.MatrixMultiplication.JointExtraction.LossCounts | |
| namespace OAI | |
| /-! Joint tensor extraction, compatibility and entropy estimates. -/ | |
| noncomputable section | |
| namespace MatrixMultiplication.JointCoarseHashing | |
| attribute [local instance] Classical.propDecidable | |
| abbrev Word (P : Type*) := P β Fin 17 | |
| abbrev Triple (P : Type*) := Word P Γ Word P Γ Word P | |
| variable {P : Type*} | |
| def HasSupportSum (S : P β β) (e : Triple P) : Prop := | |
| β p, (e.1 p).val + (e.2.1 p).val + (e.2.2 p).val = S p | |
| theorem eq_of_shared_xy (S : P β β) (e f : Triple P) | |
| (he : HasSupportSum S e) (hf : HasSupportSum S f) | |
| (hx : e.1 = f.1) (hy : e.2.1 = f.2.1) : e = f := by | |
| have hz : e.2.2 = f.2.2 := by | |
| funext p | |
| apply Fin.ext | |
| have he' := he p | |
| have hf' := hf p | |
| rw [hx, hy] at he' | |
| omega | |
| exact Prod.ext hx (Prod.ext hy hz) | |
| theorem eq_of_shared_xz (S : P β β) (e f : Triple P) | |
| (he : HasSupportSum S e) (hf : HasSupportSum S f) | |
| (hx : e.1 = f.1) (hz : e.2.2 = f.2.2) : e = f := by | |
| have hy : e.2.1 = f.2.1 := by | |
| funext p | |
| apply Fin.ext | |
| have he' := he p | |
| have hf' := hf p | |
| rw [hx, hz] at he' | |
| omega | |
| exact Prod.ext hx (Prod.ext hy hz) | |
| theorem eq_of_shared_yz (S : P β β) (e f : Triple P) | |
| (he : HasSupportSum S e) (hf : HasSupportSum S f) | |
| (hy : e.2.1 = f.2.1) (hz : e.2.2 = f.2.2) : e = f := by | |
| have hx : e.1 = f.1 := by | |
| funext p | |
| apply Fin.ext | |
| have he' := he p | |
| have hf' := hf p | |
| rw [hy, hz] at he' | |
| omega | |
| exact Prod.ext hx (Prod.ext hy hz) | |
| def castWord (k : β) (w : Word P) : P β ZMod (37 ^ k) := | |
| fun p => (w p).val | |
| def twoUnit (k : β) : (ZMod (37 ^ k))Λ£ := | |
| (JointHashing.small_nat_isUnit k 2 (by decide) (by decide)).unit | |
| theorem twoUnit_coe (k : β) : (twoUnit k : ZMod (37 ^ k)) = 2 := by | |
| exact (JointHashing.small_nat_isUnit k 2 (by decide) (by decide)).unit_spec | |
| theorem supportSum_cast (k : β) (S : P β β) (e : Triple P) | |
| (he : HasSupportSum S e) : | |
| castWord k e.1 + castWord k e.2.1 + castWord k e.2.2 = | |
| fun p => (S p : ZMod (37 ^ k)) := by | |
| funext p | |
| have h := congrArg (fun n : β => (n : ZMod (37 ^ k))) (he p) | |
| simpa only [Nat.cast_add, Pi.add_apply, castWord] using h | |
| theorem shared_z_cast_sum (k : β) (S : P β β) (e f : Triple P) | |
| (he : HasSupportSum S e) (hf : HasSupportSum S f) | |
| (hz : f.2.2 = e.2.2) : | |
| castWord k f.1 + castWord k f.2.1 = castWord k e.1 + castWord k e.2.1 := by | |
| have h := (supportSum_cast k S f hf).trans (supportSum_cast k S e he).symm | |
| rw [hz] at h | |
| exact add_right_cancel h | |
| section FiniteHashing | |
| variable [Fintype P] | |
| abbrev Sample (P : Type*) (k : β) := JointHashing.Sample P (ZMod (37 ^ k)) | |
| def Survives (k : β) (U : Finset (ZMod (37 ^ k))) (e : Triple P) (s : Sample P k) : Prop := | |
| JointHashing.Survives (U : Set (ZMod (37 ^ k))) (castWord k e.1) (castWord k e.2.1) s | |
| theorem full_survival_iff (k : β) (U : Finset (ZMod (37 ^ k))) | |
| (hAP : β x β U, β y β U, β z β U, x + y = 2 * z β x = z β§ y = z) | |
| (S : P β β) (e : Triple P) (he : HasSupportSum S e) (s : Sample P k) : | |
| (JointHashing.xHash s (castWord k e.1) β U β§ | |
| JointHashing.yHash s (castWord k e.2.1) β U β§ | |
| JointHashing.zHash (twoUnit k) s (fun p => (S p : ZMod (37 ^ k))) | |
| (castWord k e.2.2) β U) β Survives k U e s := by | |
| exact JointHashing.full_survival_iff (twoUnit k) (twoUnit_coe k) | |
| (U : Set (ZMod (37 ^ k))) hAP s (castWord k e.1) (castWord k e.2.1) | |
| (castWord k e.2.2) (fun p => (S p : ZMod (37 ^ k))) (supportSum_cast k S e he) | |
| theorem survival_card (k : β) (U : Finset (ZMod (37 ^ k))) (e : Triple P) : | |
| Fintype.card {s : Sample P k // Survives k U e s} = | |
| (37 ^ k) ^ Fintype.card P * U.card := by | |
| have h := JointHashing.survival_card U (castWord k e.1) (castWord k e.2.1) | |
| simp only [Survives, ZMod.card] at h β’ | |
| refine Eq.trans ?_ h | |
| exact Fintype.card_congr (Equiv.refl _) | |
| theorem shared_x_card_mul (k : β) (U : Finset (ZMod (37 ^ k))) | |
| (S : P β β) (e f : Triple P) (he : HasSupportSum S e) (hf : HasSupportSum S f) | |
| (hne : f β e) (hx : f.1 = e.1) : | |
| (37 ^ k) * Fintype.card {s : Sample P k // Survives k U e s β§ Survives k U f s} = | |
| Fintype.card {s : Sample P k // Survives k U e s} := by | |
| classical | |
| have hy : f.2.1 β e.2.1 := fun hy => hne (eq_of_shared_xy S f e hf he hx hy) | |
| obtain β¨i, hiβ© := JointHashing.coarse_words_unit_coordinate k f.2.1 e.2.1 hy | |
| have h := JointHashing.shared_x_collision_card_mul U (castWord k e.1) | |
| (castWord k e.2.1) (castWord k f.2.1) i hi.unit hi.unit_spec.symm | |
| simp only [Survives, hx, ZMod.card] at h β’ | |
| refine Eq.trans ?_ (Eq.trans h ?_) | |
| Β· exact congrArg (fun n : β => 37 ^ k * n) (Fintype.card_congr (Equiv.refl _)) | |
| Β· exact Fintype.card_congr (Equiv.refl _) | |
| theorem shared_y_card_mul (k : β) (U : Finset (ZMod (37 ^ k))) | |
| (S : P β β) (e f : Triple P) (he : HasSupportSum S e) (hf : HasSupportSum S f) | |
| (hne : f β e) (hy : f.2.1 = e.2.1) : | |
| (37 ^ k) * Fintype.card {s : Sample P k // Survives k U e s β§ Survives k U f s} = | |
| Fintype.card {s : Sample P k // Survives k U e s} := by | |
| classical | |
| have hx : f.1 β e.1 := fun hx => hne (eq_of_shared_xy S f e hf he hx hy) | |
| obtain β¨i, hiβ© := JointHashing.coarse_words_unit_coordinate k f.1 e.1 hx | |
| have h := JointHashing.shared_y_collision_card_mul U (castWord k e.1) | |
| (castWord k f.1) (castWord k e.2.1) i hi.unit hi.unit_spec.symm | |
| simp only [Survives, hy, ZMod.card] at h β’ | |
| refine Eq.trans ?_ (Eq.trans h ?_) | |
| Β· exact congrArg (fun n : β => 37 ^ k * n) (Fintype.card_congr (Equiv.refl _)) | |
| Β· exact Fintype.card_congr (Equiv.refl _) | |
| theorem shared_z_card_mul (k : β) (U : Finset (ZMod (37 ^ k))) | |
| (S : P β β) (e f : Triple P) (he : HasSupportSum S e) (hf : HasSupportSum S f) | |
| (hne : f β e) (hz : f.2.2 = e.2.2) : | |
| (37 ^ k) * Fintype.card {s : Sample P k // Survives k U e s β§ Survives k U f s} = | |
| Fintype.card {s : Sample P k // Survives k U e s} := by | |
| classical | |
| have hx : f.1 β e.1 := fun hx => hne (eq_of_shared_xz S f e hf he hx hz) | |
| obtain β¨i, hiβ© := JointHashing.coarse_words_unit_coordinate k f.1 e.1 hx | |
| have h := JointHashing.shared_z_collision_card_mul (twoUnit k) (twoUnit_coe k) | |
| U (castWord k e.1) (castWord k f.1) (castWord k e.2.1) (castWord k f.2.1) | |
| (shared_z_cast_sum k S e f he hf hz) i hi.unit hi.unit_spec.symm | |
| simp only [Survives, ZMod.card] at h β’ | |
| refine Eq.trans ?_ (Eq.trans h ?_) | |
| Β· exact congrArg (fun n : β => 37 ^ k * n) (Fintype.card_congr (Equiv.refl _)) | |
| Β· exact Fintype.card_congr (Equiv.refl _) | |
| def SharesSide (e f : Triple P) : Prop := | |
| f.1 = e.1 β¨ f.2.1 = e.2.1 β¨ f.2.2 = e.2.2 | |
| theorem pair_survival_card_mul (k : β) (U : Finset (ZMod (37 ^ k))) | |
| (S : P β β) (e f : Triple P) (he : HasSupportSum S e) (hf : HasSupportSum S f) | |
| (hne : f β e) (hshare : SharesSide e f) : | |
| (37 ^ k) * Fintype.card {s : Sample P k // Survives k U e s β§ Survives k U f s} = | |
| Fintype.card {s : Sample P k // Survives k U e s} := by | |
| rcases hshare with hx | hy | hz | |
| Β· exact shared_x_card_mul k U S e f he hf hne hx | |
| Β· exact shared_y_card_mul k U S e f he hf hne hy | |
| Β· exact shared_z_card_mul k U S e f he hf hne hz | |
| def ownEvent (k : β) (U : Finset (ZMod (37 ^ k))) (e : Triple P) : Finset (Sample P k) := | |
| Finset.univ.filter (Survives k U e) | |
| theorem ownEvent_card (k : β) (U : Finset (ZMod (37 ^ k))) (e : Triple P) : | |
| (ownEvent k U e).card = (37 ^ k) ^ Fintype.card P * U.card := by | |
| simpa only [ownEvent, Fintype.card_subtype] using survival_card k U e | |
| theorem pair_event_card_mul (k : β) (U : Finset (ZMod (37 ^ k))) | |
| (S : P β β) (e f : Triple P) (he : HasSupportSum S e) (hf : HasSupportSum S f) | |
| (hne : f β e) (hshare : SharesSide e f) : | |
| (37 ^ k) * ((ownEvent k U e).filter (Survives k U f)).card = (ownEvent k U e).card := by | |
| simpa only [ownEvent, Finset.filter_filter, Fintype.card_subtype] using | |
| pair_survival_card_mul k U S e f he hf hne hshare | |
| theorem competitor_union_card_mul_le (k : β) (U : Finset (ZMod (37 ^ k))) | |
| (S : P β β) (e : Triple P) (he : HasSupportSum S e) (competitors : Finset (Triple P)) | |
| (hf : β f β competitors, HasSupportSum S f) | |
| (hne : β f β competitors, f β e) | |
| (hshare : β f β competitors, SharesSide e f) : | |
| (37 ^ k) * ((ownEvent k U e).filter | |
| (fun s => β f β competitors, Survives k U f s)).card β€ | |
| competitors.card * (ownEvent k U e).card := by | |
| apply JointLossCounts.competitor_union_card_mul_le | |
| intro f hf' | |
| exact (pair_event_card_mul k U S e f he (hf f hf') (hne f hf') (hshare f hf')).le | |
| theorem competitor_union_card_le (k : β) (U : Finset (ZMod (37 ^ k))) | |
| (S : P β β) (e : Triple P) (he : HasSupportSum S e) (competitors : Finset (Triple P)) | |
| (hf : β f β competitors, HasSupportSum S f) | |
| (hne : β f β competitors, f β e) | |
| (hshare : β f β competitors, SharesSide e f) : | |
| (((ownEvent k U e).filter (fun s => β f β competitors, Survives k U f s)).card : β) β€ | |
| ((competitors.card : β) / ((37 ^ k : β) : β)) * ((ownEvent k U e).card : β) := by | |
| have hM : (0 : β) < ((37 ^ k : β) : β) := by | |
| exact_mod_cast (pow_pos (by decide : 0 < (37 : β)) k) | |
| have h := competitor_union_card_mul_le k U S e he competitors hf hne hshare | |
| have hR : ((37 ^ k : β) : β) * | |
| (((ownEvent k U e).filter (fun s => β f β competitors, Survives k U f s)).card : β) β€ | |
| (competitors.card : β) * ((ownEvent k U e).card : β) := by exact_mod_cast h | |
| rw [div_mul_eq_mul_div] | |
| exact (le_div_iffβ hM).2 (by simpa only [mul_comm] using hR) | |
| theorem pair_survival_fraction (k : β) (U : Finset (ZMod (37 ^ k))) (hU : U.Nonempty) | |
| (S : P β β) (e f : Triple P) (he : HasSupportSum S e) (hf : HasSupportSum S f) | |
| (hne : f β e) (hshare : SharesSide e f) : | |
| (((ownEvent k U e).filter (Survives k U f)).card : β) / ((ownEvent k U e).card : β) = | |
| 1 / ((37 ^ k : β) : β) := by | |
| have hM : (0 : β) < 37 ^ k := pow_pos (by decide) k | |
| have hS : 0 < (ownEvent k U e).card := by | |
| rw [ownEvent_card] | |
| exact Nat.mul_pos (pow_pos hM _) (Finset.card_pos.mpr hU) | |
| apply (div_eq_div_iff (by exact_mod_cast Nat.ne_of_gt hS) | |
| (by exact_mod_cast Nat.ne_of_gt hM)).2 | |
| have h := congrArg (fun n : β => (n : β)) (pair_event_card_mul k U S e f he hf hne hshare) | |
| simpa only [Nat.cast_mul, one_mul, mul_comm] using h | |
| end FiniteHashing | |
| end MatrixMultiplication.JointCoarseHashing | |
| end | |
| end OAI | |