introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
7.96 kB
import OAI.LinearAlgebra.MatrixMultiplication.JointExtraction.CompatibilityEntropyBound
import OAI.LinearAlgebra.MatrixMultiplication.JointExtraction.AmbientRateLimit
namespace OAI
/-! Joint tensor extraction, compatibility and entropy estimates. -/
noncomputable section
namespace MatrixMultiplication.JointCompatibilityScaling
open MatrixMultiplication.Foundation JointTypeCounts JointCompatibilityIncidence
open JointCompatibilityEntropyBound Filter
open scoped BigOperators Topology
attribute [local instance] Classical.propDecidable
variable {C : Type*} [Fintype C] [DecidableEq C]
variable {Shape : C β†’ Type*}
[βˆ€ c, Fintype (Shape c)] [βˆ€ c, DecidableEq (Shape c)]
def baseSize (base : βˆ€ c, Shape c β†’ β„•) (c : C) : β„• := βˆ‘ a, base c a
def baseTotal (base : βˆ€ c, Shape c β†’ β„•) : β„• := βˆ‘ c, baseSize base c
def baseEntropy (base : βˆ€ c, Shape c β†’ β„•) : ℝ :=
βˆ‘ c, (baseSize base c : ℝ) *
finiteEntropy (fun a => (base c a : ℝ) / baseSize base c)
def baseGroupSize (base : βˆ€ c, Shape c β†’ β„•) (d : βˆ€ c, Shape c β†’ Prop)
(g : Ξ£ c, Option (Shape c)) : β„• :=
βˆ‘ a : {a : Shape g.1 // designatedGroup (d g.1) a = g.2}, base g.1 a.val
def baseGroupedEntropy (base : βˆ€ c, Shape c β†’ β„•) (d : βˆ€ c, Shape c β†’ Prop)
(H : (Ξ£ c, Option (Shape c)) β†’ ℝ) : ℝ :=
βˆ‘ g, (baseGroupSize base d g : ℝ) * H g
omit [DecidableEq C] [βˆ€ c, DecidableEq (Shape c)] in
theorem baseSize_le_baseTotal (base : βˆ€ c, Shape c β†’ β„•) (c : C) :
baseSize base c ≀ baseTotal base :=
Finset.single_le_sum (fun _ _ => Nat.zero_le _) (Finset.mem_univ c)
omit [DecidableEq C] in
theorem sum_baseGroupSize (base : βˆ€ c, Shape c β†’ β„•)
(d : βˆ€ c, Shape c β†’ Prop) :
(βˆ‘ g, baseGroupSize base d g) = baseTotal base := by
simp only [baseGroupSize, baseTotal, Fintype.sum_sigma]
apply Finset.sum_congr rfl
intro c _
exact Fintype.sum_fiberwise (designatedGroup (d c)) (base c)
section Repeated
variable (Pos : C β†’ Type*)
[βˆ€ c, Fintype (Pos c)] [βˆ€ c, DecidableEq (Pos c)]
omit [DecidableEq C] [βˆ€ c, DecidableEq (Shape c)] [βˆ€ c, DecidableEq (Pos c)] in
theorem classEntropy_repeated (base : βˆ€ c, Shape c β†’ β„•) (t : β„•) (ht : 0 < t)
(hsize : βˆ€ c, Fintype.card (Pos c) = t * baseSize base c) :
classEntropy Pos Shape (fun c a => t * base c a) = (t : ℝ) * baseEntropy base := by
simp only [classEntropy, baseEntropy, hsize, Nat.cast_mul, Finset.mul_sum]
apply Finset.sum_congr rfl
intro c _
rw [show (fun a => (t : ℝ) * base c a / ((t : ℝ) * baseSize base c)) =
(fun a => (base c a : ℝ) / baseSize base c) by
funext a
exact mul_div_mul_left _ _ (Nat.cast_ne_zero.mpr (Nat.ne_of_gt ht))]
ring
omit [Fintype C] [DecidableEq C] [βˆ€ c, DecidableEq (Pos c)] in
theorem groupPos_card_repeated (base : βˆ€ c, Shape c β†’ β„•)
(d : βˆ€ c, Shape c β†’ Prop) (t : β„•)
(w : FixedClassWords Pos Shape (fun c a => t * base c a))
(g : Ξ£ c, Option (Shape c)) :
Fintype.card (GroupPos (w g.1).val (d g.1) g.2) =
t * baseGroupSize base d g := by
calc
Fintype.card (GroupPos (w g.1).val (d g.1) g.2) =
Fintype.card (Ξ£ a : {a : Shape g.1 // designatedGroup (d g.1) a = g.2},
{i : Pos g.1 // (w g.1).val i = a.val}) :=
Fintype.card_congr (Equiv.sigmaSubtypeFiberEquivSubtype (w g.1).val
(p := fun i => designatedGroup (d g.1) ((w g.1).val i) = g.2)
(q := fun a => designatedGroup (d g.1) a = g.2) (fun _ => Iff.rfl)).symm
_ = t * baseGroupSize base d g := by
simp only [Fintype.card_sigma]
change (βˆ‘ a : {a : Shape g.1 // designatedGroup (d g.1) a = g.2},
wordPopulation (w g.1).val a.val) = _
simp only [(w g.1).property, baseGroupSize, Finset.mul_sum]
omit [DecidableEq C] [βˆ€ c, DecidableEq (Pos c)] in
theorem groupedEntropy_repeated (base : βˆ€ c, Shape c β†’ β„•)
(d : βˆ€ c, Shape c β†’ Prop) (t : β„•)
(H : (Ξ£ c, Option (Shape c)) β†’ ℝ)
(w : FixedClassWords Pos Shape (fun c a => t * base c a)) :
groupedEntropy Pos Shape (fun c a => t * base c a) d H w =
(t : ℝ) * baseGroupedEntropy base d H := by
simp only [JointCompatibilityEntropyBound.groupedEntropy, groupPos_card_repeated, Nat.cast_mul,
baseGroupedEntropy, Finset.mul_sum, mul_assoc]
end Repeated
def typeErrorFactor (B t : β„•) : ℝ := 1 + Real.log (t * B + 1 : β„•)
theorem typeErrorFactor_nonneg (B t : β„•) : 0 ≀ typeErrorFactor B t := by
unfold typeErrorFactor
have hlog : 0 ≀ Real.log (t * B + 1 : β„•) :=
Real.log_nonneg (by exact_mod_cast Nat.succ_le_succ (Nat.zero_le (t * B)))
linarith
def classErrorCoefficient (A : C β†’ Type*) [βˆ€ c, Fintype (A c)] : ℝ :=
βˆ‘ c, ((Fintype.card (A c) : ℝ) + 1)
def groupErrorCoefficient (Shape A : C β†’ Type*)
[βˆ€ c, Fintype (Shape c)] [βˆ€ c, Fintype (A c)] : ℝ :=
βˆ‘ g : Ξ£ c, Option (Shape c), (2 * (Fintype.card (A g.1) : ℝ) + 1)
omit [DecidableEq C] in
theorem classTypeError_le (Pos A : C β†’ Type*)
[βˆ€ c, Fintype (Pos c)] [βˆ€ c, DecidableEq (Pos c)]
[βˆ€ c, Fintype (A c)] [βˆ€ c, DecidableEq (A c)]
(B t : β„•) (hsize : βˆ€ c, Fintype.card (Pos c) ≀ t * B) :
classTypeError Pos A ≀ classErrorCoefficient A * typeErrorFactor B t := by
simp only [classTypeError, classErrorCoefficient, Finset.sum_mul]
apply Finset.sum_le_sum
intro c _
apply mul_le_mul_of_nonneg_left _ (by positivity)
apply add_le_add_right
apply Real.log_le_log (by positivity)
exact_mod_cast Nat.succ_le_succ (hsize c)
theorem wordTypeError_le (I A : Type*) [Fintype I] [Fintype A]
(B t : β„•) (hsize : Fintype.card I ≀ t * B) :
wordTypeError I A ≀ (2 * (Fintype.card A : ℝ) + 1) * typeErrorFactor B t := by
have hlog : Real.log (Fintype.card I + 1 : β„•) ≀ Real.log (t * B + 1 : β„•) :=
Real.log_le_log (by positivity) (by exact_mod_cast Nat.succ_le_succ hsize)
have h₁ := mul_le_mul_of_nonneg_left (add_le_add_left hlog 1)
(by positivity : (0 : ℝ) ≀ (Fintype.card A : ℝ) + 1)
have hβ‚‚ := mul_le_mul_of_nonneg_left hlog (Nat.cast_nonneg (Fintype.card A) :
(0 : ℝ) ≀ Fintype.card A)
unfold wordTypeError typeErrorFactor
nlinarith [show (0 : ℝ) ≀ Fintype.card A from Nat.cast_nonneg _]
omit [DecidableEq C] in
theorem groupedTypeError_le (Pos A : C β†’ Type*)
[βˆ€ c, Fintype (Pos c)] [βˆ€ c, DecidableEq (Pos c)]
[βˆ€ c, Fintype (A c)] [βˆ€ c, DecidableEq (A c)]
(counts : βˆ€ c, Shape c β†’ β„•) (d : βˆ€ c, Shape c β†’ Prop)
(w : FixedClassWords Pos Shape counts)
(B t : β„•) (hsize : βˆ€ c, Fintype.card (Pos c) ≀ t * B) :
groupedTypeError Pos Shape A counts d w ≀
groupErrorCoefficient Shape A * typeErrorFactor B t := by
simp only [groupedTypeError, groupErrorCoefficient, Finset.sum_mul]
apply Finset.sum_le_sum
intro g _
apply wordTypeError_le
exact (Fintype.card_subtype_le _).trans (hsize g.1)
theorem tendsto_typeErrorFactor_div (B : β„•) :
Tendsto (fun t : β„• => typeErrorFactor B t / (t : ℝ)) atTop (𝓝 0) := by
have hratio : Tendsto (fun t : β„• => ((t * B : β„•) : ℝ) / (t : ℝ))
atTop (𝓝 (B : ℝ)) := by
apply tendsto_const_nhds.congr'
filter_upwards [eventually_ne_atTop (0 : β„•)] with t ht
simp only [Nat.cast_mul]
exact (mul_div_cancel_leftβ‚€ (B : ℝ) (Nat.cast_ne_zero.mpr ht)).symm
have hlog := JointAmbientRateLimit.tendsto_log_succ_div_nat_of_ratio hratio
have hinv : Tendsto (fun t : β„• => (1 : ℝ) / t) atTop (𝓝 0) :=
tendsto_const_nhds.div_atTop tendsto_natCast_atTop_atTop
simpa only [typeErrorFactor, add_div, add_zero] using hinv.add hlog
theorem tendsto_mul_typeErrorFactor_div (K : ℝ) (B : β„•) :
Tendsto (fun t : β„• => K * typeErrorFactor B t / (t : ℝ)) atTop (𝓝 0) := by
simpa only [mul_div_assoc, mul_zero] using (tendsto_typeErrorFactor_div B).const_mul K
end MatrixMultiplication.JointCompatibilityScaling
end
end OAI