import Mathlib import OAI.Computability.DeterministicSum.PaddedMaps namespace OAI /-! Uniform downward matrix recursion and address-capacity invariants. -/ namespace DeterministicThreeSum.Structured.Indexed.MatrixDown open Command DeterministicThreeSum.Rectangular Axis MatrixLayout def update (Q k r : ℕ) : Command := straight [ .assign 8 (.register 3), .assign 3 (.register 4), .assign 4 (.register 8), .assign 7 (.register 1), .assign 9 (.register 6), .binary 6 .quot (.register 6) (.literal Q), .binary 8 .quot (.register 1) (.register 9), .binary 8 .add (.register 8) (.literal (k-1)), .binary 8 .quot (.register 8) (.literal k), .binary 1 .mul (.register 8) (.literal r), .binary 1 .mul (.register 1) (.register 6), .assign 0 (.literal 0)] def updated (s : Data) (Q k r P S B n : ℕ) : Data := put (put (put (put (put (put (put (put (put (put (put (put s 8 P) 3 S) 4 P) 7 (n*B)) 9 B) 6 (B/Q)) 8 n) 8 (n+(k-1))) 8 ((n+(k-1))/k)) 1 (((n+(k-1))/k)*r)) 1 (((n+(k-1))/k)*r*(B/Q))) 0 0 lemma ceil_eq {k : ℕ} (hk : 0 x | d+1,t,x => value T Q k r co d (batchCount t k*r) (mapValue T (k*Q) r (Q^d) (PaddedAxis.pad x (t*Q^(d+1))) co) def capacity (Q k r : ℕ) : ℕ → ℕ → ℕ | 0,t => t | d+1,t => max (batchCount t k*(k*Q)*Q^d) (max (batchCount t k*r*Q^d) (capacity Q k r d (batchCount t k*r))) def finalCount (k r : ℕ) : ℕ → ℕ → ℕ | 0,t => t | d+1,t => finalCount k r d (batchCount t k*r) end DeterministicThreeSum.Structured.Indexed.MatrixDown namespace DeterministicThreeSum.Structured.Indexed.MatrixDown open Command DeterministicThreeSum.Rectangular Axis lemma capacity_output (Q k r d t : ℕ) : batchCount t k*r*tensorStride Q d ≤ capacity Q k r d t := by cases d with | zero => simp [tensorStride,capacity] | succ d => exact (le_max_left _ _).trans (le_max_right _ _) lemma capacity_input {Q k r d t : ℕ} (hk : 0 simp [capacity] | succ d => have h := Nat.mul_le_mul_right (Q^(d+1)) (batchCount_covers t k hk) have he : k*batchCount t k*Q^(d+1)=batchCount t k*(k*Q)*Q^d := by rw [pow_succ]; ring rw [he] at h exact h.trans (le_max_left _ _) lemma batch_pos {t k : ℕ} (ht : 0 have h6 : state.registers 6=0 := by simpa [environment,tensorStride] using hregs ⟨6,by omega⟩ have htest : test w state .lt (.literal 0) (.register 6)=false := by simp [test,operand_literal,operand_register,evalTest,h6] refine ⟨1,state,Eval.loopFalse htest,by simp,?_,?_,by simp⟩ · simpa only [tensorStride,Nat.mul_zero,banks_zero,pow_zero,Nat.mul_one,finalCount] using hregs · simpa only [value,banks_zero,pow_zero,Nat.mul_one,finalCount] using hx | succ d ih => have hlenX : batchCount t k*(k*Q)*Q^d≤L := (le_max_left _ _).trans hL have hlenY : batchCount t k*r*Q^d≤L := ((le_max_left _ _).trans (le_max_right _ _)).trans hL have hlenNext : capacity Q k r d (batchCount t k*r)≤L := ((le_max_right _ _).trans (le_max_right _ _)).trans hL have hp : 0.trans hlenY have hnL : batchCount t k*r≤L := (by simpa using Nat.mul_le_mul_left (batchCount t k*r) hp : batchCount t k*r≤batchCount t k*r*Q^d) |>.trans hlenY have hnr : batchCount (batchCount t k*r) k*r (hframe a.val a.isLt ha).trans (hregs' a) have h1u : u.registers 1=batchCount t k*r*Q^d := by simpa [environment] using hparam ⟨1,by omega⟩ (by norm_num) have h3u : u.registers 3=P := by simpa [environment] using hparam ⟨3,by omega⟩ (by norm_num) have h4u : u.registers 4=S := by simpa [environment] using hparam ⟨4,by omega⟩ (by norm_num) have h6u : u.registers 6=Q^d := by simpa [environment] using hparam ⟨6,by omega⟩ (by norm_num) have eu := update_correct u (by omega : 0 simp [v,updated,put,environment,tensorStride_div hQ,ceil_eq hk,h2u,h5u] have hc' : ∀ i, i x z 0 0 | d+1,t,x,z => leftLeaves f d (batchCount t k*r) (childLeft f x) z theorem value_leftLeaves {T a b k r d t : ℕ} (ha : 0 simpa only [value,leftLeaves,pairCode,pow_zero,Nat.mul_one,Nat.add_zero] using hx z 0 0 | succ d ih => apply ih (childLeft f x) (mapValue T (k*(a*b)) r ((a*b)^d) (PaddedAxis.pad data (t*((a*b)^(d+1)))) co) intro j u v obtain ⟨⟨g,s⟩,rfl⟩ := finProdFinEquiv.surjective j simpa only [finProdFinEquiv,Equiv.coe_fn_mk,Fin.val_mk,Nat.add_comm,Nat.mul_comm] using left_level_value ha hb hr f x data co hx hc g s u v theorem left_loop_correct {w T a b k r L d t P S C : ℕ} (state : Data) (f : FixedFormula (ZMod T) a b k r) (x : LeftBatch (ZMod T) a b d t) (data co : ℕ → ℕ) (ha : 0 y z 0 0 | d+1,t,y,z => rightLeaves f d (batchCount t k*r) (childRight f y) z theorem value_rightLeaves {T a b k r d t : ℕ} (ha : 0 simpa only [value,rightLeaves,pairCode,pow_zero,Nat.mul_one,Nat.add_zero] using hy z 0 0 | succ d ih => apply ih (childRight f y) (mapValue T (k*(b*a)) r ((b*a)^d) (PaddedAxis.pad data (t*((b*a)^(d+1)))) co) intro j u v obtain ⟨⟨g,s⟩,rfl⟩ := finProdFinEquiv.surjective j simpa only [finProdFinEquiv,Equiv.coe_fn_mk,Fin.val_mk,Nat.add_comm,Nat.mul_comm] using right_level_value ha hb hr f y data co hy hc g s u v end end DeterministicThreeSum.Structured.Indexed.MatrixDown end OAI