import ContractExact namespace Submission open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats FloatLib.Floats.ExecFloat.Binary local notation"А"=>USize.lt_iff_toNat_lt local notation"Б"=>0x7FFFFFFF local notation"В"=>FloatArray.emptyWithCapacity local notation"Г"=>mul_le_mul_of_nonneg_right local notation"Д"=>0xFFFFFFFF local notation"Е"=>USize.ofNat local notation"Ж"=>UInt32.le_iff_toNat_le local notation"З"=>Nat.mod_eq_of_lt local notation"И"=>Nat.zero_add local notation"Й"=>Array.replicate local notation"К"=>dite_eq_left local notation"Л"=>rationalWord local notation"М"=>Nat.and_two_pow_sub_one_eq_mod local notation"Н"=>Nat.mul_le_mul_right local notation"О"=>dite_eq_right local notation"П"=>UInt32.toNat_inj local notation"Р"=>0x7F800000 local notation"С"=>two_ne_zero local notation"Т"=>ite_eq_right local notation"У"=>UInt32.lt_iff_toNat_lt local notation"Ф"=>abs_sub_abs_le_abs_sub local notation"Х"=>List.range local notation"Ц"=>toDyadic_ofUInt32 local notation"Ч"=>mul_le_mul_of_nonneg_left local notation"Ш"=>getElem!_pos local notation"Щ"=>UInt64.le_iff_toNat_le local notation"Ъ"=>Model.roundDyadic local notation"Ы"=>UInt32.toNat_toUSize local notation"Ь"=>FloatLib.Numerics.Dyadic.cast_toRat local notation"Э"=>Nat.cast_nonneg local notation"Ю"=>Nat.mul_comm local notation"Я"=>mul_lt_mul_of_pos_right local notation"а"=>zpow_add₀ local notation"б"=>ite_eq_left local notation"в"=>Model.modelSign local notation"г"=>if_neg local notation"д"=>Nat.div_eq_of_lt local notation"е"=>UInt32.toNat_and local notation"ж"=>UInt64.toNat_add local notation"з"=>abs_of_nonneg local notation"и"=>Nat.add_assoc local notation"й"=>Float.ofBits local notation"к"=>Model.toReal local notation"л"=>Finset.range local notation"м"=>Nat.pos_of_ne_zero local notation"н"=>Model.isFinite local notation"о"=>zpow_le_zpow_right₀ local notation"п"=>Bool.and_eq_true_iff local notation"р"=>Nat.add_zero local notation"с"=>Nat.div_lt_of_lt_mul local notation"т"=>zpow_natCast local notation"у"=>List.mem_range local notation"ф"=>Nat.shiftLeft_eq local notation"х"=>if_pos local notation"ц"=>abs_nonneg local notation"ч"=>mul_le_mul local notation"ш"=>UInt32.ofNat local notation"щ"=>Nat.mod_lt local notation"ъ"=>Finset.sum_congr local notation"ы"=>Nat.shiftRight_eq_div_pow local notation"ь"=>USize.toNat_one local notation"э"=>UInt32.le_trans local notation"ю"=>mul_nonneg local notation"я"=>0x80000000 local notation"ذ"=>Model.modelSignBit local notation"ر"=>Nat.le_mul_of_pos_right local notation"ز"=>UInt64.toNat_shiftRight local notation"س"=>getElem!_neg local notation"ش"=>UInt32.toNat_sub_of_le local notation"ص"=>UInt64.lt_iff_toNat_lt local notation"ض"=>Float.Model.UnpackedFloat.unpack local notation"ط"=>Finset.mem_range local notation"ظ"=>Model.roundAt local notation"ع"=>List.range_eq_range' local notation"غ"=>Nat.add_mul local notation"ػ"=>zpow_pos local notation"ؼ"=>FloatLib.Numerics.binaryRadix local notation"ؽ"=>List.foldl_cons local notation"ؾ"=>Nat.mul_add_div local notation"ؿ"=>Nat.mul_add_mod local notation"ـ"=>Nat.not_lt_zero local notation"ف"=>abs_le local notation"ق"=>UInt32.toNat_ofNat' local notation"ك"=>Nat.succ_mul local notation"ل"=>Model.toModel_ofFloatModel local notation"م"=>Nat.one_mul local notation"ن"=>zpow_sub₀ local notation"ه"=>List.nodup_range local notation"و"=>List.range'_succ local notation"ى"=>UInt64.toNat_and local notation"ي"=>FloatLib.Numerics.Dyadic.toRat_eq_zero_iff local notation"Ա"=>List.mem_of_getElem? local notation"Բ"=>Model.toReal_eq_unpackedToReal_toModel local notation"Գ"=>abs_add_le local notation"ה"=>(by decide) local notation"ו"=>(by positivity) local notation"ז"=>(by simp;omega) local notation"ח"=>(by rfl) local notation"ט"=>(by rw[А];omega) open FloatLib.Numerics.Dyadic (signedSignificand toRat_eq_zero_iff add_toRat mul_toRat mulFields) open UInt64 (toNat_sub_of_le) open Bool (false_eq_true and_eq_true or_eq_true) local notation"א"=>by omega local notation"ב"=>by norm_num local notation"ג"=>by simp local notation"ד"=>by linarith local notation"ѐ"=>Array UInt32 local notation"ё"=>Array Word local notation"ђ"=>FloatArray local notation"ѓ"=>UInt32 local notation"є"=>USize local notation"ѕ"=>Float local notation"і"=>UInt64 local notation"ї"=>Array Nat local notation"ј"=>Bool local notation"љ"=>List Nat local notation"њ"=>Word local notation"ћ"=>Nat open USize (toNat) open UInt32 (toNat) open UInt64 (toNat) local notation:max"Ѡ"x:max=>toNat x section open FloatLib.Numerics end lemma qAa (n:ћ) (f:ћ->ѓ) (k:ћ) (hk:k< n) : (Array.ofFn (n:=n) λi=>f i.val)[k]! = f k:=by rw[Ш _ k (by simpa using hk),Array.getElem_ofFn] lemma qBd (w:њ):toWord (ofW w)=w:=by apply П.mp;simp only[toWord,ofW,Model.toNatBits,ofUInt32_bits,UInt32.toNat_ofNat'];exact З w.toBitVec.isLt lemma qB2 (z:Model Fmt):ofW (toWord z)=z:=by have h:z.toNatBits<2^32:=Model.toNatBits_lt_two_pow z;simp only[ofW,toWord,Model.ofUInt32,UInt32.toNat_ofNat',Nat.mod_eq_of_lt h];exact Model.ofNatBits_toNatBits z @[inline] def keyOf (w:њ):Int := if signOf w then -((mantOf w <<< idxOf w:ћ):Int) else ((mantOf w <<< idxOf w:ћ):Int) lemma qFo (w:њ):keyOf w=sv w*2^idxOf w:=by unfold keyOf sv;split<;>simp[Nat.shiftLeft_eq] lemma qAf (w:њ) : (decodeD w).toReal=(keyOf w:ℝ)*2^(-149:Int):=by rw[<-Ь,decodeD_toRat,qFo];push_cast;rw[ن С,т];ring @[inline] def maxW (a b:њ):њ := if expOf a=255∨expOf b=255 then toWord (Model.maximum (ofW a) (ofW b)) else match compare (keyOf a) (keyOf b) with | .lt=>b | .gt=>a | .eq=>toWord (if Model.isZero (ofW a) && Model.isZero (ofW b) then Model.zero Fmt (Model.signBit (ofW a) && Model.signBit (ofW b)) else ofW a) lemma qDs (a b:њ) : Model.cmpDyadic (decodeD a) (decodeD b)=compare (keyOf a) (keyOf b):=by have hs:(0:ℝ)<2^(-149:Int):=ػ two_pos _;cases h:compare (keyOf a) (keyOf b);. rw[Model.cmpDyadic_lt_iff,qAf,qAf];exact Я (by exact_mod_cast compare_lt_iff_lt.mp h) hs . rw[Model.cmpDyadic_eq_iff,qAf,qAf,compare_eq_iff_eq.mp h] . rw[Model.cmpDyadic_gt_iff,qAf,qAf];exact Я (by exact_mod_cast compare_gt_iff_gt.mp h) hs lemma qEw (a b:њ):ofW (maxW a b)=Model.maximum (ofW a) (ofW b):=by unfold maxW;split;. exact qB2 _ . rename_i hs;have ha:=Ц a (λh=>hs (Or.inl h));have hb:=Ц b (λh=>hs (Or.inr h));have hna:=Model.isNaN_eq_false_of_toDyadic?_some ha;have hnb:=Model.isNaN_eq_false_of_toDyadic?_some hb;have hc:Model.compare (ofW a) (ofW b)=some _ :=Model.compare_eq_some_cmpDyadic_of_toDyadic? _ _ ha hb;rw[qDs] at hc;have hnone:Model.chooseNaN2 (ofW a) (ofW b)=none :=(Model.chooseNaN2_eq_none_iff _ _).2 ⟨hna,hnb⟩;unfold Model.maximum;rw[Model.withNaNSelection_of_none _ _ hnone];dsimp only;rw[Model.compareNonNaN_eq_of_compare_eq_some _ _ _ _ hc];cases compare (keyOf a) (keyOf b)<;>simp only[qB2] def maxpoolW (c h w k:ћ) (inp:ё):ё := Array.ofFn (n:=c*(h/k*(w/k))) λq => let ch:=q.val/(h/k*(w/k)) let y:=q.val/(w/k)%(h/k) let x:=q.val%(w/k) (Х (k*k)).foldl (λacc t=>maxW acc (poolAt h w k inp ch y x t)) (poolAt h w k inp ch y x 0) lemma qD5 (p:ћ->њ) (l:љ) (acc:њ) : l.foldl (λm t=>Model.maximum m (ofW (p t))) (ofW acc) =ofW (l.foldl (λa t=>maxW a (p t)) acc):=by induction l generalizing acc with | nil=>rfl | cons t l ih=>simp only[List.foldl_cons,<-qEw,ih] lemma qDr:maxpoolW=maxpoolSpec:=by funext c h w k inp;simp only[maxpoolW,maxpoolSpec,qD5,qBd] lemma qDf {w:ѓ} (h:expOf w≠255):н (ofW w)=true:=by simp[ofW,isFinite_ofUInt32,h] lemma qEi (A B:ѐ) (hB:B.size=A.size) (hall:∀ k,k< A.size->expOf A[k]!≠255∧expOf B[k]!≠255) : finishWord (dsum A B 0 A.size) (-298) (A.size≠0 && dallNZ A B 0 A.size)=dotSpec A B:=by set L:=A.map (Model.ofUInt32 (fmt:=Fmt)) with hL;set R:=B.map (Model.ofUInt32 (fmt:=Fmt)) with hR;have hLR:L.size=R.size:=by simp[hL,hR,hB] have hS:L.size=A.size:=by simp[hL] have hLf:∀ k (hk:k< L.size),н L[k]=true:=λk hk=>by simpa [hL,ofW] using qDf (getElem!_pos A k א ▸ (hall k א).1) have hRf:∀ k (hk:k< R.size),н R[k]=true:=λk hk=>by simpa [hR,ofW] using qDf (getElem!_pos B k א ▸ (hall k א).2) have f k (_:0<=k) (h:k<0+L.size):=hall k א;have s:=(finiteDotSlice_eq A B 0 L.size (by simp[hL]) (by simp[hL,hB]) f).trans (by rw[hS]);have x:=(Model.Reduction.Internal.dotState_exact_toRat (destination:=Fmt) L R hLR hLf hRf).trans s;unfold dotSpec Model.dot;by_cases h0:dsum A B 0 A.size=0;. rw[Model.Reduction.dotWithStatus_eq_zero_of_finite Fmt L R .nearestEven hLR hLf hRf ((ي _).1 (by simp[x,h0])),h0,finishWord_zero];obtain ⟨h1,h2⟩:=dotStateLoop_flags A B {} 0 L.size (by simp[hL]) (by simp[hL,hB]) f;simp only[Except.map,Model.Reduction.Internal.dotState];rw[h1,h2];simp[hS] . rw[Model.Reduction.dotWithStatus_eq_round_of_finite_nonzero Fmt L R .nearestEven hLR hLf hRf (by rw[Model.Reduction.Semantics.finiteDot_toList_eq_slice L R hLR,s] simpa using h0)] exact finishWord_of_ne _ _ _ _ h0 x structure Acc where (pos neg:ї) (special negZero:ј) def Acc.total (acc:Acc):Int:=(limbVal acc.pos.toList:Int)-limbVal acc.neg.toList @[inline] def Acc.add (acc:Acc) (m idx:ћ) (neg:ј):Acc := match acc with | ⟨pos,ng,sp,nz⟩ => if m=0 then ⟨pos,ng,sp,nz && neg⟩ else if neg then ⟨pos,addLimbs64 ng m.toUInt64 idx,sp,false⟩ else ⟨addLimbs64 pos m.toUInt64 idx,ng,sp,false⟩ lemma Acc.add_spec (acc:Acc) (m idx:ћ) (neg:ј) (hp:acc.pos.size=18) (hn:acc.neg.size=18) (hm:m<2^64) (hi:idx/32+1<18) : (acc.add m idx neg).pos.size=18∧(acc.add m idx neg).neg.size=18 ∧ (acc.add m idx neg).special=acc.special ∧ (acc.add m idx neg).negZero=(acc.negZero && (decide (m=0) && neg)) ∧ (acc.add m idx neg).total=acc.total+(if neg then -(m:Int) else m)*2^idx:=by obtain ⟨pos,ng,sp,nz⟩:=acc;simp only at hp hn;by_cases hm0:m=0;. simp[Acc.add,Acc.total,hm0,hp,hn] . cases neg;. refine ⟨by simp[Acc.add,hm0,addLimbs64_size,hp],by simp[Acc.add,hm0,hn],by simp[Acc.add,hm0],by simp[Acc.add,hm0],?_⟩;simp only[Acc.add,hm0,ite_false,false_eq_true,addLimbs64_eq _ _ _ hm,Acc.total];rw[addLimbs_val _ _ _ א];push_cast;ring . refine ⟨by simp[Acc.add,hm0,hp],by simp[Acc.add,hm0,addLimbs64_size,hn],by simp[Acc.add,hm0],by simp[Acc.add,hm0],?_⟩;simp only[Acc.add,hm0,ite_false,ite_true,addLimbs64_eq _ _ _ hm,Acc.total];rw[addLimbs_val _ _ _ א];push_cast;ring @[inline] def dotStep (acc:Acc) (wa wb:ѓ):Acc := if expOf wa=255∨expOf wb=255 then ⟨acc.pos,acc.neg,true,acc.negZero⟩ else acc.add (mantOf wa*mantOf wb) (idxOf wa+idxOf wb) (Bool.xor (signOf wa) (signOf wb)) @[specialize] def dotLoop (fa fb:ћ->ѓ) (pos neg:ї) (sp nz:ј) (index:ћ):ћ->Acc | 0=>⟨pos,neg,sp,nz⟩ | count+1 => match dotStep ⟨pos,neg,sp,nz⟩ (fa index) (fb index) with | ⟨p,n,s,z⟩=>dotLoop fa fb p n s z (index+1) count lemma qB5 (fa fb:ћ->ѓ) (pos neg:ї) (nz:ј) (index count:ћ):(dotLoop fa fb pos neg true nz index count).special=true:=by induction count generalizing pos neg nz index with | zero=>rfl | succ count ih => rw[dotLoop];have hs:(dotStep ⟨pos,neg,true,nz⟩ (fa index) (fb index)).special=true:=by unfold dotStep Acc.add;split_ifs<;>rfl generalize dotStep ⟨pos,neg,true,nz⟩ (fa index) (fb index)=r at hs ⊢;obtain ⟨p,q,s,z⟩:=r;simp only at hs;subst hs;exact ih .. lemma qDe (fa fb:ћ->ѓ) (A B:ѐ) (n:ћ) (hA:∀ k,k< n->A[k]! = fa k) (hB:∀ k,k< n->B[k]! = fb k) (acc:Acc) (index remaining:ћ) (h:index+remaining<=n) (hp:acc.pos.size=18) (hn:acc.neg.size=18) (hfin:(dotLoop fa fb acc.pos acc.neg acc.special acc.negZero index remaining).special =false) : (dotLoop fa fb acc.pos acc.neg acc.special acc.negZero index remaining).pos.size=18 ∧ (dotLoop fa fb acc.pos acc.neg acc.special acc.negZero index remaining).neg.size=18 ∧ acc.special=false ∧ (∀ k,index<=k->k< index+remaining->expOf A[k]!≠255∧expOf B[k]!≠255) ∧ (dotLoop fa fb acc.pos acc.neg acc.special acc.negZero index remaining).total =acc.total+dsum A B index remaining ∧ (dotLoop fa fb acc.pos acc.neg acc.special acc.negZero index remaining).negZero =(acc.negZero && dallNZ A B index remaining):=by induction remaining generalizing acc index with | zero=>obtain ⟨p,n,s,z⟩:=acc;simp_all [dotLoop,dsum,dallNZ] | succ count ih => obtain ⟨p,q,s,z⟩:=acc simp only at hp hn have ha:=hA index א have hb:=hB index א by_cases he:expOf (fa index)=255∨expOf (fb index)=255 . exfalso;rw[dotLoop] at hfin;have hs:(dotStep ⟨p,q,s,z⟩ (fa index) (fb index)).special=true:=by simp[dotStep,he] generalize dotStep ⟨p,q,s,z⟩ (fa index) (fb index)=r at hs hfin;obtain ⟨p',q',s',z'⟩:=r;simp only at hs;subst hs;rw[qB5] at hfin;exact Bool.noConfusion hfin . have he':=not_or.mp he obtain ⟨h1,h2,h3,h4,h5⟩ := Acc.add_spec ⟨p,q,s,z⟩ (mantOf (fa index)*mantOf (fb index)) (idxOf (fa index)+idxOf (fb index)) (Bool.xor (signOf (fa index)) (signOf (fb index))) hp hn (mul_mant_lt _ _) (by have:=idxOf_le _ he'.1;have:=idxOf_le _ he'.2;omega) have hstep:dotStep ⟨p,q,s,z⟩ (fa index) (fb index) = Acc.add ⟨p,q,s,z⟩ (mantOf (fa index)*mantOf (fb index)) (idxOf (fa index)+idxOf (fb index)) (Bool.xor (signOf (fa index)) (signOf (fb index))):=by simp[dotStep,he] rw[dotLoop,hstep] at hfin ⊢;obtain ⟨hP,hN,hs,hall,hD,hz⟩:=ih _ (index+1) א h1 h2 hfin;refine ⟨hP,hN,by rw[<-h3,hs],?_,?_,?_⟩ . intro k k1 k2;rcases Nat.lt_or_ge k (index+1) with hk | hk;. have:k=index :=א;subst this;rw[ha,hb];exact he' . exact hall k hk א . have sv2:(if Bool.xor (signOf (fa index)) (signOf (fb index)) then -((mantOf (fa index)*mantOf (fb index):ћ):Int) else ((mantOf (fa index)*mantOf (fb index):ћ):Int)) =sv (fa index)*sv (fb index):=by unfold sv;cases signOf (fa index)<;>cases signOf (fb index)<;>simp rw[hD,h5,dsum,ha,hb,sv2];ring . rw[hz,h4,dallNZ,ha,hb];simp[Bool.and_assoc] def Y_low (p q:ї) (k:ћ):ћ->ћ | 0=>k | f+1=>if p[k]! = 0 && q[k]! = 0 then Y_low p q (k+1) f else k lemma qEg (p q:ї):∀ f k,(∀ j,j< k->p[j]! = 0∧q[j]! = 0) → ∀ j,j< Y_low p q k f->p[j]! = 0∧q[j]! = 0:=by intro f;induction f with | zero=>exact λk h j hj=>h j hj | succ f ih => intro k h j hj;unfold Y_low at hj;split at hj . rename_i hk;simp only[and_eq_true,decide_eq_true_eq] at hk;refine ih (k+1) (λi hi=>?_) j hj;rcases Nat.lt_or_ge i k with h1 | h1;. exact h i h1 . have:i=k :=א;subst this;exact hk . exact h j hj lemma qHd (L:љ):∀ l,(∀ j,j< l->L[j]! = 0) → limbVal L=2^(32*l)*limbVal (L.drop l):=by induction L with | nil=>simp[limbVal] | cons x xs ih => intro l h;cases l with | zero=>simp | succ l => have h0:x=0:=by simpa using h 0 א have:=ih l (λj hj=>by simpa using h (j+1) א);simp only[limbVal,List.drop_succ_cons,h0,this];rw[Nat.mul_add 32 l 1,pow_add];ring lemma qGh (A:ї) (l:ћ) (h:∀ j,j< l->A[j]! = 0) : limbVal A.toList=2^(32*l)*limbValA (A.extract l A.size):=by rw[limbValA_eq,Array.toList_extract,List.extract_eq_drop_take,<-Array.length_toList];rw[List.take_of_length_le ג];refine qHd _ l λj hj=>?_;have:=h j hj;rw[getElem!_def] at this;simp only[Array.getElem!_toList,getElem!_def] at *;cases e:A[j]?<;>simp_all lemma qJk (S:Int) (l:ћ) (e:Int) (z:ј) : finishWord S (e+32*l) z=finishWord (2^(32*l)*S) e z:=by by_cases hS:S=0;. simp[hS,finishWord] have h2:(2:Int)^(32*l)*S≠0:=mul_ne_zero ו hS rw[finishWord_of_ne (ofIntDyadic (2^(32*l)*S) e) S (e+32*l) z hS (by rw[toRat_ofIntDyadic,а ב,show (32:ℤ)*l=((32*l:ℕ):ℤ) by push_cast;ring,т];push_cast;ring)] simp[finishWord,h2] @[inline] def dotFn (n:ћ) (fa fb:ћ->ѓ):ѓ := let acc:=dotLoop fa fb (Й 18 0) (Й 18 0) false true 0 n if acc.special then dotSpec (Array.ofFn (n:=n) λi=>fa i) (Array.ofFn (n:=n) λi=>fb i) else let l:=Y_low acc.pos acc.neg 0 18 finishWord ((limbValA (acc.pos.extract l 18):Int)-limbValA (acc.neg.extract l 18)) (-298+32*l) (n≠0 && acc.negZero) lemma qAc:dotFn=dotOf:=by funext n fa fb;unfold dotFn dotOf;set A:=Array.ofFn (n:=n) λi=>fa i with hA;set B:=Array.ofFn (n:=n) λi=>fb i with hB;set acc:=dotLoop fa fb (Й 18 0) (Й 18 0) false true 0 n with hacc;by_cases hsp:acc.special=true;. simp[hsp] have hsp':acc.special=false:=by simpa using hsp have hF:=qDe fa fb A B n (qAa n fa) (qAa n fb) ⟨Й 18 0,Й 18 0,false,true⟩ 0 n א ג ג hsp';obtain ⟨hP,hN,-,hall,hD,hz⟩:=hF;have hAs:A.size=n:=by simp[hA] have hBs:B.size=n:=by simp[hB] have hL:=qEg acc.pos acc.neg 18 0 (λj hj=>absurd hj (ـ _));simp only[hsp',false_eq_true,ite_false];have e0:(⟨Й 18 0,Й 18 0,false,true⟩:Acc).total=0:=by decide rw[e0,Int.zero_add] at hD;rw[Bool.true_and] at hz;generalize Y_low acc.pos acc.neg 0 18=l at hL ⊢;have h1:=qGh acc.pos l (λj hj=>(hL j hj).1);have h2:=qGh acc.neg l (λj hj=>(hL j hj).2);rw[hP] at h1;rw[hN] at h2;rw[qJk,<-qEi A B א (λk hk=>hall k א א),hAs,<-hD,<-hz];congr 1;have:acc.total=(limbVal acc.pos.toList:Int)-limbVal acc.neg.toList:=rfl;rw[this,h1,h2];push_cast;ring lemma usz (t n:є) (h:t< n):(t+1).toNat=Ѡt+1:=by have h1:Ѡt< Ѡn:=А.mp h;have h2:Ѡn< USize.size:=n.toNat_lt_size;have h3:USize.size=2^System.Platform.numBits:=rfl;rw[USize.toNat_add,ь];exact З א lemma qAg (a b:є) (h:Ѡa+Ѡb<2^32):(a+b).toNat=Ѡa+Ѡb:=by have h1:2^32<=USize.size:=USize.le_size;have h3:USize.size=2^System.Platform.numBits:=rfl;rw[USize.toNat_add];exact З א lemma uk (k:ћ) (h:k<2^32):(Е k).toNat=k:=by have h1:2^32<=USize.size:=USize.le_size;have h3:USize.size=2^System.Platform.numBits:=rfl;rw[USize.toNat_ofNat'];exact З א lemma qBu (a:ђ) (i:є) (h:i< a.usize):Ѡi< a.size:=by have h1:=А.mp h;have h2:a.usize.toNat<=a.size:=by simp only[FloatArray.usize,Nat.toUSize_eq,USize.toNat_ofNat'];exact Nat.mod_le _ _ omega lemma qA5 (a:ѐ) (i:є) (h:i< a.usize):Ѡi< a.size:=by have h1:=А.mp h;have h2:a.usize.toNat<=a.size:=by simp only[Array.usize,Nat.toUSize_eq,USize.toNat_ofNat'];exact Nat.mod_le _ _ omega @[inline] def rdF (a:ђ) (i:є):ѕ := if h:i< a.usize then a.uget i (qBu a i h) else 0 @[inline] def rdW (a:ѐ) (i:є):ѓ := if h:i< a.usize then a.uget i (qA5 a i h) else 0 @[inline] def wrF (a:ђ) (i:є) (v:ѕ):ђ := if h:i< a.usize then a.uset i v (qBu a i h) else a @[inline] def wrW (a:ѐ) (i:є) (v:ѓ):ѐ := if h:i< a.usize then a.uset i v (qA5 a i h) else a def pwEntry (i:ћ):ѕ := let e:=(i%256).toUInt64 let s:і:=if i ≥ 256 then 0x8000000000000000 else 0 if e == 0 then й s else й (s ||| ((e+873) <<< (52:і))) def pw:ђ := (Х 512).foldl (λa i=>a.push (pwEntry i)) (В 512) lemma qDz:pw.data=(Х 512).toArray.map pwEntry:=by have key:∀ (l:љ) (a:ђ),(l.foldl (λa i=>a.push (pwEntry i)) a).data=a.data ++ (l.toArray.map pwEntry):=by intro l;induction l with | nil=>intro a;simp | cons x l ih => intro a;simp only[List.foldl_cons,ih];simp[FloatArray.push] simp only[pw,key];simp[FloatArray.emptyWithCapacity] lemma qGt:pw.size=512:=by simp[FloatArray.size,qDz] lemma qCb (w:ѓ):(w >>> (23:ѓ)).toUSize.toNat< pw.size:=by rw[qGt];have h:=w.toNat_lt_size;simp only[UInt32.toNat_toUSize,UInt32.toNat_shiftRight];norm_num [Nat.shiftRight_eq_div_pow] at h ⊢;omega def pwEntryN (i:ћ):ѕ := if i%256 == 0 || i%256 == 255 then й 0x7FF8000000000000 else pwEntry i def pwN:ђ := (Х 512).foldl (λa i=>a.push (pwEntryN i)) (В 512) lemma qCy:pwN.data=(Х 512).toArray.map pwEntryN:=by have key:∀ (l:љ) (a:ђ),(l.foldl (λa i=>a.push (pwEntryN i)) a).data=a.data ++ (l.toArray.map pwEntryN):=by intro l;induction l with | nil=>intro a;simp | cons x l ih => intro a;simp only[List.foldl_cons,ih];simp[FloatArray.push] simp only[pwN,key];simp[FloatArray.emptyWithCapacity] lemma qCi (w:ѓ):(w >>> (23:ѓ)).toUSize.toNat< pwN.size:=by have e:pwN.size=512:=by simp[FloatArray.size,qCy] rw[e];have h:=w.toNat_lt_size;simp only[UInt32.toNat_toUSize,UInt32.toNat_shiftRight];norm_num [Nat.shiftRight_eq_div_pow] at h ⊢;omega @[inline] def dvN (w:ѓ):ѕ := ((w &&& 0x7FFFFF) ||| 0x800000).toFloat*pwN.uget (w >>> (23:ѓ)).toUSize (qCi w) @[inline] def dv (w:ѓ):ѕ := ((w &&& 0x7FFFFF) ||| 0x800000).toFloat*pw.uget (w >>> (23:ѓ)).toUSize (qCb w) @[inline] def dvB (v:ѕ) (w:ѓ):ѕ := (v*pw.uget (w >>> (23:ѓ)).toUSize (qCb w))*((w &&& 0x7FFFFF) ||| 0x800000).toFloat @[inline] def good (w:ѓ):ј := let v:=w &&& Б v == 0 || v-(0x00800000:ѓ)<=(0x7EFFFFFF:ѓ) def scan (A:ѐ) (n i:є) (hn:Ѡn<=A.size) (lo hi:ѓ):ѓ := if h:i< n then have ht:=А.mp h let v:=A.uget i א &&& Б let u:=v-1 scan A n (i+1) hn (if u< lo then u else lo) (if hi< v then v else hi) else if 0x007FFFFF<=lo∧hi<Р then hi else Д termination_by Ѡn-Ѡi decreasing_by rw[usz i n h];omega def scanF (A:ѐ) (n i:є) (hn:Ѡn<=A.size) (o s:ѓ):ѓ := if h:i< n then have ht:=А.mp h let v:=A.uget i א &&& Б scanF A n (i+1) hn (o ||| v) (s &&& (v-1)) else if 0x7FFFFF<=s∧o<Р then o else Д termination_by Ѡn-Ѡi decreasing_by rw[usz i n h];omega lemma qAe (a:ѐ):a.usize.toNat<=a.size:=by simp only[Array.usize,Nat.toUSize_eq,USize.toNat_ofNat'];exact Nat.mod_le _ _ @[inline] def scanH (A:ѐ):ѓ := let h:=scanF A A.usize 0 (qAe A) 0 Д if h == Д then scan A A.usize 0 (qAe A) Д 0 else h def pushZ (n k:є) (a:ђ):ђ := if h:k< n then pushZ n (k+1) (a.push 0) else a termination_by Ѡn-Ѡk decreasing_by rw[usz k n h];have:=А.mp h;omega @[inline] def zivW (A B:і):ѓ := let x:і:=(A+(0x0FFFFFFF:і)) >>> (29:і) let y:і:=(B+(0x0FFFFFFF:і)) >>> (29:і) let ex:і:=(x >>> (23:і)) &&& (0x7FF:і) if x == y && ex-(897:і)<(254:і) then (((x >>> (34:і)) <<< (31:і)) ||| ((x &&& (0x3FFFFFFFF:і))-((896:і) <<< (23:і)))).toUInt32 else Д @[inline] def zivS (s:ѕ):ѓ := let a:=s.toBits zivW (a+2) (a-2) @[inline] def ziv (s e:ѕ):ѓ := zivW (s+e).toBits (s-e).toBits @[noinline] def slowOut (inW:ћ) (relu:ј) (X W b:ё) (i j:ћ):њ := act relu (dotFn (inW+1) (λt=>if t< inW then X[i*inW+t]! else oneW) (λt=>if t< inW then W[j*inW+t]! else b[j]!)) @[inline] def fin (inW:ћ) (relu:ј) (X W b:ё) (i j:ћ) (s nf t:ѕ) (bw:ѓ):ѓ := let bv:=dv bw let r:=ziv (s+bv) (nf*(t+bv.abs)) if r == Д then slowOut inW relu X W b i j else act relu r structure F1 where (s t:ѕ) def tightGo (X W:ѐ) (xo wo n t:є) (s a:ѕ):F1 := if h:t< n then let q:=dv (rdW X (xo+t))*dv (rdW W (wo+t)) tightGo X W xo wo n (t+1) (s+q) (a+q.abs) else ⟨s,a⟩ termination_by Ѡn-Ѡt decreasing_by rw[usz t n h];have:=А.mp h;omega @[inline] def fin8 (inW:ћ) (relu:ј) (X W b:ё) (i j:ћ) (s nf a:ѕ) (hx bw:ѓ) (n:є):ѓ := let bv:=dv bw let r:=ziv (s+bv) (nf*(dv hx*a+bv.abs)) if r == Д then let q:=tightGo X W (i*inW).toUSize (j*inW).toUSize n 0 0 0 fin inW relu X W b i j q.s nf q.t bw else act relu r structure R1 where (ix:ѐ) (buf:ђ) (c:є) (l:ѕ) def decR (X:ѐ) (o n t:є) (ix:ѐ) (buf:ђ) (c:є) (l:ѕ):R1 := if h:t< n then let w:=rdW X (o+t) if w &&& Б == 0 then decR X o n (t+1) ix buf c l else let v:=dv w decR X o n (t+1) (wrW ix c t.toUInt32) (wrF buf c v) (c+1) (l+v.abs) else ⟨ix,buf,c,l⟩ termination_by Ѡn-Ѡt decreasing_by all_goals (rw[usz t n h];have:=А.mp h;omega) lemma wi (w i m:є) (W:ѐ) (hw:Ѡw+Ѡm<=W.size) (hs:W.size<2^32) (hi:Ѡi< Ѡm):(w+i).toNat< W.size:=by rw[qAg _ _ א];omega structure F4 where (a b c d:ѕ) def go4B (x:ђ) (ix W:ѐ) (w0 w1 w2 w3 m n t:є) (hx:Ѡn<=x.size) (hi:Ѡn<=ix.size) (hs:W.size<2^32) (h0:Ѡw0+Ѡm<=W.size) (h1:Ѡw1+Ѡm<=W.size) (h2:Ѡw2+Ѡm<=W.size) (h3:Ѡw3+Ѡm<=W.size) (a b c d:ѕ):F4 := if h:t< n then have ht:=А.mp h let v:=x.uget t א let i:=(ix.uget t א).toUSize if hm:i< m then have him:=А.mp hm let q0:=v*dv (W.uget (w0+i) (wi w0 i m W h0 hs him)) let q1:=dvB v (W.uget (w1+i) (wi w1 i m W h1 hs him)) let q2:=v*dv (W.uget (w2+i) (wi w2 i m W h2 hs him)) let q3:=dvB v (W.uget (w3+i) (wi w3 i m W h3 hs him)) go4B x ix W w0 w1 w2 w3 m n (t+1) hx hi hs h0 h1 h2 h3 (a+q0) (b+q1) (c+q2) (d+q3) else go4B x ix W w0 w1 w2 w3 m n (t+1) hx hi hs h0 h1 h2 h3 a b c d else ⟨a,b,c,d⟩ termination_by Ѡn-Ѡt decreasing_by all_goals (rw[usz t n h];omega) def go1 (x:ђ) (ix W:ѐ) (w0 m n t:є) (hx:Ѡn<=x.size) (hi:Ѡn<=ix.size) (hs:W.size<2^32) (h0:Ѡw0+Ѡm<=W.size) (a:ѕ):ѕ := if h:t< n then have ht:=А.mp h let i:=(ix.uget t א).toUSize if hm:i< m then have him:=А.mp hm let q:=x.uget t א*dv (W.uget (w0+i) (wi w0 i m W h0 hs him)) go1 x ix W w0 m n (t+1) hx hi hs h0 (a+q) else go1 x ix W w0 m n (t+1) hx hi hs h0 a else a termination_by Ѡn-Ѡt decreasing_by all_goals (rw[usz t n h];omega) def go4D (x wd:ђ) (ix W:ѐ) (w0 w1 w2 w3 m n t:є) (hx:Ѡn<=x.size) (hi:Ѡn<=ix.size) (hs:W.size<2^32) (h0:Ѡw0+Ѡm<=W.size) (h1:Ѡw1+Ѡm<=W.size) (h2:Ѡw2+Ѡm<=W.size) (h3:Ѡw3+Ѡm<=W.size) (hd:wd.size=W.size) (a b c d:ѕ):F4 := if h:t< n then have ht:=А.mp h let v:=x.uget t א let i:=(ix.uget t א).toUSize if hm:i< m then have him:=А.mp hm go4D x wd ix W w0 w1 w2 w3 m n (t+1) hx hi hs h0 h1 h2 h3 hd (a+v*wd.uget (w0+i) (hd ▸ wi w0 i m W h0 hs him)) (b+v*wd.uget (w1+i) (hd ▸ wi w1 i m W h1 hs him)) (c+v*wd.uget (w2+i) (hd ▸ wi w2 i m W h2 hs him)) (d+v*wd.uget (w3+i) (hd ▸ wi w3 i m W h3 hs him)) else go4D x wd ix W w0 w1 w2 w3 m n (t+1) hx hi hs h0 h1 h2 h3 hd a b c d else ⟨a,b,c,d⟩ termination_by Ѡn-Ѡt decreasing_by all_goals (rw[usz t n h];omega) lemma qA1 {n:ћ} (h:n<=4294967295):n<2^32 :=א def cols1 (inW outW:ћ) (relu:ј) (X W b:ѐ) (i:ћ) (ix:ѐ) (x wd:ђ) (nf a:ѕ) (hx:ѓ) (m n:є) (j:ћ) (base:є) (out:ѐ):ѐ := if h4:j+4<=outW then let ju:=j.toUSize let w0:=ju*m let w1:=(ju+1)*m let w2:=(ju+2)*m let w3:=(ju+3)*m if hg:Ѡn<=x.size∧Ѡn<=ix.size∧W.size<=4294967295∧Ѡw0+Ѡm<=W.size ∧ Ѡw1+Ѡm<=W.size∧Ѡw2+Ѡm<=W.size∧Ѡw3+Ѡm<=W.size then let r:=if hd:wd.size=W.size then go4D x wd ix W w0 w1 w2 w3 m n 0 hg.1 hg.2.1 (qA1 hg.2.2.1) hg.2.2.2.1 hg.2.2.2.2.1 hg.2.2.2.2.2.1 hg.2.2.2.2.2.2 hd 0 0 0 0 else go4B x ix W w0 w1 w2 w3 m n 0 hg.1 hg.2.1 (qA1 hg.2.2.1) hg.2.2.2.1 hg.2.2.2.2.1 hg.2.2.2.2.2.1 hg.2.2.2.2.2.2 0 0 0 0 let o0:=fin8 inW relu X W b i j r.a nf a hx (rdW b ju) m let o1:=fin8 inW relu X W b i (j+1) r.b nf a hx (rdW b (ju+1)) m let o2:=fin8 inW relu X W b i (j+2) r.c nf a hx (rdW b (ju+2)) m let o3:=fin8 inW relu X W b i (j+3) r.d nf a hx (rdW b (ju+3)) m let out:=wrW out (base+ju) o0 let out:=wrW out (base+ju+1) o1 let out:=wrW out (base+ju+2) o2 let out:=wrW out (base+ju+3) o3 cols1 inW outW relu X W b i ix x wd nf a hx m n (j+4) base out else out else if h1:j< outW then let ju:=j.toUSize let w0:=ju*m if hg:Ѡn<=x.size∧Ѡn<=ix.size∧W.size<=4294967295∧Ѡw0+Ѡm<=W.size then let r:=go1 x ix W w0 m n 0 hg.1 hg.2.1 (qA1 hg.2.2.1) hg.2.2.2 0 let o:=fin8 inW relu X W b i j r nf a hx (rdW b ju) m cols1 inW outW relu X W b i ix x wd nf a hx m n (j+1) base (wrW out (base+ju) o) else out else out termination_by outW-j def rows1 (rows inW outW:ћ) (relu:ј) (X W b:ѐ) (wd:ђ) (nf:ѕ) (hv:ѓ) (m:є) (i:ћ) (ix:ѐ) (buf:ђ) (out:ѐ):ѐ := if h:i< rows then let r:=decR X (i*inW).toUSize m 0 ix buf 0 0 let out:=cols1 inW outW relu X W b i r.ix r.buf wd nf r.l hv m r.c 0 (i*outW).toUSize out rows1 rows inW outW relu X W b wd nf hv m (i+1) r.ix r.buf out else out termination_by rows-i structure F8 where (u:UInt8) (s0 s1 s2 s3 s4 s5 s6 s7:ѕ) @[inline] def f8 (r:F8):ћ->ѕ | 0=>r.s0 | 1=>r.s1 | 2=>r.s2 | 3=>r.s3 | 4=>r.s4 | 5=>r.s5 | 6=>r.s6 | _=>r.s7 lemma qC4 (a b:є) (h:Ѡa*Ѡb<2^32) : (a*b).toNat=Ѡa*Ѡb:=by have h1:2^32<=USize.size:=USize.le_size;have h3:USize.size=2^System.Platform.numBits:=rfl;rw[USize.toNat_mul];exact З א lemma xi (xs:ђ) (t n:є) (k:ћ) (hk:k<8) (ht:Ѡt< Ѡn) (hx:8*Ѡn<=xs.size) (hs:xs.size<2^32):(Е 8*t+Е k).toNat< xs.size:=by have e2:=uk 8 ב;have e3:=uk k (א:k<2^32);have e:=qC4 (Е 8) t (by rw[e2];omega);rw[qAg _ _ (by rw[e,e2,e3];omega),e,e2,e3];omega def go8 (xs:ђ) (W:ѐ) (wo n t:є) (hx:8*Ѡn<=xs.size) (hsx:xs.size<2^32) (hs:W.size<2^32) (hw:Ѡwo+Ѡn<=W.size) (s0 s1 s2 s3 s4 s5 s6 s7:ѕ) (r:F8):F8 := if h:t< n then have ht:=А.mp h let v:=dv (W.uget (wo+t) (wi wo t n W hw hs ht)) go8 xs W wo n (t+1) hx hsx hs hw (s0+xs.uget (Е 8*t+Е 0) (xi xs t n 0 א ht hx hsx)*v) (s1+xs.uget (Е 8*t+Е 1) (xi xs t n 1 א ht hx hsx)*v) (s2+xs.uget (Е 8*t+Е 2) (xi xs t n 2 א ht hx hsx)*v) (s3+xs.uget (Е 8*t+Е 3) (xi xs t n 3 א ht hx hsx)*v) (s4+xs.uget (Е 8*t+Е 4) (xi xs t n 4 א ht hx hsx)*v) (s5+xs.uget (Е 8*t+Е 5) (xi xs t n 5 א ht hx hsx)*v) (s6+xs.uget (Е 8*t+Е 6) (xi xs t n 6 א ht hx hsx)*v) (s7+xs.uget (Е 8*t+Е 7) (xi xs t n 7 א ht hx hsx)*v) r else match r with | ⟨u,_,_,_,_,_,_,_,_⟩=>⟨u,s0,s1,s2,s3,s4,s5,s6,s7⟩ termination_by Ѡn-Ѡt decreasing_by rw[usz t n h];omega def lnSum (x:ё) (base n i:є) (s t:ѕ):F1 := if h:i< n then let v:=dv (rdW x (base+i)) lnSum x base n (i+1) (s+v) (t+v.abs) else ⟨s,t⟩ termination_by Ѡn-Ѡi decreasing_by rw[usz i n h];have:=А.mp h;omega def mkL (X:ѐ) (n:є):F8 := ⟨0,(lnSum X (0*n) n 0 0 0).t,(lnSum X (1*n) n 0 0 0).t,(lnSum X (2*n) n 0 0 0).t,(lnSum X (3*n) n 0 0 0).t, (lnSum X (4*n) n 0 0 0).t,(lnSum X (5*n) n 0 0 0).t,(lnSum X (6*n) n 0 0 0).t,(lnSum X (7*n) n 0 0 0).t⟩ def out8 (inW outW:ћ) (relu:ј) (X W b:ѐ) (nf:ѕ) (hv:ѓ) (l r:F8) (j:ћ) (bw:ѓ) (n:є) (k:ћ) (out:ѐ):ѐ := if k<8 then out8 inW outW relu X W b nf hv l r j bw n (k+1) (wrW out (k*outW+j).toUSize (fin8 inW relu X W b k j (f8 r k) nf (f8 l k) hv bw n)) else out termination_by 8-k def cols8 (inW outW:ћ) (relu:ј) (X W b:ѐ) (xs:ђ) (l:F8) (nf:ѕ) (hv:ѓ) (n:є) (j:ћ) (r0:F8) (out:ѐ):ѐ := if hj:j< outW then let ju:=j.toUSize let wo:=ju*n if hg:8*Ѡn<=xs.size∧xs.size<=4294967295∧W.size<=4294967295∧Ѡwo+Ѡn<=W.size then let r:=go8 xs W wo n 0 hg.1 (qA1 hg.2.1) (qA1 hg.2.2.1) hg.2.2.2 0 0 0 0 0 0 0 0 r0 cols8 inW outW relu X W b xs l nf hv n (j+1) r (out8 inW outW relu X W b nf hv l r j (rdW b ju) n 0 out) else out else out termination_by outW-j def wdQ (X:ѐ) (n e q:є) (a:ђ):ђ := if h:q< e then wdQ X n e (q+1) (a.push (dv (rdW X (q%Е 8*n+q/Е 8)))) else a termination_by Ѡe-Ѡq decreasing_by rw[usz q e h];have:=А.mp h;omega def linF (rows inW outW:ћ) (relu:ј) (X W b:ѐ):ѐ := let hw:=scanH W if rows*inW=X.size∧outW*inW=W.size∧outW=b.size∧0< inW∧0< outW∧inW<2^20 ∧ X.size<=4294967295∧W.size<=4294967295∧b.size<=4294967295∧rows*outW<=4294967295 ∧ scanH X≠Д∧hw≠Д∧scanH b≠Д then let n:=inW.toUSize let nf:ѕ:=(inW.toUInt64+8).toFloat*й 0x3CA0000000000000 let out:ѐ:=Й (rows*outW) 0 if 8<=rows∧rows<11 then rows1 rows inW outW relu X W b (В 0) nf hw n 8 (Й inW 0) (pushZ n 0 (В inW)) (cols8 inW outW relu X W b (wdQ X n (Е 8*n) 0 (В (8*inW))) (mkL X n) nf hw n 0 ⟨0,0,0,0,0,0,0,0,0⟩ out) else let wd:=if 8< rows then W.foldl (λa w=>a.push (dv w)) (В W.size) else В 0 rows1 rows inW outW relu X W b wd nf hw n 0 (Й inW 0) (pushZ n 0 (В inW)) out else linearWith dotFn rows inW outW relu X W b section local notation:max x:max "ᶠ"=>Float.isFinite x=true local notation "⟡"=>FloatFormat.binary64 local notation "⟠"=>Float.Model.Format.binary64 noncomputable def R (f:ѕ):ℝ:=к (toModel (ofFloat f)) noncomputable def wv (w:ѓ):ℝ:=к (ofW w) noncomputable def exactS (n:ћ) (xw ww:ћ->ѓ) (bw:ѓ):ℝ := (л n).sum (λt=>wv (xw t)*wv (ww t))+wv bw def nfOf (n:ћ):ѕ:=(n.toUInt64+8).toFloat*й 0x3CA0000000000000 lemma qAl:⟡.isIEEE=true:=by decide lemma qBr (a:Model ⟡) : н a=(Model.toModel a).isFinite:=by rw[<-Model.toDyadic?_isSome_eq_isFinite,Model.toDyadic?_ieee_eq_model _ qAl];cases Model.toModel a<;>rfl abbrev A_M (x:ѕ):Model ⟡:=Model.ofFloatModel x.toModel lemma A_R (x:ѕ):R x=к (A_M x):=rfl lemma qAm (x:ѕ):н (A_M x)=x.isFinite:=by rw[qBr,ل];rfl lemma qAw {x:ѕ} (h:xᶠ):н (A_M x)=true :=(qAm x).trans h lemma qA6 (e:ℤ):Formats.Flocq.bpow ؼ e=(2:ℝ)^e:=by simp[Formats.Flocq.bpow,FloatLib.Numerics.binaryRadix,FloatLib.Numerics.Radix.toReal] lemma qB7:(2:ℝ)^(1000:ℤ)<=к (Model.posMaxFinite ⟡):=by rw[Model.toReal_posMaxFinite,show Model.pow2 ⟡.fracWidth + ⟡.maxFiniteFracField=2^53-1 by decide +kernel, show ⟡.maxNormalExponent-Int.ofNat ⟡.fracWidth=971 by decide +kernel,Model.bpow,qA6,show (1000:ℤ)=29+971 from rfl,а С] exact Г ב ו open Float.Model.UnpackedFloat in lemma qD2 {b:BitVec 64} {s m e h} (a:unpack ⟠ b=.finite s m e h) : 2^52<=m∨e=-1074:=by unfold Float.Model.UnpackedFloat.unpack at a;simp only at a;rw[show (⟠.exponentBias:ℤ)=1023 by decide] at a;split_ifs at a with h1 h2 h3;. injection a with _ _ he;rw[show (unpackExponent (spec:=⟠) b).toNat=0 by simp[h3]] at he;omega . injection a with _ hm;rw[<-hm,BitVec.toNat_append];exact .inl (le_trans (by simp[Nat.shiftLeft_eq]) Nat.left_le_or) open Float.Model.UnpackedFloat in lemma qIi {s₁ s₂ m₁ m₂ e₁ e₂ h₁ h₂} {f g:BitVec 64} (e:unpack ⟠ f=.finite s₁ m₁ e₁ h₁) (b:unpack ⟠ g=.finite s₂ m₂ e₂ h₂) : e₁+e₂<=⟠.targetExponent (Float.Model.totalExponent (m₁*m₂) (e₁+e₂)):=by have a:2^52<=m₁*m₂->52<=(m₁*m₂).log2:=(Nat.le_log2 (Nat.mul_ne_zero h₁.ne' h₂.ne')).2 simp only[Float.Model.Format.targetExponent,Float.Model.totalExponent,Float.Model.Format.mantissaBits,Float.Model.Format.minExponent] norm_num rcases qD2 e with c | c . have:=a (c.trans (ر _ h₂));omega rcases qD2 b with d | d . have:=a (d.trans (Nat.le_mul_of_pos_left _ h₁));omega omega lemma qBq (x y:ѕ) (a:xᶠ) (b:yᶠ) : A_M (x*y)=Model.mul (A_M x) (A_M y):=by change Model.ofFloatModel (Float.Model.mul x.toModel y.toModel)=_;rw[Float.Model.mul,Model.ofFloatModel_pack,Model.Proof.mul_eq_spec,Model.Spec.mul];simp only[Model.toDyadic?_ieee_eq_model _ qAl,Model.toModel_ofFloatModel];change x.toModel.unpack.isFinite=true at a;change y.toModel.unpack.isFinite=true at b;have c:∀ s t:Float.Model.UnpackedFloat.Sign,Model.ofModel ⟡ (.zero (s*t)) =Model.zero _ (ذ s ^^ ذ t):=λs t=>by cases s<;>cases t<;>simp[Model.modelSignBit] <;>first | exact (Model.negZero_eq_ofModel_zero _).symm | exact (Model.posZero_eq_ofModel_zero _).symm cases hxm:x.toModel.unpack<;>cases hym:y.toModel.unpack <;>simp only[hxm,hym,Float.Model.UnpackedFloat.isFinite,false_eq_true] at a b case finite.finite s₁ m₁ e₁ h₁ s₂ m₂ e₂ h₂ => have d:(m₁ == 0 || m₂ == 0)=false:=by simp[h₁.ne',h₂.ne'] have e:в (ذ s₁ ^^ ذ s₂)=s₁*s₂:=by cases s₁<;>cases s₂<;>rfl simp only[Model.unpackedToDyadic?,Float.Model.UnpackedFloat.mul];rw[Model.roundWithAccuracy_exact_eq_round_of_le_targetExponent _ _ _ _ (qIi hxm hym),d];simp only[false_eq_true,ite_false,Model.roundDyadic,qAl,ite_true,Model.ieeeRoundDyadic,e];rfl all_goals simp[Model.unpackedToDyadic?,Float.Model.UnpackedFloat.mul,c] lemma A_E (z:ѕ) {a:Model ⟡} {c:ℝ} (h:A_M z=a) (b:н a=true) (d:к a=ظ ⟡ c) : zᶠ∧R z=ظ ⟡ c:=by subst h;exact ⟨(qAm z).symm.trans b,d⟩ lemma qEm (x y:ѕ) (hx:xᶠ) (hy:yᶠ) (hb : |R x| * |R y| ≤ 2^(1000:ℤ)) : (x*y)ᶠ ∧ R (x*y)=ظ ⟡ (R x*R y) := A_E _ (qBq x y hx hy) (Model.isFinite_mul_of_abs_mul_le_posMaxFinite _ _ qAl (qAw hx) (qAw hy) (hb.trans qB7)) (Model.toReal_mul_eq_roundAt_of_abs_mul_le_posMaxFinite _ _ qAl (qAw hx) (qAw hy) (hb.trans qB7)) noncomputable def A_z:ℝ:=2^(-1021:ℤ) noncomputable def A_B:ℝ:=2^(300:ℤ) lemma qAr:0< A_z:=by unfold A_z;positivity lemma A_p (a b c d:ℤ) (h:a+b<=c+d):(2:ℝ)^a*2^b<=2^c*2^d:=by rw[<-а С,<-а С];exact о one_le_two h lemma qC2:A_z<=1/2^200*(1/2^200):=by simpa [A_z] using A_p (-1021) 0 (-200) (-200) ב lemma qJo:0< A_B:=by unfold A_B;positivity lemma qBk:(2:ℝ)^256*2^42<=A_B:=by simpa [A_B] using A_p 256 42 300 0 ב lemma qAi:(2:ℝ)^128<=A_B :=(by norm_num:(2:ℝ)^128<=2^256*2^42).trans qBk lemma qFv {k:ℕ} (h:k<=2^128):(k:ℝ)<=A_B :=(show (k:ℝ)<=2^128 by exact_mod_cast h).trans qAi lemma qE8:A_B*2<=2^(1000:ℤ):=by simpa [A_B] using A_p 300 1 1000 0 ב lemma qC3:A_B*A_B<=2^(1000:ℤ):=by simpa [A_B] using A_p 300 300 1000 0 ב lemma qIr (x:ℝ) : |ظ ⟡ x-x| ≤ 1/2^53*(|x| + A_z):=by rw[show (1/2^53*(|x| + A_z):ℝ)=2^(-53:ℤ) * |x| + 2^(-1074:ℤ) by rw[show (1/2^53:ℝ)=2^(-53:ℤ) by norm_num,show (-1074:ℤ)=-53+-1021 from rfl,а С (-53),mul_add] rfl] by_cases a:x=0 . rw[a,Model.roundAt_zero,sub_self,abs_zero];positivity refine (Model.abs_roundAt_sub_le _ x).trans ?_ change Formats.Flocq.ulp _ (Model.fexpOf ⟡) x/2<=_ rw[Formats.Flocq.ulp.of_ne_zero _ _ x a] simp only[Formats.Flocq.cexp,Model.fexpOf,Formats.Flocq.fltExp] have b:=Formats.Flocq.bpow_magnitude_sub_one_le ؼ x a rw[qA6] at b rw[qA6,show ((⟡.fracWidth:ℤ)+1)=53 from rfl,show ⟡.minSubnormalExponent=-1074 from rfl] generalize Formats.Flocq.magnitude ؼ x=M at b ⊢ have d:(0:ℝ)<=2^(-53:ℤ) * |x|:=by positivity have c:(0:ℝ)<2^(-1074:ℤ):=by positivity rcases le_total (-1074) (M-53) with h | h . rw[max_eq_left h,show (2:ℝ)^(M-53)/2=2^(-53:ℤ)*2^(M-1) by rw[<-а С,show (-53:ℤ)+(M-1)=M-53-1 by ring,ن С _ 1,zpow_one]] linarith [Ч b (show (0:ℝ)<=2^(-53:ℤ) by positivity)] . rw[max_eq_right h];exact (half_le_self c.le).trans (le_add_of_nonneg_left d) lemma qFn {z:ѕ} {c:ℝ} (h:zᶠ∧R z=ظ ⟡ c) : zᶠ∧|R z-c| ≤ 1/2^53*(|c| + A_z) :=⟨h.1,h.2 ▸ qIr c⟩ lemma qEj {a b:ℝ} (d : |a| ≤ A_B) (c : |b| ≤ A_B) : |a| + |b| ≤ к (Model.posMaxFinite ⟡):=by have:=qE8 have:=qB7 linarith section variable (x y:ѕ) (hx:xᶠ) (hy:yᶠ) (h1 : |R x| ≤ A_B) (h2 : |R y| ≤ A_B) include x y hx hy h1 h2 lemma qBl : (x+y)ᶠ∧|R (x+y)-(R x+R y)| ≤ 1/2^53*(|R x+R y| + A_z) := qFn (A_E _ (toModel_ofFloat_add_of_isFinite x y hx hy) (Model.isFinite_add_of_abs_add_le_posMaxFinite _ _ qAl (qAw hx) (qAw hy) (qEj h1 h2)) (Model.toReal_add_eq_roundAt_of_abs_add_le_posMaxFinite _ _ qAl (qAw hx) (qAw hy) (qEj h1 h2))) lemma qHc : (x-y)ᶠ∧|R (x-y)-(R x-R y)| ≤ 1/2^53*(|R x-R y| + A_z) := qFn (A_E _ (toModel_ofFloat_sub_of_isFinite x y hx hy) (Model.isFinite_sub_of_abs_add_le_posMaxFinite _ _ qAl (qAw hx) (qAw hy) (qEj h1 h2)) (Model.toReal_sub_eq_roundAt_of_abs_add_le_posMaxFinite _ _ qAl (qAw hx) (qAw hy) (qEj h1 h2))) lemma qDg : (x*y)ᶠ∧|R (x*y)-R x*R y| ≤ 1/2^53*(|R x*R y| + A_z) :=qFn (qEm x y hx hy ((ч h1 h2 (ц _) qJo.le).trans qC3)) end lemma qAu (m k:ℤ) (hm:m.natAbs<2^53) (hk:-1074<=k) : ظ ⟡ ((m:ℝ)*2^k)=(m:ℝ)*2^k:=by apply Formats.Flocq.round_preserves_generic;exact (Formats.Flocq.generic_format_FLT_iff (⟡.minSubnormalExponent) (⟡.fracWidth+1) ה _).2 ⟨by decide,⟨m,k⟩,by simp[Formats.Flocq.toReal,qA6],hm,hk⟩ lemma qBe (x:ѕ):R x=Model.unpackedToReal x.toModel.unpack:=by rw[A_R,Բ qAl,ل] lemma qFj {f:ѕ} {s m e h} (c:f.toModel.unpack=.finite s m e h) : (Model.ofModel ⟡ (.finite .positive m e h)).toModel=.finite .positive m e h:=by have a:Model.toModel (A_M f)=.finite s m e h:=(ل _).trans c;cases s;. have b:=Model.toModel_neg_of_isFinite qAl (A_M f) (by rw[a];rfl);rw[a] at b;change _=Float.Model.UnpackedFloat.finite .positive m e h at b;rw[<-b];exact Model.toModel_canonicalizeModel _ . rw[<-a];exact Model.toModel_canonicalizeModel _ lemma qEq (f:ѕ) (hf:fᶠ):f.absᶠ∧R f.abs = |R f|:=by have a:A_M f.abs=Model.ofModel ⟡ f.toModel.unpack.abs :=Model.ofFloatModel_pack _;change f.toModel.unpack.isFinite=true at hf;cases hu:f.toModel.unpack with | notANumber | infinity _=>simp[hu,Float.Model.UnpackedFloat.isFinite] at hf | zero s => rw[hu] at a;refine ⟨by rw[<-qAm,a];exact Model.isFinite_ofModel_zero _ qAl _,?_⟩;rw[A_R f.abs,a,qBe f,hu];simp[Float.Model.UnpackedFloat.abs,Model.toReal_ofModel_zero _ qAl] | finite s m e h => have b:=qFj hu;rw[hu] at a;have c:A_M f.abs=Model.ofModel ⟡ (.finite .positive m e h):=a;refine ⟨by rw[<-qAm,c,qBr,b];rfl,?_⟩;rw[A_R f.abs,c,qBe f,hu,Բ qAl,b];simp only[Model.unpackedToReal_finite,qA6];cases s<;>simp[Model.modelSignBit,abs_of_nonneg (by positivity:(0:ℝ)<=(m:ℝ)*2^e)] lemma qCe (f:ѕ) (k:ℕ) (g:k<2^53) (e:f.toModel=Float.Model.pack (Float.Model.UnpackedFloat.ofNat ⟠ k)) : fᶠ∧R f=k:=by obtain ⟨d,b,c⟩:∃ d,A_M f=Ъ ⟡ d∧d.toReal=k:=by refine ⟨.ofScaledInt k 0,?_,?_⟩;. change Model.ofFloatModel f.toModel=_;rw[e,Model.ofFloatModel_pack];exact Model.ofModel_ofInt_eq_roundDyadic _ qAl (k:ℤ) . rw[<-Ь,FloatLib.Numerics.Dyadic.ofScaledInt_toRat];simp have a:н (A_M f)=true:=by rw[b];apply Model.isFinite_roundDyadic_of_isIEEE_of_abs_toReal_le_posMaxFinite _ qAl;rw[c,з (Э _)];exact ((qFv (א:k<=2^128)).trans ((le_mul_of_one_le_right qJo.le one_le_two).trans qE8)).trans qB7 refine ⟨(qAm f).symm.trans a,?_⟩;rw[A_R,b,Model.toReal_roundDyadic_eq_roundAt _ qAl _ (b ▸ a),c];simpa using qAu k 0 (by simpa using g) ב lemma qGm (m:і) (h:Ѡm<2^53):m.toFloatᶠ∧R m.toFloat=Ѡm :=qCe _ _ h rfl lemma qJp:(0:ѕ)ᶠ∧R 0=0:=by simpa using qCe 0 0 ב rfl def A_bits (i:ℕ):і := let e:=(i%256).toUInt64 let s:і:=if i ≥ 256 then 0x8000000000000000 else 0 if e == 0 then s else s ||| ((e+873) <<< (52:і)) lemma qCw (i:ℕ):pwEntry i=й (A_bits i):=by unfold pwEntry A_bits;dsimp only;split<;>rfl def A_chk (i:ℕ):ј := match ض ⟠ (A_bits i).toBitVec with | .finite s m e _=>(ذ s == decide (256<=i)) && m == 2^52 && e == ((i%256:ℕ):ℤ)-202 && (i%256 != 0) | .zero _=>i%256 == 0 | _=>false lemma qFc:∀ i,i<512->A_chk i=true:=by decide +kernel lemma qDi (i:ℕ) (b:i<512) : (pwEntry i)ᶠ∧R (pwEntry i) =if i%256=0 then 0 else (if 256<=i then -1 else 1)*2^(((i%256:ℕ):ℤ)-150):=by have a:=qFc i b unfold A_chk at a rw[qCw,qBe] change (й (A_bits i)).toModel.unpack.isFinite=true∧_ rw[show (й (A_bits i)).toModel.unpack =ض ⟠ (A_bits i).toBitVec from Model.unpack_pack_unpack _ _] generalize ض ⟠ (A_bits i).toBitVec=u at a cases u with | notANumber | infinity _=>simp at a | zero s => simp only[beq_iff_eq] at a exact ⟨rfl,by simp[a]⟩ | finite s m e h => simp only[and_eq_true,beq_iff_eq,bne_iff_ne] at a obtain ⟨⟨⟨g,d⟩,f⟩,c⟩:=a refine ⟨rfl,?_⟩ rw[Model.unpackedToReal_finite,qA6,f,g,d,mul_assoc, show ((2^52:ℕ):ℝ)*2^(((i%256:ℕ):ℤ)-202) =2^(((i%256:ℕ):ℤ)-150) by rw[Nat.cast_pow,Nat.cast_ofNat,<-т,<-а С] congr 1 push_cast ring] simp only[decide_eq_true_eq,c,↓reduceIte] lemma qCv (i:ℕ) (a:i<512):∃ σ:ℤ,σ.natAbs<=1 ∧ R (pwEntry i)=σ*2^(((i%256:ℕ):ℤ)-150):=by rw[(qDi i a).2];split_ifs;exacts [⟨0,by norm_num,by simp⟩,⟨-1,by norm_num,by simp⟩,⟨1,by norm_num,by simp⟩] lemma qCp (f:ѕ) (i:ℕ) (g:i<512) (q:fᶠ) (c k:ℤ) (r:c.natAbs<2^53) (s:-900<=k) (m : |R f| ≤ A_B) (l:R f=c*2^k) : (f*pwEntry i)ᶠ∧R (f*pwEntry i)=R f*R (pwEntry i):=by obtain ⟨b,a,d⟩:=qCv i g;have o:((i%256:ℕ):ℤ)-150<=128∧-150<=((i%256:ℕ):ℤ)-150 :=א;generalize ((i%256:ℕ):ℤ)-150=E at o d;have p : |(b:ℝ)| ≤ 1:=by rw[<-Int.cast_abs,<-Nat.cast_natAbs];exact_mod_cast a have e:=ػ (two_pos (α:=ℝ)) E;have h : |R (pwEntry i)| ≤ A_B:=by rw[d,abs_mul,abs_of_pos e,<-one_mul A_B];exact ч p (о one_le_two א) e.le zero_le_one obtain ⟨j,n⟩:=qEm f _ q (qDi i g).1 ((ч m h (ц _) qJo.le).trans qC3);refine ⟨j,?_⟩ have:=qAu (c*b) (k+E) (by rw[Int.natAbs_mul];have:=Nat.mul_le_mul_left c.natAbs a omega) א rw[а С] at this;push_cast at this;rw[n,d,l,show (c:ℝ)*2^k*(b*2^E)=c*b*(2^k*2^E) by ring];exact this lemma qC5 (i:є) (h:Ѡi< pw.size):pw.uget i h=pwEntry Ѡi:=by simp[FloatArray.uget,qDz] section variable (w:ѓ) include w lemma qAs:(w &&& Б).toNat=Ѡw%2^31:=by rw[е];exact М Ѡw 31 lemma qBp:(w &&& 0x7FFFFF).toNat=Ѡw%2^23:=by rw[е];exact М Ѡw 23 lemma qEr:(w >>> 23).toNat=Ѡw/2^23:=by rw[UInt32.toNat_shiftRight,ы];rfl lemma qBc:(expOf w).toNat=Ѡw/2^23%2^8:=by unfold expOf;rw[е,qEr];exact М _ 8 lemma qE6:signOf w=decide (2^31<=Ѡw):=by have b:=w.toNat_lt;unfold signOf;have h:((w &&& я).toNat)=(Ѡw/2^31%2)*2^31:=by rw[е];show Ѡw &&& 2^31=_;rw[Nat.and_two_pow,Nat.testBit_eq_decide_div_mod_eq];have a:Ѡw/2^31%2=0∨Ѡw/2^31%2=1 :=א;rcases a with a | a<;>rw[a]<;>rfl rw[Bool.eq_iff_iff];simp only[bne_iff_ne,ne_eq,decide_eq_true_eq];rw[<-П,h];simp only[UInt32.toNat_zero];omega lemma qHj : ((w &&& 0x7FFFFF) ||| 0x800000).toNat=2^23+Ѡw%2^23:=by rw[UInt32.toNat_or,qBp,Nat.or_comm];simpa using (Nat.two_pow_add_eq_or_of_lt (Nat.mod_lt Ѡw (by positivity:2^23 > 0)) 1).symm lemma qA0 (h:good w=true) : Ѡw%2^31=0∨(2^23<=Ѡw%2^31∧Ѡw%2^31<Р):=by have a:=qAs w;have b:=(w &&& Б).toNat_lt;unfold good at h;simp only[or_eq_true,beq_iff_eq,decide_eq_true_eq] at h;rcases h with h | h;. left;rw[<-a,h];rfl . right;rw[Ж,UInt32.toNat_sub] at h;rw[<-a];generalize (w &&& Б).toNat=v at *;simp at h;omega lemma qAd (h:good w=true):expOf w≠255:=by intro he;have a:=qBc w;rw[he] at a;change 255=_ at a;have:=qA0 w h;have:=w.toNat_lt;omega end noncomputable def A_mag (v:ℕ):ℝ := if v=0 then 0 else ((2^23+v%2^23:ℕ):ℝ)*2^(((v/2^23:ℕ):ℤ)-150) lemma qAz (v:ℕ):0<=A_mag v:=by unfold A_mag;split_ifs<;>positivity lemma qA9 (w:ѓ) (hg:good w=true) : wv w=(if 2^31<=Ѡw then -1 else 1)*A_mag (Ѡw%2^31):=by have g:=w.toNat_lt;have:=qA0 w hg;have a:=qBc w;have d:(fracOf w).toNat=Ѡw%2^23:=qBp w;have f:wv w=(decodeD w).toReal:=by unfold wv ofW;rw[Model.toReal_eq,Ц w (qAd w hg)] rw[f];unfold decodeD A_mag;by_cases c:Ѡw%2^31=0;. have b:expOf w=0:=П.mp (by rw[a];show _=0;omega);have e:fracOf w=0:=П.mp (by rw[d];show _=0;omega);simp only[b,e,c,↓reduceIte];simp[FloatLib.Numerics.Dyadic.toReal,signedSignificand] . have b:expOf w≠0:=λh=>by rw[h] at a;change 0=_ at a;omega simp only[b,c,↓reduceIte];rw[show Ѡw%2^31%2^23=(fracOf w).toNat by rw[d];omega,show Ѡw%2^31/2^23=(expOf w).toNat by rw[a];omega];simp only[FloatLib.Numerics.Dyadic.toReal,signedSignificand,qE6];simp;split_ifs<;>ring lemma qFw (v:ℕ) (h:v<Р):A_mag v<=2^128:=by unfold A_mag;split_ifs with h0;. positivity . exact (ч (show ((2^23+v%2^23:ℕ):ℝ)<=((2^24:ℕ):ℝ) from Nat.cast_le.2 א) (о one_le_two (by omega:_<=(104:ℤ))) ו ו).trans_eq ב lemma qB1 (a b:ℕ) (hab:a<=b) (ha:a=0∨2^23<=a):A_mag a<=A_mag b:=by rcases ha with rfl | ha;. simpa [A_mag] using qAz b unfold A_mag;simp only[show ¬ a=0 by omega,show ¬ b=0 by omega,↓reduceIte];have c:a/2^23<=b/2^23:=Nat.div_le_div_right hab;rcases c.eq_or_lt with d | g;. rw[d];exact Г (Nat.cast_le.2 א) ו . have e:(2:ℝ)^(((a/2^23:ℕ):ℤ)-150)*2<=2^(((b/2^23:ℕ):ℤ)-150):=by rw[<-zpow_add_one₀ С];exact о one_le_two א have f:((2^23:ℕ):ℝ)<=((2^23+b%2^23:ℕ):ℝ):=Nat.cast_le.2 א calc _<=((2^23*2:ℕ):ℝ)*2^(((a/2^23:ℕ):ℤ)-150) := Г (Nat.cast_le.2 א) ו _=((2^23:ℕ):ℝ)*(2^(((a/2^23:ℕ):ℤ)-150)*2):=by push_cast;ring _<=_:=ч f e ו ו abbrev A_mf (w:ѓ):ѕ:=((w &&& 0x7FFFFF) ||| 0x800000).toFloat lemma qD4 (w:ѓ):pw.uget (w >>> 23).toUSize (qCb w) =pwEntry (Ѡw/2^23)∧Ѡw/2^23<512∧(A_mf w)ᶠ ∧ (A_mf w*pwEntry (Ѡw/2^23))ᶠ ∧ R (A_mf w*pwEntry (Ѡw/2^23))=R (A_mf w)*R (pwEntry (Ѡw/2^23)) ∧ R (A_mf w)=((2^23+Ѡw%2^23:ℕ):ℝ):=by have g:=w.toNat_lt;have c:=qHj w;obtain ⟨d,a⟩:=qCe (A_mf w) _ (by rw[c];omega) rfl;rw[c] at a;have b:Ѡw/2^23<512 :=א obtain ⟨f,e⟩:=qCp _ _ b d ((2^23+Ѡw%2^23:ℕ):ℤ) 0 א ב (by rw[a,з (Э _)] exact qFv א) (by rw[a,zpow_zero,mul_one,Int.cast_natCast]) refine ⟨?_,b,d,f,e,a⟩;rw[qC5,Ы,qEr] lemma qAp (w:ѓ) (hg:good w=true):(dv w)ᶠ∧R (dv w)=wv w:=by obtain ⟨g,a,-,e,d,c⟩:=qD4 w;have j:=w.toNat_lt;unfold dv;rw[g];refine ⟨e,?_⟩;rw[d,c,(qDi _ a).2,qA9 w hg];have b:=qA0 w hg;generalize Ѡw=v at *;rcases b with h | ⟨l,k⟩;. rw[h];simp only[show v/2^23%256=0 by omega,↓reduceIte,mul_zero,show A_mag 0=0 by simp[A_mag]] . have f:(256<=v/2^23) ↔ 2^31<=v :=א;unfold A_mag;rw[(א:v%2^31/2^23=v/2^23%256),(א:v%2^31%2^23=v%2^23),г (א:¬ v/2^23%256=0),г (א:¬ v%2^31=0)];by_cases i:2^31<=v<;>simp only[f,i,↓reduceIte]<;>ring def A_fsum (l:List ѕ):ѕ:=l.foldl (.+·) 0 noncomputable def A_sa (l:List ѕ):ℝ:=(l.map λq=>|R q|).sum noncomputable def A_sr (l:List ѕ):ℝ:=(l.map R).sum lemma qDk {a:Type} (l:List a) (f:a->ℝ):0<=(l.map λq=>|f q|).sum :=List.sum_nonneg (by simp only[List.mem_map];rintro _ ⟨q,_,rfl⟩;exact ц _) lemma qB8 (l:List ѕ):0<=A_sa l:=qDk l R lemma qHv (l:List ѕ) (q:ѕ):A_sa (l ++ [q])=A_sa l + |R q| ∧ A_sr (l ++ [q])=A_sr l+R q∧A_fsum (l ++ [q])=A_fsum l+q:=by simp[A_sa,A_sr,A_fsum] lemma qDm (l:List ѕ) : |A_sr l| ≤ A_sa l:=by induction l with | nil=>simp[A_sa,A_sr] | cons q l ih => simp only[A_sa,A_sr,List.map_cons,List.sum_cons] at ih ⊢;exact (Գ _ _).trans ד lemma qJg:A_z<=1:=qC2.trans ב section variable (l:List ѕ) (hf:∀ q∈l,qᶠ) (hlen:l.length<=2^21) (hb:A_sa l<=A_B/4) include l hf hlen hb lemma qC1 : (A_fsum l)ᶠ ∧ |R (A_fsum l)-A_sr l| ≤ (1+1/2^22)*l.length*(1/2^53*(A_sa l+A_z)):=by induction l using List.reverseRecOn with | nil=>exact ⟨qJp.1,by simp[A_fsum,qJp.2,A_sr]⟩ | append_singleton l q ih => obtain ⟨e,f,n⟩:=qHv l q rw[e] at hb rw[List.length_append,List.length_singleton] at hlen have t:=qB8 l have j:=ц (R q) obtain ⟨h,v⟩:=ih (λx i=>hf x (by simp[i])) א ד have u:(l.length:ℝ)+1<=2^21:=by exact_mod_cast hlen have d:(0:ℝ)<=l.length:=Э _ have y:=qDm l have o:=qAr have m:=qJg have p:=qAi have s:=Ф (R (A_fsum l)) (A_sr l) have w:=Г (show (l.length:ℝ)<=2^21 by linarith) (ד:0<=A_sa l+A_z) obtain ⟨b,z⟩:=qBl _ q h (hf q ג) ד ד rw[n,e,f,List.length_append,List.length_singleton] refine ⟨b,?_⟩ push_cast generalize R (A_fsum l)=r at * generalize R q=a at * generalize A_sa l=P at * generalize A_sr l=S at * generalize (l.length:ℝ)=k at * obtain ⟨W,g⟩:∃ W:ℝ,W=1/2^53*(P + |a| + A_z):=⟨_,rfl⟩ have c:0<=W :=ד rw[<-g] refine (abs_sub_le _ (r+a) (S+a)).trans ?_ rw[add_sub_add_right_eq_sub] linarith [Գ r a,Ч (show 1/2^53*(P+A_z)<=W by linarith) d,Г (ד:k<=2^21) c] lemma qDt : (A_fsum l)ᶠ ∧ |R (A_fsum l)-A_sr l| ≤ 2*l.length*(1/2^53*(A_sa l+A_z)):=by obtain ⟨a,b⟩:=qC1 l hf hlen hb have c:=ю (Nat.cast_nonneg (α:=ℝ) l.length) (add_nonneg (qB8 l) qAr.le) exact ⟨a,b.trans ד⟩ end lemma qCa (l:List ѕ) (c:∀ q∈l,qᶠ) (d:l.length<=2^21) (f:A_sa l<=A_B/4) : (A_fsum (l.map Float.abs))ᶠ ∧ |R (A_fsum (l.map Float.abs))-A_sa l| ≤ 2*l.length*(1/2^53*(A_sa l+A_z)):=by have a:A_sa (l.map Float.abs)=A_sa l∧A_sr (l.map Float.abs)=A_sa l:=by constructor<;>simp only[A_sa,A_sr,List.map_map]<;>congr 1 <;>exact List.map_congr_left λq b=>by simp[(qEq q (c q b)).2] have:=qDt (l.map Float.abs) (λq b=>by obtain ⟨x,e,rfl⟩:=List.mem_map.mp b exact (qEq x (c x e)).1) (by simpa) (a.1.symm ▸ f) rwa [a.1,a.2,List.length_map] at this lemma qI9 (n:ℕ) (g:n<2^20) : (nfOf n)ᶠ∧R (nfOf n)=((n:ℝ)+8)/2^53:=by have b:(n.toUInt64+8).toNat=n+8:=by have c:(8:і).toNat=8:=rfl;simp[UInt64.toNat_add,c];omega obtain ⟨f,a⟩:=qGm (n.toUInt64+8) (by rw[b];omega);rw[b] at a obtain ⟨e,d⟩:=qCp _ 97 ב f ((n+8:ℕ):ℤ) 0 א ב (by rw[a,з (Э _)] exact qFv א) (by rw[a];simp) push_cast at a;rw[show nfOf n=(n.toUInt64+8).toFloat*pwEntry 97 by rw[nfOf,qCw];rfl];refine ⟨e,?_⟩;rw[d,a,(qDi 97 ב).2];norm_num;ring lemma qGo {n:ℕ} (l:n<2^20) {s t:ѕ} (k:sᶠ) (m:tᶠ) {X Y:ℝ} (e:0<=Y) (v:Y<=A_B/8) (y : |R s-X| ≤ ((n:ℝ)+4)*(1/2^53*Y)+1/2^250) (u : |R t-Y| ≤ Y/2^30+1/2^250) (I : |R s| ≤ 2*Y) (c:(2:ℝ)^(-127:ℤ) ≤ |R (s+nfOf n*t)|) : R (s-nfOf n*t)< X∧X< R (s+nfOf n*t):=by have w:(8:ℝ)<=A_B:=(by norm_num:(8:ℝ)<=2^128).trans qAi;have A:(n:ℝ)+1<=2^20:=by exact_mod_cast l have o:(0:ℝ)<=n:=Э n;obtain ⟨h,b⟩:=qI9 n l;have E:=qAr;have B:=qC2;have a:0<=((n:ℝ)+8)/2^53:=by positivity have C:=Ф (R t) Y;rw[з e] at C;have i : |R t| ≤ 2*Y+1/2^250 :=ד;have x : |R (nfOf n)*R t| ≤ 1/2^30*(2*Y+1/2^250):=by rw[abs_mul,b,з a];exact ч ד i (ц _) ב obtain ⟨d,f⟩:=qDg _ _ h m (by rw[b,з a];linarith) ד;have D:=Ф (R (nfOf n*t)) (R (nfOf n)*R t);obtain ⟨z,j⟩:=qBl s _ k d ד ד;obtain ⟨r,g⟩:=qHc s _ k d ד ד;have H:=Ф (R (s+nfOf n*t)) (R s+R (nfOf n*t));have q:=Գ (R s) (R (nfOf n*t));have p:=abs_sub (R s) (R (nfOf n*t));rw[show (2:ℝ)^(-127:ℤ)=1/2^127 by norm_num] at c;have G:=Ч (ف.mp u).1 a;have M:=Г (show (n:ℝ)<=2^20 by linarith) e;rw[b] at f x D;obtain ⟨K,J⟩:=ف.mp y;obtain ⟨O,N⟩:=ف.mp j;obtain ⟨F,L⟩:=ف.mp g;obtain ⟨P,-⟩:=ف.mp f;constructor<;>linarith lemma qHs (g h:ℕ->ѕ) (l:List ℕ) (a b:ѕ) : l.foldl (λ(d:F1) t=>F1.mk (d.s+g t) (d.t+h t)) ⟨a,b⟩ =⟨l.foldl (λ(c:ѕ) t=>c+g t) a,l.foldl (λ(c:ѕ) t=>c+h t) b⟩:=by induction l generalizing a b with | nil=>rfl | cons x l ih=>simp only[List.foldl_cons,ih] lemma qDa (a:List ℕ) (f:ℕ->ℝ) (M:ℝ) (h:∀ t∈a,f t<=M) : (a.map f).sum<=a.length*M:=by simpa using List.sum_le_sum h section variable (w:ѓ) (hg:good w=true) include w hg lemma qBv : |wv w| = A_mag (Ѡw%2^31):=by rw[qA9 w hg,abs_mul,з (qAz _)];split_ifs<;>simp lemma qAh : |wv w| ≤ 2^128:=by rw[qBv w hg];apply qFw;rcases qA0 w hg with h | h<;>omega lemma qA3 (h:w &&& Б=0):wv w=0:=by rw[qA9 w hg,<-qAs w,h];simp[A_mag] end lemma qEy (a b:ѓ) (ha:good a=true) (hb:good b=true) : (dv a*dv b)ᶠ ∧ |R (dv a*dv b)-wv a*wv b| ≤ 1/2^53*(|wv a*wv b| + A_z) ∧ |wv a*wv b| ≤ 2^256:=by obtain ⟨j,g⟩:=qAp a ha obtain ⟨i,d⟩:=qAp b hb have c:=qAh a ha have e:=qAh b hb obtain ⟨h,f⟩:=qDg _ _ j i (g ▸ c.trans qAi) (d ▸ e.trans qAi) rw[g,d] at f exact ⟨h,f,by rw[abs_mul] exact (ч c e (ц _) ו).trans_eq ב⟩ lemma qBi {n:ℕ} {a b:ℕ->ѓ} {f:ѓ} (g:∀ t,t< n->good (a t)=true) (j:∀ t,t< n->good (b t)=true) {l:List ℕ} (k:l.Nodup) (d:∀ t∈l,t< n) (i:∀ t,t< n->t∉l->a t &&& Б=0∨b t &&& Б=0) : exactS n a b f=(l.map λt=>wv (a t)*wv (b t)).sum+wv f:=by unfold exactS;congr 1;rw[<-List.sum_toFinset _ k];refine (Finset.sum_subset (λt c=>by simpa using d t (by simpa using c)) λt c h=>?_).symm;have e:=ط.mp c;rcases i t e (by simpa using h) with h | h;. simp[qA3 (a t) (g t e) h] . simp[qA3 (b t) (j t e) h] lemma qEh {n:ℕ} {l:List ℕ} (b:l.Nodup) (a:∀ t∈l,t< n):l.length<=n:=by simpa using (b.subperm λt h=>List.mem_range.2 (a t h)).length_le abbrev A_G (ts:List ℕ) (g:ℕ->ѕ) (p:ℕ->ℝ):Prop := ∀ t∈ts,(g t)ᶠ∧|R (g t)-p t| ≤ 1/2^53*(|p t| + A_z)∧|p t| ≤ 2^256 lemma qFs {b c:ℕ->ѓ} {l:List ℕ} {n:ℕ} (a:∀ t∈l,t< n) (e:∀ t,t< n->good (b t)=true) (f:∀ t,t< n->good (c t)=true) : A_G l (λt=>dv (b t)*dv (c t)) λt=>wv (b t)*wv (c t) :=λt d=>qEy _ _ (e t (a t d)) (f t (a t d)) lemma qCl {a:List ℕ} {g:ℕ->ѕ} {p:ℕ->ℝ} (b:ѕ) (c:A_G a g p) : let A:=(a.map λt=>|p t|).sum let L:=a.map g ++ [b] |A_sr L-((a.map p).sum+R b)| ≤ 1/2^53*(A+a.length*A_z) ∧ A_sa L<=A + |R b| + 1/2^53*(A+a.length*A_z)∧A<=2*A_sa L+a.length*A_z:=by induction a with | nil=>simp[A_sa,A_sr] | cons x a ih => obtain ⟨d,i,j⟩:=ih (λt e=>c t (by simp[e])) have f:=(c x ג).2.1 simp only[A_sa,A_sr,List.map_cons,List.sum_cons,List.length_cons,List.cons_append] at d i j ⊢ have m:=qAr obtain ⟨o,r⟩:=ف.mp f obtain ⟨n,q⟩:=ف.mp d have k:=ц (p x) have h:=Ф (p x) (R (g x)) have l:=Ф (R (g x)) (p x) rw[abs_sub_comm] at h refine ⟨ف.mpr ⟨?_,?_⟩,?_,?_⟩<;>push_cast<;>linarith lemma qG8 (l:List ℕ) (g:ℕ->ѕ) (b:ѕ) : ((l.map g ++ [b]).length:ℝ)=l.length+1:=by simp lemma qHw {a:List ℕ} {g:ℕ->ѕ} {p:ℕ->ℝ} {b:ѕ} (d:A_G a g p) (c:bᶠ):∀ q∈a.map g ++ [b],qᶠ:=by simp only[List.mem_append,List.mem_map,List.mem_singleton];rintro q (⟨t,e,rfl⟩ | rfl);. exact (d t e).1 . exact c lemma qCk {a:ѓ} (d:good a=true) (h:a<=Б) : 0<=wv a∧wv a<=2^128 ∧ ∀ w,good w=true->w &&& Б<=a->|wv w| ≤ wv a:=by have g:Ѡa<=2147483647:=Ж.mp h have b:wv a=A_mag Ѡa:=by rw[qA9 a d,З א] simp only[show ¬ 2^31<=Ѡa by omega,↓reduceIte,one_mul] refine ⟨b ▸ qAz _,(le_abs_self _).trans (qAh a d),λw c f=>?_⟩ have e:=Ж.mp f rw[qAs] at e rw[qBv _ c,b] exact qB1 _ _ e ((qA0 _ c).imp_right (·.1)) section variable {n:ℕ} {ts:List ℕ} {g:ℕ->ѕ} {p:ℕ->ℝ} {bw hh:ѓ} {X Y J:ℝ} {s t a e:ѕ} (A:n<2^20) (B:ts.length<=n) (C:A_G ts g p) (D:good bw=true) (E:X=(ts.map p).sum+wv bw) (F:s=ts.foldl (λ(c:ѕ) t=>c+g t) 0+dv bw) (G:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|) include A B C D E F G lemma qEt (k:tᶠ) (b:Y<=A_B/8) (o:A_sa (ts.map g ++ [dv bw])+A_z<=(1+1/2^22)*Y) (u:(1+1/2^22)*((ts.length:ℝ)+1)*(A_sa (ts.map g ++ [dv bw])+A_z) + ((ts.map λt=>|p t|).sum+ts.length*A_z)<=((ts.length:ℝ)+4)*Y+1/2^240) (m : |R t-Y| ≤ Y/2^30+1/2^250) (h:e=nfOf n*t) : R (s-e)< X∧X< R (s+e):=by subst h obtain rfl:s=A_fsum (ts.map g ++ [dv bw]):=by simp[F,A_fsum,List.foldl_map] obtain ⟨c,d⟩:=qAp bw D obtain ⟨j,-,-⟩:=qCl (dv bw) C rw[d,<-E] at j have i:=qB8 (ts.map g ++ [dv bw]) have r:=qAr have l:(ts.length:ℝ)<=n:=by exact_mod_cast B have q:(n:ℝ)+1<=2^20:=by exact_mod_cast A have a:0<=Y :=ד obtain ⟨f,h⟩:=qC1 _ (qHw C c) ז ד rw[qG8] at h generalize ts.map g ++ [dv bw]=L at * have x:=qDm L have y:=Г l a have v:=ч (show (ts.length:ℝ)+1<=2^20 by linarith) o ד ב have w:=abs_sub_le (R (A_fsum L)) (A_sr L) X have z:=Ф (R (A_fsum L)) (A_sr L) exact qGo A f k a b ד m ד G lemma qC7 (P:good hh=true) (hp:hh<=Б) (U:(ts.map λt=>|p t|).sum<=wv hh*J) (m:aᶠ) (d:0<=J) (z:J<=2^149) (v : |R a-J| ≤ (J+A_z)/2^32) (he:e=nfOf n*(dv hh*a+(dv bw).abs)) : R (s-e)< X∧X< R (s+e):=by obtain ⟨b,h,-⟩:=qCk P hp;obtain ⟨Q,N⟩:=qAp hh P;obtain ⟨q,j⟩:=qAp bw D;obtain ⟨-,K,-⟩:=qCl (dv bw) C;rw[j] at K;have S:=qAh bw D;generalize wv hh=H at *;have hk:(ts.length:ℝ)+1<=2^20:=by exact_mod_cast (by omega:ts.length+1<=2^20) have L:(0:ℝ)<=ts.length:=Э _;have M:=qAr;have x:=qC2;have r:=qDk ts p;have hBl:=qBk;have T:=qAi;have u:=Ф (R a) J;rw[з d] at u;obtain ⟨e1,W⟩:=ف.mp v;have e3:=Ч e1 b;have e4:=Ч W b;have e5:=ч h x M.le ב;have e6 : |H*R a| ≤ H*(J+(J+A_z)/2^32):=by rw[abs_mul,з b];exact Ч ד b have hW:=ю b d;have hHJ:H*J<=2^128*2^149:=ч h z d ב;obtain ⟨o,i⟩:=qDg _ a Q m (by rw[N,з b];linarith) ד;rw[N] at i;have e7:=Ф (R (dv hh*a)) (H*R a);obtain ⟨w,f⟩:=qEq (dv bw) q;rw[j] at f;obtain ⟨y,k⟩:=qBl _ _ o w ד (by rw[f,abs_abs];linarith);rw[f] at k;have e8:=Գ (R (dv hh*a)) |wv bw|;rw[abs_abs] at e8;obtain ⟨e9,Z⟩:=ف.mp i;obtain ⟨e11,V⟩:=ف.mp k;have hkz:=Г (show (ts.length:ℝ)<=2^20 by linarith) M.le;have O:=ц (wv bw);obtain ⟨Y,hY⟩:∃ Y,Y=H*J + |wv bw| + A_z:=⟨_,rfl⟩;have c:A_sa (ts.map g ++ [dv bw])+A_z<=(1+1/2^22)*Y :=ד;have I:(ts.map λt=>|p t|).sum+A_z<=Y :=ד;have l : |R (dv hh*a+(dv bw).abs)-Y| ≤ Y/2^30+1/2^250 :=ف.mpr ⟨by linarith,by linarith⟩;refine qEt A B C D E F G y ד c ?_ l he;have h3:=Ч c (show 0<=(ts.length:ℝ)+1 by linarith);have h4:=Г (show (ts.length:ℝ)<=2^20 by linarith) (ד:0<=Y);linarith lemma qBh {a:ℕ->ѓ} (u:good hh=true) (w:hh<=Б) (i:∀ t∈ts,good (a t)=true) (o:∀ t∈ts,|p t| ≤ wv hh * |wv (a t)|) (r:e=nfOf n*(dv hh*ts.foldl (λ(c:ѕ) t=>c+(dv (a t)).abs) 0+(dv bw).abs)) : R (s-e)< X∧X< R (s+e):=by have f:(ts.length:ℝ)<=2^20:=by exact_mod_cast (by omega:ts.length<=2^20) have k:A_sa (ts.map λt=>dv (a t))=(ts.map λt=>|wv (a t)|).sum:=by simp only[A_sa,List.map_map];congr 1;exact List.map_congr_left λt d=>by simp[(qAp _ (i t d)).2] have h:=(qDa ts _ _ λt d=>qAh _ (i t d)).trans (Г f ב);rw[<-k] at h;have m:=qBk obtain ⟨j,b⟩:=qCa _ (λq v=>by obtain ⟨t,d,rfl⟩:=List.mem_map.mp v exact (qAp _ (i t d)).1) ז ד rw[List.length_map] at b;have g:=qB8 (ts.map λt=>dv (a t));have l:=qAr;refine qC7 A B C D E F G u w ?_ j g (h.trans ב) ?_ (r.trans (by simp[A_fsum,List.foldl_map]));. rw[k,<-List.sum_map_mul_left];exact List.sum_le_sum o . refine b.trans ?_;have:=Г f (add_nonneg g l.le);linarith end section variable (n:ћ) (hn:n<2^20) (xw ww:ћ->ѓ) (bw:ѓ) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (l:љ) (hl:l.Nodup) (hl2:∀ t∈l,t< n) include n hn xw ww bw hx hw hb l hl hl2 lemma qBs (hz:∀ t,t< n->t∉l->xw t &&& Б=0∨ww t &&& Б=0) (s e:ѕ) (hs:let r:=l.foldl (λ(acc:F1) t=>let q:=dv (xw t)*dv (ww t) F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩;s=r.s+dv bw∧e=nfOf n*(r.t+(dv bw).abs)) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|) : R (s-e)< exactS n xw ww bw∧exactS n xw ww bw< R (s+e):=by simp only[qHs] at hs obtain ⟨hs,E⟩:=hs have a:=qFs hl2 hx hw have f:=qEh hl hl2 obtain ⟨h,i⟩:=qAp bw hb obtain ⟨-,m,y⟩:=qCl (dv bw) a rw[i] at m have b:=qB8 (l.map (λt=>dv (xw t)*dv (ww t)) ++ [dv bw]) have k:=qDa l _ _ λt u=>(a t u).2.2 have v:(l.length:ℝ)+1<=2^20:=by exact_mod_cast (by omega:l.length+1<=2^20) have o:(0:ℝ)<=l.length:=Э _ have d:=qAr have D:=qC2 have w:=qAh bw hb have z:=qBk have A:=qDk l λt=>wv (xw t)*wv (ww t) have p:=k.trans (Г (show (l.length:ℝ)<=2^20 by linarith) ב) have C:=Г (show (l.length:ℝ)<=2^20 by linarith) d.le have g:A_sa (l.map (λt=>dv (xw t)*dv (ww t)) ++ [dv bw])+A_z<=A_B/8 :=ד obtain ⟨j,c⟩:=qCa _ (qHw a h) ז ד rw[qG8] at c have B:=ю o b have x:=Г v (add_nonneg b d.le) obtain ⟨G,F⟩:=ف.mp c exact qEt hn f a hb (qBi hx hw hl hl2 hz) hs hbig j g ד ד (ف.mpr ⟨by linarith,by linarith⟩) (E.trans (by simp[A_fsum,List.foldl_map])) lemma qD6 (hz:∀ t,t< n->t∉l->xw t &&& Б=0) (s e:ѕ) (hs:let r:=l.foldl (λ(acc:F1) t=>let q:=dv (xw t)*dv (ww t) F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩;s=r.s+dv bw∧e=nfOf n*(r.t+(dv bw).abs)) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|) : R (s-e)< exactS n xw ww bw∧exactS n xw ww bw< R (s+e) := qBs n hn xw ww bw hx hw hb l hl hl2 (λt b a=>.inl (hz t b a)) s e hs hbig end lemma qDb (n:ћ) (hn:n<2^20) (xw ww:ћ->ѓ) (bw hv:ѓ) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (hgv:good hv=true) (hmv:∀ t,t< n->ww t &&& Б<=hv) (hpv:hv<=Б) (l:љ) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& Б=0) (s e:ѕ) (hs:s=l.foldl (λ(acc:ѕ) t=>acc+dv (xw t)*dv (ww t)) 0+dv bw ∧ e=nfOf n*(dv hv*(l.foldl (λ(acc:ѕ) t=>acc+(dv (xw t)).abs) 0)+(dv bw).abs)) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|) : R (s-e)< exactS n xw ww bw∧exactS n xw ww bw< R (s+e):=by obtain ⟨hs,b⟩:=hs obtain ⟨-,-,c⟩:=qCk hgv hpv exact qBh hn (qEh hl hl2) (qFs hl2 hx hw) hb (qBi hx hw hl hl2 λt i z=>.inl (hz t i z)) hs hbig hgv hpv (λt a=>hx t (hl2 t a)) (λt a=>by rw[abs_mul,mul_comm] exact Г (c _ (hw t (hl2 t a)) (hmv t (hl2 t a))) (ц _)) b lemma qDl (n:ћ) (hn:n<2^20) (xw ww:ћ->ѓ) (bw hh:ѓ) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (hg:good hh=true) (hm:∀ t,t< n->xw t &&& Б<=hh) (hp:hh<=Б) (l:љ) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& Б=0∨ww t &&& Б=0) (s e:ѕ) (hs:s=l.foldl (λ(acc:ѕ) t=>acc+dv (xw t)*dv (ww t)) 0+dv bw ∧ e=nfOf n*(dv hh*(l.foldl (λ(acc:ѕ) t=>acc+(dv (ww t)).abs) 0)+(dv bw).abs)) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|) : R (s-e)< exactS n xw ww bw∧exactS n xw ww bw< R (s+e):=by obtain ⟨hs,b⟩:=hs obtain ⟨-,-,c⟩:=qCk hg hp exact qBh hn (qEh hl hl2) (qFs hl2 hx hw) hb (qBi hx hw hl hl2 hz) hs hbig hg hp (λt a=>hw t (hl2 t a)) (λt a=>by rw[abs_mul] exact Г (c _ (hx t (hl2 t a)) (hm t (hl2 t a))) (ц _)) b end section local notation:max x:max "ᶠ"=>Float.isFinite x=true noncomputable def A_r (b:ј):ℝ:=if b then -1 else 1 lemma qG4 (s t:ј):A_r (s ^^ t)=A_r s*A_r t:=by cases s<;>cases t<;>simp[A_r] lemma qG0 (S T:ј) (X Y:ℝ) (a:0< X) (b:0< Y) (h:A_r S*X=A_r T*Y) : S=T:=by cases S<;>cases T<;>simp[A_r] at h ⊢<;>linarith section variable (d:FloatLib.Numerics.Dyadic) include d lemma qEc : d.toReal=A_r d.negative*((d.significand:ℝ)*2^d.exponent):=by obtain ⟨n,m,e⟩:=d;cases n<;>simp[FloatLib.Numerics.Dyadic.toReal,signedSignificand,A_r] lemma A_s:d.toReal=0 ↔ d.significand=0:=by have h:(2:ℝ)^d.exponent≠0:=zpow_ne_zero _ С;rw[qEc];cases d.negative<;>simp[A_r,h] lemma qFx (h:d.significand≠0) : 0<(d.significand:ℝ)*2^d.exponent :=mul_pos (Nat.cast_pos.mpr (м h)) (ػ two_pos _) end lemma qIm (a b:ѕ) (h:A_M a=A_M b):a=b:=by have:=Model.ofFloatModel_injective h;cases a;cases b;simp_all lemma qDh (f:ѕ) (h:fᶠ) : ∃ d,Model.toDyadic? (A_M f)=some d∧d.toReal=R f:=by obtain ⟨d,a⟩:=Model.exists_toDyadic?_of_isFinite ((qAm f).trans h);exact ⟨d,a,by rw[A_R,Model.toReal_eq,a]⟩ lemma qCn {a b:ѕ} {x y:FloatLib.Numerics.Dyadic} (f:Model.toDyadic? (A_M a)=some x) (d:Model.toDyadic? (A_M b)=some y) (e:aᶠ) (c:bᶠ) : A_M (a*b)=if x.significand*y.significand=0 then Model.zero _ (x.negative ^^ y.negative) else Ъ _ ⟨x.negative ^^ y.negative,x.significand*y.significand,x.exponent+y.exponent⟩:=by rw[qBq a b e c,Model.Proof.mul_eq_spec,Model.Spec.mul,f,d] simp lemma qHu {a b:ѕ} {x y z:FloatLib.Numerics.Dyadic} (m:Model.toDyadic? (A_M a)=some x) (n:Model.toDyadic? (A_M b)=some y) (e:Model.toDyadic? (A_M (a*b))=some z) (k:aᶠ) (l:bᶠ) (c:z.toReal=x.toReal*y.toReal):z.negative=(x.negative ^^ y.negative):=by by_cases g:x.significand*y.significand=0;. have d:=qCn m n k l;simp only[g,↓reduceIte] at d;rw[d,Model.toDyadic?_zero] at e;cases e;show Model.signBit _=_;cases x.negative<;>cases y.negative<;>simp[Model.zero] . obtain ⟨f,i⟩:=Nat.mul_ne_zero_iff.mp g;have j:z.significand≠0:=λh=>by rw[(A_s z).2 h] at c;exact mul_ne_zero (mt (A_s x).1 f) (mt (A_s y).1 i) c.symm rw[qEc z,qEc x,qEc y,mul_mul_mul_comm,<-qG4] at c;exact qG0 _ _ _ _ (qFx z j) (mul_pos (qFx x f) (qFx y i)) c lemma qG3 (v m p:ѕ) (f:vᶠ) (i:mᶠ) (h:pᶠ) (u:(v*p)ᶠ) (s:(m*p)ᶠ) (q:R (v*p)=R v*R p) (r:R (m*p)=R m*R p):v*p*m=v*(m*p):=by obtain ⟨a,o,n⟩:=qDh v f;obtain ⟨b,y,k⟩:=qDh p h;obtain ⟨c,w,A⟩:=qDh m i;obtain ⟨d,t,B⟩:=qDh _ u;obtain ⟨e,l,x⟩:=qDh _ s;have D:=qHu o y t f h (by rw[B,n,k,q]);have E:=qHu w y l i h (by rw[x,A,k,r]);have C:(d.negative ^^ c.negative)=(a.negative ^^ e.negative):=by rw[D,E];cases a.negative<;>cases b.negative<;>cases c.negative<;>rfl have j:d.toReal*c.toReal=a.toReal*e.toReal:=by rw[B,A,n,x,q,r];ring have g:d.significand*c.significand=0 ↔ a.significand*e.significand=0:=by simp only[Nat.mul_eq_zero,<-A_s,<-mul_eq_zero,j] apply qIm;rw[qCn t w u i,qCn o l f s];by_cases z:d.significand*c.significand=0;. simp only[z,g.1 z,↓reduceIte,C] . simp only[z,mt g.2 z,↓reduceIte];refine ExactRuntime.roundDyadic_congr _ qAl _ _ z (mt g.2 z) (Rat.cast_injective (α:=ℝ) ?_);rw[Ь,Ь,Model.Dyadic.toReal_mul,Model.Dyadic.toReal_mul,j] lemma qIq (x w:ѓ) (hx:good x=true) : dvB (dv x) w=dv x*dv w:=by obtain ⟨e,o⟩:=qAp x hx;obtain ⟨p,f,j,q,g,-⟩:=qD4 w;obtain ⟨d,a,-,-,l,b⟩:=qD4 x;obtain ⟨m,c,k⟩:=qCv _ a;have r:=x.toNat_lt;have h:R (dv x)=(((2^23+Ѡx%2^23:ℕ)*m:ℤ):ℝ) * 2^(((Ѡx/2^23%256:ℕ):ℤ)-150):=by unfold dv;rw[d,l,b,k,Int.cast_mul,Int.cast_natCast,mul_assoc] obtain ⟨n,i⟩:=qCp (dv x) _ f e _ _ (by rw[Int.natAbs_mul,Int.natAbs_natCast];have:=Nat.mul_le_mul_left (2^23+Ѡx%2^23) c omega) א (by rw[o];exact (qAh x hx).trans qAi) h show dv x*pw.uget _ _*_=dv x*(_*pw.uget _ _);rw[p];exact qG3 _ _ _ e j (qDi _ f).1 n q i g end section section open Float.Model Float.Model.UnpackedFloat lemma qCx (H a b:ћ) (hb:b< H):(H*a+b)/H=a∧(H*a+b)%H=b:=by have c:0< H :=א;constructor;. rw[ؾ c,д hb,р] . rw[ؿ,З hb] lemma qGi (t:Int):Format.binary32.targetExponent t=max (t-24) (-149):=by simp[Format.targetExponent,Format.mantissaBits,Format.minExponent] lemma B_z (a b:ℤ) (n:ℕ) (h:a=n+b):(2:ℝ)^a=2^n*2^b:=by rw[h,а С,т] lemma qCz (m L:ћ) (h1:2^L<=m) (h2:m<2^(L+1)):m.log2=L :=(Nat.log2_eq_iff (by have:=Nat.two_pow_pos L;omega)).2 ⟨h1,h2⟩ lemma qEs (M:ћ) (e T:Int) (K r:ћ) (d:T=e+(K+1:ћ)) (c:0< r) (f:2*(r*2^K)< M+2^K) (g:M<2*(r*2^K)+2^K) : Model.roundMantissaAtExponentEven M e T=r:=by have b:r <<< (K+1)=2*(r*2^K):=by rw[ф,Nat.pow_succ];ring have a:Model.pow2 (K+1-1)=2^K:=Model.pow2_eq_two_pow K;rw[Model.roundMantissaAtExponentEven,б א,show (T-e).toNat=K+1 by omega];rcases le_total (2*(r*2^K)) M with h | h;. have:=Model.roundShiftRightEven_shiftLeft_add_of_lt_half r (M-2*(r*2^K)) (K+1) א (by rw[a];omega);rwa [b,Nat.add_sub_cancel' h] at this . have:=Model.roundShiftRightEven_shiftLeft_sub_of_lt_half r (2*(r*2^K)-M) (K+1) c א (by rw[a];omega);rwa [b,Nat.sub_sub_self h] at this lemma qEn (s:Sign) (P:ћ) (E:Int) (h1:2^23<=P) (h2:P<2^24) (hE:-149<=E) (hp:0< P):Model.finishRoundedMantissa Format.binary32 s (P,E)=.finite s P E hp:=by have:=Model.finishRoundedMantissa_normalized FloatFormat.binary32 s P (E+23) h1 h2 (by show -126<=E+23;omega) rwa [show E+23-(FloatFormat.binary32.fracWidth:ℤ)=E from Int.add_sub_cancel E 23] at this lemma qAy (neg:ј) (m:ћ) (e:Int) : Ъ Fmt ⟨neg,m,e⟩ =Model.ofModel Fmt (UnpackedFloat.round Format.binary32 (в neg) m e):=by simp[Model.roundDyadic,Model.ieeeRoundDyadic] rfl lemma qBg (s:Sign) (M:ћ) (E:Int) (h:0< M) (hM1:2^23<=M) (hM2:M<2^24) (hb1:1<=E+150) (hb2:E+150<=254) : (UnpackedFloat.pack Format.binary32 (.finite s M E h)).toNat =(if s=.negative then 2^31 else 0)+(E+150).toNat*2^23+M%2^23:=by simp only[UnpackedFloat.pack,qCz M 23 hM1 hM2] rw[show (E+↑Format.binary32.exponentBias + ↑Format.binary32.mantissaBitsWithoutImplicit).toNat=(E+150).toNat by simp only[Format.exponentBias];norm_num;ring_nf] have c:¬ (2^Format.binary32.exponentBits<=(E+150).toNat+1):=by show ¬ (2^8<=(E+150).toNat+1);omega have a:(23+1=Format.binary32.mantissaBits):=rfl have b:s.toBitVec.toNat=if s=.negative then 1 else 0:=by cases s<;>rfl simp only[c,ite_false,a,ite_true,packComponents,BitVec.toNat_append,b,BitVec.toNat_ofNat] rw[З (by omega:(E+150).toNat<2^8), <-Nat.shiftLeft_add_eq_or_of_lt (by omega:(E+150).toNat<2^8), <-Nat.shiftLeft_add_eq_or_of_lt (щ _ ו:M%2^23<2^23)] split_ifs<;>simp[Nat.shiftLeft_eq] omega lemma qH3 (neg:ј) (m:ћ) (e:Int) (P:ћ) (E:Int) (hm:m≠0) (hP1:2^23<=P) (hP2:P<2^24) (hE:-149<=E) (hE2:E+150<=254) (hhi:2*((m:ℝ)*2^e)<(2*P+1)*2^E) (hlo1:P≠2^23->(2*P-1)*2^E<2*((m:ℝ)*2^e)) (hlo2:P=2^23->33554431*2^E<4*((m:ℝ)*2^e)) : toWord (Ъ Fmt ⟨neg,m,e⟩) =ш ((if neg then 2^31 else 0)+(E+150).toNat*2^23+P%2^23):=by have l:0< P :=א obtain ⟨k,F⟩:∃ k:ћ,e-E+2<=k:=⟨(e-E+2).toNat,by omega⟩ obtain ⟨g,z⟩:∃ g:ћ,(e-k)+(g+2:ћ)=E:=⟨(E-(e-k)-2).toNat,by omega⟩ have u:(0:ℝ)<2^(e-k):=ػ two_pos _ have s:(m:ℝ)*2^e=((m*2^k:ћ):ℝ)*2^(e-k):=by push_cast rw[mul_assoc,<-B_z e _ k א] have i:(2:ℝ)^E=4*((2^g:ћ):ℝ)*2^(e-k):=by rw[B_z E (e-k) (g+2) א] push_cast ring rw[qAy,<-ExactRuntime.round_scale _ _ m e k hm] have p:m*2^k≠0:=Nat.mul_ne_zero hm ו rw[s,i] at hhi rw[s,i] at hlo1 hlo2 have H:0<2^g:=by positivity generalize m*2^k=M at * generalize e-k=q at * generalize (2:ℝ)^q=b at * have f:∀ A B:ℕ,(A:ℝ)*b< B*b->A< B :=λA B h=>by exact_mod_cast lt_of_mul_lt_mul_right h u.le push_cast at hhi hlo1 hlo2 have G:M<4*(P*2^g)+2*2^g:=f _ _ (by push_cast;linarith) have t:P≠2^23->4*(P*2^g)< M+2*2^g :=λh=>f _ _ (by push_cast;linarith [hlo1 h]) have y:P=2^23->33554431*2^g< M :=λh=>f _ _ (by push_cast;linarith [hlo2 h]) have C:P*2^g<=(2^24-1)*2^g:=Н _ א have r:2*(P*2^(g+1))=4*(P*2^g):=by rw[Nat.pow_succ];ring have n:2^(g+1)=2*2^g:=by rw[Nat.pow_succ];ring have w:UnpackedFloat.round Format.binary32 (в neg) M q =.finite (в neg) P E l:=by rw[Model.round_exact_eq_finishRoundedMantissa _ _ _ _ p] have o:∀ (L:ℕ) (T:ℤ),2^L*2^g<=M->M<2^L*2^g*2 → max (((L+g:ℕ):ℤ)+1+q-24) (-149)=T → Format.binary32.targetExponent (totalExponent M q)=T:=λL T a c d=>by rw[<-Nat.pow_add] at a c simp only[totalExponent,qCz M _ a c,qGi];exact d by_cases v:2^25*2^g<=M . rw[o 25 E v א א,qEs M q E (g+1) P א l ?_ (by rw[r,n];omega)];. exact qEn _ P E hP1 hP2 hE l . rw[r,n];by_cases j:P=2^23;. subst j;omega . have:=t j;omega have j:P=2^23:=by by_contra hne have:=t hne have:(2^23+1)*2^g<=P*2^g:=Н _ א omega have D:=y j subst j by_cases x:-148<=E . rw[o 24 (E-1) א א א,qEs M q (E-1) g (2^24) א ב א א];have:=Model.finishRoundedMantissa_normalized_carry FloatFormat.binary32 (в neg) (E-1+23) (by show -126<=E-1+23;omega);rw[show (FloatFormat.binary32.fracWidth:ℤ)=23 from rfl] at this;rwa [(א:E-1+23-23=E-1),(א:E-1+23+1-23=E)] at this . rw[o 24 E א א א,qEs M q E (g+1) (2^23) א ב (by rw[n];omega) (by rw[n];omega)];exact qEn _ _ E ב ב hE l rw[w] simp only[toWord,Model.toNatBits,Model.ofModel,Model.ofModelBits] congr 1 show (UnpackedFloat.pack Format.binary32 _).toNat=_ rw[qBg _ _ _ _ hP1 hP2 א א] cases neg<;>simp[Model.modelSign] end abbrev B_D:=ExecFloat.Binary.format (exponentBits:=11) (fractionBits:=52) lemma qI0 (f:ѕ):Model.signBit (toModel (ofFloat f))=decide (2^63<=f.toBits.toNat):=by rw[Model.signBit_eq_msb];show f.toBits.toBitVec.msb=_;rw[BitVec.msb_eq_decide];rfl lemma B_R (f:ѕ) (s:ј) (E F:ћ) (hE1:1<=E) (hE:E<2047) (hF:F<2^52) (h:f.toBits.toNat=(if s then 2^63 else 0)+E*2^52+F) : f.isFinite=true ∧ R f=(if s then -1 else 1)*((2^52+F:ћ):ℝ)*2^((E:Int)-1075):=by have g:Model.expField (toModel (ofFloat f))=f.toBits.toNat/2^52%2^11:=by show ((f.toBits.toBitVec >>> 52) &&& BitVec.ofNat 64 2047).toNat=_ rw[BitVec.toNat_and,BitVec.toNat_ushiftRight,BitVec.toNat_ofNat,ы] exact М _ 11 have e:Model.fracField (toModel (ofFloat f))=f.toBits.toNat%2^52:=by show (f.toBits.toBitVec &&& BitVec.ofNat 64 (2^52-1)).toNat=_ rw[BitVec.toNat_and,BitVec.toNat_ofNat] exact М _ 52 have i:Model.signBit (toModel (ofFloat f))=s :=(qI0 f).trans (by cases s<;>simp at h ⊢<;>omega) have c:=Model.ofFields_signBit_expField_fracField (toModel (ofFloat f)) rw[g,e,i,show f.toBits.toNat/2^52%2^11=E by split at h<;>omega,show f.toBits.toNat%2^52=F by split at h<;>omega] at c have b:E<2^11 :=א have a:н (Model.ofFields B_D s E F)=true:=by rw[Model.isFinite_ofFields B_D _ _ _ b hF] show (E != 2047)=true simp;omega have d:=Model.toDyadic?_ofFields_of_isFinite B_D _ _ _ b hF a rw[c,г (א:¬ E=0)] at d rw[c] at a refine ⟨(qAm f).symm.trans a,?_⟩ rw[R,Model.toReal_eq,d] simp only[show Model.pow2 B_D.fracWidth=2^52 from rfl, show Int.ofNat E-Int.ofNat B_D.exponentBias-Int.ofNat B_D.fracWidth=(E:ℤ)-1075 by show (E:ℤ)-1023-52=_;omega] cases s<;>simp[FloatLib.Numerics.Dyadic.toReal,signedSignificand] def B_near (A:ћ) (S:ј) (ex fr:ћ):Prop := ∃ M,A=(if S then 2^63 else 0)+M∧(ex*2^23+fr)*2^29< M+2^28 ∧ M<=(ex*2^23+fr)*2^29+2^28 lemma B_x (A:і) (x:і) (hx:(A+0x0FFFFFFF) >>> 29=x) (h1:897<=Ѡx/2^23%2^11) (h2:Ѡx/2^23%2^11<1151) : Ѡx<2^35∧B_near A.toNat (decide (2^34<=Ѡx)) (Ѡx/2^23%2^11) (Ѡx%2^23):=by have a:=A.toNat_lt have e:Ѡx=(A.toNat+268435455)%2^64/2^29:=by rw[<-hx,ز,ж] norm_num [Nat.shiftRight_eq_div_pow] refine ⟨by omega,?_⟩ unfold B_near by_cases h:2^34<=Ѡx . simp only[h,decide_true,ite_true];exact ⟨A.toNat-2^63,by omega,by omega,by omega⟩ . simp only[h,decide_false,false_eq_true,ite_false];exact ⟨A.toNat,by omega,by omega,by omega⟩ lemma qIo (x:і) (hx:Ѡx<2^35) (h1:897<=Ѡx/2^23%2^11) (h2:Ѡx/2^23%2^11<1151) : (((x >>> 34) <<< 31) ||| ((x &&& (0x3FFFFFFFF:і))-((896:і) <<< 23))).toUInt32 = ш ((if decide (2^34<=Ѡx) then 2^31 else 0) + (Ѡx/2^23%2^11-896)*2^23+Ѡx%2^23):=by have b:((x >>> 34) <<< 31).toNat=(Ѡx/2^34)*2^31:=by rw[UInt64.toNat_shiftLeft,ز] norm_num [Nat.shiftRight_eq_div_pow,Nat.shiftLeft_eq] omega have a:(x &&& (0x3FFFFFFFF:і)).toNat=Ѡx%2^34:=by rw[ى] exact М _ 34 have d:((x &&& (0x3FFFFFFFF:і))-((896:і) <<< 23)).toNat =Ѡx%2^34-896*2^23:=by rw[show ((896:і) <<< 23)=7516192768 by decide,toNat_sub_of_le,a] . rfl . rw[Щ,a];norm_num;omega have c:(Ѡx/2^34)*2^31 ||| (Ѡx%2^34-896*2^23) =(Ѡx/2^34)*2^31+(Ѡx%2^34-896*2^23):=by rw[Ю (Ѡx/2^34)] exact (Nat.two_pow_add_eq_or_of_lt א _).symm apply П.mp rw[UInt64.toNat_toUInt32,UInt64.toNat_or,b,d,c,ق] simp only[decide_eq_true_eq] split_ifs<;>omega lemma qDc (A B:і) (h:zivW A B≠Д) : ∃ (S:ј) (ex fr:ћ),897<=ex∧ex<1151∧fr<2^23 ∧ zivW A B=ш ((if S then 2^31 else 0)+(ex-896)*2^23+fr) ∧ B_near A.toNat S ex fr∧B_near B.toNat S ex fr:=by unfold zivW at h ⊢ dsimp only at h ⊢ generalize hx:(A+(0x0FFFFFFF:і)) >>> (29:і)=x at h ⊢ generalize hy:(B+(0x0FFFFFFF:і)) >>> (29:і)=y at h ⊢ have hex:((x >>> (23:і)) &&& (0x7FF:і)).toNat=Ѡx/2^23%2^11:=by rw[ى,ز] have:=М (Ѡx >>> 23) 11 norm_num [Nat.shiftRight_eq_div_pow] at this ⊢ exact this generalize (x >>> (23:і)) &&& (0x7FF:і)=ex at h hex ⊢ split at h . rename_i hc;have hc0:=hc;obtain ⟨hxy,hc⟩:=п.mp hc;have hxy:x=y:=by simpa using hxy subst hxy;have hc:ex-(897:і)<(254:і):=by simpa using hc rw[ص,UInt64.toNat_sub] at hc;have hv:=ex.toNat_lt;norm_num at hc hv;have h1:897<=Ѡx/2^23%2^11 :=א;have h2:Ѡx/2^23%2^11<1151 :=א;simp only[hc0,ite_true];obtain ⟨hx35,ha⟩:=B_x A x hx h1 h2;exact ⟨_,_,_,h1,h2,by omega,qIo x hx35 h1 h2,ha,(B_x B x hy h1 h2).2⟩ . exact absurd rfl h lemma qB0 (s e:ѕ) (h:ziv s e≠Д) : ∃ (S:ј) (ex fr:ћ),897<=ex∧ex<1151∧fr<2^23 ∧ ziv s e=ш ((if S then 2^31 else 0)+(ex-896)*2^23+fr) ∧ B_near (s+e).toBits.toNat S ex fr∧B_near (s-e).toBits.toNat S ex fr := qDc (s+e).toBits (s-e).toBits h set_option maxRecDepth 8192 in lemma qAn (f:ѕ) (S:ј) (ex fr:ћ) (h1:897<=ex) (h2:ex<1151) (hfr:fr<2^23) (hn:B_near f.toBits.toNat S ex fr) : f.isFinite=true∧∃ v:ℝ,R f=(if S then -v else v) ∧ v<=(2^53+fr*2^30+2^29)*2^((ex:Int)-1076) ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)< v) ∧ (2^53-2^28)*2^((ex:Int)-1076)< v:=by obtain ⟨M,k,o,n⟩:=hn have d:M%2^52<2^52:=щ _ ב have b:M=(M/2^52)*2^52+M%2^52 :=א generalize M/2^52=E at b generalize M%2^52=F at b d subst b have c:(0:ℝ)<2^((ex:Int)-1076):=ػ ב _ have m:E=ex∨(E+1=ex∧fr=0) :=א obtain ⟨a,l⟩:=B_R f S E F א א d (by rw[k,и]) obtain ⟨w,j,i,e,g⟩:∃ w:ℕ,((2^52+F:ћ):ℝ)*2^((E:Int)-1075) =w*2^((ex:Int)-1076)∧w<=2^53+fr*2^30+2^29 ∧ (fr≠0->2^53+fr*2^30< w+2^29)∧2^53< w+2^28:=by rcases m with rfl | ⟨rfl,rfl⟩ . refine ⟨2*(2^52+F),?_,by omega,by omega,by omega⟩;rw[show (E:Int)-1075=1+((E:Int)-1076) by ring,а С,zpow_one];push_cast;ring . exact ⟨2^52+F,by congr 2;push_cast;ring,by omega,by omega,by omega⟩ refine ⟨a,(w:ℝ)*2^((ex:Int)-1076),?_,Г ?_ c.le,λh=>Я ?_ c,Я ?_ c⟩ . rw[l,mul_assoc,j];cases S<;>simp . exact_mod_cast i . rw[sub_lt_iff_lt_add];exact_mod_cast e h . rw[sub_lt_iff_lt_add];exact_mod_cast g lemma qBn (s e:ѕ) (h:ziv s e≠Д) : (s+e).isFinite=true∧(s-e).isFinite=true∧(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|:=by obtain ⟨S,f,c,b,d,a,-,j,k⟩:=qB0 s e h;obtain ⟨g,v,i,-,-,l⟩:=qAn _ S f c b d a j;refine ⟨g,(qAn _ S f c b d a k).1,?_⟩ exact (show (2:ℝ)^(-127:ℤ)<=(2^53-2^28)*2^(-179:ℤ) by norm_num).trans ((Ч (о one_le_two א) ב).trans (l.le.trans (by rw[i];cases S<;>simp[le_abs_self]))) lemma qAq (w:ѓ) (h:expOf w≠255):wv w=(Л w:ℝ):=by rw[wv,Model.toReal_eq,ofW,Ц w h];simp only[rationalWord,FloatLib.Numerics.Dyadic.cast_toRat] lemma qFk (a b:ѐ) (start n:ћ) : (dsum a b start n:ℚ)*2^(-298:Int) =∑ j∈л n,Л a[start+j]! * Л b[start+j]!:=by have c:∀ a b:ѓ,((sv a:ℚ)*sv b*(2:ℚ)^(idxOf a+idxOf b))*2^(-298:Int) =Л a*Л b:=λa b=>by simp only[rationalWord,decodeD_toRat] rw[show (-298:Int)=-149+-149 by norm_num,а С] rw[ن С,ن С,т,т,pow_add] norm_num [zpow_neg,zpow_natCast] ring induction n generalizing start with | zero=>simp[dsum] | succ n ih => rw[dsum,Int.cast_add,Int.cast_mul,Int.cast_mul,Int.cast_pow,Int.cast_ofNat,add_mul,ih,Finset.sum_range_succ',р,c,add_comm] congr 1 apply ъ rfl intro j _ rw[и,Nat.add_comm 1 j] lemma qB6 (n:ћ) (fa fb:ћ->ѓ) (hf:∀ j,j< n->expOf (fa j)≠255∧expOf (fb j)≠255) (hn:(∑ j∈л n,Л (fa j)*Л (fb j))≠0) : ∃ d:FloatLib.Numerics.Dyadic, d.toRat=(∑ j∈л n,Л (fa j)*Л (fb j)) ∧ dotOf n fa fb=toWord (Ъ Fmt d):=by let a:=Array.ofFn (n:=n) λi=>fa i let b:=Array.ofFn (n:=n) λi=>fb i have e:a.size=n:=by simp[a] have g:b.size=n:=by simp[b] have c:(dsum a b 0 n:ℚ)*2^(-298:Int) =∑ j∈л n,Л (fa j)*Л (fb j):=by rw[qFk] refine ъ rfl λj m=>?_ have h:=ط.mp m rw[И,qAa n fa j h,qAa n fb j h] have l:dsum a b 0 n≠0:=λh=>hn (by rw[<-c,h];simp) have Z:=qEi a b (by rw[e,g]) λk f=>by rw[e] at f rw[qAa n fa k f,qAa n fb k f];exact hf k f rw[e] at Z refine ⟨ofIntDyadic (dsum a b 0 n) (-298),by rw[toRat_ofIntDyadic,c],?_⟩ show dotSpec a b=_ rw[<-Z];simp[finishWord,l] lemma qGd:Л oneW=1:=by norm_num [rationalWord,decodeD,show expOf oneW=127 by decide,show fracOf oneW=0 by decide,show signOf oneW=false by decide,FloatLib.Numerics.Dyadic.toRat,signedSignificand] lemma qIc (n:ћ) (xw ww:ћ->ѓ) (bw:ѓ) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) : ((∑ j∈л (n+1),Л (if j< n then xw j else oneW) * Л (if j< n then ww j else bw):ℚ):ℝ)=exactS n xw ww bw:=by rw[Finset.sum_range_succ] simp only[lt_self_iff_false,ite_false,qGd,one_mul] rw[Rat.cast_add,Rat.cast_sum,exactS,qAq bw (qAd bw hb)] congr 1 refine ъ rfl λt b=>?_ have a:=ط.mp b simp only[a,ite_true] rw[qAq _ (qAd _ (hx t a)),qAq _ (qAd _ (hw t a)),Rat.cast_mul] section variable (s e:ѕ) (V:ℝ) (hlo:R (s-e)< V) (hhi:V< R (s+e)) (h:ziv s e≠Д) include s e V hlo hhi h lemma qEu : ∃ (S:ј) (ex fr:ћ) (W:ℝ),897<=ex∧ex<1151∧fr<2^23 ∧ ziv s e=ш ((if S then 2^31 else 0)+(ex-896)*2^23+fr) ∧ V=(if S then -W else W) ∧ W<(2^53+fr*2^30+2^29)*2^((ex:Int)-1076) ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)< W) ∧ (2^53-2^28)*2^((ex:Int)-1076)< W:=by obtain ⟨S,ex,fr,h1,h2,h3,hz,ha,hb'⟩:=qB0 s e h obtain ⟨-,va,hva,hia,loa1,loa⟩:=qAn _ S ex fr h1 h2 h3 ha obtain ⟨-,vb,hvb,hib,lob1,lob⟩:=qAn _ S ex fr h1 h2 h3 hb' rw[hva] at hhi rw[hvb] at hlo refine ⟨S,ex,fr,if S then -V else V,h1,h2,h3,hz,?_,?_,?_,?_⟩ all_goals cases S all_goals simp only[false_eq_true,ite_false,ite_true,neg_neg] at hhi hlo ⊢ . exact lt_of_lt_of_le hhi hia . linarith . exact λh=>(lob1 h).trans hlo . exact λh=>by linarith [loa1 h] . exact lob.trans hlo . linarith lemma qHl:V≠0:=by obtain ⟨S,ex,fr,W,-,-,-,-,hV,-,-,hW3⟩:=qEu s e V hlo hhi h;have hb0:(0:ℝ)<2^((ex:Int)-1076):=ػ ב _;have hA:(0:ℝ)<=(2^53-2^28)*2^((ex:Int)-1076):=ю ב hb0.le;intro h0;rw[h0] at hV;cases S;. simp only[false_eq_true,ite_false] at hV;linarith . simp only[ite_true] at hV;linarith end lemma qFd (d:FloatLib.Numerics.Dyadic) (S:ј) (ex fr:ћ) (h1:897<=ex) (h2:ex<1151) (h3:fr<2^23) (W:ℝ) (hd:(d.toRat:ℝ)=if S then -W else W) (hW1:W<(2^53+fr*2^30+2^29)*2^((ex:Int)-1076)) (hW2:fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)< W) (hW3:(2^53-2^28)*2^((ex:Int)-1076)< W) : toWord (Ъ Fmt d) =ш ((if S then 2^31 else 0)+(ex-896)*2^23+fr):=by have hb0:(0:ℝ)<2^((ex:Int)-1076):=ػ ב _ have hE:(2:ℝ)^((ex:Int)-1046)=2^30*2^((ex:Int)-1076):=by rw[<-т,<-а С] congr 1;push_cast;ring have hW0:0< W:=lt_of_le_of_lt (ю ב hb0.le) hW3 obtain ⟨neg,m,q⟩:=d have hdV:(FloatLib.Numerics.Dyadic.mk neg m q).toReal=if S then -W else W:=by rw[<-Ь,hd] have hq:(0:ℝ)<2^q:=ػ ב _ have hm0:(0:ℝ)<=m:=Э _ have hmq:=ю hm0 hq.le have hMW:(m:ℝ)*2^q=W∧neg=S:=by simp only[FloatLib.Numerics.Dyadic.toReal,signedSignificand] at hdV cases neg<;>cases S <;> simp only[false_eq_true,ite_false,ite_true,Int.ofNat_eq_natCast,Int.cast_neg,Int.cast_natCast] at hdV . exact ⟨by linarith,rfl⟩ . exfalso;linarith . exfalso;linarith . exact ⟨by linarith,rfl⟩ obtain ⟨hMW,rfl⟩:=hMW have hm:m≠0:=by rintro rfl simp at hMW linarith have hword:=qH3 neg m q (2^23+fr) ((ex:Int)-1046) hm א א א א (by rw[hMW,hE];push_cast;linarith) (by intro hfr have hfr':fr≠0:=by intro h0;exact hfr א have:=hW2 hfr' rw[hMW,hE];push_cast;linarith) (by intro _ rw[hMW,hE];linarith) have e1:((ex:Int)-1046+150).toNat=ex-896 :=א have e2:(2^23+fr)%2^23=fr :=א rw[hword,e1,e2] lemma qHr (s e:ѕ) (d:FloatLib.Numerics.Dyadic) (hlo:R (s-e)<(d.toRat:ℝ)) (hhi:(d.toRat:ℝ)< R (s+e)) (h:ziv s e≠Д) : ziv s e=toWord (Ъ Fmt d):=by obtain ⟨S,ex,fr,W,h1,h2,h3,hz,hV,hW1,hW2,hW3⟩:=qEu s e _ hlo hhi h;rw[hz];exact (qFd d S ex fr h1 h2 h3 W hV hW1 hW2 hW3).symm lemma qCo (n:ћ) (hn:n<2^20) (xw ww:ћ->ѓ) (bw:ѓ) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (s e:ѕ) (hlo:R (s-e)< exactS n xw ww bw) (hhi:exactS n xw ww bw< R (s+e)) (h:ziv s e≠Д) : ziv s e=dotOf (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw):=by have hsum:=qIc n xw ww bw hx hw hb obtain ⟨d,hd,hw'⟩:=qB6 (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw) (λj _=>by by_cases hj:j< n . simp only[hj,ite_true];exact ⟨qAd _ (hx j hj),qAd _ (hw j hj)⟩ . simp only[hj,ite_false] exact ⟨by decide,qAd _ hb⟩) λh0=>qHl s e _ hlo hhi h (by rw[<-hsum,h0,Rat.cast_zero]) have q:(d.toRat:ℝ)=exactS n xw ww bw:=by rw[hd,hsum] rw[hw'] exact qHr s e d (by rw[q];exact hlo) (by rw[q];exact hhi) h end section lemma qBt (n:ћ) (hn:n<2^20) (xw ww:ћ->ѓ) (bw:ѓ) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (l:љ) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& Б=0∨ww t &&& Б=0) (s e:ѕ) (hs:let r:=l.foldl (λ(acc:F1) t=>let q:=dv (xw t)*dv (ww t) F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩;s=r.s+dv bw∧e=nfOf n*(r.t+(dv bw).abs)) (h:ziv s e≠Д) : ziv s e=dotOf (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw):=by obtain ⟨-,-,hbig⟩:=qBn s e h obtain ⟨hlo,hhi⟩:=qBs n hn xw ww bw hx hw hb l hl hl2 hz s e hs hbig exact qCo n hn xw ww bw hx hw hb s e hlo hhi h lemma qC0 (n:ћ) (hn:n<2^20) (xw ww:ћ->ѓ) (bw hh:ѓ) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (hg:good hh=true) (hm:∀ t,t< n->xw t &&& Б<=hh) (hp:hh<=Б) (l:љ) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& Б=0∨ww t &&& Б=0) (s e:ѕ) (hs:s=l.foldl (λ(acc:ѕ) t=>acc+dv (xw t)*dv (ww t)) 0+dv bw ∧ e=nfOf n*(dv hh*(l.foldl (λ(acc:ѕ) t=>acc+(dv (ww t)).abs) 0)+(dv bw).abs)) (h:ziv s e≠Д) : ziv s e=dotOf (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw):=by obtain ⟨-,-,hbig⟩:=qBn s e h obtain ⟨hlo,hhi⟩:=qDl n hn xw ww bw hh hx hw hb hg hm hp l hl hl2 hz s e hs hbig exact qCo n hn xw ww bw hx hw hb s e hlo hhi h end section lemma C_z:(0:є).toNat=0:=rfl lemma qAb (k:ћ) (h:k<2^32):k.toUSize.toNat=k:=by rw[Nat.toUSize_eq];exact uk k h lemma qCd (a b:є) (h:Ѡa*Ѡb<2^32) : (a*b).toNat=Ѡa*Ѡb:=qC4 a b h lemma qBo {α:Type} (a:Array α) (h:a.size<2^32):a.usize.toNat=a.size :=qAb _ h lemma qAk (a:ѐ) (i:є) (hs:a.size<2^32):rdW a i=a[Ѡi]!:=by unfold rdW;split;. rename_i h;have h2:=qA5 a i h;rw[Ш a Ѡi h2];rfl . rename_i h;have h':¬ Ѡi< a.size:=by intro h2;apply h;rw[А,qBo a hs];exact h2 rw[س a Ѡi h'];rfl lemma qAx (a:ѐ) (i:є) (v:ѓ):(wrW a i v).size=a.size:=by unfold wrW;split<;>simp lemma qGg {α:Type} [Inhabited α] (a:Array α) (i q:ћ) (v:α) (hi:i< a.size) : (a.set i v hi)[q]! = if q=i then v else a[q]!:=by by_cases hq:q< a.size . rw[Ш _ q (by simpa using hq),getElem!_pos a q hq,Array.getElem_set];by_cases e:q=i;. simp[e] . rw[Т e,Т (λh=>e h.symm)] . rw[س _ q (by simpa using hq),getElem!_neg a q hq,ite_eq_right א] lemma qA4 (a:ѐ) (i:є) (v:ѓ) (q:ћ) (hs:a.size<2^32) (hi:Ѡi< a.size):(wrW a i v)[q]! = if q=Ѡi then v else a[q]!:=by have h:i< a.usize:=by rw[А,qBo a hs];exact hi unfold wrW;rw[К h];exact qGg a _ q v hi def C_Inv (N:ћ) (sp:ћ->њ) (S:ћ->Prop) (out:ё):Prop := out.size=N∧∀ q,q< N->S q->out[q]! = sp q section variable {N:ћ} {sp:ћ->њ} include N sp lemma qDj {S:ћ->Prop} {out:ё} (hN:N<2^32) (h:C_Inv N sp S out) {u:є} {p:ћ} {v:њ} (hu:Ѡu=p) (hp:p< N) (hv:v=sp p):C_Inv N sp (λq=>q=p∨S q) (wrW out u v):=by obtain ⟨h1,h2⟩:=h;refine ⟨by rw[qAx,h1],λq hq hS=>?_⟩;rw[qA4 _ _ _ _ א א,hu];by_cases e:q=p;. rw[б e,e,hv] . rw[Т e];exact h2 q hq (hS.resolve_left e) lemma qAo {S S':ћ->Prop} {out:ё} (h:C_Inv N sp S out) (hS:∀ q,q< N->S' q->S q):C_Inv N sp S' out :=⟨h.1,λq hq h'=>h.2 q hq (hS q hq h')⟩ lemma qB4 {S:ћ->Prop} {out:ё} (h:C_Inv N sp S out) (hS:∀ q,q< N->S q) : out=Array.ofFn (n:=N) λq=>sp q.val:=by obtain ⟨h1,h2⟩:=h;apply Array.ext;. simp[h1] . intro q h3 h4;have h5:q< N :=א;have:=h2 q h5 (hS q h5);rw[Ш out q h3] at this;simp[this] end lemma qE0 (v:ѓ) (h1:(0x7FFFFF:ѓ)<=v-1) (h2:v<Р) : (v == 0 || v-(0x00800000:ѓ)<=(0x7EFFFFFF:ѓ))=true:=by have a1:(0x7FFFFF:ѓ).toNat=0x7FFFFF:=rfl;have a2:(Р:ѓ).toNat=Р:=rfl;have a3:(0x00800000:ѓ).toNat=0x00800000:=rfl;have a4:(0x7EFFFFFF:ѓ).toNat=0x7EFFFFFF:=rfl;have a5:(1:ѓ).toNat=1:=rfl;by_cases h0:v=0;. simp[h0] . have h3:(1:ѓ)<=v:=by rw[Ж];have:Ѡv≠0:=λh=>h0 (П.mp h);omega rw[Ж,ش _ _ h3] at h1;rw[У] at h2;have h4:(0x00800000:ѓ)<=v:=by rw[Ж];omega simp only[or_eq_true,decide_eq_true_eq];right;rw[Ж,ش _ _ h4];omega lemma qI1 (c u lo:ѓ) (h:c<=if u< lo then u else lo):c<=u∧c<=lo:=by simp only[UInt32.le_iff_toNat_le,UInt32.lt_iff_toNat_lt] at *;split at h<;>omega lemma qIf (c v hi:ѓ) (h:(if hi< v then v else hi)< c):v< c∧hi< c:=by simp only[UInt32.lt_iff_toNat_lt] at *;split at h<;>omega lemma qDn (w:ѓ) (h:good w=true):good (w &&& Б)=true:=by have e:(w &&& Б) &&& Б=w &&& Б:=by rw[UInt32.and_assoc,UInt32.and_self] unfold good at h ⊢;simpa only [e] using h lemma qCu {h:ѓ} (h1:h<Р):h<=Б:=by have a1:(Р:ѓ).toNat=Р:=rfl;have a2:(Б:ѓ).toNat=Б:=rfl;rw[У] at h1;rw[Ж];omega def C_Hv (A:ё) (h:њ):Prop := good h=true∧h<=Б∧(∀ k,k< A.size->good A[k]! = true) ∧ ∀ k,k< A.size->A[k]! &&& Б<=h lemma qF1 (r v hi:ѓ) (h:(if hi< v then v else hi)<=r):v<=r∧hi<=r:=by simp only[UInt32.le_iff_toNat_le,UInt32.lt_iff_toNat_lt] at *;split at h<;>omega lemma qHx (A:ѐ) (n:є) (hn:Ѡn<=A.size) (d:ћ) : ∀ (i:є) (lo hi:ѓ),Ѡi+d=Ѡn->scan A n i hn lo hi≠Д → (0x7FFFFF:ѓ)<=lo∧hi<Р∧hi<=scan A n i hn lo hi ∧ scan A n i hn lo hi<Р∧(good hi=true->good (scan A n i hn lo hi)=true) ∧ ∀ k,Ѡi<=k->k< Ѡn → good A[k]! = true∧A[k]! &&& Б<=scan A n i hn lo hi:=by induction d with | zero => intro i lo hi he h have e:scan A n i hn lo hi =if 0x007FFFFF<=lo∧hi<Р then hi else Д:=by rw[scan,О ט] rw[e] at h ⊢ by_cases c:0x007FFFFF<=lo∧hi<Р . rw[б c];exact ⟨c.1,c.2,UInt32.le_refl _,c.2,id,λk h1 h2=>by omega⟩ . rw[Т c] at h;exact absurd rfl h | succ d ih => intro i lo hi he h have hi':i< n:=by rw[А];omega have hb:Ѡi< A.size :=א have e:scan A n i hn lo hi=scan A n (i+1) hn (if (A.uget i hb &&& Б)-1< lo then (A.uget i hb &&& Б)-1 else lo) (if hi< A.uget i hb &&& Б then A.uget i hb &&& Б else hi):=by rw[scan,К hi'] rw[e] at h ⊢ obtain ⟨h1,h2,h3,h4,h5,h6⟩:=ih (i+1) _ _ (by rw[usz i n hi'];omega) h obtain ⟨h7,h8⟩:=qI1 _ _ _ h1 obtain ⟨h9,h10⟩:=qIf _ _ _ h2 obtain ⟨h11,h12⟩:=qF1 _ _ _ h3 have hg:good (A.uget i hb)=true:=qE0 _ h7 h9 refine ⟨h8,h10,h12,h4,λg0=>h5 ?_,λk hk1 hk2=>?_⟩ . split;. exact qDn _ hg . exact g0 . by_cases e2:k=Ѡi;. subst e2;rw[Ш A Ѡi hb];exact ⟨hg,h11⟩ . exact h6 k (by rw[usz i n hi'];omega) hk2 lemma qBa (A:ѐ) (hs:A.size<2^32) (h:scan A A.usize 0 (qAe A) Д 0≠Д) : C_Hv A (scan A A.usize 0 (qAe A) Д 0):=by have e:=qBo A hs;have h0:(0:є).toNat=0:=rfl;obtain ⟨-,-,-,h4,h5,h6⟩ :=qHx A A.usize (qAe A) A.size 0 _ _ (by rw[h0,e];omega) h;have h7:∀ k,k< A.size->good A[k]! = true ∧ A[k]! &&& Б<=scan A A.usize 0 (qAe A) Д 0 :=λk hk=>h6 k א א;exact ⟨h5 rfl,qCu h4,λk hk=>(h7 k hk).1,λk hk=>(h7 k hk).2⟩ lemma qG5 (a b:ѓ):a<=a ||| b∧b<=a ||| b:=by rw[Ж,Ж,UInt32.toNat_or];exact ⟨Nat.left_le_or,Nat.right_le_or⟩ lemma qBj (v:ѓ) (h1:v=0∨0x800000<=v) (h2:v<Р) : (v == 0 || v-(0x00800000:ѓ)<=(0x7EFFFFFF:ѓ))=true:=by rcases h1 with rfl | h1;. rfl . have a1:(Р:ѓ).toNat=Р:=rfl;have a2:(0x00800000:ѓ).toNat=0x00800000:=rfl;have a3:(0x7EFFFFFF:ѓ).toNat=0x7EFFFFFF:=rfl;simp only[or_eq_true,decide_eq_true_eq];right;rw[Ж,ش _ _ h1];rw[У] at h2;rw[Ж] at h1;omega lemma qFq (a b:ѓ):a &&& b<=a∧a &&& b<=b:=by rw[Ж,Ж,е];exact ⟨Nat.and_le_left,Nat.and_le_right⟩ lemma qGw (v:ѓ) (h:0x7FFFFF<=v-1):v=0∨0x800000<=v:=by by_cases z:v=0;. exact .inl z . right;have a1:(0x7FFFFF:ѓ).toNat=0x7FFFFF:=rfl;have a2:(0x800000:ѓ).toNat=0x800000:=rfl;have a3:(1:ѓ).toNat=1:=rfl;have a4:Ѡv≠0:=λe=>z (П.mp (by rw[e];rfl));have a5:=v.toNat_lt;have h1:=Ж.mp h;rw[a1,UInt32.toNat_sub,a3] at h1;rw[Ж,a2];omega lemma qGj (A:ѐ) (n:є) (hn:Ѡn<=A.size) (d:ћ) : ∀ (i:є) (o s:ѓ),Ѡi+d=Ѡn->scanF A n i hn o s≠Д → o<=scanF A n i hn o s∧scanF A n i hn o s<Р∧0x7FFFFF<=s ∧ ((o=0∨0x800000<=o)->scanF A n i hn o s=0∨0x800000<=scanF A n i hn o s) ∧ ∀ k,Ѡi<=k->k< Ѡn → good A[k]! = true∧A[k]! &&& Б<=scanF A n i hn o s:=by induction d with | zero => intro i o s he h rw[scanF,О ט] at h ⊢ by_cases c:0x7FFFFF<=s∧o<Р . rw[б c];exact ⟨UInt32.le_refl _,c.2,c.1,id,λk h1 h2=>by omega⟩ . rw[Т c] at h;exact absurd rfl h | succ d ih => intro i o s he h have hi':i< n:=by rw[А];omega have hb:Ѡi< A.size :=א rw[scanF,К hi'] at h ⊢ obtain ⟨h1,h2,h3,h4,h5⟩:=ih (i+1) _ _ (by rw[usz i n hi'];omega) h obtain ⟨h6,h7⟩:=qG5 o _ obtain ⟨h8,h9⟩:=qFq s ((A.uget i hb &&& Б)-1) have hv:=qGw _ (э h3 h9) have hl:=э h7 h1 refine ⟨э h6 h1,h2,э h3 h8,λg0=>h4 ?_,λk hk1 hk2=>?_⟩ . rcases hv with hv | hv . rcases g0 with g0 | g0;. left;rw[g0,hv];rfl . right;exact э g0 h6 . right;exact э hv h7 . by_cases e2:k=Ѡi;. subst e2;rw[Ш A Ѡi hb];exact ⟨qBj _ hv (UInt32.lt_of_le_of_lt hl h2),hl⟩ . exact h5 k (by rw[usz i n hi'];omega) hk2 lemma qDu (A:ѐ) (hs:A.size<2^32) (h:scanF A A.usize 0 (qAe A) 0 Д≠Д) : C_Hv A (scanF A A.usize 0 (qAe A) 0 Д):=by have e:=qBo A hs;have h0:(0:є).toNat=0:=rfl;obtain ⟨-,h2,-,h4,h5⟩ :=qGj A A.usize (qAe A) A.size 0 _ _ (by rw[h0,e];omega) h;generalize scanF A A.usize 0 (qAe A) 0 Д=S at *;have hp:=qCu h2;have hm:S &&& Б=S:=by have a:(Б:ѓ).toNat=2^31-1:=rfl;apply П.mp;rw[Ж,a] at hp;rw[е,a,М];omega have hg:good S=true:=by unfold good;dsimp only;rw[hm];exact qBj _ (h4 (Or.inl rfl)) h2 exact ⟨hg,hp,λk hk=>(h5 k א א).1,λk hk=>(h5 k א א).2⟩ lemma qA8 (A:ѐ) (hs:A.size<2^32) (h:scanH A≠Д) : C_Hv A (scanH A):=by revert h;unfold scanH;dsimp only;split;. exact qBa A hs . exact qDu A hs def C_out (I:ћ) (Y:ј) (X W b:ё) (i j:ћ):њ := act Y (dotOf (I+1) (λt=>if t< I then X[i*I+t]! else oneW) (λt=>if t< I then W[j*I+t]! else b[j]!)) structure C_G (R I O:ћ) (X W b:ё):Prop where hX:R*I=X.size hW:O*I=W.size hb:O=b.size pi:0< I po:0< O ni:I<2^20 sX:X.size<2^32 sW:W.size<2^32 sb:b.size<2^32 sO:R*O<2^32 gX:∀ q,q< X.size->good X[q]! = true gW:∀ q,q< W.size->good W[q]! = true gb:∀ q,q< b.size->good b[q]! = true lemma qAv {R I i t:ћ} (hi:i< R) (ht:t< I):i*I+t< R*I:=by have h:(i+1)*I<=R*I:=Н _ hi;rw[غ,م] at h;omega lemma qAt {R I i t:ћ} (hi:i< R) (ht:t<=I) : i*I+t<=R*I:=by have h:(i+1)*I<=R*I:=Н _ hi;rw[غ,م] at h;omega section variable {R I O:ћ} {X W b:ё} section variable (G:C_G R I O X W b) include G lemma C_G.gx {i:ћ} (hi:i< R) : ∀ t,t< I->good X[i*I+t]! = true :=λ_ ht=>G.gX _ (by rw[<-G.hX];exact qAv hi ht) lemma C_G.gw {j:ћ} (hj:j< O) : ∀ t,t< I->good W[j*I+t]! = true :=λ_ ht=>G.gW _ (by rw[<-G.hW];exact qAv hj ht) lemma qIw (Y:ј) {i j:ћ} (hi:i< R) (hj:j< O) {s e:ѕ} (hs:let r:=(Х I).foldl (λ(acc:F1) t => let q:=dv X[i*I+t]! * dv W[j*I+t]! F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩;s=r.s∧e=r.t) : fin I Y X W b i j s (nfOf I) e b[j]! = C_out I Y X W b i j:=by obtain ⟨h1,h2⟩:=hs unfold fin dsimp only split . unfold slowOut C_out;rw[qAc] . rename_i h;unfold C_out;congr 1;exact qBt I G.ni (λt=>X[i*I+t]!) (λt=>W[j*I+t]!) b[j]! (G.gx hi) (G.gw hj) (G.gb j (by rw[<-G.hb];exact hj)) _ ه (λt ht=>у.mp ht) (λt ht h3=>absurd (у.mpr ht) h3) _ _ ⟨by rw[h1],by rw[h2]⟩ (by simpa using h) end lemma qE3 {X W:ѐ} {xo wo n:є} (hX:X.size<2^32) (hW:W.size<2^32) (h1:Ѡxo+Ѡn<=X.size) (h2:Ѡwo+Ѡn<=W.size) (d:ћ):∀ (t:є) (s a:ѕ),Ѡt+d=Ѡn → tightGo X W xo wo n t s a=(List.range' Ѡt d).foldl (λ(acc:F1) k =>let q:=dv X[Ѡxo+k]! * dv W[Ѡwo+k]! F1.mk (acc.s+q) (acc.t+q.abs)) ⟨s,a⟩:=by induction d with | zero => intro t s a he rw[tightGo,О ט] rfl | succ d ih => intro t s a he have ht:t< n:=by rw[А];omega rw[tightGo,К ht,و,ؽ] dsimp only rw[qAk _ _ hX,qAk _ _ hW,qAg _ _ א,qAg _ _ א] rw[ih (t+1) _ _ (by rw[usz t n ht];omega),usz t n ht] section variable (G:C_G R I O X W b) (Y:ј) {i j:ћ} (hi:i< R) (hj:j< O) include G Y i j hi hj lemma qGl {s a:ѕ} {hh:њ} (hc:ziv (s+dv b[j]!) (nfOf I*(dv hh*a+(dv b[j]!).abs))≠Д → ziv (s+dv b[j]!) (nfOf I*(dv hh*a+(dv b[j]!).abs)) = dotOf (I+1) (λt=>if t< I then X[i*I+t]! else oneW) (λt=>if t< I then W[j*I+t]! else b[j]!)) : fin8 I Y X W b i j s (nfOf I) a hh b[j]! I.toUSize=C_out I Y X W b i j:=by have hin:I<2^32:=lt_trans G.ni ב have hX1:i*I+I<=X.size:=by rw[<-G.hX];exact qAt hi (le_refl _) have hW1:j*I+I<=W.size:=by rw[<-G.hW];exact qAt hj (le_refl _) have sX:=G.sX have sW:=G.sW have e1:(i*I).toUSize.toNat=i*I:=qAb _ א have e2:(j*I).toUSize.toNat=j*I:=qAb _ א have e3:I.toUSize.toNat=I:=qAb _ hin unfold fin8 dsimp only split . have e:=qE3 (xo:=(i*I).toUSize) (wo:=(j*I).toUSize) (n:=I.toUSize) G.sX G.sW (by rw[e1,e3];exact hX1) (by rw[e2,e3];exact hW1) I 0 0 0 (by rw[e3,C_z,И]);rw[C_z,e1,e2,<-ع] at e;rw[qIw G Y hi hj (s:=(tightGo X W (i*I).toUSize (j*I).toUSize I.toUSize 0 0 0).s) (e:=(tightGo X W (i*I).toUSize (j*I).toUSize I.toUSize 0 0 0).t) ⟨by rw[e],by rw[e]⟩] . rename_i h;unfold C_out;congr 1;exact hc (by simpa using h) lemma qD0 {s:ѕ} {hv:њ} (g1:good hv=true) (g2:hv<=Б) (g4:∀ t,t< I->W[j*I+t]! &&& Б<=hv) (L:љ) (hl:L.Nodup) (hl2:∀ t∈L,t< I) (hz:∀ t,t< I->t∉L->X[i*I+t]! &&& Б=0) (hs:s=L.foldl (λ(acc:ѕ) t=>acc+dv X[i*I+t]! * dv W[j*I+t]!) 0) : fin8 I Y X W b i j s (nfOf I) (L.foldl (λ(a:ѕ) t=>a+(dv X[i*I+t]!).abs) 0) hv b[j]! I.toUSize=C_out I Y X W b i j:=by have gb:=G.gb j (by rw[<-G.hb];exact hj) exact qGl G Y hi hj λh=>by obtain ⟨-,-,k⟩:=qBn _ _ h obtain ⟨c1,c2⟩:=qDb I G.ni (λt=>X[i*I+t]!) (λt=>W[j*I+t]!) b[j]! hv (G.gx hi) (G.gw hj) gb g1 g4 g2 L hl hl2 hz _ _ ⟨by rw[hs],rfl⟩ k exact qCo I G.ni _ _ _ (G.gx hi) (G.gw hj) gb _ _ c1 c2 h end end lemma qBw (x:ђ) (i:є) (h:Ѡi< x.size) : x.uget i h=x.data[Ѡi]!:=by obtain ⟨ds⟩:=x;have h':Ѡi< ds.size:=h;rw[Ш ds Ѡi h'];rfl lemma qCm (a:ѐ) (i:є) (h:Ѡi< a.size) : a.uget i h=a[Ѡi]!:=by rw[Ш a Ѡi h];rfl lemma qEk (a:ђ) (i:є) (v:ѕ):(wrF a i v).size=a.size:=by unfold wrF;split;. obtain ⟨ds⟩:=a;exact Array.size_uset _ . rfl lemma qE5 (a:ђ) (i:є) (v:ѕ) (q:ћ) (hs:a.size<2^32) (hi:Ѡi< a.size) : (wrF a i v).data[q]! = if q=Ѡi then v else a.data[q]!:=by have h:i< a.usize:=by rw[А,FloatArray.usize];rwa [qAb _ hs] unfold wrF;rw[К h];obtain ⟨ds⟩:=a;exact qGg ds _ q v hi def C_keep (xw:ћ->њ) (t:ћ):љ := (Х t).filter (λp=>xw p &&& Б != 0) section variable (xw:ћ->њ) (t:ћ) include xw t lemma qBb:(C_keep xw t).length<=t:=by have h:=List.length_filter_le (λp=>xw p &&& Б != 0) (Х t);simpa [C_keep] using h lemma qDq (h:xw t &&& Б=0) : C_keep xw (t+1)=C_keep xw t:=by simp[C_keep,List.range_succ,List.filter_append,h] lemma qEp (h:¬ xw t &&& Б=0) : C_keep xw (t+1)=C_keep xw t ++ [t]:=by simp[C_keep,List.range_succ,List.filter_append,h] end def C_Dec (xw:ћ->њ) (sz t:ћ) (ix:ѐ) (buf:ђ) (c:є) (l:ѕ):Prop := Ѡc=(C_keep xw t).length∧ix.size=sz∧buf.size=sz ∧ (∀ k p:ћ,(C_keep xw t)[k]?=some p → (ix[k]! : ѓ).toNat=p∧buf.data[k]! = dv (xw p)) ∧ l=(C_keep xw t).foldl (λ(a:ѕ) p=>a+(dv (xw p)).abs) 0 lemma qH5 {X:ѐ} {o n:є} (xw:ћ->њ) (hn:Ѡn<2^32) (hxw:∀ t:є,t< n->rdW X (o+t)=xw Ѡt) (hg:∀ t,t< Ѡn->good (xw t)=true) (d:ћ) : ∀ (t:є) (ix:ѐ) (buf:ђ) (c:є) (hh:ѕ),Ѡt+d=Ѡn->C_Dec xw Ѡn Ѡt ix buf c hh → C_Dec xw Ѡn Ѡn (decR X o n t ix buf c hh).ix (decR X o n t ix buf c hh).buf (decR X o n t ix buf c hh).c (decR X o n t ix buf c hh).l:=by induction d with | zero => intro t ix buf c hh he H rw[decR,О ט] have e:Ѡt=Ѡn :=א rw[e] at H;exact H | succ d ih => intro t ix buf c hh he H have ht:t< n:=by rw[А];omega have hu:=usz t n ht rw[decR,К ht] dsimp only rw[hxw t ht] obtain ⟨h1,h2,h3,h4,h5⟩:=H by_cases hz:xw Ѡt &&& Б=0 . rw[б (by simpa using hz)];apply ih (t+1) _ _ _ _ א;rw[hu,C_Dec,qDq xw _ hz];exact ⟨h1,h2,h3,h4,h5⟩ . rw[Т (by simpa using hz)];apply ih (t+1) _ _ _ _ א;have hk:=qBb xw Ѡt;have hc:Ѡc< Ѡt+1 :=א;rw[hu,C_Dec,qEp xw _ hz];have hcn:Ѡc< Ѡn :=א;refine ⟨?_,by rw[qAx];exact h2,by rw[qEk];exact h3,λk p hk2=>?_,by rw[List.foldl_append,<-h5];rfl⟩;. rw[qAg _ _ (by rw[ь];omega),ь,h1];simp . rw[qA4 _ _ _ _ א א,qE5 _ _ _ _ א א];rw[List.getElem?_append,<-h1] at hk2;by_cases e:k=Ѡc;. subst e;simp at hk2;subst hk2;simp;omega . rw[Т e,Т e];by_cases e2:k< Ѡc;. rw[б e2] at hk2;exact h4 k p hk2 . rw[Т e2] at hk2;obtain ⟨m,hm⟩:∃ m,k-Ѡc=m+1:=⟨k-Ѡc-1,by omega⟩;rw[hm] at hk2;simp at hk2 lemma qA2 (a:ђ) (v:ѕ):(a.push v).size=a.size+1:=by obtain ⟨ds⟩:=a;exact Array.size_push _ lemma qCh (n:є) (d:ћ):∀ (k:є) (a:ђ),Ѡk+d=Ѡn → (pushZ n k a).size=a.size+d:=by induction d with | zero => intro k a he;rw[pushZ,О ט];rfl | succ d ih => intro k a he;have ht:k< n:=by rw[А];omega rw[pushZ,К ht,ih (k+1) _ (by rw[usz k n ht];omega),qA2];omega lemma qI5 {x:ђ} {ix W:ѐ} {w0 m n:є} (hx:Ѡn<=x.size) (hi:Ѡn<=ix.size) (hs:W.size<2^32) (h0:Ѡw0+Ѡm<=W.size) (xf:ћ->ѕ) (ww:ћ->њ) (hww:∀ p,p< Ѡm->W[Ѡw0+p]! = ww p) (L:љ):∀ (t:є) (a:ѕ),Ѡt+L.length=Ѡn → (∀ k p:ћ,L[k]?=some p->p< Ѡm∧(ix[Ѡt+k]! : ѓ).toNat=p ∧ x.data[Ѡt+k]! = xf p) → go1 x ix W w0 m n t hx hi hs h0 a =L.foldl (λ(acc:ѕ) p=>acc+xf p*dv (ww p)) a:=by induction L with | nil => intro t a he _ rw[go1,О (by rw[А];simp at he;omega)] rfl | cons p L ih => intro t a he H simp only[List.length_cons] at he have ht:t< n:=by rw[А];omega have htn:Ѡt< Ѡn :=א obtain ⟨h1,h2,h3⟩:=H 0 p rfl rw[р] at h2 h3 have hit:Ѡt< ix.size :=א have e0:(ix.uget t hit).toUSize.toNat=p:=by rw[Ы,qCm];exact h2 have hm:(ix.uget t hit).toUSize< m:=by rw[А,e0];exact h1 have e1:x.uget t א=xf p:=by rw[qBw,h3] have e2:∀ pf,W.uget (w0+(ix.uget t hit).toUSize) pf=ww p:=by intro pf rw[qCm,qAg _ _ (by rw[e0];omega),e0,hww p h1] rw[go1,К ht] dsimp only rw[К hm] rw[e1,e2,ih (t+1) _ (by rw[usz t n ht];omega),ؽ] intro k q hq have:=H (k+1) q (by simpa using hq) rw[usz t n ht,show Ѡt+1+k=Ѡt+(k+1) by omega] exact this def C_Dw (wd:ђ) (W:ё):Prop := wd.size=W.size->∀ q,q< W.size->wd.data[q]! = dv W[q]! section variable {x:ђ} {ix W:ѐ} {w0 w1 w2 w3 m n:є} (hx:Ѡn<=x.size) (hi:Ѡn<=ix.size) (hs:W.size<2^32) (h0:Ѡw0+Ѡm<=W.size) (h1:Ѡw1+Ѡm<=W.size) (h2:Ѡw2+Ѡm<=W.size) (h3:Ѡw3+Ѡm<=W.size) lemma qIp (hv:∀ j,j< Ѡn->∃ w,good w=true∧x.data[j]! = dv w) (k:ћ) : ∀ (t:є) (a b c d:ѕ),Ѡt+k=Ѡn → go4B x ix W w0 w1 w2 w3 m n t hx hi hs h0 h1 h2 h3 a b c d = ⟨go1 x ix W w0 m n t hx hi hs h0 a,go1 x ix W w1 m n t hx hi hs h1 b,go1 x ix W w2 m n t hx hi hs h2 c,go1 x ix W w3 m n t hx hi hs h3 d⟩:=by induction k with | zero => intro t a b c d he have hn:¬ t< n:=by rw[А];omega unfold go4B go1 simp only[dite_eq_right hn] | succ k ih => intro t a b c d he have ht:t< n:=by rw[А];omega have htn:Ѡt< Ѡn :=א unfold go4B go1 simp only[dite_eq_left ht] obtain ⟨w,g,e⟩:=hv _ htn have B:∀ (u:ѓ) p0,dvB (x.uget t p0) u=x.uget t p0*dv u:=by intro u p0 rw[qBw,e] exact qIq w u g have J:=λa b c d=>ih (t+1) a b c d (by rw[usz t n ht];omega) split<;>simp only[B,J] lemma qH2 {wd:ђ} (hd:wd.size=W.size) (hv:C_Dw wd W) (k:ћ) : ∀ (t:є) (a b c d:ѕ),Ѡt+k=Ѡn → go4D x wd ix W w0 w1 w2 w3 m n t hx hi hs h0 h1 h2 h3 hd a b c d = ⟨go1 x ix W w0 m n t hx hi hs h0 a,go1 x ix W w1 m n t hx hi hs h1 b,go1 x ix W w2 m n t hx hi hs h2 c,go1 x ix W w3 m n t hx hi hs h3 d⟩:=by induction k with | zero => intro t a b c d he have hn:¬ t< n:=by rw[А];omega unfold go4D go1 simp only[dite_eq_right hn] | succ k ih => intro t a b c d he have ht:t< n:=by rw[А];omega have htn:Ѡt< Ѡn :=א unfold go4D go1 simp only[dite_eq_left ht] have B:∀ (u:є) p1,dv (W.uget u p1)=wd.uget u (hd ▸ p1):=λu p1=>by rw[qBw,hv hd _ p1,qCm] have J:=λa b c d=>ih (t+1) a b c d (by rw[usz t n ht];omega) split<;>simp only[B,J] end def C_sp (I O:ћ) (Y:ј) (X W b:ё) (q:ћ):њ := C_out I Y X W b (q/O) (q%O) lemma qD1 (I O:ћ) (Y:ј) (X W b:ё) {i j:ћ} (hj:j< O) : C_sp I O Y X W b (i*O+j)=C_out I Y X W b i j:=by unfold C_sp;have h0:0< O :=א;rw[Ю i O,ؾ h0,ؿ,д hj,З hj,р] lemma qAj {N:ћ} {sp:ћ->њ} {p:ћ} {out:ё} (hN:N<2^32) (h:C_Inv N sp (λq=>q< p) out) {u:є} {v:њ} (hu:Ѡu=p) (hp:p< N) (hv:v=sp p):C_Inv N sp (λq=>q< p+1) (wrW out u v) := qAo (qDj hN h hu hp hv) (λq _ (hq:q< p+1) =>(by omega:q=p∨q< p)) lemma qG7 {u:є} {p:ћ} (hu:Ѡu=p) (h:p+3<2^32) : (u+1).toNat=p+1∧(u+2).toNat=p+2∧(u+3).toNat=p+3 ∧ (p+4<2^32->(u+4).toNat=p+4):=by have l:∀ k,k<5->p+k<2^32->(u+Е k).toNat=p+k:=λk hk h=>by rw[qAg _ _ (by rw[uk k א];omega),uk k א,hu] exact ⟨l 1 א א,l 2 א א,l 3 א א,l 4 א⟩ lemma qHz {N:ћ} {sp:ћ->њ} {p:ћ} {out:ё} (hN:N<2^32) (h:C_Inv N sp (λq=>q< p) out) {u:є} {v0 v1 v2 v3:њ} (hu:Ѡu=p) (hp:p+4<=N) (h0:v0=sp p) (h1:v1=sp (p+1)) (h2:v2=sp (p+2)) (h3:v3=sp (p+3)):C_Inv N sp (λq=>q< p+4) (wrW (wrW (wrW (wrW out u v0) (u+1) v1) (u+2) v2) (u+3) v3):=by obtain ⟨a1,a2,a3,-⟩:=qG7 hu א exact qAj hN (qAj hN (qAj hN (qAj hN h hu א h0) a1 א h1) a2 א h2) a3 א h3 lemma qEv {X:ѐ} {a n:ћ} (hX:X.size<2^32) (hn:a+n<=X.size) (hg:∀ t,t< n->good X[a+t]! = true) {ix:ѐ} {buf:ђ} (h1:ix.size=n) (h2:buf.size=n) : C_Dec (λp=>X[a+p]!) n n (decR X a.toUSize n.toUSize 0 ix buf 0 0).ix (decR X a.toUSize n.toUSize 0 ix buf 0 0).buf (decR X a.toUSize n.toUSize 0 ix buf 0 0).c (decR X a.toUSize n.toUSize 0 ix buf 0 0).l:=by have eo:a.toUSize.toNat=a:=qAb _ א have ek:n.toUSize.toNat=n:=qAb _ א have D:=qH5 (X:=X) (o:=a.toUSize) (n:=n.toUSize) (λp=>X[a+p]!) (by rw[ek];omega) (λt ht=>by have h4:=А.mp ht rw[ek] at h4 rw[qAk _ _ hX,qAg _ _ (by rw[eo];omega),eo]) (λt ht=>hg t (by rwa [ek] at ht)) n 0 ix buf 0 0 (by rw[C_z,ek,И]) ⟨by simp[C_keep],by rw[ek];exact h1,by rw[ek];exact h2,λk p hk=>by simp[C_keep] at hk,by simp[C_keep]⟩ rw[ek] at D exact D lemma qDw (xw:ћ->њ) (n:ћ):(C_keep xw n).Nodup∧(∀ t∈C_keep xw n,t< n) ∧ ∀ t,t< n->t∉C_keep xw n->xw t &&& Б=0 := ⟨ه.filter _,λt ht=>у.mp (List.mem_filter.mp ht).1, λt ht hn=>by by_contra hc exact hn (List.mem_filter.mpr ⟨у.mpr ht,by simpa using hc⟩)⟩ structure C_Row (I:ћ) (X:ё) (i:ћ) (ix:ѐ) (x:ђ) (c:є) (L:љ) (l:ѕ):Prop where len:Ѡc=L.length cle:L.length<=I six:ix.size=I sx:x.size=I nd:L.Nodup lt:∀ t∈L,t< I hz:∀ t,t< I->t∉L->X[i*I+t]! &&& Б=0 rd:∀ k p:ћ,L[k]?=some p → (ix[k]! : ѓ).toNat=p∧x.data[k]! = dv X[i*I+p]! ok:l=L.foldl (λ(a:ѕ) t=>a+(dv X[i*I+t]!).abs) 0 section variable {R I O:ћ} {X W b:ё} section variable (G:C_G R I O X W b) include G lemma C_G.nm:I.toUSize.toNat=I :=qAb _ (lt_trans G.ni ב) lemma C_G.wj {j:ћ} {u:є} (hu:Ѡu=j) (hj:j< O):(u*I.toUSize).toNat=j*I ∧ (u*I.toUSize).toNat+I.toUSize.toNat<=W.size:=by have h1:j*I+I<=O*I:=qAt hj (le_refl _) have h2:=G.hW have h3:=G.sW have e:(u*I.toUSize).toNat=j*I:=by rw[qCd _ _ (by rw[hu,G.nm];omega),hu,G.nm] exact ⟨e,by rw[e,G.nm];omega⟩ lemma qB9 (Y:ј) {i j:ћ} (hi:i< R) (hj:j< O) {hv:њ} (Hv:C_Hv W hv) {ix:ѐ} {x:ђ} {c:є} {L:љ} {l:ѕ} (Rw:C_Row I X i ix x c L l) {u:є} (hu:Ѡu=j) (hx:Ѡc<=x.size) (hi':Ѡc<=ix.size) (hs:W.size<2^32) (h0:(u*I.toUSize).toNat+I.toUSize.toNat<=W.size) : fin8 I Y X W b i j (go1 x ix W (u*I.toUSize) I.toUSize c 0 hx hi' hs h0 0) (nfOf I) l hv (rdW b u) I.toUSize=C_out I Y X W b i j:=by have e:=qI5 hx hi' hs h0 (λp=>dv X[i*I+p]!) (λp=>W[j*I+p]!) (λp _=>by rw[(G.wj hu hj).1]) L 0 0 (by rw[C_z,И];exact Rw.len.symm) (λk p hk=>by have h1:=Rw.rd k p hk rw[C_z,И,G.nm] exact ⟨Rw.lt p (Ա hk),h1⟩) rw[qAk _ _ G.sb,hu,Rw.ok] exact qD0 G Y hi hj Hv.1 Hv.2.1 (λt ht=>Hv.2.2.2 _ (by rw[<-G.hW];exact qAv hj ht)) L Rw.nd Rw.lt Rw.hz e lemma qG1 (Y:ј) {i:ћ} (hi:i< R) {hv:њ} (Hv:C_Hv W hv) {ix:ѐ} {x wd:ђ} {c:є} {L:љ} {l:ѕ} (Rw:C_Row I X i ix x c L l) {base:є} (hb:Ѡbase=i*O) (Hd:C_Dw wd W) (d:ћ):∀ (j:ћ) (out:ё),j+d=O → C_Inv (R*O) (C_sp I O Y X W b) (λq=>q< i*O+j) out → C_Inv (R*O) (C_sp I O Y X W b) (λq=>q< i*O+O) (cols1 I O Y X W b i ix x wd (nfOf I) l hv I.toUSize c j base out):=by induction d using Nat.strong_induction_on with | _ d ih => intro j out he H have hN:=G.sO have sW:W.size<=4294967295:=by have:=G.sW;omega have hbs:=G.sb have hob:=G.hb have hle:=Rw.cle have h1c:Ѡc<=x.size:=by rw[Rw.sx,Rw.len];exact hle have h2c:Ѡc<=ix.size:=by rw[Rw.six,Rw.len];exact hle have hsp:∀ k,j+k< O->C_out I Y X W b i (j+k) =C_sp I O Y X W b (i*O+j+k):=λk hk=>by rw[и];exact (qD1 I O Y X W b hk).symm by_cases h4:j+4<=O . have hP:=qAv hi (א:j+3< O);have e0:j.toUSize.toNat=j:=qAb _ א;obtain ⟨e1,e2,e3,-⟩:=qG7 e0 א;have g0:=(G.wj e0 א).2;have g1:=(G.wj e1 א).2;have g2:=(G.wj e2 א).2;have g3:=(G.wj e3 א).2;have a0:(base+j.toUSize).toNat=i*O+j:=by rw[qAg _ _ (by rw[hb,e0];omega),hb,e0] have o0:=qB9 G Y hi (א:j< O) Hv Rw e0 h1c h2c G.sW g0;have o1:=qB9 G Y hi (א:j+1< O) Hv Rw e1 h1c h2c G.sW g1;have o2:=qB9 G Y hi (א:j+2< O) Hv Rw e2 h1c h2c G.sW g2;have o3:=qB9 G Y hi (א:j+3< O) Hv Rw e3 h1c h2c G.sW g3;rw[cols1,К h4];dsimp only;rw[К ⟨h1c,h2c,sW,g0,g1,g2,g3⟩];split;rename_i hd;rw[qH2 _ _ _ _ _ _ _ hd Hd Ѡc 0 _ _ _ _ (И _)];swap rw[qIp _ _ _ _ _ _ _ (λj hj=>by rw[Rw.len] at hj;have hp:=List.getElem?_eq_getElem hj exact ⟨_,G.gx hi _ (Rw.lt _ (Ա hp)),(Rw.rd j _ hp).2⟩) Ѡc 0 _ _ _ _ (И _)] all_goals dsimp only;rw[o0,o1,o2,o3];refine ih (d-4) א (j+4) _ א ?_;rw[show i*O+(j+4)=i*O+j+4 by omega];exact qHz hN H a0 א (hsp 0 א) (hsp 1 א) (hsp 2 א) (hsp 3 א) . by_cases h1:j< O . have hP:=qAv hi h1;have e0:j.toUSize.toNat=j:=qAb _ א;have g0:=(G.wj e0 h1).2;have a0:(base+j.toUSize).toNat=i*O+j:=by rw[qAg _ _ (by rw[hb,e0];omega),hb,e0] have o0:=qB9 G Y hi h1 Hv Rw e0 h1c h2c G.sW g0;rw[cols1,О h4,К h1];dsimp only;rw[К ⟨h1c,h2c,sW,g0⟩];rw[o0];exact ih (d-1) א (j+1) _ א (qAj hN H a0 hP (hsp 0 א)) . rw[cols1,О h4,О h1];have e:j=O :=א;rw[e] at H;exact H lemma qD3 (Y:ј) {hv:њ} (Hv:C_Hv W hv) {wd:ђ} (Hd:C_Dw wd W) (d:ћ):∀ (i:ћ) (ix:ѐ) (buf:ђ) (out:ё),i+d=R->ix.size=I->buf.size=I → C_Inv (R*O) (C_sp I O Y X W b) (λq=>q< i*O) out → C_Inv (R*O) (C_sp I O Y X W b) (λq=>q< R*O) (rows1 R I O Y X W b wd (nfOf I) hv I.toUSize i ix buf out):=by induction d with | zero => intro i ix buf out he _ _ H rw[rows1,О א] have e:i=R :=א rw[e] at H;exact H | succ d ih => intro i ix buf out he h1 h2 H have hi:i< R :=א have hin:I<2^32:=lt_trans G.ni ב have hN:=G.sO have sX:=G.sX have hX:=G.hX have hP:i*I+I<=R*I:=qAt hi (le_refl _) have hbs:(i*O).toUSize.toNat=i*O:=by have h3:i*O+O<=R*O:=qAt hi (le_refl _) exact qAb _ א obtain ⟨d1,d2,d3,d4,d5⟩:=qEv sX א (G.gx hi) h1 h2 obtain ⟨k1,k2,k3⟩:=qDw (λp=>X[i*I+p]!) I have Rw:=C_Row.mk d1 (qBb _ _) d2 d3 k1 k2 k3 d4 d5 rw[rows1,К hi] dsimp only refine ih (i+1) _ _ _ א d2 d3 ?_ have h5:=qG1 G Y hi Hv Rw hbs Hd O 0 out א (by rw[р];exact H) rw[show (i+1)*O=i*O+O by rw[غ,م]] exact h5 end end lemma qEb (xs:ђ) (v:ѕ) (q:ћ) : (xs.push v).data[q]! = if q=xs.size then v else xs.data[q]!:=by obtain ⟨ds⟩:=xs;show (ds.push v)[q]! = if q=ds.size then v else ds[q]!;by_cases hq:q< ds.size . have h3:q<(ds.push v).size:=by simp;omega rw[Ш _ q h3,Ш ds q hq,Т א,Array.getElem_push_lt] . by_cases e:q=ds.size . subst e;have h3:ds.size<(ds.push v).size:=by simp rw[Ш (ds.push v) ds.size h3,б rfl,Array.getElem_push_eq] . have h3:¬ q<(ds.push v).size:=by simp;omega rw[س _ q h3,س ds q hq,Т e] lemma qH9 (l:List ѓ) (a:ђ) : (l.foldl (λa w=>a.push (dv w)) a).data=a.data ++ (l.map dv).toArray:=by induction l generalizing a with | nil=>simp | cons w l ih => rw[ؽ,ih];simp[FloatArray.push] lemma qIv (A:ѐ) (hs:A.size<2^32) : (A.foldl (λa w=>a.push (dv w)) (В A.size)).size=A.size∧∀ q,q< A.size → (A.foldl (λa w=>a.push (dv w)) (В A.size)).data[q]! = dv A[q]!:=by rw[<-Array.foldl_toList] constructor . have e:=qH9 A.toList (В A.size);show (A.toList.foldl (λa w=>a.push (dv w)) (В A.size)).data.size=A.size;rw[e];simp[FloatArray.emptyWithCapacity] . intro q hq;rw[qH9];simp[hq,FloatArray.emptyWithCapacity] lemma qJd (X:ѐ) (n e:є) (hs:X.size<2^32) (he:Ѡe<=8*Ѡn) (hl:8*Ѡn<2^32) (d:ћ):∀ (q:є) (a:ђ),Ѡq+d=Ѡe → a.size=Ѡq->(∀ p,p< Ѡq->a.data[p]! = dv X[p%8*Ѡn+p/8]!) → (wdQ X n e q a).size=Ѡe ∧ ∀ p,p< Ѡe->(wdQ X n e q a).data[p]! = dv X[p%8*Ѡn+p/8]!:=by induction d with | zero => intro q a h1 h2 h3 rw[wdQ,О ט] exact ⟨by omega,λp hp=>h3 p א⟩ | succ d ih => intro q a h1 h2 h3 have hq:q< e:=by rw[А];omega have hu:=usz q e hq have u8:=uk 8 ב have m1:(q%Е 8).toNat=Ѡq%8:=by rw[USize.toNat_mod,u8] have d1:(q/Е 8).toNat=Ѡq/8:=by rw[USize.toNat_div,u8] have c0:Ѡq%8*Ѡn<=7*Ѡn:=Н _ א have c1:Ѡq/8< Ѡn :=א have e0:=qC4 (q%Е 8) n (by rw[m1];omega) have e1:(q%Е 8*n+q/Е 8).toNat=Ѡq%8*Ѡn+Ѡq/8:=by rw[qAg _ _ (by rw[e0,m1,d1];omega),e0,m1,d1] rw[wdQ,К hq] have s1:∀ (b:ђ) (v:ѕ),(b.push v).size=b.size+1:=λb v=>by cases b;simp[FloatArray.push,FloatArray.size] refine ih (q+1) _ (by rw[hu];omega) (by rw[s1,hu,h2]) λp hp=>?_ rw[hu] at hp rw[qEb] split . rename_i c;rw[qAk _ _ hs,e1,c,h2] . exact h3 p א section variable {I O:ћ} {X W b:ё} lemma qHh (x:ѐ) (hx:x.size<2^32) (base nu:є) (hb:Ѡbase+Ѡnu<2^32) (d:ℕ) (i:є) (s t:ѕ) (h:Ѡi+d=Ѡnu) : lnSum x base nu i s t = ⟨((List.range' Ѡi d).map λj=>dv x[Ѡbase+j]!).foldl (.+·) s,((List.range' Ѡi d).map λj=>(dv x[Ѡbase+j]!).abs).foldl (.+·) t⟩:=by induction d generalizing i s t with | zero => rw[lnSum,dif_neg ט];simp | succ d ih => have hn:i< nu:=by rw[А];omega have e1:=usz i nu hn rw[lnSum,dif_pos hn,ih (i+1) _ _ (by rw[e1];omega),e1,qAk x _ hx,qAg base i א] simp[List.range'_succ] lemma qIt (hs:X.size<2^32) (hX:8*I<=X.size) (hI:I.toUSize.toNat=I) (k:ћ) (hk:k<8) : f8 (mkL X I.toUSize) k=(Х I).foldl (λ(a:ѕ) t=>a+(dv X[k*I+t]!).abs) 0:=by have key:∀ (u:є),Ѡu=k->(lnSum X (u*I.toUSize) I.toUSize 0 0 0).t =(Х I).foldl (λ(a:ѕ) t=>a+(dv X[k*I+t]!).abs) 0:=λu hu=>by have h1:(k+1)*I<=8*I:=Н _ hk;rw[غ,م] at h1;have e:(u*I.toUSize).toNat=k*I:=by rw[qCd _ _ (by rw[hu,hI];omega),hu,hI] rw[qHh X hs _ _ (by rw[e,hI];omega) I 0 0 0 (by rw[C_z,hI,И])];simp only[C_z,e,<-List.range_eq_range',List.foldl_map] interval_cases k<;>exact key _ (uk _ ב) lemma qJl {xs:ђ} {t n:є} {xf:ћ->ћ->ѕ} (hxf:∀ r k:ћ,r<8->k< Ѡn->xs.data[8*k+r]! = xf r k) (ht:Ѡt< Ѡn) (hsx:xs.size<2^32) (hx:8*Ѡn<=xs.size) (r:ћ) (hr:r<8) (pf:(Е 8*t+Е r).toNat< xs.size) : xs.uget (Е 8*t+Е r) pf=xf r Ѡt:=by have e2:=uk 8 ב;have e3:=uk r (א:r<2^32);have e:=qC4 (Е 8) t (by rw[e2];omega);rw[qBw,qAg _ _ (by rw[e,e2,e3];omega),e,e2,e3,hxf r _ hr ht] lemma qIg {xs:ђ} {W:ѐ} {wo n:є} (hx:8*Ѡn<=xs.size) (hsx:xs.size<2^32) (hs:W.size<2^32) (hw:Ѡwo+Ѡn<=W.size) (xf:ћ->ћ->ѕ) (ww:ћ->њ) (hxf:∀ r k:ћ,r<8->k< Ѡn->xs.data[8*k+r]! = xf r k) (hww:∀ k,k< Ѡn->W[Ѡwo+k]! = ww k) (d:ћ) : ∀ (t:є) (s0 s1 s2 s3 s4 s5 s6 s7:ѕ) (r0:F8),Ѡt+d=Ѡn->∀ r,r<8 → f8 (go8 xs W wo n t hx hsx hs hw s0 s1 s2 s3 s4 s5 s6 s7 r0) r = (List.range' Ѡt d).foldl (λ(acc:ѕ) k=>acc+xf r k*dv (ww k)) (f8 ⟨0,s0,s1,s2,s3,s4,s5,s6,s7⟩ r):=by induction d with | zero => intro t s0 s1 s2 s3 s4 s5 s6 s7 r0 he r hr rw[go8,О ט] cases r0 interval_cases r<;>rfl | succ d ih => intro t s0 s1 s2 s3 s4 s5 s6 s7 r0 he r hr have ht:t< n:=by rw[А];omega have htn:Ѡt< Ѡn :=א have hu:=usz t n ht have e:dv (W.uget (wo+t) (wi wo t n W hw hs htn))=dv (ww Ѡt):=by rw[qCm,qAg _ _ א,hww _ htn] rw[go8,К ht] dsimp only have Q:=qJl hxf htn hsx hx rw[e,Q 0 א,Q 1 א,Q 2 א,Q 3 א,Q 4 א,Q 5 א,Q 6 א,Q 7 א] rw[ih (t+1) _ _ _ _ _ _ _ _ _ א r hr,hu,و,ؽ] interval_cases r<;>rfl section variable {R:ћ} (G:C_G R I O X W b) (h8:8<=R) (Y:ј) {hv:њ} (Hv:C_Hv W hv) include R G h8 Y hv Hv lemma qHe {l r:F8} {j:ћ} (hj:j< O) (Hl:∀ k,k<8->f8 l k=(Х I).foldl (λ(a:ѕ) t=>a+(dv X[k*I+t]!).abs) 0) (Hr:∀ k,k<8->f8 r k =(Х I).foldl (λ(acc:ѕ) t=>acc+dv X[k*I+t]! * dv W[j*I+t]!) 0) (d:ћ) : ∀ (k:ћ) (out:ё),k+d=8 → C_Inv (R*O) (C_sp I O Y X W b) (λq=>q<8*O∧(q%O< j∨(q%O=j∧q/O< k))) out → C_Inv (R*O) (C_sp I O Y X W b) (λq=>q<8*O∧q%O< j+1) (out8 I O Y X W b (nfOf I) hv l r j b[j]! I.toUSize k out):=by have pO:=G.po induction d with | zero => intro k out he H rw[out8,г א] refine qAo H λq _ h=>⟨h.1,?_⟩ have h3:q/O<8:=с (by rw[Ю];exact h.1) by_cases c:q%O< j . exact .inl c . exact .inr ⟨by omega,by omega⟩ | succ d ih => intro k out he H have hk:k<8 :=א rw[out8,х hk] refine ih (k+1) _ א ?_ have hN:=G.sO have hp:k*O+j< R*O:=qAv א hj have W1:=qDj hN H (u:=(k*O+j).toUSize) (p:=k*O+j) (qAb _ א) hp (v:=fin8 I Y X W b k j (f8 r k) (nfOf I) (f8 l k) hv b[j]! I.toUSize) (by rw[qD1 I O Y X W b hj,Hl k hk] exact qD0 G Y א hj Hv.1 Hv.2.1 (λt ht=>Hv.2.2.2 _ (by rw[<-G.hW];exact qAv hj ht)) (Х I) ه (λt ht=>у.1 ht) (λt ht hn=>absurd (у.2 ht) hn) (Hr k hk)) refine qAo W1 λq _ h=>?_ rcases h.2 with h2 | ⟨h1,h2⟩ . exact .inr ⟨h.1,.inl h2⟩ . by_cases c:q/O=k;. left;have e:=Nat.div_add_mod' q O;rw[c,h1] at e;exact e.symm . exact .inr ⟨h.1,.inr ⟨h1,by omega⟩⟩ lemma qGx {xs:ђ} {l:F8} (hxs:xs.size=8*I) (Hx:∀ r t,r<8->t< I->xs.data[8*t+r]! = dv X[r*I+t]!) (Hl:∀ k,k<8->f8 l k=(Х I).foldl (λ(a:ѕ) t=>a+(dv X[k*I+t]!).abs) 0) (d:ћ):∀ (j:ћ) (r0:F8) (out:ё),j+d=O → C_Inv (R*O) (C_sp I O Y X W b) (λq=>q<8*O∧q%O< j) out → C_Inv (R*O) (C_sp I O Y X W b) (λq=>q<8*O∧q%O< O) (cols8 I O Y X W b xs l (nfOf I) hv I.toUSize j r0 out):=by have hnm:=G.nm induction d with | zero => intro j r0 out he H rw[cols8,О א] have e:j=O :=א rw[e] at H;exact H | succ d ih => intro j r0 out he H have hj:j< O :=א have hN:=G.sO have hO8:8*O<=R*O:=Н _ h8 have e0:j.toUSize.toNat=j:=qAb _ א obtain ⟨w1,w2⟩:=G.wj e0 hj have hx:8*I.toUSize.toNat<=xs.size:=by rw[hnm,hxs] have hX:=G.hX have hle:8*I<=R*I:=Н _ h8 have sX:=G.sX have sW:=G.sW have sx:xs.size<=4294967295:=by rw[hxs];omega have sw:W.size<=4294967295 :=א rw[cols8,К hj] dsimp only rw[К ⟨hx,sx,sw,w2⟩] refine ih (j+1) _ _ א ?_ rw[qAk _ _ G.sb,e0] refine qHe G h8 Y Hv hj Hl (λk hk=>?_) 8 0 out rfl (qAo H λq _ h=>⟨h.1,h.2.resolve_right λh2=>absurd h2.2 (ـ _)⟩) have L:=qIg hx (qA1 sx) (qA1 sw) w2 (λr k=>dv X[r*I+k]!) (λk=>W[j*I+k]!) (λr k hr hk=>by rw[hnm] at hk;exact Hx r k hr hk) (λk _=>by rw[w1]) I.toUSize.toNat 0 0 0 0 0 0 0 0 0 r0 (by rw[C_z,И]) k hk refine L.trans ?_ have Z:f8 ⟨0,0,0,0,0,0,0,0,0⟩ k=0:=by interval_cases k<;>rfl rw[C_z,hnm,<-ع,Z] end end lemma qEz:linF=linearSpec:=by funext rows inW outW relu X W b;unfold linF;dsimp only;have Hs:=qA8 W;generalize scanH W=hw at Hs ⊢;split . rename_i h;obtain ⟨h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13⟩:=h;replace h7:=qA1 h7;replace h8:=qA1 h8;replace h9:=qA1 h9;replace h10:=qA1 h10 have G0:=λ(A:∀ q,q< W.size->good W[q]! = true) => (⟨h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,(qA8 X h7 h11).2.2.1,A,(qA8 b h9 h13).2.2.1⟩:C_G rows inW outW X W b) have e:linearSpec rows inW outW relu X W b=Array.ofFn (n:=rows*outW) λq =>C_sp inW outW relu X W b q.val:=rfl;have hnf:(inW.toUInt64+8).toFloat*й 0x3CA0000000000000=nfOf inW:=rfl;rw[e,hnf];split . rename_i hr;obtain ⟨r1,r2⟩:=hr;have Hv:=Hs h8 h12;have G:=G0 Hv.2.2.1;have H0:C_Inv (rows*outW) (C_sp inW outW relu X W b) (λq=>q<8*outW∧q%outW<0) (Й (rows*outW) 0) :=⟨by simp,λq _ h=>absurd h.2 (ـ _)⟩;have eI:inW.toUSize.toNat=inW:=qAb _ א;have u8:=uk 8 ב;have hl:8*inW<=X.size:=by rw[<-h1];exact Н _ r1 have e8:(Е 8*inW.toUSize).toNat=8*inW:=by rw[qC4 _ _ (by rw[u8,eI];omega),u8,eI] have K:=qJd X inW.toUSize (Е 8*inW.toUSize) h7 (by rw[e8,eI]) (by rw[eI];omega) (8*inW) 0 (В (8*inW)) (by rw[e8,show (0:є).toNat=0 from rfl,И]) (by simp[FloatArray.size,FloatArray.emptyWithCapacity]) λp hp=>absurd hp (ـ _);have P0:(pushZ inW.toUSize 0 (В inW)).size=inW:=by rw[qCh inW.toUSize inW 0 _ (by rw[G.nm,C_z,И])];exact И _ refine qB4 (qD3 G relu Hv (λh q hq=>?_) (rows-8) 8 _ _ _ א ג P0 ?_) (λq hq=>hq);. have e:0=W.size:=h;omega . refine qAo (qGx G r1 relu Hv (K.1.trans e8) (λr t hr ht=>?_) (qIt h7 hl (qAb _ א)) outW 0 _ _ (И _) H0) λq _ h=>⟨h,щ _ h5⟩;have Q:=K.2 (8*t+r) (by rw[e8];omega);rwa [show (8*t+r)%8=r by omega,show (8*t+r)/8=t by omega,eI] at Q . have Hv:=Hs h8 h12;have G:=G0 Hv.2.2.1;have H0:C_Inv (rows*outW) (C_sp inW outW relu X W b) (λq=>q<0*outW) (Й (rows*outW) 0) :=⟨by simp,λq _ h=>by rw[Nat.zero_mul] at h;exact absurd h (ـ _)⟩;have P0:(pushZ inW.toUSize 0 (В inW)).size=inW:=by rw[qCh inW.toUSize inW 0 _ (by rw[G.nm,C_z,И])];exact И _ refine qB4 (qD3 G relu Hv ?_ rows 0 _ _ _ א ג P0 H0) (λq hq=>hq);split;. intro _ q hq;exact (qIv W h8).2 q hq . intro h q hq;have e:0=W.size:=h;omega . rw[qAc];rfl end lemma qDy:linF=linearSpec:=qEz section structure D_Sc where (lo hi:ѓ) def D_scan (A:ѐ) (n i:є) (hn:Ѡn<=A.size) (lo hi:ѓ):D_Sc := if h:i< n then have ht:=А.mp h let v:=A.uget i א &&& Б let u:=v-1 D_scan A n (i+1) hn (if u< lo then u else lo) (if hi< v then v else hi) else ⟨lo,hi⟩ termination_by Ѡn-Ѡi decreasing_by rw[usz i n h];omega def D_fill (v:ѕ) (n k:є) (a:ђ):ђ := if h:k< n then D_fill v n (k+1) (a.push v) else a termination_by Ѡn-Ѡk decreasing_by rw[usz k n h];have:=А.mp h;omega def D_copy (X:ѐ) (o n t:є) (hs:X.size<2^32) (ho:Ѡo+Ѡn<=X.size) (a:ђ):ђ := if h:t< n then D_copy X o n (t+1) hs ho (a.push (dv (X.uget (o+t) (wi o t n X ho hs (А.mp h))))) else a termination_by Ѡn-Ѡt decreasing_by rw[usz t n h];have:=А.mp h;omega def D_tq (k kw kh hp wp t c r q:ћ) (a:ѐ):ѐ := if t< k then let a:=a.push ((c*hp+r)*wp+q).toUInt32 if q+1< kw then D_tq k kw kh hp wp (t+1) c r (q+1) a else if r+1< kh then D_tq k kw kh hp wp (t+1) c (r+1) 0 a else D_tq k kw kh hp wp (t+1) (c+1) 0 0 a else a termination_by k-t def D_img (s:ConvShape) (X:ѐ) (hs:X.size<2^32) (z:ѕ) (R r:ћ) (a:ђ):ђ := if hr:r< R then let hp:=s.h+2*s.pad let yy:=r%hp let o:=((r/hp*s.h+(yy-s.pad))*s.w).toUSize let pu:=s.pad.toUSize if hg:s.pad<=yy∧yy-s.pad< s.h∧Ѡo+s.w.toUSize.toNat<=X.size then D_img s X hs z R (r+1) (D_fill z pu 0 (D_copy X o s.w.toUSize 0 hs hg.2.2 (D_fill z pu 0 a))) else D_img s X hs z R (r+1) (D_fill z (s.w+2*s.pad).toUSize 0 a) else a termination_by R-r def D_off (s:ConvShape) (t:ћ):ћ := (t/(s.kh*s.kw)*(s.h+2*s.pad)+t/s.kw%s.kh)*(s.w+2*s.pad)+t%s.kw structure D_C where (dw relu f4:ј) (s:ConvShape) (X W b:ѐ) (k C ho wo wp:ћ) (step:є) (hi:ѓ) (xd:ђ) (nf:ѕ) (tab:ѐ) structure D_P where (g:D_C) (wd:ђ) (off:ѐ) (e l1 l8:є) (e0 bv ze:ѕ) (he:Ѡe<=off.size) (hw:Ѡe<=wd.size) (hs:g.xd.size<2^32) (h1:∀ b:є,b< l1->∀ j (h:j< off.size),Ѡb+off[j].toNat+0< g.xd.size) (h8:∀ b:є,b< l8->∀ j (h:j< off.size),Ѡb+off[j].toNat+7< g.xd.size) (hz:ze=0) lemma qHb {n d:ћ} (b:є) (u:ѓ) (hs:n<2^32) (h:Ѡb+Ѡu+d< n):(b+u.toUSize).toNat+d< n:=by rw[qAg _ _ (by rw[Ы];omega),Ы];exact h lemma qBx {n:ћ} (p:є) (k:ћ) (hs:n<2^32) (h:Ѡp+k< n) : (p+Е k).toNat< n:=by rw[qAg _ _ (by rw[uk k א];omega),uk k א];exact h structure D_R where (t:UInt8) (A B C D E F G I:ѕ) def D_go8R (xd wd:ђ) (off:ѐ) (b e i:є) (he:Ѡe<=off.size) (hw:Ѡe<=wd.size) (hs:xd.size<2^32) (hb:∀ j (h:j< off.size),Ѡb+off[j].toNat+7< xd.size) (A B C D E F G I:ѕ) (r:D_R):D_R := if h:i< e then have ht:=А.mp h let p:=b+(off.uget i א).toUSize have hp:Ѡp+7< xd.size:=qHb _ _ hs (hb Ѡi א) let v:=wd.uget i א D_go8R xd wd off b e (i+1) he hw hs hb (A+xd.uget p א*v) (B+xd.uget (p+Е 1) (qBx p 1 hs א)*v) (C+xd.uget (p+Е 2) (qBx p 2 hs א)*v) (D+xd.uget (p+Е 3) (qBx p 3 hs א)*v) (E+xd.uget (p+Е 4) (qBx p 4 hs א)*v) (F+xd.uget (p+Е 5) (qBx p 5 hs א)*v) (G+xd.uget (p+Е 6) (qBx p 6 hs א)*v) (I+xd.uget (p+Е 7) (qBx p 7 hs א)*v) r else match r with | ⟨t,_,_,_,_,_,_,_,_⟩=>⟨t,A,B,C,D,E,F,G,I⟩ termination_by Ѡe-Ѡi decreasing_by rw[usz i e h];omega def D_goT (xd wd:ђ) (off:ѐ) (b e i:є) (he:Ѡe<=off.size) (hw:Ѡe<=wd.size) (hs:xd.size<2^32) (hb:∀ j (h:j< off.size),Ѡb+off[j].toNat+0< xd.size) (a t:ѕ):F1 := if h:i< e then have ht:=А.mp h let q:=xd.uget (b+(off.uget i א).toUSize) (qHb (d:=0) _ _ hs (hb Ѡi א))*wd.uget i א D_goT xd wd off b e (i+1) he hw hs hb (a+q) (t+q.abs) else ⟨a,t⟩ termination_by Ѡe-Ѡi decreasing_by rw[usz i e h];omega @[noinline] def D_slow (g:D_C) (o y x:ћ):ѓ := act g.relu (dotFn (g.k+1) (λt=>if t< g.k then windowAt g.s g.X y x ((if g.dw then o*g.k else 0)+t) else oneW) (λt=>if t< g.k then g.W[o*g.k+t]! else g.b[o]!)) @[noinline] def D_finT (c:D_P) (o y x:ћ) (b:є):ѓ := if hb:b< c.l1 then let q:=D_goT c.g.xd c.wd c.off b c.e 0 c.he c.hw c.hs (c.h1 b hb) c.ze c.ze let r:=ziv (q.s+c.bv) (c.g.nf*(q.t+c.bv.abs)) if r == Д then D_slow c.g o y x else act c.g.relu r else D_slow c.g o y x @[inline] def D_fin (c:D_P) (o y x:ћ) (b:є) (a:ѕ):ѓ := let r:=ziv (a+c.bv) c.e0 if r == Д then D_finT c o y x b else act c.g.relu r @[inline] def D_l (c:D_P) (o y x:ћ) (b q:є) (k:ћ) (a:ѕ) (out:ѐ) : ѐ := if x+k< c.g.wo then wrW out (q+Е k) (D_fin c o y (x+k) (b+Е k) a) else out def D_cols (c:D_P) (o y x:ћ) (b q:є) (rr:D_R) (out:ѐ) : ѐ := if h8:x< c.g.wo∧c.g.f4=true∧b< c.l8 then let r:=D_go8R c.g.xd c.wd c.off b c.e 0 c.he c.hw c.hs (c.h8 b h8.2.2) c.ze c.ze c.ze c.ze c.ze c.ze c.ze c.ze rr D_cols c o y (x+8) (b+8) (q+8) r (D_l c o y x b q 7 r.I (D_l c o y x b q 6 r.G (D_l c o y x b q 5 r.F (D_l c o y x b q 4 r.E (D_l c o y x b q 3 r.D (D_l c o y x b q 2 r.C (D_l c o y x b q 1 r.B (D_l c o y x b q 0 r.A out)))))))) else if x< c.g.wo then D_cols c o y (x+1) (b+c.g.step) (q+1) rr (wrW out q (D_finT c o y x b)) else out termination_by c.g.wo-x def D_rows (c:D_P) (o y:ћ) (q:є) (out:ѐ):ѐ := if y< c.g.ho then D_rows c o (y+1) (q+c.g.wo.toUSize) (D_cols c o y 0 (y.toUSize*c.g.step*c.g.wp.toUSize) q ⟨0,0,0,0,0,0,0,0,0⟩ out) else out termination_by c.g.ho-y structure D_T where (off:ѐ) (a:ѕ) def D_tap (g:D_C) (o:ћ) (ix:ѐ) (buf:ђ) (e i:є) (off:ѐ) (a:ѕ):D_T := if h:i< e then D_tap g o ix buf e (i+1) (wrW off i ((if g.dw then (o*((g.s.h+2*g.s.pad)*g.wp)).toUInt32 else 0) + rdW g.tab (rdW ix i).toUSize)) (a+(rdF buf i).abs) else ⟨off,a⟩ termination_by Ѡe-Ѡi decreasing_by rw[usz i e h];have:=А.mp h;omega lemma qCr (a:ѐ) (j:ћ) (h:j< a.size) : a[j].toNat<=a.foldl (λm v=>max m v.toNat) 0 := Array.foldl_induction (λn m=>∀ k (h:k< a.size),k< n->a[k].toNat<=m) nofun (λi b H k h hk=>by by_cases e:k=i . subst e;exact le_max_right .. exact (H k h א).trans (le_max_left ..)) j h h lemma qF9 {n m:ћ} (d:ћ) (off:ѐ) (hs:n<2^32) (hm:∀ j (h:j< off.size),off[j].toNat<=m) (b:є) (hb:b<(n-(m+d)).toUSize) (j:ћ) (h:j< off.size):Ѡb+off[j].toNat+d< n:=by have h1:=А.1 hb;rw[qAb _ א] at h1;have:=hm j h;omega def D_outs (g:D_C) (hs:g.xd.size<2^32) (o:ћ) (q:є) (ix off:ѐ) (buf:ђ) (out:ѐ):ѐ := if o< g.C then let r:=decR g.W (o*g.k).toUSize g.k.toUSize 0 ix buf 0 0 let t:=D_tap g o r.ix r.buf r.c 0 off 0 let m:=t.off.foldl (λm v=>max m v.toNat) 0 if h:r.c.toNat<=t.off.size∧r.c.toNat<=r.buf.size then let c:D_P:=⟨g,r.buf,t.off,r.c,(g.xd.size-(m+0)).toUSize, (g.xd.size-(m+7)).toUSize,g.nf*(dv g.hi*t.a+(dv (rdW g.b o.toUSize)).abs), dv (rdW g.b o.toUSize),0,h.1,h.2,hs, qF9 0 t.off hs (qCr t.off),qF9 7 t.off hs (qCr t.off),rfl⟩ D_outs g hs (o+1) (q+(g.ho*g.wo).toUSize) r.ix t.off r.buf (D_rows c o 0 q out) else out else out termination_by g.C-o def D_F (dw:ј) (s:ConvShape) (relu:ј) (X W b:ѐ) (k C:ћ) : ѐ := let hp:=s.h+2*s.pad let wp:=s.w+2*s.pad let rx:=D_scan X X.usize 0 (qAe X) Д 0 if hg:X.size<2^32∧0x007FFFFF<=rx.lo∧rx.hi<Р∧X.size=s.cin*s.h*s.w ∧ W.size=C*k∧b.size=C∧0< s.stride∧s.stride<2^32∧0< k∧k<2^20 ∧ s.kh<=hp∧s.kw<=wp∧hp<2^32∧wp<2^32∧W.size<2^32∧b.size<2^32 ∧ C*(s.ho*s.wo)<2^32∧s.cin*hp*wp<2^32 ∧ scanH W≠Д ∧ scanH b≠Д then let tight:=(rx.hi >>> (23:ѓ))-((rx.lo+1) >>> (23:ѓ)) > 99 let R:=s.cin*hp let xd:=pushZ 7 0 (D_img s X hg.1 (dv 0) R 0 (В (R*wp+7))) if hs:xd.size<2^32 then let g:D_C:=⟨dw,relu,s.stride == 1 && !tight,s,X,W,b,k,C,s.ho,s.wo,wp, s.stride.toUSize,rx.hi,xd,(k.toUInt64+8).toFloat*й 0x3CA0000000000000, D_tq k s.kw s.kh hp wp 0 0 0 0 (Array.emptyWithCapacity k)⟩ D_outs g hs 0 0 (Й k 0) (Й k 0) (pushZ k.toUSize 0 (В k)) (Й (C*(s.ho*s.wo)) 0) else if dw then depthwiseWith dotFn s relu X W b else conv2dWith dotFn s relu X W b else if dw then depthwiseWith dotFn s relu X W b else conv2dWith dotFn s relu X W b def convF (s:ConvShape) (relu:ј) (X W b:ѐ):ѐ := D_F false s relu X W b (s.cin*s.kh*s.kw) s.cout def depthF (s:ConvShape) (relu:ј) (X W b:ѐ):ѐ := D_F true s relu X W b (s.kh*s.kw) s.cin def D_px (s:ConvShape) (X:ё) (c yy xx:ћ):њ := if yy< s.pad∨xx< s.pad∨yy-s.pad ≥ s.h∨xx-s.pad ≥ s.w then 0 else X[(c*s.h+(yy-s.pad))*s.w+(xx-s.pad)]! def D_Fx (s:ConvShape) (X:ё) (q:ћ):ѕ := dv (D_px s X (q/(s.w+2*s.pad)/(s.h+2*s.pad)) (q/(s.w+2*s.pad)%(s.h+2*s.pad)) (q%(s.w+2*s.pad))) def D_Pre (F:ћ->ѕ) (m:ћ) (a:ђ):Prop := a.size=m∧∀ q,q< m->a.data[q]! = F q lemma qB3 {F:ћ->ѕ} {m:ћ} {a:ђ} {v:ѕ} (h:D_Pre F m a) (hv:v=F m):D_Pre F (m+1) (a.push v):=by refine ⟨by rw[qA2,h.1],λq hq=>?_⟩;rw[qEb,h.1];split<;>rename_i e;. rw[e,hv] exact h.2 q א lemma qHq (m d:ћ):m+(d+1)=m+1+d :=א def D_Sp (F:ћ->ѕ) (m:ћ) (a:ђ):Prop := m<=a.size∧∀ q,q< m->a.data[q]! = F q lemma qJf {F:ћ->ѕ} {m:ћ} (n:є) (d:ћ):∀ (k:є) (a:ђ),Ѡk+d=Ѡn->D_Sp F m a->D_Sp F m (pushZ n k a):=by induction d with | zero => intro k a h H;rw[pushZ,О ט];exact H | succ d ih => intro k a h H;have ht:k< n:=by rw[А];omega rw[pushZ,К ht];refine ih (k+1) _ (by rw[usz k n ht];omega) ⟨by rw[qA2];have:=H.1;omega,λq hq=>?_⟩;have:=H.1;rw[qEb,г א];exact H.2 q hq lemma qCj {i e:є} (h:Ѡe<=Ѡi):¬i< e :=mt А.1 (Nat.not_lt.2 h) lemma qBy {i e:є} {d:ћ} (h:Ѡi+(d+1)=Ѡe) : i< e∧(i+1).toNat+d=Ѡe:=by have t:=А.2 (א:Ѡi< Ѡe);exact ⟨t,by rw[usz i e t];omega⟩ lemma qEd {F:ћ->ѕ} {v:ѕ} {n:є} (d:ћ) : ∀ (k:є) (m:ћ) (a:ђ),Ѡk+d=Ѡn->D_Pre F m a → (∀ i,m<=i->i< m+d->F i=v)->D_Pre F (m+d) (D_fill v n k a):=by induction' d with d ih<;>intro k m a he H hv . rw[D_fill,О (qCj he.ge)];exact H obtain ⟨ht,hu⟩:=qBy he rw[D_fill,К ht,qHq] exact ih (k+1) (m+1) _ hu (qB3 H (hv m א א).symm) λi h1 h2=>hv i א א lemma qEo {F:ћ->ѕ} {X:ѐ} {o n:є} (hs:X.size<2^32) (ho:Ѡo+Ѡn<=X.size) (d:ћ) : ∀ (t:є) (m:ћ) (a:ђ),Ѡt+d=Ѡn->D_Pre F m a → (∀ j,j< d->F (m+j)=dv X[Ѡo+Ѡt+j]!) → D_Pre F (m+d) (D_copy X o n t hs ho a):=by induction' d with d ih<;>intro t m a he H hv . rw[D_copy,О (qCj he.ge)];exact H obtain ⟨ht,hu⟩:=qBy he rw[D_copy,К ht,qHq] refine ih (t+1) (m+1) _ hu (qB3 H ?_) λj hj=>?_ . rw[qCm,qAg _ _ א];exact (hv 0 א).symm simpa only [usz t n ht,Nat.add_assoc,Nat.add_comm 1] using hv (j+1) א lemma qCg {c:ћ} (a:ћ) {b:ћ} (h:b< c):(a*c+b)/c=a∧(a*c+b)%c=b:=by rw[Ю a c,ؾ א,ؿ,д h,З h];exact ⟨rfl,rfl⟩ lemma qBf (s:ConvShape) (X:ё) (r:ћ) {i:ћ} (hi:i< s.w+2*s.pad) : D_Fx s X (r*(s.w+2*s.pad)+i) =dv (D_px s X (r/(s.h+2*s.pad)) (r%(s.h+2*s.pad)) i):=by simp only[D_Fx,qCg r hi] lemma qF2 {F:ћ->ѕ} {v:ѕ} {n:є} {m d:ћ} {a:ђ} (hn:Ѡn=d) (H:D_Pre F m a) (hv:∀ u,u< d->F (m+u)=v):D_Pre F (m+d) (D_fill v n 0 a) := qEd d 0 m a (by rw[hn];exact И d) H λi h1 h2=>by obtain ⟨u,rfl⟩:=Nat.exists_eq_add_of_le h1 exact hv u א lemma qJi (m p w:ћ):m+(w+2*p)=m+p+w+p :=א lemma qFh (s:ConvShape) (X:ѐ) (hs:X.size<2^32) (hX:X.size=s.cin*s.h*s.w) (hh:s.h+2*s.pad<2^32) (hw:s.w+2*s.pad<2^32) {R:ћ} (hR:R=s.cin*(s.h+2*s.pad)) (d:ћ) : ∀ (r:ћ) (a:ђ),r+d=R → D_Pre (D_Fx s X) (r*(s.w+2*s.pad)) a → D_Pre (D_Fx s X) (R*(s.w+2*s.pad)) (D_img s X hs (dv 0) R r a):=by induction' d with d ih<;>intro r a he H . rw[D_img,О א,<-he];exact H have hr:r< R :=א have hc:=с (hr.trans_eq (hR.trans (Ю _ _))) have ep:=qAb s.pad א have ew:=qAb s.w א have hO:=λg:r%(s.h+2*s.pad)-s.pad< s.h=>qAt (qAv hc g) (Nat.le_refl s.w) rw[D_img,К hr] dsimp only split<;>rename_i g . obtain ⟨g1,g2,g3⟩:=g;have:=hO g2;apply ih (r+1) _ א;rw[ك,qJi];refine qF2 ep (qEo hs g3 s.w 0 _ _ (by rw[ew];exact И _) (qF2 ep H λu hu=>?_) λj hj=>?_) λu hu=>?_;. rw[qBf _ _ _ א,D_px,х א] . rw[и,qBf _ _ _ א,D_px,г א,Nat.add_sub_cancel_left,qAb _ א];rfl . rw[и,и,qBf _ _ _ א,D_px,х א] apply ih (r+1) _ א rw[ك] exact qF2 (qAb _ hw) H λu hu=>by rw[qBf _ _ _ hu,D_px,х] by_contra n have:=hO א exact g ⟨by omega,by omega,by rw[qAb _ א,ew,hX];exact this⟩ def D_t0 (g:D_C) (o:ћ):ћ:=if g.dw then o*g.k else 0 def D_xw (g:D_C) (o y x t:ћ):њ:=windowAt g.s g.X y x (D_t0 g o+t) def D_out (g:D_C) (o y x:ћ):њ := act g.relu (dotOf (g.k+1) (λt=>if t< g.k then D_xw g o y x t else oneW) (λt=>if t< g.k then g.W[o*g.k+t]! else g.b[o]!)) lemma qCt (g:D_C) (o y x:ћ):D_slow g o y x=D_out g o y x:=by unfold D_slow D_out D_xw D_t0;rw[qAc] def D_B (g:D_C) (y x:ћ):ћ:=y*g.s.stride*g.wp+x*g.s.stride structure D_Ok (g:D_C):Prop where hk:g.k=if g.dw then g.s.kh*g.s.kw else g.s.cin*g.s.kh*g.s.kw hC:g.C=if g.dw then g.s.cin else g.s.cout ho:g.ho=g.s.ho wo:g.wo=g.s.wo wp:g.wp=g.s.w+2*g.s.pad st:g.step.toNat=g.s.stride nf:g.nf=nfOf g.k f4:g.f4=true->g.s.stride=1 p0:0< g.s.stride pk:0< g.k nk:g.k<2^20 kh:g.s.kh<=g.s.h+2*g.s.pad kw:g.s.kw<=g.s.w+2*g.s.pad xd:D_Sp (D_Fx g.s g.X) (g.s.cin*(g.s.h+2*g.s.pad)*(g.s.w+2*g.s.pad)) g.xd xs:g.xd.size<2^32 Hx:C_Hv g.X g.hi HW:∀ q:ћ,good g.W[q]! = true Hb:∀ q:ћ,good g.b[q]! = true sW:g.W.size=g.C*g.k sW2:g.W.size<2^32 sb:g.b.size<2^32 sN:g.C*(g.ho*g.wo)<2^32 wp2:g.wp<2^32 tb:g.tab=Array.ofFn (n:=g.k) λt=>(D_off g.s t.val).toUInt32 lemma qE4 {A:ё} {h:њ}:good (0:њ)=true∧(0:њ) &&& Б<=h :=⟨rfl,Ж.2 (Nat.zero_le _)⟩ lemma qGb {A:ё} (H:∀ k:ћ,k< A.size->good A[k]! = true) (q:ћ) : good A[q]! = true:=by by_cases h:q< A.size;. exact H q h rw[س A q h];rfl lemma qCf {s:ConvShape} {X:ё} {hi:њ} (H:C_Hv X hi) (c yy xx:ћ) : good (D_px s X c yy xx)=true∧D_px s X c yy xx &&& Б<=hi:=by unfold D_px;split;. exact qE4 (A:=X) generalize (c*s.h+(yy-s.pad))*s.w+(xx-s.pad)=n;by_cases h:n< X.size;. exact ⟨H.2.2.1 n h,H.2.2.2 n h⟩ rw[س X n h];exact qE4 (A:=X) lemma qHp {g:D_C} (ok:D_Ok g) {o t:ћ} (hC:o< g.C) (ht:t< g.k) : D_t0 g o+t< g.s.cin*(g.s.kh*g.s.kw):=by have h1:=ok.hk;have h2:=ok.hC;unfold D_t0;cases hd:g.dw<;>simp[hd] at h1 h2 ⊢;. rwa [<-Nat.mul_assoc,<-h1] exact h1 ▸ qAv (h2 ▸ hC) (h1 ▸ ht) lemma qHo {n a b c:ћ} (h:n< a*(b*c)):0< b∧0< c:=by constructor<;>refine м λe=>?_<;>simp[e] at h lemma qF8 {a b st n m:ћ} (h0:0< st) (hk:b<=a) (hy:n<(a-b)/st+1) (hm:m< b) : n*st+m< a:=by have:=Н st (show n<=(a-b)/st by omega);have:=Nat.div_mul_le_self (a-b) st;omega lemma qJb (a p w v u y x:ћ) : v*w+u+((a*p+y)*w+x)=(a*p+(v+y))*w+(u+x):=by ring lemma qCc {g:D_C} (ok:D_Ok g) {o y x t:ћ} (hC:o< g.C) (hy:y< g.ho) (hx:x< g.wo) (ht:t< g.k) : D_B g y x+D_off g.s (D_t0 g o+t)< g.xd.size ∧ g.xd.data[D_B g y x+D_off g.s (D_t0 g o+t)]! = dv (D_xw g o y x t):=by have hb:=qHp ok hC ht have hk:=qHo hb unfold D_xw generalize D_t0 g o+t=u at hb ⊢ have hyy:=qF8 ok.p0 ok.kh (hy.trans_eq ok.ho) (щ (u/g.s.kw) hk.1) have hxx:=qF8 ok.p0 ok.kw (hx.trans_eq ok.wo) (щ u hk.2) have hq:=qAv (qAv (с (hb.trans_eq (Ю _ _))) hyy) hxx rw[D_B,D_off,ok.wp,qJb] refine ⟨Nat.lt_of_lt_of_le hq ok.xd.1,?_⟩ rw[ok.xd.2 _ hq] simp only[D_Fx,qCg _ hxx,qCg _ hyy] rfl lemma qBm {u v:є} {a b:ћ} (hu:Ѡu=a) (hv:Ѡv=b) (h:a+b<2^32) : (u+v).toNat=a+b:=by rw[qAg _ _ א,hu,hv] lemma qHt {n d:ћ} {b i:є} {off:ѐ} (hs:n<2^32) (hb:∀ j (h:j< off.size),Ѡb+off[j].toNat+d< n) (hi:Ѡi< off.size) : (b+(off.uget i hi).toUSize).toNat=Ѡb+(off[Ѡi]! : ѓ).toNat ∧ Ѡb+(off[Ѡi]! : ѓ).toNat+d< n:=by have h:Ѡb+(off.uget i hi).toNat+d< n:=hb _ hi rw[<-qCm _ _ hi] exact ⟨qBm rfl (Ы _) א,h⟩ lemma qF6 {c:D_P} {b:є} (hb:∀ j (h:j< c.off.size),Ѡb+c.off[j].toNat+0< c.g.xd.size) (xf wf:ћ->ѕ) (L:љ):∀ (i:є) (a t:ѕ),Ѡi+L.length=c.e.toNat → (∀ j p:ћ,L[j]?=some p->c.g.xd.data[Ѡb+(c.off[Ѡi+j]! : ѓ).toNat]! = xf p ∧ c.wd.data[Ѡi+j]! = wf p) → D_goT c.g.xd c.wd c.off b c.e i c.he c.hw c.hs hb a t=L.foldl (λ(acc:F1) p =>let q:=xf p*wf p F1.mk (acc.s+q) (acc.t+q.abs)) ⟨a,t⟩:=by have:=c.he induction' L with p L ih<;>intro i a t h0 H . rw[D_goT,О (qCj h0.ge)];rfl rw[List.length_cons] at h0 obtain ⟨ht,hu⟩:=qBy h0 have h1:=H 0 p rfl rw[р] at h1 rw[D_goT,К ht] simp only[qBw] rw[(qHt c.hs hb א).1,h1.1,h1.2,ih (i+1) _ _ hu,@List.foldl_cons] intro j q hq rw[usz i _ ht,Nat.add_right_comm,и] exact H (j+1) q hq lemma qI4 (a:ђ) (i:є) (hs:a.size<2^32) (hi:Ѡi< a.size) : rdF a i=a.data[Ѡi]!:=by rw[rdF,К (by rw[А,FloatArray.usize,qAb _ hs];exact hi),qBw] def D_ff (xf:ћ->ћ->ѕ) (wf:ћ->ѕ) (L:љ) (k:ћ) (a:ѕ):ѕ := L.foldl (λacc p=>acc+xf k p*wf p) a lemma qFz {c:D_P} {b:є} (hb:∀ j (h:j< c.off.size),Ѡb+c.off[j].toNat+7< c.g.xd.size) (r:D_R) (xf:ћ->ћ->ѕ) (wf:ћ->ѕ) (L:љ):∀ (i:є) (A B C D E F G I:ѕ),Ѡi+L.length=c.e.toNat → (∀ j p:ћ,L[j]?=some p->c.wd.data[Ѡi+j]! = wf p∧∀ d,d<8 → c.g.xd.data[Ѡb+(c.off[Ѡi+j]! : ѓ).toNat+d]! = xf d p) → D_go8R c.g.xd c.wd c.off b c.e i c.he c.hw c.hs hb A B C D E F G I r=⟨r.t,D_ff xf wf L 0 A,D_ff xf wf L 1 B,D_ff xf wf L 2 C,D_ff xf wf L 3 D,D_ff xf wf L 4 E,D_ff xf wf L 5 F,D_ff xf wf L 6 G,D_ff xf wf L 7 I⟩:=by have:=c.he have:=c.hs induction' L with p L ih<;>intro i A B C D E F G I h0 J . rw[D_go8R,О (qCj h0.ge)];cases r;rfl rw[List.length_cons] at h0 obtain ⟨ht,hu⟩:=qBy h0 obtain ⟨h1,h2⟩:=J 0 p rfl rw[р] at h1 h2 obtain ⟨ep,hbd⟩:=qHt c.hs hb (א:Ѡi< c.off.size) have U:=λ(k:ћ) (hk:k<8)=>(qBm ep (uk k א) א).symm ▸ h2 k hk rw[D_go8R,К ht] simp only[qBw] rw[ep,h1,U 1 א,U 2 א,U 3 א,U 4 א,U 5 א,U 6 א,U 7 א,<-р (_+_),h2 0 א] refine ih (i+1) _ _ _ _ _ _ _ _ hu λj q hq=>?_ rw[usz i _ ht,Nat.add_right_comm,и] exact J (j+1) q hq structure D_PO (c:D_P) (o:ћ) (L:љ):Prop where ok:D_Ok c.g hC:o< c.g.C len:c.e.toNat=L.length nd:L.Nodup lt:∀ t∈L,t< c.g.k hz:∀ t,t< c.g.k->t∉L->c.g.W[o*c.g.k+t]! &&& Б=0 rd:∀ j p:ћ,L[j]?=some p → (c.off[j]! : ѓ).toNat=D_off c.g.s (D_t0 c.g o+p) ∧ c.wd.data[j]! = dv c.g.W[o*c.g.k+p]! e0:c.e0=nfOf c.g.k*(dv c.g.hi*L.foldl (λacc t=>acc+(dv c.g.W[o*c.g.k+t]!).abs) 0 + (dv c.g.b[o]!).abs) bv:c.bv=dv c.g.b[o]! section variable {c:D_P} {o:ћ} {L:љ} (h:D_PO c o L) {y x:ћ} {b:є} (hy:y< c.g.ho) (hx:x< c.g.wo) (hb:Ѡb=D_B c.g y x) include c o L h y x b hy hx hb lemma qCq : D_finT c o y x b=D_out c.g o y x:=by unfold D_finT;split<;>rename_i hl;swap;. exact qCt .. have e:=qF6 (c.h1 b hl) (dv ∘ D_xw c.g o y x) (λp=>dv c.g.W[o*c.g.k+p]!) L 0 0 0 ((И _).trans h.len.symm) λj p hj=>by have h1:=h.rd j p hj;rw[C_z,И,h1.1,hb];exact ⟨(qCc h.ok h.hC hy hx (h.lt p (Ա hj))).2,h1.2⟩ simp only[c.hz,e,h.bv,h.ok.nf];split<;>rename_i hz;. exact qCt .. exact congrArg (act _) (qBt c.g.k h.ok.nk _ _ _ (λt _=>(qCf h.ok.Hx _ _ _).1) (λt _=>h.ok.HW _) (h.ok.Hb _) L h.nd h.lt (λt a b=>.inr (h.hz t a b)) _ _ ⟨rfl,rfl⟩ (by simpa using hz)) lemma qFp {a:ѕ} (ha:a=L.foldl (λacc t=>acc+dv (D_xw c.g o y x t)*dv c.g.W[o*c.g.k+t]!) 0) : D_fin c o y x b a=D_out c.g o y x:=by subst ha;simp only[D_fin,h.bv,h.e0];split<;>rename_i hz;. exact qCq h hy hx hb exact congrArg (act _) (qC0 c.g.k h.ok.nk _ _ _ _ (λt _=>(qCf h.ok.Hx _ _ _).1) (λt _=>h.ok.HW _) (h.ok.Hb _) h.ok.Hx.1 (λt _=>(qCf h.ok.Hx _ _ _).2) h.ok.Hx.2.1 L h.nd h.lt (λt a b=>.inr (h.hz t a b)) _ _ ⟨rfl,rfl⟩ (by simpa using hz)) end def D_sp (g:D_C) (q:ћ):њ := D_out g (q/(g.ho*g.wo)) (q/g.wo%g.ho) (q%g.wo) lemma qGv (g:D_C) {o y x:ћ} (hy:y< g.ho) (hx:x< g.wo) : D_out g o y x=D_sp g ((o*g.ho+y)*g.wo+x):=by simp only[D_sp,Nat.mul_comm g.ho g.wo,<-Nat.div_div_eq_div_mul,qCg _ hx,qCg _ hy] lemma qFr {g:D_C} (ok:D_Ok g) {o y x:ћ} (hC:o< g.C) (hy:y< g.ho) (hx:x< g.wo):D_B g y x<2^32:=by have h:=(qCc ok hC hy hx ok.pk).1;have:=ok.xs;omega lemma qH6 (g:D_C) (y x k:ћ):D_B g y (x+k)=D_B g y x+k*g.s.stride:=by unfold D_B;rw[غ];omega def D_xf (c:D_P) (o:ћ) (b:є) (k p:ћ):ѕ := c.g.xd.data[Ѡb+D_off c.g.s (D_t0 c.g o+p)+k]! def D_I (c:D_P) (Q x:ћ) (out:ё):Prop := C_Inv (c.g.C*(c.g.ho*c.g.wo)) (D_sp c.g) (λp=>p< Q+min x c.g.wo) out lemma qFg {c:D_P} {o:ћ} {L:љ} (h:D_PO c o L) {y Q:ћ} (hy:y< c.g.ho) (hQ:Q=(o*c.g.ho+y)*c.g.wo) (hN:Q+c.g.wo<=c.g.C*(c.g.ho*c.g.wo)) (d:ћ) : ∀ (x:ћ) (b q:є) (rr:D_R) (out:ё),c.g.wo-x=d → (x< c.g.wo->Ѡb=D_B c.g y x)->(x< c.g.wo->Ѡq=Q+x)->D_I c Q x out → C_Inv (c.g.C*(c.g.ho*c.g.wo)) (D_sp c.g) (λp=>p< Q+c.g.wo) (D_cols c o y x b q rr out):=by induction' d using Nat.strong_induction_on with d ih intro x b q rr out he hb hq H have sN:=h.ok.sN have S:∀ x',x'< c.g.wo->D_out c.g o y x'=D_sp c.g (Q+x'):=λ_ hx=>hQ ▸ qGv c.g hy hx rw[D_cols] split<;>rename_i h8 . obtain ⟨hx,hf,hl⟩:=h8;have hb0:=hb hx;have hq0:=hq hx;have hB:=λk=>(qH6 c.g y x k).trans (by rw[h.ok.f4 hf,Nat.mul_one]);have G:=λk (hk:x+k< c.g.wo) =>qBm hb0 (uk k א) (hB k ▸ qFr h.ok h.hC hy hk);have V:=λk (hk:x+k<=c.g.wo) =>(qBm hq0 (uk k א) א).trans (и Q x k) have R:=qFz (c.h8 b hl) rr (D_xf c o b) (λp=>dv c.g.W[o*c.g.k+p]!) L 0 0 0 0 0 0 0 0 0 ((И _).trans h.len.symm) λj p hj=>by have h1:=h.rd j p hj;rw[C_z,И,h1.1];exact ⟨h1.2,λ_ _=>rfl⟩ have A:∀ k u,D_I c Q (x+k) u->D_I c Q (x+k+1) (D_l c o y x b q k (D_ff (D_xf c o b) (λp=>dv c.g.W[o*c.g.k+p]!) L k 0) u):=λk u H=>by unfold D_l D_I at *;split<;>rename_i hk;. rw[Nat.min_eq_left hk.le] at H;rw[Nat.min_eq_left hk];refine qAj sN H (V k א) א ((qFp h hy hk ((G k hk).trans (hB k).symm) (List.foldl_ext _ _ _ λ_ p hp=>?_)).trans (S _ hk));simp only[D_xf];rw[Nat.add_right_comm,hb0,<-hB,(qCc h.ok h.hC hy hk (h.lt p hp)).2] . rw[Nat.min_eq_right (Nat.not_lt.1 hk)] at H;rwa [Nat.min_eq_right א] simp only[c.hz,R];exact ih (c.g.wo-(x+8)) א (x+8) _ _ _ _ rfl (λhk=>(G 8 hk).trans (hB 8).symm) (λhk=>V 8 hk.le) (A 7 _ (A 6 _ (A 5 _ (A 4 _ (A 3 _ (A 2 _ (A 1 _ (A 0 _ H)))))))) split<;>rename_i h1 . have hb0:=hb h1;unfold D_I at *;rw[Nat.min_eq_left h1.le] at H;refine ih (c.g.wo-(x+1)) א (x+1) _ _ _ _ rfl (λhx=>?_) (λ_=>(qBm (hq h1) ь א).trans rfl) ?_;. have:=qFr h.ok h.hC hy hx;rw[qH6,م] at this ⊢;exact qBm hb0 h.ok.st this . rw[qCq h hy h1 hb0,Nat.min_eq_left h1];exact qAj sN H (hq h1) א (S _ h1) unfold D_I at H rwa [Nat.min_eq_right א] at H lemma qHy {u v:є} {a b:ћ} (hu:Ѡu=a) (hv:Ѡv=b) (h:a*b<2^32) : (u*v).toNat=a*b:=by rw[qCd _ _ (by rw[hu,hv];exact h),hu,hv] lemma qFm {c:D_P} {o:ћ} {L:љ} (h:D_PO c o L) (hN:(o*c.g.ho+c.g.ho)*c.g.wo<=c.g.C*(c.g.ho*c.g.wo)) (d:ћ) : ∀ (y:ћ) (q:є) (out:ё),y+d=c.g.ho → Ѡq=(o*c.g.ho+y)*c.g.wo → C_Inv (c.g.C*(c.g.ho*c.g.wo)) (D_sp c.g) (λp=>p<(o*c.g.ho+y)*c.g.wo) out → C_Inv (c.g.C*(c.g.ho*c.g.wo)) (D_sp c.g) (λp=>p<(o*c.g.ho+c.g.ho)*c.g.wo) (D_rows c o y q out):=by induction' d with d ih<;>intro y q out he hq H . rw[D_rows,Т א];rwa [(א:y=c.g.ho)] at H have hy:y< c.g.ho :=א have sN:=h.ok.sN have hA:=qAt (א:o*c.g.ho+y< o*c.g.ho+c.g.ho) (Nat.le_refl c.g.wo) have eW:=qAb c.g.wo א have bB:=qFr h.ok h.hC hy (x:=0) (h.ok.wo ▸ Nat.succ_pos _) have hwp:0< c.g.wp:=h.ok.wp ▸ (qHo (qHp h.ok h.hC h.ok.pk)).2.trans_le h.ok.kw simp[D_B] at bB have:=ر (y*c.g.s.stride) hwp have:=ر y h.ok.p0 rw[D_rows,б hy] refine ih (y+1) _ _ א ?_ ?_<;>rw[<-и,Nat.add_one_mul] . exact qBm hq eW א exact qFg h hy rfl א c.g.wo 0 _ _ _ _ (Nat.sub_zero _) (λ_ =>(qHy (qHy (qAb y א) h.ok.st א) (qAb _ h.ok.wp2) bB).trans (by simp[D_B])) (λ_=>hq) (by unfold D_I;rw[Nat.zero_min];exact H) lemma qGn (s:ConvShape) (o:ћ) {p:ћ} (hp:p< s.kh*s.kw) : D_off s (o*(s.kh*s.kw)+p) =o*((s.h+2*s.pad)*(s.w+2*s.pad))+D_off s p:=by unfold D_off rw[(qCg o hp).1,д hp,<-Nat.mul_assoc,Nat.add_comm (o*s.kh*s.kw),Nat.add_mul_div_right _ _ (Nat.pos_of_mul_pos_left (Nat.zero_lt_of_lt hp)),Nat.add_mul_mod_self_right,Nat.add_mul_mod_self_right] ring lemma qIe (a b:ћ):a.toUInt32+b.toUInt32=(a+b).toUInt32 :=(UInt32.ofNat_add a b).symm lemma qHi {g:D_C} (ok:D_Ok g) (o:ћ) {p:ћ} (hp:p< g.k) {u:ѓ} (hu:Ѡu=p) : (if g.dw then (o*((g.s.h+2*g.s.pad)*g.wp)).toUInt32 else 0)+rdW g.tab u.toUSize =(D_off g.s (D_t0 g o+p)).toUInt32:=by have h2:=ok.nk rw[qAk _ _ (by simp[ok.tb];omega),Ы,hu,ok.tb,Ш _ p (by simpa using hp),Array.getElem_ofFn] have h1:=ok.hk unfold D_t0 cases hd:g.dw . simp simp only[hd,if_true] at h1 ⊢ rw[h1,qGn g.s o (h1 ▸ hp),ok.wp,qIe] def D_tv (g:D_C) (o:ћ) (L:љ) (j:ћ):њ := (D_off g.s (D_t0 g o+L[j]?.getD 0)).toUInt32 lemma qF3 {g:D_C} (ok:D_Ok g) (o:ћ) {ix:ѐ} {buf:ђ} {e:є} {L:љ} {N:ћ} (wf:ћ->ѕ) (hk:∀ p∈L,p< g.k) (hN:N<2^32) (hix:ix.size<2^32) (hbuf:buf.size<2^32) (hL:Ѡe=L.length) (he:Ѡe<=buf.size) (heN:Ѡe<=N) (hr:∀ j p:ћ,L[j]?=some p->(ix[j]! : ѓ).toNat=p∧buf.data[j]! = wf p) (d:ћ) : ∀ (i:є) (off:ѐ) (a:ѕ),Ѡi+d=Ѡe → C_Inv N (D_tv g o L) (λq=>q< Ѡi) off → C_Inv N (D_tv g o L) (λq=>q< Ѡe) (D_tap g o ix buf e i off a).off ∧ (D_tap g o ix buf e i off a).a =(L.drop Ѡi).foldl (λacc t=>acc+(wf t).abs) a:=by induction' d with d ih<;>intro i off a h0 H . rw[D_tap,О (qCj h0.ge),(א:Ѡi=Ѡe)] at *;exact ⟨H,by rw[List.drop_eq_nil_of_le א];rfl⟩ obtain ⟨ht,hu⟩:=qBy h0 have hl:Ѡi< L.length :=א have hp:=List.getElem?_eq_getElem hl obtain ⟨h1,h2⟩:=hr _ _ hp rw[D_tap,К ht,qAk _ _ hix,qHi ok o (hk _ (List.getElem_mem hl)) h1,qI4 _ _ hbuf א,h2,List.drop_eq_getElem_cons hl,@List.foldl_cons,<-usz i e ht] exact ih (i+1) _ _ hu (by rw[usz i e ht];exact qAj hN H rfl א (by rw[D_tv,hp];rfl)) lemma qFe {g:D_C} (ok:D_Ok g) (hs:g.xd.size<2^32) (d:ћ) : ∀ (o:ћ) (q:є) (ix off:ѐ) (buf:ђ) (out:ё),o+d=g.C->Ѡq=o*(g.ho*g.wo)->ix.size=g.k->off.size=g.k->buf.size=g.k → C_Inv (g.C*(g.ho*g.wo)) (D_sp g) (λp=>p< o*(g.ho*g.wo)) out → C_Inv (g.C*(g.ho*g.wo)) (D_sp g) (λp=>p< g.C*(g.ho*g.wo)) (D_outs g hs o q ix off buf out):=by induction' d with d ih<;>intro o q ix off buf out he hq h1 h2 h3 H . rw[D_outs,Т א];rwa [(א:o=g.C)] at H have hC:o< g.C :=א have hkn:=ok.nk have sW2:=ok.sW2 have sN:=ok.sN have hP:=(qAt hC (Nat.le_refl g.k)).trans_eq ok.sW.symm have hO:=qAt hC (Nat.le_refl (g.ho*g.wo)) have eB:rdW g.b o.toUSize=g.b[o]!:=by rw[qAk _ _ ok.sb,qAb _ (by have:=ر o ok.pk;omega)] obtain ⟨d1,d2,d3,d4,-⟩:=qEv sW2 hP (λt _=>ok.HW _) h1 h3 obtain ⟨k1,k2,k3⟩:=qDw (λp=>g.W[o*g.k+p]!) g.k have hlen:=d1.trans_le (qBb _ _) obtain ⟨⟨T1,T3⟩,T2⟩:=qF3 ok o (λp=>dv g.W[o*g.k+p]!) k2 א א א d1 א א d4 _ 0 off 0 ((И _).trans d1.symm) ⟨h2,nofun⟩ rw[D_outs,б hC] dsimp only rw[К א,eB] refine ih (o+1) _ _ _ _ _ א (Nat.add_one_mul o _ ▸ qBm hq (qAb _ א) א) d2 T1 d3 ?_ have E:(o*g.ho+g.ho)*g.wo=o*(g.ho*g.wo)+g.ho*g.wo:=by ring rw[Nat.add_one_mul,<-E] rw[<-Nat.mul_assoc o] at hq H refine qFm (L:=C_keep _ g.k) ⟨ok,hC,d1,k1,k2,k3,λj p hj=>?_,by dsimp only;rw[ok.nf,T2];rfl,rfl⟩ (by dsimp only;omega) g.ho 0 q out (И _) hq H have hjl:=(List.getElem?_eq_some_iff.mp hj).1 refine ⟨?_,(d4 j p hj).2⟩ dsimp only rw[T3 j א א,D_tv,hj] simpa using Nat.mod_eq_of_lt ((Nat.lt_of_add_left_lt (qCc ok hC (y:=0) (x:=0) (ok.ho ▸ Nat.succ_pos _) (ok.wo ▸ Nat.succ_pos _) (k2 _ (List.mem_of_getElem? hj))).1).trans ok.xs) lemma qE7 (A:ѐ) (n:є) (hn:Ѡn<=A.size) (d:ћ) : ∀ (i:є) (lo hi:ѓ),Ѡi+d=Ѡn->scan A n i hn lo hi = if 0x007FFFFF<=(D_scan A n i hn lo hi).lo∧(D_scan A n i hn lo hi).hi<Р then (D_scan A n i hn lo hi).hi else Д:=by induction' d with d ih<;>intro i lo hi he<;>rw[scan,D_scan] . simp only[dite_eq_right (qCj he.ge)] simp only[dite_eq_left (qBy he).1] exact ih (i+1) _ _ (qBy he).2 lemma qFy (s:ConvShape) {c r q:ћ} (hr:r< s.kh) (hq:q< s.kw) : D_off s ((c*s.kh+r)*s.kw+q)=(c*(s.h+2*s.pad)+r)*(s.w+2*s.pad)+q:=by have k0:0< s.kw :=א;have h0:0< s.kh :=א;have e1:((c*s.kh+r)*s.kw+q)/s.kw=c*s.kh+r:=by rw[Nat.add_comm,Nat.add_mul_div_right _ _ k0,д hq,И] have e2:((c*s.kh+r)*s.kw+q)%s.kw=q:=by rw[Nat.add_comm,Nat.add_mul_mod_self_right,З hq] have e3:(c*s.kh+r)%s.kh=r:=by rw[Nat.add_comm,Nat.add_mul_mod_self_right,З hr] have e4:(c*s.kh+r)/s.kh=c:=by rw[Nat.add_comm,Nat.add_mul_div_right _ _ h0,д hr,И] unfold D_off;rw[Ю s.kh s.kw,<-Nat.div_div_eq_div_mul,e1,e4,e3,e2] lemma qGq (s:ConvShape) (k d:ћ):∀ (t c r q:ћ) (a:ѐ),t+d=k → t=(c*s.kh+r)*s.kw+q->r< s.kh->q< s.kw → a=(Array.ofFn (n:=t) λi=>(D_off s i.val).toUInt32) → D_tq k s.kw s.kh (s.h+2*s.pad) (s.w+2*s.pad) t c r q a =Array.ofFn (n:=k) λi=>(D_off s i.val).toUInt32:=by induction d with | zero => intro t c r q a h1 _ _ _ h5 have e:t=k:=h1 subst e rw[D_tq,г (Nat.lt_irrefl _)] exact h5 | succ d ih => intro t c r q a h1 h2 h3 h4 h5 have hp:a.push ((c*(s.h+2*s.pad)+r)*(s.w+2*s.pad)+q).toUInt32 =Array.ofFn (n:=t+1) λi=>(D_off s i.val).toUInt32:=by rw[<-qFy s h3 h4,<-h2,h5,Array.ofFn_succ] rfl rw[D_tq,х א] dsimp only rw[hp] split . exact ih (t+1) c r (q+1) _ א א h3 (by assumption) rfl split . have e:(c*s.kh+(r+1))*s.kw=(c*s.kh+r)*s.kw+s.kw:=by ring exact ih (t+1) c (r+1) 0 _ א א (by assumption) א rfl . have e2:s.kh=r+1 :=א;have e:((c+1)*s.kh+0)*s.kw=(c*s.kh+r)*s.kw+s.kw:=by rw[e2];ring exact ih (t+1) (c+1) 0 0 _ א א א א rfl lemma qHm {g:D_C} (ok:D_Ok g) (hs:g.xd.size<2^32) : D_outs g hs 0 0 (Й g.k 0) (Й g.k 0) (pushZ g.k.toUSize 0 (В g.k)) (Й (g.C*(g.ho*g.wo)) 0) =if g.dw then depthwiseSpec g.s g.relu g.X g.W g.b else conv2dSpec g.s g.relu g.X g.W g.b:=by refine (qB4 (qFe ok hs g.C 0 0 _ _ _ _ (И _) ג ג ג ((qCh g.k.toUSize g.k 0 _ ((И _).trans (qAb g.k (ok.nk.trans ב)).symm)).trans (И _)) ⟨by simp,λq _ h=>א⟩) λq h=>h).trans ?_ obtain ⟨dw,_,_,s,_,_,_,k,C,ho,wo⟩:=g obtain ⟨h1,h2,h3,h4⟩:=ok cases dw<;>simp at h1 h2 h3 h4<;>subst h1 h2 h3 h4 <;>refine congrArg (Array.ofFn (n:=_)) (funext λq=>?_)<;>simp[D_sp,D_out,D_xw,D_t0] lemma qFf (dw:ј) (s:ConvShape) (relu:ј) (X W b:ѐ) {k C:ћ} (hk:k=if dw then s.kh*s.kw else s.cin*s.kh*s.kw) (hC:C=if dw then s.cin else s.cout) : D_F dw s relu X W b k C =if dw then depthwiseSpec s relu X W b else conv2dSpec s relu X W b:=by unfold D_F dsimp only rw[qAc] split;swap;rfl split;swap;rfl rename_i hg hs obtain ⟨a,c,d,e,f,-,i,j,l,m,n,o,p,q,r,t,u,-,v,x⟩:=hg have y:=qE7 X X.usize (qAe X) X.usize.toNat 0 Д 0 (И _) rw[б ⟨c,d⟩] at y have Hx:=y ▸ qBa X a (by rw[y];intro h;rw[h] at d;exact absurd d ה) refine (qHm ?_ _).trans ?_ have hk0:k≠0 :=א rw[hk] at hk0 have k1:0< s.kh:=м λz=>hk0 (by rw[z];simp) have k2:0< s.kw:=м λz=>hk0 (by rw[z];simp) have Pz:=qFh s X a e p q rfl _ 0 (В (s.cin*(s.h+2*s.pad)*(s.w+2*s.pad)+7)) (И _) ⟨by rw[Nat.zero_mul];rfl,λq hq=>א⟩ exact ⟨hk,hC,rfl,rfl,rfl,qAb _ j,rfl,λh=>beq_iff_eq.1 (п.1 h).1,i,l,m,n,o,qJf 7 7 0 _ (by rw[C_z,И];exact (uk 7 ב).symm) ⟨Nat.le_of_eq Pz.1.symm,Pz.2⟩,hs,Hx,qGb (qA8 W r v).2.2.1,qGb (qA8 b t x).2.2.1,f,r,t,u,q,qGq s k k 0 0 0 0 _ (И _) ג k1 k2 (by simp[Array.emptyWithCapacity_eq])⟩ rfl lemma qD7:convF=conv2dSpec:=by funext s r X W b;exact qFf false s r X W b rfl rfl lemma qDo:depthF=depthwiseSpec:=by funext s r X W b;exact qFf true s r X W b rfl rfl end @[inline] def sm (w:ѓ):ј:=good w && (w &&& Б)<=0x70FFFFFF section open Float.Model Float.Model.UnpackedFloat def T_r (N:ћ):ћ := N/2^39+if 2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1) then 1 else 0 lemma qIl (N:ћ):FloatLib.Numerics.roundShiftRightEven N 39=T_r N:=by have a:Nat.shiftRight N 39=N/2^39:=ы N 39;have b:N-Nat.shiftLeft (N/2^39) 39=N%2^39:=by rw[show Nat.shiftLeft (N/2^39) 39=N/2^39*2^39 from ф _ _];omega rw[FloatLib.Numerics.roundShiftRightEven_def];simp only[show (39 == 0)=false from rfl,false_eq_true,ite_false,a,b,beq_iff_eq];unfold T_r;split_ifs<;>omega set_option maxRecDepth 8192 in lemma qH4 (neg:ј) (N:ћ) (e':Int) (hN1:2^62<=N) (hN2:N<2^63) (h1:-148<=e'+39) (h2:e'+39+150<=253) : toWord (Model.ofModel Fmt (UnpackedFloat.round Format.binary32 (в neg) N e')) =ш ((if neg then 2^31 else 0)+(e'+39+149).toNat*2^23+T_r N):=by have hr1:2^23<=T_r N:=by unfold T_r;split<;>omega have hr2:T_r N<=2^24:=by unfold T_r;split<;>omega have hT:Format.binary32.targetExponent (totalExponent N e')=e'+39:=by simp only[totalExponent,qCz N 62 hN1 hN2,qGi];omega have K:∀ (P:ћ) (E:Int) (hp:0< P),2^23<=P->P<2^24->-149<=E->E<=104 → (E+150).toNat*2^23+P%2^23=(e'+39+149).toNat*2^23+T_r N → toWord (Model.ofModel Fmt (.finite (в neg) P E hp)) =ш ((if neg then 2^31 else 0)+(e'+39+149).toNat*2^23+T_r N):=by intro P E hp a b c d e simp only[toWord,Model.toNatBits,Model.ofModel,Model.ofModelBits] congr 1 show (UnpackedFloat.pack Format.binary32 _).toNat=_ rw[qBg _ _ _ _ a b א א] cases neg<;>simp[Model.modelSign]<;>omega rw[Model.round_exact_eq_finishRoundedMantissa _ _ _ _ א,hT,Model.roundMantissaAtExponentEven,б א,show (e'+39-e').toNat=39 by omega,qIl] by_cases hc:T_r N=2^24 . have h:=Model.finishRoundedMantissa_normalized_carry FloatFormat.binary32 (в neg) (e'+39+23) (by show -126<=e'+39+23;omega);rw[show e'+39+23-(FloatFormat.binary32.fracWidth:ℤ)=e'+39 by show e'+39+23-23=_;omega,show e'+39+23+1-(FloatFormat.binary32.fracWidth:ℤ)=e'+40 by show e'+39+23+1-23=_;omega] at h;rw[show Model.finishRoundedMantissa Format.binary32 (в neg) (T_r N,e'+39) =.finite (в neg) (2^23) (e'+40) ב by rw[hc];exact h] exact K (2^23) (e'+40) ב le_rfl ב א א (by rw[hc,show (e'+40+150).toNat=(e'+39+149).toNat+2 by omega];omega) . rw[qEn _ _ _ hr1 א א א] exact K _ _ _ hr1 א א א (by rw[show (e'+39+150).toNat=(e'+39+149).toNat+1 by omega];omega) @[inline] def T_rd (neg:ј) (mag e0 L:і):ѓ := let n:=mag <<< (62-L) let q:=n >>> 39 let rm:=n &&& 0x7FFFFFFFFF let w:=((e0+L-24) <<< 23)+q + (if rm > 0x4000000000 || (rm == 0x4000000000 && q &&& 1 == 1) then 1 else 0) (if neg then w ||| я else w).toUInt32 lemma qGz (neg:ј) (mag e0:і) (h0:Ѡmag≠0) (h1:Ѡmag<2^63) (h2:24< Ѡe0+mag.toNat.log2) (h3:Ѡe0+mag.toNat.log2<277) : T_rd neg mag e0 mag.log2=toWord (Ъ Fmt ⟨neg,Ѡmag,(Ѡe0:Int)-150⟩):=by have l0:=Nat.log2_self_le h0;have l1:Ѡmag<2^(mag.toNat.log2+1):=Nat.lt_log2_self;have hL:mag.toNat.log2<=62:=Nat.lt_succ_iff.1 ((Nat.log2_lt h0).2 h1);obtain ⟨L,hLd⟩:∃ L,L=mag.toNat.log2:=⟨_,rfl⟩;rw[<-hLd] at l0 l1 hL h2 h3;have eL:mag.log2.toNat=L:=hLd.symm;obtain ⟨N,hN⟩:∃ N,N=Ѡmag*2^(62-L):=⟨_,rfl⟩;have p:2^L*2^(62-L)=2^62:=by rw[<-Nat.pow_add];congr 1;omega have hN1:2^62<=N:=by rw[hN,<-p];exact Н _ l0 have hN2:N<2^63:=by have:=Nat.mul_lt_mul_of_pos_right l1 (Nat.two_pow_pos (62-L));rwa [<-hN,<-Nat.pow_add,show L+1+(62-L)=63 by omega] at this rw[qAy,<-ExactRuntime.round_scale _ _ _ _ (62-L) h0,<-hN,qH4 neg N _ hN1 hN2 א א];have eT:((Ѡe0:Int)-150-((62-L:ћ):Int)+39+149).toNat=Ѡe0+L-24:=by omega rw[eT];unfold T_rd;dsimp only;have e1:(62-mag.log2).toNat=62-L:=by rw[toNat_sub_of_le _ _ (Щ.2 (by rw[eL];exact hL)),eL];rfl have a1:(mag <<< (62-mag.log2)).toNat=N:=by rw[UInt64.toNat_shiftLeft,e1,З (by omega:62-L<64),ф,<-hN,З א] generalize mag <<< (62-mag.log2)=n at a1;have a2:(n >>> 39).toNat=N/2^39:=by rw[ز,a1];simp[Nat.shiftRight_eq_div_pow] have a3:(n &&& 0x7FFFFFFFFF).toNat=N%2^39:=by rw[ى,a1];exact М N 39 generalize n >>> 39=q at a2;generalize n &&& 0x7FFFFFFFFF=rm at a3;have a4:(q &&& 1).toNat=Ѡq%2:=by rw[ى];exact М _ 1 have a5:(e0+mag.log2-24).toNat=Ѡe0+L-24:=by rw[toNat_sub_of_le _ _ (Щ.2 (by rw[ж,eL,З א];show 24<=_;omega)), ж,eL,З א] rfl have a6:((e0+mag.log2-24) <<< 23).toNat=(Ѡe0+L-24)*2^23:=by rw[UInt64.toNat_shiftLeft,a5,ф];show _*2^23%_=_;rw[З א] generalize (e0+mag.log2-24) <<< 23=hi at a6;have hup:(decide (rm > 0x4000000000) || (rm == 0x4000000000 && q &&& 1 == 1)) =decide (2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1)):=by apply Bool.eq_iff_iff.2;simp only[or_eq_true,and_eq_true,decide_eq_true_eq,beq_iff_eq,gt_iff_lt,UInt64.lt_iff_toNat_lt,<-UInt64.toNat_inj,a3,a4,a2];rfl rw[hup];obtain ⟨c,hc⟩:∃ c,c=decide (2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1)):=⟨_,rfl⟩;rw[<-hc];have hr:T_r N=N/2^39+if c=true then 1 else 0:=by rw[hc,T_r];simp only[decide_eq_true_eq] have hq:N/2^39<2^24 :=א;have hw:(hi+q+(if c=true then (1:і) else 0)).toNat=(Ѡe0+L-24)*2^23+T_r N:=by rw[ж,ж,a6,a2,hr];cases c<;>simp only[false_eq_true,ite_false,ite_true] <;>(try rw[show (1:і).toNat=1 from rfl])<;>(try rw[show (0:і).toNat=0 from rfl])<;>omega generalize hi+q+(if c=true then (1:і) else 0)=w at hw;have hw2:Ѡw<2^31:=by rw[hw,hr];split<;>omega apply П.mp;rw[ق];cases neg;. simp only[false_eq_true,ite_false,UInt64.toNat_toUInt32,Nat.zero_add,hw] . simp only[ite_true,UInt64.toNat_toUInt32,UInt64.toNat_or];have h5:Ѡw ||| (2147483648:і).toNat=2^31+Ѡw:=by have:=Nat.two_pow_add_eq_or_of_lt hw2 1;simp only[Nat.mul_one] at this;rw[this,Nat.or_comm];rfl rw[h5];congr 1;omega lemma qI7 (w:ѓ) (h:(w &&& Б)=0):Л w=0:=by have h1:=qAs w;rw[h] at h1;have h2:=w.toNat_lt have h3:w=0∨w=я:=by have:Ѡw=0∨Ѡw=2147483648:=by simp at h1;omega rcases this with e | e;. exact Or.inl (П.mp (by simpa using e)) . exact Or.inr (П.mp (by simpa using e)) have h4:expOf w=0∧fracOf w=0:=by rcases h3 with rfl | rfl<;>decide simp[rationalWord,decodeD,h4,toRat_eq_zero_iff] @[inline] def T_e (w:ѓ):і := if w &&& Б == 0 then 255 else (expOf w).toUInt64 @[inline] def T_m (w:ѓ):і := if w &&& Б == 0 then 0 else (fracOf w+0x800000).toUInt64 @[inline] def T_s (e0:і) (w:ѓ):і:=T_m w <<< (T_e w-e0) @[inline] def T_ok (e0:і) (w:ѓ):ј := good w && (w &&& Б == 0 || (e0<=T_e w && T_e w-e0<=37)) @[inline] def T_p (e0:і) (w:ѓ) (p:і):і := if signOf w then p else p+T_s e0 w @[inline] def T_n (e0:і) (w:ѓ) (n:і):і := if signOf w then n+T_s e0 w else n lemma qId (e0:і) (w:ѓ) (h:T_ok e0 w=true) : (T_s e0 w).toNat<2^61∧Л w = (if signOf w then -((T_s e0 w).toNat:ℚ) else ((T_s e0 w).toNat:ℚ)) * 2^((Ѡe0:Int)-150):=by unfold T_ok at h simp only[and_eq_true,or_eq_true,beq_iff_eq,decide_eq_true_eq] at h obtain ⟨hg,hz⟩:=h by_cases h0:w &&& Б=0 . have hs:T_s e0 w=0:=by unfold T_s T_m;simp[h0] rw[hs,qI7 w h0];simp . obtain ⟨h1,h2⟩:=hz.resolve_left h0;have g:=qA0 w hg;have a0:(w &&& Б).toNat≠0:=λe=>h0 (П.mp (by simpa using e));rw[qAs] at a0;have hx:=qBc w;have hf:(fracOf w).toNat=Ѡw%2^23:=qBp w;have hl:=w.toNat_lt;have x1:1<=(expOf w).toNat :=א;have x2:(expOf w).toNat<=254 :=א;have f1:(fracOf w).toNat<2^23 :=א;have ee:(T_e w).toNat=(expOf w).toNat:=by unfold T_e;simp[h0] have em:(T_m w).toNat=2^23+(fracOf w).toNat:=by unfold T_m;simp only[h0,beq_iff_eq,if_false,UInt32.toNat_toUInt64];rw[UInt32.toNat_add,З (by show _+8388608< _;omega)];show _+8388608=_;omega have l1:=Щ.1 h1;have l2:=Щ.1 h2;rw[toNat_sub_of_le _ _ h1,ee] at l2;rw[ee] at l1;have l3:(expOf w).toNat-Ѡe0<=37:=l2 have es:(T_s e0 w).toNat=(2^23+(fracOf w).toNat)*2^((expOf w).toNat-Ѡe0):=by unfold T_s;have sh:(T_e w-e0).toNat%64=(expOf w).toNat-Ѡe0:=by rw[toNat_sub_of_le _ _ h1,ee];exact З א rw[UInt64.toNat_shiftLeft,em,sh,ф];apply З;have:2^((expOf w).toNat-Ѡe0)<=2^37:=Nat.pow_le_pow_right ב l3;have:(2^23+(fracOf w).toNat)*2^((expOf w).toNat-Ѡe0)<=2^24*2^37 :=Nat.mul_le_mul א this;omega refine ⟨?_,?_⟩;. have a:2^((expOf w).toNat-Ѡe0)<=2^37:=Nat.pow_le_pow_right ב l3;have b:(2^23+(fracOf w).toNat)*2^((expOf w).toNat-Ѡe0)<=(2^24-1)*2^37 :=Nat.mul_le_mul א a;rw[es];omega . have nz:expOf w≠0:=λe=>by rw[e] at x1;simp at x1 have ew:(2:ℚ)^(((expOf w).toNat:Int)-150) =2^((expOf w).toNat-Ѡe0)*2^((Ѡe0:Int)-150):=by rw[<-т,<-а С];congr 1;omega rw[es];simp only[rationalWord,decodeD,nz,ite_false,FloatLib.Numerics.Dyadic.toRat,signedSignificand,ew];cases signOf w<;>simp<;>ring lemma qEa (e0:і) (w:ѓ) (p n:і) (V:ℚ) (B C:ћ) (hC:C=2^61-1) (hB:B+C<2^63) (hw:T_ok e0 w=true) (hp:Ѡp<=B) (hn:Ѡn<=B) (hV:V=((Ѡp:ℚ)-Ѡn)*2^((Ѡe0:Int)-150)) : (T_p e0 w p).toNat<=B+C∧(T_n e0 w n).toNat<=B+C ∧ V+Л w=(((T_p e0 w p).toNat:ℚ)-(T_n e0 w n).toNat) * 2^((Ѡe0:Int)-150):=by obtain ⟨h1,h2⟩:=qId e0 w hw have h3:(T_s e0 w).toNat<=C :=א unfold T_p T_n rw[h2,hV] cases signOf w . simp only[false_eq_true,ite_false];have e:(p+T_s e0 w).toNat=Ѡp+(T_s e0 w).toNat:=by rw[ж];exact З א rw[e];refine ⟨by omega,by omega,?_⟩;push_cast;ring . simp only[ite_true];have e:(n+T_s e0 w).toNat=Ѡn+(T_s e0 w).toNat:=by rw[ж];exact З א rw[e];refine ⟨by omega,by omega,?_⟩;push_cast;ring @[inline] def T_fin (p n e0:і) (f:Unit->ѓ):ѓ := if p == n then f () else let mag:=if p< n then n-p else p-n let L:=mag.log2 if e0+L<=24 || e0+L ≥ 277 then f () else T_rd (decide (p< n)) mag e0 L lemma qC6 (p n e0:і) (f:Unit->ѓ) (hp:Ѡp<2^63) (hn:Ѡn<2^63) (he:Ѡe0<=255) (R:ѓ) (hf:f ()=R) (hR:p≠n->∃ d:FloatLib.Numerics.Dyadic, d.toRat=((Ѡp:ℚ)-Ѡn)*2^((Ѡe0:Int)-150) ∧ R=toWord (Ъ Fmt d)):T_fin p n e0 f=R:=by unfold T_fin split . exact hf . rename_i hne;have hne':p≠n:=λe=>hne (by simp[e]);have hnn:Ѡp≠Ѡn:=λe=>hne' (UInt64.toNat_inj.mp e);obtain ⟨d,hd,hR⟩:=hR hne';dsimp only;obtain ⟨mag,hm⟩:∃ m,m=if p< n then n-p else p-n:=⟨_,rfl⟩;rw[<-hm] have hm1:Ѡmag≠0∧Ѡmag<2^63 ∧ (((if decide (p< n)=true then -(Ѡmag:ℚ) else (Ѡmag:ℚ))) =(Ѡp:ℚ)-Ѡn):=by by_cases c:p< n . have c':=ص.1 c;have:Ѡmag=Ѡn-Ѡp:=by rw[hm,х c,toNat_sub_of_le _ _ (Щ.2 c'.le)] refine ⟨by omega,by omega,?_⟩;rw[this,Nat.cast_sub c'.le];simp[c] . have c':Ѡn<=Ѡp:=by have:=ص.not.1 c;omega have:Ѡmag=Ѡp-Ѡn:=by rw[hm,г c,toNat_sub_of_le _ _ (Щ.2 c')] refine ⟨by omega,by omega,?_⟩;rw[this,Nat.cast_sub c'];simp[c] obtain ⟨m0,m1,m2⟩:=hm1;split;. exact hf . rename_i hb;simp only[or_eq_true,decide_eq_true_eq,not_or,ge_iff_le] at hb;obtain ⟨b1,b2⟩:=hb;have eL:mag.log2.toNat=mag.toNat.log2:=rfl;have hL:mag.toNat.log2<=62:=by have:=(Nat.log2_lt m0).2 m1;omega have eA:(e0+mag.log2).toNat=Ѡe0+mag.toNat.log2:=by rw[ж,eL];exact З א have b3:¬ (e0+mag.log2).toNat<=(24:і).toNat:=λh=>b1 (Щ.2 h);have b4:¬ (277:і).toNat<=(e0+mag.log2).toNat:=λh=>b2 (Щ.2 h);rw[eA] at b3 b4;have t1:(24:і).toNat=24:=rfl;have t2:(277:і).toNat=277:=rfl;rw[qGz _ _ _ m0 m1 א א,hR];congr 1 have s0:d.significand≠0:=by intro h0;have:d.toRat=0:=(ي d).2 h0;rw[hd] at this;have q:(Ѡp:ℚ)-Ѡn≠0:=by rw[sub_ne_zero];exact_mod_cast hnn exact (mul_ne_zero q (zpow_ne_zero _ С)) this refine roundDyadic_congr Fmt ה _ _ m0 s0 ?_;rw[hd,<-m2];simp only[FloatLib.Numerics.Dyadic.toRat,signedSignificand];cases decide (p< n)<;>simp lemma qFl (w:ѓ):(T_e w).toNat<=255:=by unfold T_e;split;. decide . have:=qBc w;simp only[UInt32.toNat_toUInt64];omega @[inline] def T_min (x y:і):і:=if x< y then x else y lemma qBz (x y:і):(T_min x y).toNat<=Ѡx:=by unfold T_min;split;. exact le_rfl . have:=ص.not.1 ‹_›;omega lemma qDx (e0:і) (w0 w1:ѓ) (h0:T_ok e0 w0=true) (h1:T_ok e0 w1=true) : (T_p e0 w1 (T_p e0 w0 0)).toNat<=2*(2^61-1)∧(T_n e0 w1 (T_n e0 w0 0)).toNat<=2*(2^61-1) ∧ Л w0+Л w1=(((T_p e0 w1 (T_p e0 w0 0)).toNat:ℚ) - (T_n e0 w1 (T_n e0 w0 0)).toNat)*2^((Ѡe0:Int)-150):=by obtain ⟨a1,a2,a3⟩:=qEa e0 w0 0 0 0 0 _ rfl ב h0 ג ג ג obtain ⟨b1,b2,b3⟩:=qEa e0 w1 _ _ _ _ _ rfl ב h1 a1 a2 a3 rw[zero_add] at b3 exact ⟨by omega,by omega,b3⟩ lemma qF4 (e0:і) (w0 w1:ѓ) (p n:і) (V:ℚ) (hp:Ѡp<=2*(2^61-1)) (hn:Ѡn<=2*(2^61-1)) (hV:V=((Ѡp:ℚ)-Ѡn)*2^((Ѡe0:Int)-150)) (h0:T_ok e0 w0=true) (h1:T_ok e0 w1=true) : (T_p e0 w1 (T_p e0 w0 p)).toNat<2^63∧(T_n e0 w1 (T_n e0 w0 n)).toNat<2^63 ∧ V+Л w0+Л w1=(((T_p e0 w1 (T_p e0 w0 p)).toNat:ℚ) - (T_n e0 w1 (T_n e0 w0 n)).toNat)*2^((Ѡe0:Int)-150):=by obtain ⟨a1,a2,a3⟩:=qEa e0 w0 p n V _ _ rfl ב h0 hp hn hV obtain ⟨b1,b2,b3⟩:=qEa e0 w1 _ _ _ _ _ rfl ב h1 a1 a2 a3 exact ⟨by omega,by omega,b3⟩ lemma qA7 (e0:і) (w:ѓ) (h:T_ok e0 w=true):expOf w≠255:=by unfold T_ok at h;simp only[and_eq_true] at h;exact qAd w h.1 end @[noinline] def addSlow (relu:ј) (x y:њ):њ := act relu (toWord (FloatLib.Floats.Formats.BinaryInterchange.Model.add (ofW x) (ofW y))) section open Float.Model Float.Model.UnpackedFloat @[noinline] def addT (relu:ј) (x y:њ):њ := let e0:=T_min (T_e x) (T_e y) if T_ok e0 x && T_ok e0 y then act relu (T_fin (T_p e0 y (T_p e0 x 0)) (T_n e0 y (T_n e0 x 0)) e0 λ_=>toWord (FloatLib.Floats.Formats.BinaryInterchange.Model.add (ofW x) (ofW y))) else addSlow relu x y lemma qGs (relu:ј) (x y:њ):addT relu x y=addSlow relu x y:=by unfold addT;dsimp only;split . rename_i hc;simp only[and_eq_true] at hc;obtain ⟨hx,hy⟩:=hc;obtain ⟨p1,n1,v⟩:=qDx _ x y hx hy;have ex:=qA7 _ x hx;have ey:=qA7 _ y hy;unfold addSlow;congr 1 refine qC6 _ _ _ _ א א ((qBz _ _).trans (qFl x)) _ rfl λ_ =>⟨Model.addDyadic (decodeD x) (decodeD y),?_,?_⟩ . rw[Model.addDyadic,add_toRat];exact v . rw[Model.Proof.add_eq_spec];have e:=Ц x ex;have f:=Ц y ey;simp only[Model.Spec.add,ofW,e,f] . rfl end @[inline] def addOne (relu:ј) (x y:њ):њ := if sm x && sm y then let r:=zivS (dv x+dv y) if r == Д then addT relu x y else act relu r else addSlow relu x y section lemma qJm (w:ѓ) (h0:expOf w≠0) (h1:expOf w≠255):wv w≠0:=by have:sv w≠0:=by unfold sv mantOf;split<;>simp[h0]<;>omega simp[qAq w h1,rationalWord,decodeD_toRat,this,zpow_ne_zero] lemma qG9 (w:ѓ) (h:sm w=true):good w=true∧|wv w| ≤ 2^99:=by obtain ⟨hg,hl⟩:=п.1 h;have h1:=Ж.1 (of_decide_eq_true hl);rw[qAs] at h1;have h2:=qA0 w hg;rw[qBv w hg];refine ⟨hg,(qB1 _ _ h1 א).trans ?_⟩;unfold A_mag;norm_num lemma qIx (A P Q T g:ћ) (hg:g=0∨g=2^63) (ha:A<2^64) (e1:(A+2)%2^64=g+P) (e2:(2^64-2+A)%2^64=g+Q) (t1:2^61<=T) (t2:T<2^63-2^60) (l1:T< P+2^28) (u1:P<=T+2^28) (l2:T< Q+2^28) (u2:Q<=T+2^28) : A=g+(Q+2)∧T+2< Q+2+2^28∧Q+2+2<=T+2^28:=by omega lemma qJe (G f F:ћ) (hl:G+f+2< G+F+2^28) (hu:G+F+2<=G+f+2^28) : F+2<=f+2^28∧f+2< F+2^28:=by omega lemma qE1 (s:ѕ) (h:zivS s≠Д) : ∃ (S:ј) (ex fr M:ћ),897<=ex∧ex<1151∧fr<2^23 ∧ zivS s=ш ((if S then 2^31 else 0)+(ex-896)*2^23+fr) ∧ s.toBits.toNat=(if S then 2^63 else 0)+M ∧ (ex*2^23+fr)*2^29+2< M+2^28 ∧ M+2<=(ex*2^23+fr)*2^29+2^28:=by obtain ⟨S,ex,fr,h1,h2,h3,hz,⟨P,e1,l1,u1⟩,⟨Q,e2,l2,u2⟩⟩:=qDc _ _ h rw[ж] at e1 rw[UInt64.toNat_sub] at e2 obtain ⟨a,b,c⟩:=qIx _ P Q ((ex*2^23+fr)*2^29) _ (by cases S<;>simp) s.toBits.toNat_lt e1 e2 א א l1 u1 l2 u2 exact ⟨S,ex,fr,Q+2,h1,h2,h3,hz,a,b,c⟩ lemma qIs (v W z u:ℝ) (hu:0< u) (hz:z<=u) (h0:0<=v) (hv:v<=2^53*u) (h : |v-W| ≤ 1/2^53*(|W| + z)):v-2*u< W∧W< v+2*u:=by have h1 : |W| ≤ v + |v-W|:=by have:=Գ (v-(v-W)) (-(v-W)+(v-W));have e:W=v-(v-W):=by ring have a:=Ф W v;rw[abs_sub_comm W v,з h0] at a;linarith have h2:=abs_lt.mp (show |v-W| < 2*u by nlinarith);constructor<;>linarith [h2.1,h2.2] set_option maxRecDepth 8192 in lemma qJa (ex fr E F:ћ) (h1:897<=ex) (h2:ex<1151) (hfr:fr<2^23) (hF:F<2^52) (hl:(ex*2^23+fr)*2^29+2< E*2^52+F+2^28) (hu:E*2^52+F+2<=(ex*2^23+fr)*2^29+2^28) (W:ℝ) (h : |((2^52+F:ћ):ℝ)*2^((E:Int)-1075)-W| ≤ 1/2^53*(|W| + A_z)) : W<(2^53+fr*2^30+2^29)*2^((ex:Int)-1076) ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)< W) ∧ (2^53-2^28)*2^((ex:Int)-1076)< W:=by have hE:E=ex∨(E+1=ex∧fr=0) :=א have hu0:(0:ℝ)<2^((E:Int)-1075):=ػ ב _ have hz:A_z<=2^((E:Int)-1075):=by have e:A_z=2^(-1021:ℤ):=rfl rw[e] exact о ב א have hN:((2^52+F:ћ):ℝ)<=2^53:=by have:2^52+F<=2^53 :=א exact_mod_cast this have hN0:(0:ℝ)<=((2^52+F:ћ):ℝ):=Э _ obtain ⟨a,b⟩:=qIs _ W A_z _ hu0 hz (ю hN0 hu0.le) (Г hN hu0.le) h push_cast at a b rcases hE with rfl | ⟨rfl,rfl⟩ . have e:(2:ℝ)^((E:Int)-1075)=2*2^((E:Int)-1076):=by rw[show (E:Int)-1075=1+((E:Int)-1076) by ring,а С,zpow_one] rw[e] at a b;have hb:(0:ℝ)<2^((E:Int)-1076):=ػ ב _;generalize (2:ℝ)^((E:Int)-1076)=c at a b hb ⊢;have e1:(E*2^23+fr)*2^29=E*2^52+fr*2^29:=by ring rw[e1] at hl hu;obtain ⟨n3,n4⟩:=qJe _ _ _ hl hu;have h3:(F:ℝ)+2<=fr*2^29+2^28:=by exact_mod_cast n3 have h4:(fr:ℝ)*2^29+2< F+2^28:=by exact_mod_cast n4 have h5:(0:ℝ)<=F:=Э _;have h6:(0:ℝ)<=fr:=Э _;have h7:=Г h3 hb.le;have h8:=Я h4 hb;have h9:=ю h5 hb.le;have h10:=ю h6 hb.le;refine ⟨?_,?_,?_⟩;. nlinarith . intro _;nlinarith . nlinarith . have e:(2:ℝ)^(((E+1:ћ):Int)-1076)=2^((E:Int)-1075):=by congr 1;push_cast;ring rw[e];generalize (2:ℝ)^((E:Int)-1075)=c at a b hu0 ⊢;have h3:(F:ℝ)<=2^52:=by exact_mod_cast hF.le have h4:(2:ℝ)^52+2< F+2^28:=by exact_mod_cast (by omega:2^52+2< F+2^28) have h7:=Г h3 hu0.le;have h8:=Я h4 hu0;refine ⟨?_,?_,?_⟩;. nlinarith . intro h0;exact absurd rfl h0 . nlinarith lemma qDd (s:ѕ) (d:FloatLib.Numerics.Dyadic) (hV : |R s-(d.toRat:ℝ)| ≤ 1/2^53*(|(d.toRat:ℝ)| + A_z)) (h:zivS s≠Д):zivS s=toWord (Ъ Fmt d):=by obtain ⟨S,ex,fr,M,h1,h2,h3,hz,hb,hl,hu⟩:=qE1 s h;have hM:M=(M/2^52)*2^52+M%2^52 :=א;have hF:M%2^52<2^52:=щ _ ב;generalize M/2^52=E at hM;generalize M%2^52=F at hM hF;subst hM;obtain ⟨-,hR⟩:=B_R s S E F א א hF (by rw[hb,и]);rw[hR] at hV obtain ⟨W,hVW,hW⟩:∃ W:ℝ,(d.toRat:ℝ)=(if S then -W else W) ∧ |((2^52+F:ћ):ℝ)*2^((E:Int)-1075)-W| ≤ 1/2^53*(|W| + A_z):=by cases S . simp only[false_eq_true,ite_false,one_mul] at hV ⊢;exact ⟨_,rfl,hV⟩ . simp only[ite_true] at hV ⊢;refine ⟨-(d.toRat:ℝ),by simp,?_⟩;rw[abs_neg,show ((2^52+F:ћ):ℝ)*2^((E:Int)-1075) - -(d.toRat:ℝ) =-(-1*((2^52+F:ћ):ℝ)*2^((E:Int)-1075)-(d.toRat:ℝ)) by ring,abs_neg];exact hV obtain ⟨a,b,c⟩:=qJa ex fr E F h1 h2 h3 hF א א W hW;rw[hz];exact (qFd d S ex fr h1 h2 h3 W hVW a b c).symm lemma qFa (w:ѓ) (hg:good w=true) : ∃ (m k:ℤ),wv w=(m:ℝ)*2^k∧m.natAbs<2^25∧-160<=k:=by have hN:=w.toNat_lt;rw[qA9 w hg];unfold A_mag;by_cases h0:Ѡw%2^31=0;. exact ⟨0,0,by rw[х h0];simp,by norm_num,by norm_num⟩ . simp only[h0,ite_false];have hk:-160<=(((Ѡw%2^31/2^23:ℕ):ℤ)-150) :=א;have hM:2^23+Ѡw%2^31%2^23<2^25 :=א;generalize ((Ѡw%2^31/2^23:ℕ):ℤ)-150=k at hk ⊢;generalize 2^23+Ѡw%2^31%2^23=M at hM ⊢;by_cases hs:2^31<=Ѡw;. exact ⟨-(M:ℤ),k,by rw[х hs];push_cast;ring,by simpa using hM,hk⟩ . exact ⟨(M:ℤ),k,by rw[г hs];push_cast;ring,by simpa using hM,hk⟩ lemma qHk (a b:ѓ) (ha:good a=true) (hb:good b=true) : (dv a*dv b).isFinite=true∧R (dv a*dv b)=wv a*wv b:=by obtain ⟨fa,ra⟩:=qAp a ha;obtain ⟨fb,rb⟩:=qAp b hb;have la:=(qAh a ha).trans qAi;have lb:=(qAh b hb).trans qAi obtain ⟨h1,h2⟩:=qEm _ _ fa fb (by rw[ra,rb] exact (ч la lb (ц _) qJo.le).trans qC3) refine ⟨h1,?_⟩;obtain ⟨m,k,e1,h3,h4⟩:=qFa a ha;obtain ⟨n,l,e2,h5,h6⟩:=qFa b hb;have e:wv a*wv b=((m*n:ℤ):ℝ)*2^(k+l):=by rw[e1,e2,а С];push_cast;ring rw[h2,ra,rb,e];refine qAu _ _ ?_ א;rw[Int.natAbs_mul];have:=Nat.mul_lt_mul'' h3 h5;omega lemma S_E (x y z:ѓ) (hx:good x=true) (hy:good y=true) (hz:good z=true) (d:FloatLib.Numerics.Dyadic) (hd:(d.toRat:ℝ)=wv x*wv y+wv z) : |R (dv x*dv y+dv z)-(d.toRat:ℝ)| ≤ 1/2^53*(|(d.toRat:ℝ)| + A_z):=by obtain ⟨f1,r1⟩:=qHk x y hx hy obtain ⟨-,-,l⟩:=qEy x y hx hy obtain ⟨f2,r2⟩:=qAp z hz have h256:(2:ℝ)^256<=A_B:=by have:(2:ℝ)^256<=2^256*2^42 :=ב linarith [qBk] obtain ⟨-,he⟩:=qBl _ _ f1 f2 (by rw[r1];exact l.trans h256) (by rw[r2];exact (qAh z hz).trans qAi) rw[r1,r2] at he rw[hd] exact he lemma qCs (relu:ј) (x y:њ) : addOne relu x y=act relu (toWord (Model.add (ofW x) (ofW y))):=by unfold addOne;split . rename_i hc;simp only[and_eq_true] at hc;obtain ⟨gx,-⟩:=qG9 x hc.1;obtain ⟨gy,-⟩:=qG9 y hc.2;dsimp only;split;. exact qGs relu x y . rename_i hr;have hn:zivS (dv x+dv y)≠Д:=λh=>hr (by simp[h]);have ex:=qAd _ gx;have ey:=qAd _ gy;have hd:((Model.addDyadic (decodeD x) (decodeD y)).toRat:ℝ)=wv x+wv y:=by rw[Model.addDyadic,add_toRat,qAq x ex,qAq y ey];simp only[rationalWord,Rat.cast_add] obtain ⟨fx,rx⟩:=qAp x gx;obtain ⟨fy,ry⟩:=qAp y gy;obtain ⟨-,he⟩:=qBl _ _ fx fy (by rw[rx];exact (qAh x gx).trans qAi) (by rw[ry];exact (qAh y gy).trans qAi);rw[rx,ry,<-hd] at he;rw[qDd _ _ he hn];congr 2;rw[Model.Proof.add_eq_spec];have e:=Ц x ex;have f:=Ц y ey;simp only[Model.Spec.add,ofW,e,f] . rfl lemma qEx {L:є->ѐ->ѐ} (f:є->ѓ->ѓ) {e:є} (hL:∀ i x,L i x=if i< e then L (i+1) (wrW x i (f i (rdW x i))) else x) (d:ћ) : ∀ (i:є) (x:ѐ),Ѡi+d=Ѡe->Ѡe<=x.size->x.size<2^32 → (L i x).size=x.size∧∀ q,(L i x)[q]! =if Ѡi<=q∧q< Ѡe then f (Е q) x[q]! else x[q]!:=by induction d with | zero => intro i x h1 _ _ rw[hL,г ט] exact ⟨rfl,λq=>(г א).symm⟩ | succ d ih => intro i x h1 h2 h3 have hi:i< e:=А.2 א have hs:=qAx x i (f i (rdW x i)) rw[hL,х hi] obtain ⟨h4,h5⟩:=ih (i+1) (wrW x i (f i (rdW x i))) (by rw[usz i e hi];omega) א א refine ⟨h4.trans hs,λq=>?_⟩ rw[h5,qA4 _ _ _ _ h3 א,usz i e hi,qAk _ _ h3] by_cases h6:q=Ѡi . subst h6;rw[г א,х rfl,х א,USize.ofNat_toNat] . rw[г h6];exact if_congr א rfl rfl def addGo (relu:ј) (y:ё) (e i:є) (x:ё) : ё := if h:i< e then addGo relu y e (i+1) (wrW x i (addOne relu (rdW x i) (rdW y i))) else x termination_by Ѡe-Ѡi decreasing_by rw[usz i e h];have:=А.mp h;omega def addF (relu:ј) (a b:ё):ё := if a.size=b.size∧a.size<2^32 then addGo relu b a.usize 0 a else Array.ofFn (n:=a.size) λi=>addOne relu a[i] b[i.val]! lemma qF0:addF=addReluSpec:=by funext relu a b;unfold addF;split . rename_i h;obtain ⟨h1,h2⟩:=h;have hu:a.usize.toNat=a.size:=qAb _ h2;have hg:=qEx (L:=addGo relu b a.usize) (λi w=>addOne relu w (rdW b i)) (λ_ _=>by rw[addGo];rfl) a.size 0 a (by rw[hu,show (0:є).toNat=0 from rfl,И]) א h2;apply Array.ext (by rw[hg.1];simp[addReluSpec]);intro i h3 h4;have hi:i< a.size:=by rwa [hg.1] at h3 rw[<-Ш _ i h3,hg.2 i,х ⟨Nat.zero_le _,by omega⟩,qCs,Ш a i hi,qAk _ _ א,uk i א];simp[addReluSpec] . simp only[addReluSpec,qCs] end section @[noinline] def E_fma (relu:ј) (w sw tw:ѓ):ѓ := act relu (toWord (Model.fma (ofW w) (ofW sw) (ofW tw))) @[inline] def E_act (relu:ј) (r:ѓ):ѓ := if relu && signOf r then 0 else r @[noinline] def E_fz (relu g:ј) (sw tw:ѓ) (y z:ѕ) (w:ѓ):ѓ := if g && good w then let r:=zivS (dv w*y+z) if r == Д then E_fma relu w sw tw else E_act relu r else E_fma relu w sw tw @[inline] def E_el (relu g:ј) (sw tw:ѓ) (y z:ѕ) (w:ѓ):ѓ := let r:=zivS (dvN w*y+z) if g && r != Д then E_act relu r else E_fz relu g sw tw y z w @[specialize] def E_map (f:ѓ->ѓ) (e i:є) (x:ѐ):ѐ := if h:i< e then E_map f e (i+1) (wrW x i (f (rdW x i))) else x termination_by Ѡe-Ѡi decreasing_by rw[usz i e h];have:=А.mp h;omega def E_chs (relu:ј) (c hw:ћ) (s t:ѐ) (ch:ћ) (x:ѐ):ѐ := if ch< c then let sw:=s[ch]! let tw:=t[ch]! let y:=dv sw let z:=dv tw E_chs relu c hw s t (ch+1) (E_map (E_el relu (good sw && good tw) sw tw y z) ((ch+1)*hw).toUSize (ch*hw).toUSize x) else x termination_by c-ch def scaleF (relu:ј) (c hw:ћ) (x s t:ѐ):ѐ := if x.size=c*hw∧x.size<2^32 then E_chs relu c hw s t 0 x else scaleShiftSpec relu c hw x s t @[noinline] def E_dot (X:ѐ) (b:є) (k w:ћ):ѓ := dotFn (k*k) (λt=>X[Ѡb+(t/k*w+t%k)]!) (λ_=>oneW) section open Float.Model Float.Model.UnpackedFloat def E_dotT (X:ѐ) (b:є) (k w:ћ):ѓ := if k=2 then let i:=Ѡb let a0:=X[i]! let a1:=X[i+1]! let a2:=X[i+w]! let a3:=X[i+(w+1)]! let e0:=T_min (T_min (T_e a0) (T_e a1)) (T_min (T_e a2) (T_e a3)) if T_ok e0 a0 && T_ok e0 a1 && T_ok e0 a2 && T_ok e0 a3 then T_fin (T_p e0 a3 (T_p e0 a2 (T_p e0 a1 (T_p e0 a0 0)))) (T_n e0 a3 (T_n e0 a2 (T_n e0 a1 (T_n e0 a0 0)))) e0 λ_=>E_dot X b k w else E_dot X b k w else E_dot X b k w lemma qFi (X:ѐ) (b:є) (k w:ћ):E_dotT X b k w=E_dot X b k w:=by unfold E_dotT;split;swap;rfl;subst_vars;dsimp only;split;swap;rfl;rename_i hc;simp at hc;obtain ⟨⟨⟨h0,h1⟩,h2⟩,h3⟩:=hc;obtain ⟨p1,n1,v⟩:=qDx _ _ _ h0 h1;obtain ⟨p2,n2,v⟩:=qF4 _ _ _ _ _ _ p1 n1 v h2 h3;refine qC6 _ _ _ _ p2 n2 ((qBz _ _).trans ((qBz _ _).trans (qFl _))) _ rfl λe=>?_;have B:=qB6 (2*2) (λj=>X[Ѡb+(j/2*w+j%2)]!) (λ_=>oneW) λj g =>⟨by interval_cases j<;>simp[qA7 _ _ h0,qA7 _ _ h1,qA7 _ _ h2,qA7 _ _ h3],by decide⟩;simp[Finset.sum_range_succ,qGd] at B;rw[v] at B;rw[E_dot,qAc];exact B (mul_ne_zero (sub_ne_zero.2 λh=>e (UInt64.toNat_inj.1 (Nat.cast_injective h))) (zpow_ne_zero _ С)) end @[noinline] def E_div (n:ћ) (s:ѓ):ѓ := toWord (Model.div (ofW s) (ofW (natW n))) @[inline] def E_quo (n:ћ) (j s:ѓ):ѓ := if j< expOf s∧expOf s≠255 then s-(j <<< (23:ѓ)) else E_div n s @[inline] def E_tl (X:ѐ) (b:є) (k w n:ћ) (j z:ѓ) (nf zb s a:ѕ) (o:ѓ):ѓ := if o=0 then z else let r:=ziv (s+zb) (nf*(a+zb.abs)) E_quo n j (if r == Д then E_dotT X b k w else r) def E_win (X off:ѐ) (k w n:ћ) (j z:ѓ) (u nf zb:ѕ) (b m t:є) (s a:ѕ) (o:ѓ):ѓ := if h:t< m then let v:=rdW X (b+(rdW off t).toUSize) let d:=dv v*u E_win X off k w n j z u nf zb b m (t+1) (s+d) (a+d.abs) (o ||| v) else E_tl X b k w n j z nf zb s a o termination_by Ѡm-Ѡt decreasing_by rw[usz t m h];have:=А.mp h;omega @[specialize] def G_x (f:є->ѓ) (sx:є) (W x:ћ) (b q:є) (out:ѐ):ѐ := if x< W then G_x f sx W (x+1) (b+sx) (q+1) (wrW out q (f b)) else out termination_by W-x @[specialize] def G_y (f:є->ѓ) (sy sx:є) (W H y:ћ) (b q:є) (out:ѐ):ѐ := if y< H then G_y f sy sx W H (y+1) (b+sy) (q+W.toUSize) (G_x f sx W 0 b q out) else out termination_by H-y @[specialize] def G_c (f:є->ѓ) (sc sy sx:є) (W H c ch:ћ) (b q:є) (out:ѐ):ѐ := if ch< c then G_c f sc sy sx W H c (ch+1) (b+sc) (q+(H*W).toUSize) (G_y f sy sx W H 0 b q out) else out termination_by c-ch def E_off (k w:ћ):ѐ := Array.ofFn (n:=k*k) λt=>(t.val/k*w+t.val%k).toUInt32 @[noinline] def E_grid (c h w k n:ћ) (X off:ѐ) (j z:ѓ) (u nf zb zr:ѕ) : ѐ := G_c (λb=>E_win X off k w n j z u nf zb b n.toUSize 0 zr zr 0) (h*w).toUSize (k*w).toUSize k.toUSize (w/k) (h/k) c 0 0 0 (Й (c*(h/k*(w/k))) 0) @[inline] def E_w4 (X:ѐ) (k w n:ћ) (j z:ѓ) (u nf zb zr:ѕ) (a0 a1 a2 a3 b:є):ѓ := let v0:=rdW X (b+a0) let d0:=dv v0*u let v1:=rdW X (b+a1) let d1:=dv v1*u let v2:=rdW X (b+a2) let d2:=dv v2*u let v3:=rdW X (b+a3) let d3:=dv v3*u E_tl X b k w n j z nf zb (zr+d0+d1+d2+d3) (zr+d0.abs+d1.abs+d2.abs+d3.abs) (0 ||| v0 ||| v1 ||| v2 ||| v3) @[noinline] def E_gridK (c h w k n:ћ) (X off:ѐ) (j z:ѓ) (u nf zb zr:ѕ) : ѐ := if k == 2 then let a0:=(rdW off 0).toUSize let a1:=(rdW off (0+1)).toUSize let a2:=(rdW off (0+1+1)).toUSize let a3:=(rdW off (0+1+1+1)).toUSize G_c (λb=>E_w4 X k w n j z u nf zb zr a0 a1 a2 a3 b) (h*w).toUSize (k*w).toUSize k.toUSize (w/k) (h/k) c 0 0 0 (Й (c*(h/k*(w/k))) 0) else E_grid c h w k n X off j z u nf zb zr def avgF (c h w k:ћ) (X:ѐ):ѐ := if c*h*w<=X.size∧X.size<2^32∧k*k<2^20 ∧ scan X X.usize 0 (qAe X) Д 0≠Д then let n:=k*k let off:=E_off k w let j:ѓ:=if n=4 then 2 else if n=64 then 6 else 255 let z:ѓ:=if 0< k∧k<17 then 0 else E_div n (dotFn n (λ_=>0) (λ_=>oneW)) E_gridK c h w k n X off j z (dv oneW) (nfOf n) (dv 0) 0 else avgpoolSpec c h w k X def E_d (x y z:ѓ):FloatLib.Numerics.Dyadic := Model.addDyadic ⟨Bool.xor (decodeD x).negative (decodeD y).negative, (decodeD x).significand*(decodeD y).significand, (decodeD x).exponent+(decodeD y).exponent⟩ (decodeD z) section variable (x y z:ѓ) (hx:expOf x≠255) (hy:expOf y≠255) (hz:expOf z≠255) include x y z hx hy hz lemma qC9 : Model.fma (ofW x) (ofW y) (ofW z)=Ъ Fmt (E_d x y z):=by rw[Model.Proof.fma_eq_spec];have e:=Ц x hx;have f:=Ц y hy;have g:=Ц z hz;simp[Model.Spec.fma,ofW,(Model.chooseNaN3_eq_none_iff _ _ _).2 ⟨Model.isNaN_eq_false_of_toDyadic?_some e,Model.isNaN_eq_false_of_toDyadic?_some f,Model.isNaN_eq_false_of_toDyadic?_some g⟩,Model.isInf_eq_false_of_toDyadic?_some e,Model.isInf_eq_false_of_toDyadic?_some f,Model.isInf_eq_false_of_toDyadic?_some g,e,f,g];rfl lemma qDv : ((E_d x y z).toRat:ℝ)=wv x*wv y+wv z:=by have j:=mul_toRat (decodeD x) (decodeD y);simp only[FloatLib.Numerics.Dyadic.mul,mulFields] at j;rw[E_d,Model.addDyadic,add_toRat,j,qAq x hx,qAq y hy,qAq z hz];simp[rationalWord] end lemma qG6 (relu:ј) (r:ѓ) (hf:expOf r≠255):E_act relu r=act relu r:=by cases relu<;>simp[E_act,act,reluW,hf,signOf] lemma qGc (relu:ј) (s:ѕ) (h:zivS s≠Д) : E_act relu (zivS s)=act relu (zivS s):=by apply qG6;obtain ⟨S,ex,fr,h1,h2,h3,hz,-,-⟩:=qDc _ _ h;have hz':zivS s=_:=hz;intro h0;have h4:(expOf (zivS s)).toNat=255:=by rw[h0];rfl rw[qBc,hz',ق] at h4;split at h4<;>omega section open Float.Model Float.Model.UnpackedFloat local notation:max x:max "ᶠ"=>Float.isFinite x=true lemma qD9 (s:Sign):(Float.Model.pack (UnpackedFloat.infinity s)).unpack.isFinite=false:=by cases s<;>decide +kernel lemma qFu:(Float.Model.pack UnpackedFloat.notANumber).unpack.isFinite=false:=by decide +kernel lemma qGf (x y:ѕ) (h:(x*y)ᶠ):xᶠ∧yᶠ:=by change (Float.Model.mul x.toModel y.toModel).unpack.isFinite=true at h;change x.toModel.unpack.isFinite=true∧y.toModel.unpack.isFinite=true;rw[Float.Model.mul] at h;cases a:x.toModel.unpack<;>cases b:y.toModel.unpack <;>simp only[a,b,UnpackedFloat.mul,qD9,qFu,false_eq_true] at h<;>exact ⟨rfl,rfl⟩ lemma qIk (x y:ѕ) (h:(x+y)ᶠ):xᶠ∧yᶠ:=by change (Float.Model.add x.toModel y.toModel).unpack.isFinite=true at h;change x.toModel.unpack.isFinite=true∧y.toModel.unpack.isFinite=true;rw[Float.Model.add] at h cases a:x.toModel.unpack<;>cases b:y.toModel.unpack <;>simp only[a,b,UnpackedFloat.add,qD9,qFu,false_eq_true] at h <;> first | exact ⟨rfl,rfl⟩ | (split at h<;>simp only[qD9,qFu,false_eq_true] at h) lemma qIj:(й 0x7FF8000000000000).isFinite=false:=by decide +kernel lemma qGe (i:є) (h:Ѡi< pwN.size):pwN.uget i h=pwEntryN Ѡi:=by simp[FloatArray.uget,qCy] def N_ok (w:ѓ):Prop:=0x800000<=w &&& Б∧w &&& Б<Р lemma qIa {w:ѓ} (h:N_ok w):good w=true :=qBj _ (Or.inr h.1) h.2 lemma N_w (w:ѓ) (h:(pwN.uget (w >>> (23:ѓ)).toUSize (qCi w))ᶠ) : pwN.uget (w >>> (23:ѓ)).toUSize (qCi w)=pw.uget (w >>> (23:ѓ)).toUSize (qCb w) ∧ N_ok w:=by have s:=qEr w rw[qGe,Ы,s] at h rw[qGe,qC5,Ы,s] unfold pwEntryN at h ⊢ have e:=qAs w have g:=w.toNat_lt by_cases c:(Ѡw/2^23%256 == 0 || Ѡw/2^23%256 == 255)=true . rw[х c,qIj] at h;exact absurd h ה . rw[г c];simp only[or_eq_true,beq_iff_eq,not_or] at c;refine ⟨rfl,?_,?_⟩;. rw[Ж,e,show (0x800000:ѓ).toNat=0x800000 from rfl];omega . rw[У,e,show (Р:ѓ).toNat=Р from rfl];omega lemma qJn {w:ѓ} (h:(dvN w)ᶠ):dvN w=dv w∧N_ok w:=by obtain ⟨e,o⟩:=N_w w (qGf _ _ h).2;exact ⟨by unfold dvN dv;rw[e],o⟩ end lemma S_w (T M:ћ) (h1:T+2< M+2^28) (h2:M+2<=T+2^28) : T< M+2^28∧M<=T+2^28:=by omega lemma qIz (s:ѕ) (h:zivS s≠Д):s.isFinite=true:=by obtain ⟨S,ex,fr,M,h1,h2,h3,-,hb,hl,hu⟩:=qE1 s h;exact (qAn _ S ex fr h1 h2 h3 ⟨M,hb,(S_w _ _ hl hu).1,(S_w _ _ hl hu).2⟩).1 section variable (relu g:ј) (sw tw w:ѓ) (hg:g=true->good sw=true∧good tw=true) include relu g sw tw w hg lemma qGk : E_fz relu g sw tw (dv sw) (dv tw) w=E_fma relu w sw tw:=by unfold E_fz;split;swap;rfl;rename_i hc;obtain ⟨h1,h2⟩:=п.1 hc;obtain ⟨h3,h4⟩:=hg h1;dsimp only;split;rfl;rename_i hr;have r:=mt beq_iff_eq.2 hr;have hf:=qAd _ h2;have g1:=qAd _ h3;have g2:=qAd _ h4;rw[qGc _ _ r,qDd _ (E_d w sw tw) (S_E w sw tw h2 h3 h4 _ (qDv _ _ _ hf g1 g2)) r,E_fma,qC9 _ _ _ hf g1 g2] lemma qGr : E_el relu g sw tw (dv sw) (dv tw) w=E_fma relu w sw tw:=by unfold E_el;dsimp only;split;. rename_i hc;obtain ⟨h1,hb⟩:=п.1 hc;obtain ⟨h3,h4⟩:=hg h1;have hr0:zivS (dvN w*dv sw+dv tw)≠Д:=bne_iff_ne.1 hb;obtain ⟨e,o⟩:=qJn (qGf _ _ (qIk _ _ (qIz _ hr0)).1).1;rw[e] at hr0 ⊢;have hr':=hr0;have h2:=qIa o;have hf:=qAd _ h2;have g1:=qAd _ h3;have g2:=qAd _ h4;rw[qGc _ _ hr',qDd _ (E_d w sw tw) (S_E w sw tw h2 h3 h4 _ (qDv _ _ _ hf g1 g2)) hr',E_fma,qC9 _ _ _ hf g1 g2] . exact qGk relu g sw tw w hg end lemma qD8 (relu:ј) (c hw:ћ) (s t:ѐ) (d:ћ) : ∀ (ch:ћ) (x:ѐ),ch+d=c->x.size=c*hw->x.size<2^32 → (E_chs relu c hw s t ch x).size=x.size∧∀ q,q< c*hw → (E_chs relu c hw s t ch x)[q]! =if ch*hw<=q then E_fma relu x[q]! s[q/hw]! t[q/hw]! else x[q]!:=by induction d with | zero => rintro ch x rfl _ _ rw[E_chs,г א] exact ⟨rfl,λq hq=>(г (by simpa using hq)).symm⟩ | succ d ih => intro ch x h1 h2 h3 rw[E_chs,х א] dsimp only have h4:=Н hw (א:ch+1<=c) have h5:=Nat.add_one_mul ch hw have e1:=qAb ((ch+1)*hw) א have e0:=qAb (ch*hw) א obtain ⟨h6,h7⟩:=qEx (λ_=>E_el relu (good s[ch]! && good t[ch]!) s[ch]! t[ch]! (dv s[ch]!) (dv t[ch]!)) (L:=E_map _ ((ch+1)*hw).toUSize) (λ_ _=>by rw[E_map];rfl) hw (ch*hw).toUSize x א א h3 obtain ⟨h8,h9⟩:=ih (ch+1) _ א (h6.trans h2) א refine ⟨h8.trans h6,λq hq=>?_⟩ rw[h9 q hq,h7,e0,e1,qGr _ _ _ _ _ п.1] by_cases a:(ch+1)*hw<=q . rw[х a,г א,х א] . rw[г a];by_cases b:ch*hw<=q;. rw[х א,х b,Nat.div_eq_of_lt_le b א] . rw[г א,г b] lemma qE9:scaleF=scaleShiftSpec:=by funext relu c hw x s t;unfold scaleF;split . rename_i h;obtain ⟨h1,h2⟩:=h;obtain ⟨h3,h4⟩:=qD8 relu c hw s t c 0 x א h1 h2;apply Array.ext;. simp[scaleShiftSpec,h3,h1] . intro i hi hj;have hi':i< c*hw:=by rwa [h3,h1] at hi rw[<-Ш _ i hi,h4 i hi',х ג];simp[scaleShiftSpec,E_fma] . rfl lemma qC8 (w:ѓ) (h:expOf w≠255) : к (ofW w)=(sv w:ℝ)*2^((idxOf w:ℤ)-149):=by simp[show Model.toReal (ofW w)=wv w from rfl,qAq w h,rationalWord,decodeD_toRat] lemma qI3 (s j:ѓ) (hj:Ѡj<=6) (a:Ѡj<(expOf s).toNat) : (expOf (s-(j <<< (23:ѓ)))).toNat=(expOf s).toNat-Ѡj ∧ fracOf (s-(j <<< (23:ѓ)))=fracOf s ∧ signOf (s-(j <<< (23:ѓ)))=signOf s:=by rw[qBc] at a have hs:=s.toNat_lt have e:(j <<< (23:ѓ)).toNat=Ѡj*2^23:=by rw[UInt32.toNat_shiftLeft,ф] exact З ז generalize j <<< (23:ѓ)=J at e ⊢ have d:=ش s J (by rw[Ж,e];omega) rw[e] at d simp only[qBc,fracOf,<-UInt32.toNat_inj,qBp,qE6,d,decide_eq_decide] omega lemma qE2 (s y j:ѓ) (f:Ѡj<=6) (a:expOf s≠255) (b:Ѡj<(expOf s).toNat) (d:expOf y≠255) (g:signOf y=false) (e:mantOf y=2^23) (k:idxOf y=126+Ѡj) : toWord (Model.div (ofW s) (ofW y))=s-(j <<< (23:ѓ)):=by have B:Model.isZero (ofW y)=false:=by rw[ofW,Model.isZero_eq_beq_zero_of_toDyadic?_some (Ц y d),decodeD_sig_zero,e];decide obtain ⟨C,D,E⟩:=qI3 s j f b;generalize s-(j <<< (23:ѓ))=r at C D E ⊢;have N:=expOf_toNat_lt s;have O:=mt П.1 a;simp at O;have l:expOf r≠255:=λh=>by simp[h] at C;omega have q:expOf r≠0:=λh=>by simp[h] at C;omega have t:expOf s≠0:=λh=>by simp[h] at b have x:к (ofW y)=2^Ѡj:=by rw[qC8 _ d,sv,g,e,k];push_cast;rw[show (126+Ѡj-149:ℤ)=Ѡj-23 by ring,ن С,т];field_simp;norm_num have I:к (ofW r)=к (ofW s)/к (ofW y):=by rw[x,qC8 _ l,qC8 _ a,show sv r=sv s by unfold sv mantOf;rw[E,D,г q,г t],show (idxOf r:ℤ)=idxOf s-Ѡj by unfold idxOf;rw[г q,г t,C];omega,sub_right_comm,ن С,т,mul_div_assoc] have o:=qDf a;have u:=qDf d;have v:=qDf l;have z:=Model.isFinite_div_of_abs_div_le_posMaxFinite _ _ ה o u B (I ▸ Model.abs_toReal_le_posMaxFinite_of_isIEEE_of_isFinite _ ה v);have p:=Model.toReal_div_eq_roundAt _ _ ה o u B z;rw[<-I,Model.roundAt_toReal_eq _ v] at p;rw[Model.eq_of_toReal_eq_of_nonzero z v p (p ▸ qJm r q l),qBd] lemma qF7 (n:ћ) (j s:ѓ) (hj:j=255∨(j=2∧n=4)∨(j=6∧n=64)) : E_quo n j s=E_div n s:=by unfold E_quo;split;swap;rfl;rename_i h;have h1:=У.1 h.1;have h3:=expOf_toNat_lt s;rcases hj with rfl | ⟨rfl,rfl⟩ | ⟨rfl,rfl⟩;. simp at h1;omega all_goals exact (qE2 s _ _ ה h.2 h1 ה ה ה ה).symm def E_f (xw:ћ->ѓ) (l:љ) (c:F1):F1 := l.foldl (λ(acc:F1) t=>let q:=dv (xw t)*dv oneW F1.mk (acc.s+q) (acc.t+q.abs)) c lemma qG2 (X off:ѐ) (k w n:ћ) (j z:ѓ) (u nf zb zr:ѕ) (b:є) (hn:n.toUSize.toNat=4) : E_win X off k w n j z u nf zb b n.toUSize 0 zr zr 0=E_w4 X k w n j z u nf zb zr (rdW off 0).toUSize (rdW off (0+1)).toUSize (rdW off (0+1+1)).toUSize (rdW off (0+1+1+1)).toUSize b:=by have h0:(0:є)< n.toUSize:=by rw[А,hn];decide have e1:=usz _ _ h0 have h1:(0+1:є)< n.toUSize:=by rw[А,hn,e1];decide have e2:=usz _ _ h1 have h2:(0+1+1:є)< n.toUSize:=by rw[А,hn,e2,e1];decide have e3:=usz _ _ h2 have h3:(0+1+1+1:є)< n.toUSize:=by rw[А,hn,e3,e2,e1];decide have e4:=usz _ _ h3 have h4:¬ (0+1+1+1+1:є)< n.toUSize:=by rw[А,hn,e4,e3,e2,e1];decide rw[E_win,К h0] dsimp only rw[E_win,К h1] dsimp only rw[E_win,К h2] dsimp only rw[E_win,К h3] dsimp only rw[E_win,О h4] rfl lemma qEe (c h w k:ћ) (X off:ѐ) (j z:ѓ) (u nf zb zr:ѕ) : E_gridK c h w k (k*k) X off j z u nf zb zr=E_grid c h w k (k*k) X off j z u nf zb zr:=by unfold E_gridK;split;. rename_i hk;have hk:=eq_of_beq hk;subst hk;have e:(λb=>E_w4 X 2 w (2*2) j z u nf zb zr (rdW off 0).toUSize (rdW off (0+1)).toUSize (rdW off (0+1+1)).toUSize (rdW off (0+1+1+1)).toUSize b) =λb=>E_win X off 2 w (2*2) j z u nf zb b (2*2).toUSize 0 zr zr 0 :=funext λb=>(qG2 X off 2 w (2*2) j z u nf zb zr b (qAb _ ב)).symm;dsimp only;rw[e,E_grid] . rfl lemma qF5 (X off:ѐ) (k w n:ћ) (j z:ѓ) (nf zb:ѕ) (b m:є) (xw:ћ->ѓ) (hx:∀ t:є,t< m->rdW X (b+(rdW off t).toUSize)=xw Ѡt) (d:ћ):∀ (t:є) (s a:ѕ) (o:ѓ),Ѡt+d=Ѡm → E_win X off k w n j z (dv oneW) nf zb b m t s a o=E_tl X b k w n j z nf zb (E_f xw (List.range' Ѡt d) ⟨s,a⟩).s (E_f xw (List.range' Ѡt d) ⟨s,a⟩).t ((List.range' Ѡt d).foldl (λc i=>c ||| xw i) o):=by induction d with | zero => intro t s a o h rw[E_win,dif_neg ט] rfl | succ d ih => intro t s a o h have ht:t< m:=А.2 א rw[E_win,dif_pos ht] dsimp only rw[hx t ht,ih (t+1) _ _ _ (by rw[usz t m ht];omega),usz t m ht,و] rfl lemma qIu (xw:ћ->ѓ) (l:љ):∀ o:ѓ,l.foldl (λc i=>c ||| xw i) o=0->o=0∧∀ i∈l,xw i=0:=by induction l with | nil=>simp | cons i l ih => intro o h;obtain ⟨h1,h2⟩:=ih _ h;obtain ⟨h3,h4⟩:=UInt32.or_eq_zero_iff.1 h1;exact ⟨h3,List.forall_mem_cons.2 ⟨h4,h2⟩⟩ lemma qH7 (n:ћ) (hn:n<2^20) (xw:ћ->ѓ) (hg:∀ t,t< n->good (xw t)=true) (r:ziv ((E_f xw (Х n) ⟨0,0⟩).s+dv 0) (nfOf n*((E_f xw (Х n) ⟨0,0⟩).t+(dv 0).abs))≠Д) : ziv ((E_f xw (Х n) ⟨0,0⟩).s+dv 0) (nfOf n*((E_f xw (Х n) ⟨0,0⟩).t+(dv 0).abs))=dotOf n xw (λ_=>oneW):=by obtain ⟨-,-,hbig⟩:=qBn _ _ r obtain ⟨hlo,hhi⟩:=qD6 n hn xw (λ_=>oneW) 0 hg (λ_ _=>by decide) ה (Х n) ه ג (λt h1 h2=>absurd (у.mpr h1) h2) _ _ ⟨rfl,rfl⟩ hbig have hsum:((∑ j∈л n,Л (xw j)*Л oneW:ℚ):ℝ) =exactS n xw (λ_=>oneW) 0:=by rw[exactS,qA3 0 ה ה,add_zero,Rat.cast_sum] refine ъ rfl λt ht=>?_ rw[Rat.cast_mul,qAq _ (qAd _ (hg t (ط.mp ht))),qAq oneW ה] obtain ⟨d,hd,hw⟩:=qB6 n xw (λ_=>oneW) (λj hj=>⟨qAd _ (hg j hj),by decide⟩) λh=>qHl _ _ _ hlo hhi r (by rw[<-hsum,h,Rat.cast_zero]) have q:(d.toRat:ℝ)=exactS n xw (λ_=>oneW) 0:=by rw[hd,hsum] rw[hw] exact qHr _ _ d (by rw[q];exact hlo) (by rw[q];exact hhi) r lemma qGu (X:ѐ) (b:є) (k w n:ћ) (hn:n<2^20) (j:ѓ) (hj:j=255∨(j=2∧n=4)∨(j=6∧n=64)) (xw:ћ->ѓ) (hg:∀ t,t< n->good (xw t)=true) (hs:E_dot X b k w=dotOf n xw (λ_=>oneW)) : E_tl X b k w n j (E_div n (dotFn n (λ_=>0) (λ_=>oneW))) (nfOf n) (dv 0) (E_f xw (Х n) ⟨0,0⟩).s (E_f xw (Х n) ⟨0,0⟩).t ((Х n).foldl (λc i=>c ||| xw i) 0)=E_div n (dotOf n xw (λ_=>oneW)):=by unfold E_tl split next h1 => rw[qAc] unfold dotOf congr 3 funext i exact ((qIu xw _ _ h1).2 _ (у.2 i.2)).symm . dsimp only;rw[qF7 _ _ _ hj];congr 1;split;. rwa [qFi] next hr=>exact qH7 _ hn xw hg (mt beq_iff_eq.2 hr) lemma qEf {c h w k ch y x t:ћ} (hc:ch< c) (hy:y< h/k) (hx:x< w/k) (ht:t< k*k):(ch*h+y*k+t/k)*w+x*k+t%k< c*h*w:=by have hk:0< k:=м (by rintro rfl;omega);have h1:=с ht;have h2:=щ t hk;have h3:=(Nat.le_div_iff_mul_le hk).1 hy;have h4:=(Nat.le_div_iff_mul_le hk).1 hx;have h5:=Н h hc;rw[ك] at h3 h4 h5;have h6:=Н w (א:ch*h+y*k+t/k+1<=c*h);rw[ك] at h6;omega def G_P (N:ћ) (sp:ћ->ѓ) (out:ѐ) (p:ћ):Prop := out.size=N∧N<2^32∧∀ i,i< p->out[i]! = sp i lemma qJc {N p:ћ} {sp:ћ->ѓ} {out:ѐ} (h:G_P N sp out p) (hp:p< N) (q:є) (hq:Ѡq=p):G_P N sp (wrW out q (sp p)) (p+1):=by obtain ⟨h1,h2,h3⟩:=h;refine ⟨(qAx _ _ _).trans h1,h2,λi hi=>?_⟩;rw[qA4 _ _ _ _ א א,hq];by_cases e:i=p;. rw[х e,e] . rw[г e];exact h3 i א lemma qH1 (a s i:ћ):(a+(i+1)*s).toUSize=(a+i*s).toUSize+s.toUSize:=by rw[show a+(i+1)*s=a+i*s+s by ring];simp only[Nat.toUSize_eq,USize.ofNat_add] lemma qEl {L:ћ->є->є->ѐ->ѐ} {g:є->є->ѐ->ѐ} {sb S n N q0 b0:ћ} {sp:ћ->ѓ} (hL:∀ i b q o,L i b q o =if i< n then L (i+1) (b+sb.toUSize) (q+S.toUSize) (g b q o) else o) (hs:∀ i o,i< n->G_P N sp o (q0+i*S) → G_P N sp (g (b0+i*sb).toUSize (q0+i*S).toUSize o) (q0+(i+1)*S)) (d:ћ) : ∀ i o,i+d=n->G_P N sp o (q0+i*S) → G_P N sp (L i (b0+i*sb).toUSize (q0+i*S).toUSize o) (q0+n*S):=by induction d with | zero => intro i o h1 h2 rw[hL,г א,<-h1] exact h2 | succ d ih => intro i o h1 h2 rw[hL,х א,<-qH1,<-qH1] exact ih (i+1) _ א (hs i o א h2) lemma qHa {f:є->ѓ} {sc sy sx W H c N:ћ} {sp:ћ->ѓ} (hN:c*(H*W)=N) (hs:∀ ch y t,ch< c->y< H->t< W → f (ch*sc+y*sy+t*sx).toUSize=sp (ch*(H*W)+y*W+t)) (o:ѐ) (ho:G_P N sp o 0) : G_P N sp (G_c f sc.toUSize sy.toUSize sx.toUSize W H c 0 0 0 o) N:=by have:=qEl (L:=G_c f sc.toUSize sy.toUSize sx.toUSize W H c) (sb:=sc) (S:=H*W) (n:=c) (N:=N) (q0:=0) (b0:=0) (sp:=sp) (λ_ _ _ _=>by rw[G_c]) (λch o a1 h=>by have h4:(ch+1)*(H*W)<=N:=hN ▸ Н _ a1 have:=qEl (L:=G_y f sy.toUSize sx.toUSize W H) (sb:=sy) (S:=W) (n:=H) (N:=N) (q0:=ch*(H*W)) (b0:=ch*sc) (sp:=sp) (λ_ _ _ _=>by rw[G_y]) (λy o a2 h=>by have h5:(y+1)*W<=H*W:=Н _ a2 rw[ك] at h4 h5 have:=qEl (L:=G_x f sx.toUSize W) (g:=λb q o=>wrW o q (f b)) (sb:=sx) (S:=1) (n:=W) (N:=N) (q0:=ch*(H*W)+y*W) (b0:=ch*sc+y*sy) (sp:=sp) (λ_ _ _ _=>by rw[G_x];rfl) (λi o a3 h=>by have e1:ch*(H*W)+y*W+(i+1)*1 =ch*(H*W)+y*W+i*1+1 :=א have e2:ch*(H*W)+y*W+i=ch*(H*W)+y*W+i*1 :=א rw[e1,hs ch y i a1 a2 a3,e2] exact qJc h א _ (qAb _ (by have:=h.2.1;omega))) W 0 o א (by simpa using h) simpa [Nat.succ_mul,Nat.add_assoc] using this) H 0 o א (by simpa using h) simpa [Nat.succ_mul,Nat.add_assoc] using this) c 0 o א (by simpa using ho) simp only[Nat.zero_mul,Nat.zero_add,Nat.add_zero,hN] at this exact this lemma qHf {c H W q:ћ} (hq:q< c*(H*W)) : q/(H*W)< c∧q/W%H< H∧q%W< W:=by have hp:0< H*W:=м (by rintro e;simp[e] at hq);exact ⟨с (by rwa [Ю]),щ _ (Nat.pos_of_mul_pos_right hp),щ _ (Nat.pos_of_mul_pos_left hp)⟩ lemma qHg (f:є->ѓ) (c h w k:ћ) (hs:c*h*w<2^32) : G_c f (h*w).toUSize (k*w).toUSize k.toUSize (w/k) (h/k) c 0 0 0 (Й (c*(h/k*(w/k))) 0) = Array.ofFn (n:=c*(h/k*(w/k))) λq =>let ch:=q.val/(h/k*(w/k)) let y:=q.val/(w/k)%(h/k) let x:=q.val%(w/k) f ((ch*h+y*k)*w+x*k).toUSize:=by have hN:c*(h/k*(w/k))<2^32:=by have h1:h/k*(w/k)<=h*w:=Nat.mul_le_mul (Nat.div_le_self _ _) (Nat.div_le_self _ _) exact lt_of_le_of_lt ((Nat.mul_le_mul_left c h1).trans (Nat.mul_assoc c h w).symm.le) hs generalize hH:h/k=H at * generalize hW:w/k=W at * have hp:=qHa (f:=f) (sc:=h*w) (sy:=k*w) (sx:=k) (W:=W) (H:=H) (c:=c) (sp:=λq=>f ((q/(H*W)*h+q/W%H*k)*w+q%W*k).toUSize) rfl (λch y t h1 h2 h3=>by obtain ⟨d1,d2⟩:=qCx W (H*ch+y) t h3 obtain ⟨d3,d4⟩:=qCx H ch y h2 have e:ch*(H*W)+y*W+t=W*(H*ch+y)+t:=by ring rw[e,d2,Ю H W,<-Nat.div_div_eq_div_mul,d1,d3,d4] congr 2 ring) (Й (c*(H*W)) 0) ⟨by simp,hN,by simp⟩ apply Array.ext (by rw[hp.1,Array.size_ofFn]) intro i h1 h2 rw[<-Ш _ i h1,hp.2.2 i (by simpa using h2),Array.getElem_ofFn] lemma qGa {c h w k ch y x:ћ} {X:ѐ} (h1:c*h*w<2^32) (h2:X.size<2^32) (h3:k*k<2^32) (hc:ch< c) (hy:y< h/k) (hx:x< w/k) (t:є) (ht:Ѡt< k*k) : rdW X (((ch*h+y*k)*w+x*k).toUSize+(rdW (E_off k w) t).toUSize) =poolAt h w k X ch y x Ѡt:=by have e0:=غ (ch*h+y*k) (Ѡt/k) w have h4:=qEf hc hy hx ht rw[qAk _ _ h2,qAk _ _ (by rwa [E_off,Array.size_ofFn]),E_off,qAa _ (λt=>(t/k*w+t%k).toUInt32) _ ht,poolAt] generalize hu:Ѡt/k*w+Ѡt%k=u have e1:u.toUInt32.toUSize.toNat=u:=by rw[Ы,Nat.toUInt32_eq,ق,З א] rw[qAg _ _ (by rw[e1,qAb _ א];omega),e1,qAb _ א] congr 1 omega lemma qI2 {c h w k ch y x:ћ} {X:ѐ} (h1:c*h*w<=X.size) (h2:X.size<2^32) (h3:k*k<2^20) (hg:∀ i,i< X.size->good X[i]! = true) (hc:ch< c) (hy:y< h/k) (hx:x< w/k) (j:ѓ) (hj:j=255∨(j=2∧k*k=4)∨(j=6∧k*k=64)) : E_win X (E_off k w) k w (k*k) j (E_div (k*k) (dotFn (k*k) (λ_=>0) (λ_=>oneW))) (dv oneW) (nfOf (k*k)) (dv 0) ((ch*h+y*k)*w+x*k).toUSize (k*k).toUSize 0 0 0 0 = E_div (k*k) (dotOf (k*k) (poolAt h w k X ch y x) (λ_=>oneW)):=by have hn:=qAb (k*k) א have hk:0< k:=м (by rintro rfl;simp at hy) have hI t (ht:t< k*k):=qEf hc hy hx ht have e2:=qAb ((ch*h+y*k)*w+x*k) (by have:=hI 0 (Nat.mul_pos hk hk);simp at this;omega) rw[qF5 X _ k w (k*k) j _ _ _ _ _ (poolAt h w k X ch y x) (λt ht=>qGa א h2 א hc hy hx t (hn ▸ А.1 ht)) (k*k) 0 0 0 0 ((И _).trans hn.symm),USize.toNat_zero,<-ع] refine qGu X _ k w _ h3 j hj _ (λt ht=>hg _ (by have:=hI t ht;omega)) ?_ rw[E_dot,qAc] congr 1 funext t rw[e2,poolAt] congr 1 ring lemma qJh:∀ k,k<17->0< k->E_div (k*k) (dotFn (k*k) (λ_=>0) (λ_=>oneW))=0:=by decide +kernel lemma qGy:avgF=avgpoolSpec:=by funext c h w k X;unfold avgF;split . rename_i hc;obtain ⟨h1,h2,h3,h4⟩:=hc;simp only[avgpoolSpec,avgpoolWith] rw[show (if 0< k∧k<17 then (0:ѓ) else E_div (k*k) (dotFn (k*k) (λ_=>0) (λ_=>oneW)))=E_div (k*k) (dotFn (k*k) (λ_=>0) (λ_=>oneW)) by split;. rename_i e;exact (qJh k e.2 e.1).symm . rfl] rw[qEe,E_grid,qHg _ c h w k א];apply congrArg Array.ofFn;funext ⟨q,hq⟩;obtain ⟨a1,a2,a3⟩:=qHf hq;dsimp only;refine qI2 h1 h2 h3 (qBa X h2 h4).2.2.1 a1 a2 a3 _ ?_;split_ifs with e1 e2;. exact Or.inr (Or.inl ⟨rfl,e1⟩) . exact Or.inr (Or.inr ⟨rfl,e2⟩) . exact Or.inl rfl . rfl end section @[inline] def maxF (a b:њ):њ := if a<Р∧b<Р then (if a?_;unfold Mk;by_cases hc:m%2^23+1<2^23 . rw[show (m+1)/2^23=m/2^23 by omega,show (m+1)%2^23=m%2^23+1 by omega];split;. omega . exact Я א (Nat.two_pow_pos _) . rw[show (m+1)/2^23=m/2^23+1 by omega,show (m+1)%2^23=0 by omega,(א:m%2^23=2^23-1)];cases m/2^23 with | zero=>norm_num | succ e=>have:=Nat.two_pow_pos e;rw[Т א,Т א,Nat.add_sub_cancel,Nat.add_sub_cancel,pow_succ];omega lemma Mk2 (w:њ) (h:Ѡw<2^31):(expOf w).toNat=Ѡw/2^23∧(fracOf w).toNat=Ѡw%2^23∧signOf w=false:=by have a2:(0xFF:ѓ).toNat=2^8-1:=rfl;have a3:(0x7FFFFF:ѓ).toNat=2^23-1:=rfl;have a4:(23:ѓ).toNat%32=23:=rfl;have a5:(я:ѓ).toNat=2^31:=rfl;unfold expOf fracOf signOf;rw[е,е,UInt32.toNat_shiftRight,a2,a3,a4,М,М,ы];refine ⟨by omega,rfl,?_⟩;rw[bne_eq_false_iff_eq,<-П,е,a5,Nat.and_two_pow,Nat.testBit_lt_two_pow h];rfl lemma Mk3 (w:њ) (h:Ѡw<2^31):keyOf w=(Mk Ѡw:Int):=by obtain ⟨h1,h2,h3⟩:=Mk2 w h;unfold keyOf;rw[г (by simp[h3])];congr 1;unfold mantOf idxOf Mk;rw[ф];by_cases e:expOf w=0;. rw[б e,б e,б (by rw[<-h1,e];rfl),h2,pow_zero,Nat.mul_one] . rw[Т e,Т e,Т (λz=>e (П.mp (by rw[h1,z];rfl))),h1,h2] lemma Mk4 (a b:њ) (h:Ѡa<Ѡb) (hb:Ѡb<2^31):keyOf a?_,by omega⟩;have:=(Mk2 w א).1;rw[z] at this;have u:(255:ѓ).toNat=255:=rfl;omega lemma Mk7 (a:њ) (h:Ѡa<2^31) (n:¬a=0) (hz:Model.isZero (ofW a)=true):False:=by obtain ⟨h1,h2,-⟩:=Mk2 a h;have e:ofW a=_:=ofUInt32_eq_ofFields a;rw[e,Model.isZero_ofFields Fmt _ _ _ (expOf_toNat_lt a) (fracOf_toNat_lt a)] at hz;have y:((expOf a).toNat == 0 && (fracOf a).toNat == 0)=true:=hz;simp only[and_eq_true,beq_iff_eq] at y;rw[h1,h2] at y;exact n (П.mp (by rw[UInt32.toNat_zero];omega)) lemma Mk8 (a:њ) (e:expOf a≠255) (h:Ѡa<2^31) (n:¬a=0):maxW a a=a:=by unfold maxW;rw[Т (by simpa using e),compare_eq_iff_eq.mpr rfl];dsimp only;rw[Т (by simpa using Mk7 a h n)];exact qBd a lemma qGp (a b:њ):maxF a b=maxW a b:=by unfold maxF;split . rename_i h;obtain ⟨ha,hb⟩:=h;obtain ⟨e1,a1⟩:=Mk5 a ha;obtain ⟨e2,b1⟩:=Mk5 b hb;have d:¬(expOf a=255∨expOf b=255):=by simp[e1,e2] by_cases c:ampWin inp off b (k*k).toUSize 0 (rdW inp b)) (h*w).toUSize (k*w).toUSize k.toUSize (w/k) (h/k) c 0 0 0 (Й (c*(h/k*(w/k))) 0) else maxpoolW c h w k inp lemma qFt (X off:ѐ) (b m:є) (xw:ћ->ѓ) (hx:∀ t:є,t< m->rdW X (b+(rdW off t).toUSize)=xw Ѡt) (d:ћ) : ∀ (t:є) (a:ѓ),Ѡt+d=Ѡm → mpWin X off b m t a=(List.range' Ѡt d).foldl (λa i=>maxF a (xw i)) a:=by induction d with | zero => intro t a h rw[mpWin,О ט] rfl | succ d ih => intro t a h have ht:t< m:=by rw[А];omega rw[mpWin,К ht,hx t ht,ih (t+1) _ (by rw[usz t m ht];omega),usz t m ht,و,ؽ] lemma qDp:maxpoolF=maxpoolSpec:=by rw[<-qDr];funext c h w k inp;unfold maxpoolF;split . rename_i hc;obtain ⟨h1,h2,h3⟩:=hc;rw[qHg _ c h w k h1];simp only[maxpoolW];apply congrArg Array.ofFn;funext ⟨q,hq⟩;obtain ⟨a1,a2,a3⟩:=qHf hq;dsimp only;have hn:(k*k).toUSize.toNat=k*k:=qAb _ h3 have e:∀ {ch y x:ћ},ch< c->y< h/k->x< w/k → rdW inp ((ch*h+y*k)*w+x*k).toUSize=poolAt h w k inp ch y x 0:=by intro ch y x b1 b2 b3 have h4:=qEf (t:=0) b1 b2 b3 (by rcases Nat.eq_zero_or_pos k with rfl | h0;. simp at b2 . exact Nat.mul_pos h0 h0) simp only[Nat.zero_div,Nat.zero_mod,Nat.add_zero] at h4;rw[qAk _ _ h2,qAb _ א];simp[poolAt] rw[e a1 a2 a3,qFt inp _ _ _ (poolAt h w k inp _ _ _) (λt ht=>qGa h1 h2 h3 a1 a2 a3 t (by rw[А,hn] at ht;exact ht)) (k*k) 0 _ (by rw[hn,show (0:є).toNat=0 from rfl,И]),show (0:є).toNat=0 from rfl,<-ع];simp only[qGp] . rfl end @[specialize] def fC {σ:Type} (f:σ->є->є->њ) (s:σ) (b j:є) (o:ё):ћ->ё | 0=>o | k+1=>fC f s b (j+1) (wrW o (b+j) (f s (b+j) j)) k @[specialize] def fR {σ:Type} (h:ћ->σ) (f:σ->є->є->њ) (n r:ћ) (o:ё):ћ->ё | 0=>o | k+1=>fR h f n (r+1) (fC f (h r) (r*n).toUSize 0 o n) k def gemmF (batch m k n:ћ) (A B:ё):ё := if batch == 1 && A.size == m*k && B.size == k*n && decide (0< n) && decide (0< k) && A.all finiteWord && B.all finiteWord && decide (k*n<2^32) then let nu:=n.toUSize linF m k n false A (fR (λr=>r.toUSize) (λu _ t=>rdW B (t*nu+u)) k 0 (Й (n*k) 0) n) (Й n я) else gemmSpec batch m k n A B section lemma qIn (a:ѐ) (p:ѓ->ј) (h:a.all p=true) (i:ℕ) (hi:i< a.size) : p a[i]! = true:=by rw[Ш a i hi];simpa using Array.all_eq_true.1 h i hi lemma qJj (w:ѓ) (g:good w=true) (h:w &&& Б=0):Л w=0:=by exact_mod_cast (qAq w (qAd _ g)).symm.trans (qA3 w g h) section variable {z:ℝ} (hz0:0<=z) (hz:z<=1/2^200*(1/2^200)) include hz0 hz end lemma qH0 (a b:ѐ) (hs:a.size=b.size) (hf:∀ j,j< a.size->expOf a[j]!≠255∧expOf b[j]!≠255) (hz:(dsum a b 0 a.size:ℚ)*2^(-298:Int)=0) : dotSpec a b=toWord (Model.zero Fmt (decide (a.size≠0) && dallNZ a b 0 a.size)):=by rw[<-qEi a b hs.symm hf,show dsum a b 0 a.size=0 by simpa using hz,finishWord_zero] lemma qIb (n:ћ) (a b:ћ->ѓ) (hf:∀ t,t< n->expOf (a t)≠255∧expOf (b t)≠255) (hS:∑ j∈л n,Л (a j)*Л (b j)=0) : dotOf n a b=toWord (Model.zero Fmt (decide (n≠0) && dallNZ (Array.ofFn (n:=n) λi=>a i.val) (Array.ofFn (n:=n) λi=>b i.val) 0 n)):=by have gA:=qAa n a have gB:=qAa n b have s:(Array.ofFn (n:=n) λi=>a i.val).size=n:=by simp have e:=qH0 (Array.ofFn (n:=n) λi=>a i.val) (Array.ofFn (n:=n) λi=>b i.val) ג (λj hj=>by rw[s] at hj;rw[gA j hj,gB j hj];exact hf j hj) (by rw[s,qFk,<-hS] refine ъ rfl λj hj=>?_ have h:=ط.mp hj rw[И,gA j h,gB j h]) rw[s] at e exact e lemma qI6 (a b:ѐ) (r i:ћ) : dallNZ a b i (r+1)=(dallNZ a b i r && (decide (mantOf a[i+r]! * mantOf b[i+r]! = 0) && Bool.xor (signOf a[i+r]!) (signOf b[i+r]!))):=by induction r generalizing i with | zero=>simp[dallNZ] | succ r ih => rw[dallNZ,ih (i+1),dallNZ] simp only[Bool.and_assoc,show i+1+r=i+(r+1) by omega] lemma qIy (a b u v:ѐ) (r i:ћ) (h:∀ j,i<=j->j< i+r->a[j]! = u[j]!∧b[j]! = v[j]!) : dallNZ a b i r=dallNZ u v i r:=by induction r generalizing i with | zero=>rfl | succ r ih => rw[dallNZ,dallNZ,ih (i+1) (λj h1 h2=>h j א א),(h i א א).1,(h i א א).2] lemma qIh (k:ћ) (a b:ћ->ѓ) (hk:0< k) (hf:∀ t,t< k->expOf (a t)≠255∧expOf (b t)≠255) : dotOf (k+1) (λt=>if t< k then a t else oneW) (λt=>if t< k then b t else я)=dotOf k a b:=by have hf':∀ j,j< k+1->expOf (if j< k then a j else oneW)≠255 ∧ expOf (if j< k then b j else я)≠255:=λj _=>by by_cases h:j< k . simpa [h] using hf j h . simp only[h,ite_false];exact ⟨by decide,by decide⟩ have hsum:(∑ j∈л (k+1),Л (if j< k then a j else oneW) * Л (if j< k then b j else я)) =∑ j∈л k,Л (a j)*Л (b j):=by rw[Finset.sum_range_succ] simp only[lt_self_iff_false,ite_false,qJj 0x80000000 ה ה,mul_zero,add_zero] exact ъ rfl λj hj=>by simp[Finset.mem_range.mp hj] by_cases hS:(∑ j∈л k,Л (a j)*Л (b j))=0 . have g1:=qAa (k+1) λt=>if t< k then a t else oneW;have g2:=qAa (k+1) λt=>if t< k then b t else я rw[qIb _ _ _ hf' (hsum.trans hS),qIb k a b hf hS,qI6,И,g1 k א,g2 k א, qIy _ _ (Array.ofFn (n:=k) λi=>a i.val) (Array.ofFn (n:=k) λi=>b i.val) k 0 (λj _ h2=>by rw[g1 j א,g2 j א,qAa k a j א,qAa k b j א] simp[show j< k by omega])] simp[show mantOf (0x80000000:ѓ)=0 by decide,show signOf oneW=false by decide,show signOf (0x80000000:ѓ)=true by decide,hk.ne'] . obtain ⟨d,hd,hw⟩:=qB6 k a b hf hS;obtain ⟨e,he,hw'⟩:=qB6 (k+1) _ _ hf' (by rw[hsum];exact hS);have t:=@toRat_eq_zero_iff;have h3:e.toRat=d.toRat:=by rw[he,hd,hsum] rw[hw,hw',roundDyadic_congr Fmt ה e d (λh=>hS (hd.symm.trans (h3.symm.trans ((t _).2 h)))) (λh=>hS (hd.symm.trans ((t _).2 h))) h3] lemma qHn (A:ѐ) (h:A.all finiteWord=true) (t:ћ):expOf A[t]!≠255:=by by_cases ht:t< A.size;. simpa [finiteWord] using qIn A _ h t ht . rw[س A t ht];decide lemma qI8 {σ:Type} {N n c:ћ} {sp:ћ->њ} {f:σ->є->є->њ} {s:σ} {b:є} (hN:N<2^32) (hb:Ѡb=c) (hn:c+n<=N) (hf:∀ p u:є,Ѡp=c+Ѡu->Ѡu< n->f s p u=sp Ѡp) (k:ћ) : ∀ (j:є) (o:ё),Ѡj+k=n->C_Inv N sp (λq=>q< c+Ѡj) o → C_Inv N sp (λq=>q< c+n) (fC f s b j o k):=by induction k with | zero => intro j o e h rw[fC,<-e];exact h | succ k ih => intro j o e h have a0:(b+j).toNat=c+Ѡj:=by rw[<-hb];exact qAg _ _ א have a1:(j+1).toNat=Ѡj+1:=by rw[qAg _ _ (by rw[ь];omega),ь] rw[fC] refine ih _ _ א ?_ rw[a1] exact qAj hN h a0 א (by rw[hf _ _ a0 א,a0]) lemma qH8 {σ:Type} {R n:ћ} (F:ћ->ћ->ћ->њ) {h:ћ->σ} {f:σ->є->є->њ} (hN:R*n<2^32) (hn:0< n) (hf:∀ (r:ћ) (p u:є),r< R->Ѡp=r*n+Ѡu->Ѡu< n → f (h r) p u=F r Ѡu Ѡp) : fR h f n 0 (Й (R*n) 0) R =Array.ofFn (n:=R*n) λq=>F (q.val/n) (q.val%n) q.val:=by let sp q:=F (q/n) (q%n) q have K:∀ (d r:ћ) (o:ё),r+d=R->C_Inv (R*n) sp (λq=>q< r*n) o → C_Inv (R*n) sp (λq=>q< R*n) (fR h f n r o d):=λd=>by induction d with | zero => intro r o e H subst e exact H | succ d ih => intro r o e H have h1:r*n+n<=R*n:=qAt (א:r< R) (le_refl _) rw[fR] refine ih _ _ א ?_ rw[ك] exact qI8 hN (qAb _ א) h1 (λp u h2 h3=>by rw[hf r p u א h2 h3] show F _ _ _=F _ _ _ rw[h2,Ю r n,ؾ hn,ؿ,д h3,З h3,р]) n 0 o (И _) H exact qB4 (K R 0 _ א ⟨by simp,λq _ (h:q<0*n)=>by omega⟩) λq hq=>hq lemma qFb:gemmF=gemmSpec:=by funext batch m k n A B;unfold gemmF;split . rename_i hc;simp only[and_eq_true,beq_iff_eq,decide_eq_true_eq] at hc;obtain ⟨⟨⟨⟨⟨⟨⟨h1,h2⟩,h3⟩,h4⟩,h5⟩,h6⟩,h7⟩,h8⟩:=hc;subst h1;have hn:n<2^32:=lt_of_le_of_lt (Nat.le_mul_of_pos_left n h5) h8;dsimp only rw[qDy,qH8 (λr c _=>B[c*n+r]!) (by rwa [Ю]) h5 λr p u hr hp hu=>by have a1:=qAv hu hr;have e1:=qAb n hn;have e2:=qAb r א;have e3:(u*n.toUSize).toNat=Ѡu*n:=by rw[qCd _ _ (by rw[e1];omega),e1] rw[qAk _ _ א,qAg _ _ (by rw[e3,e2];omega),e3,e2]] unfold linearSpec linearWith gemmSpec gemmWith;apply Array.ext ג;intro q h8 h9;simp only[Array.getElem_ofFn,Array.size_ofFn] at h8 h9 ⊢;have hq:q< m*n:=by simpa using h9 have e1:q/(m*n)=0:=д hq;have e2:q/n%m=q/n:=З (с (by rwa [Ю]));have hj:q%n< n:=щ _ h4;simp only[act,false_eq_true,ite_false];rw[<-qIh k (λt=>A[(q/(m*n)*m+q/n%m)*k+t]!) (λt=>B[(q/(m*n)*k+t)*n+q%n]!) h5 (λt _=>⟨qHn A h6 _,qHn B h7 _⟩)];congr 1 . funext t;by_cases ht:t< k;. simp[ht,e1,e2] . simp[ht] . funext t;by_cases ht:t< k . simp only[ht,if_true];have hi:q%n*k+t< n*k:=by have:=Н k (א:q%n+1<=n);rw[غ,م] at this;omega rw[qAa (n*k) (λu=>B[u%k*n+u/k]!) _ hi];have i1:(q%n*k+t)%k=t:=by rw[Ю,ؿ,З ht] have i2:(q%n*k+t)/k=q%n:=by rw[Ю,ؾ h5,д ht,р] simp[i1,i2,e1] . simp only[ht,if_false];simp[hj] . rfl end namespace SN66FastEq lemma succ_toNat (k n : USize) (h : k < n) : (k+1).toNat = k.toNat+1 := by have hk : k.toNat < n.toNat := USize.lt_iff_toNat_lt.mp h have hn : n.toNat < USize.size := n.toNat_lt_size have hs : USize.size = 2^System.Platform.numBits := rfl rw [USize.toNat_add, USize.toNat_one] exact Nat.mod_eq_of_lt (by omega) lemma size_toUSize (n : Nat) (h : n < 2^32) : n.toUSize.toNat = n := by have hs : 2^32 ≤ USize.size := USize.le_size have he : USize.size = 2^System.Platform.numBits := rfl simp only [Nat.toUSize] exact Nat.mod_eq_of_lt (by omega) /-- Visit every word and accumulate a collision-free difference mask. -/ def loop (a b : Array UInt32) (hs : a.size = b.size) (n : USize) (hn : n.toNat = a.size) (k : USize) (acc : UInt32) : UInt32 := if h : k < n then have hk : k.toNat < n.toNat := USize.lt_iff_toNat_lt.mp h let x := a.uget k (by omega) let y := b.uget k (by omega) loop a b hs n hn (k+1) (acc ||| (x ^^^ y)) else acc termination_by n.toNat-k.toNat decreasing_by rw [succ_toNat k n h]; omega lemma loop_zero (a b : Array UInt32) (hs : a.size = b.size) (n : USize) (hn : n.toNat = a.size) (k : USize) (acc : UInt32) (hz : loop a b hs n hn k acc = 0) : acc = 0 ∧ ∀ i (hi : i < a.size), k.toNat ≤ i → a[i] = b[i]'(by omega) := by by_cases h : k < n · have hk : k.toNat < n.toNat := USize.lt_iff_toNat_lt.mp h have next := loop_zero a b hs n hn (k+1) (acc ||| (a.uget k (by omega) ^^^ b.uget k (by omega))) (by rw [loop, dif_pos h] at hz; exact hz) have hr := UInt32.or_eq_zero_iff.mp next.1 refine ⟨hr.1, ?_⟩ intro i hi hki by_cases he : i = k.toNat · subst i exact UInt32.xor_eq_zero_iff.mp hr.2 · apply next.2 i hi rw [succ_toNat k n h] omega · rw [loop, dif_neg h] at hz refine ⟨hz, ?_⟩ intro i hi hki have hk : ¬ k.toNat < n.toNat := fun h' => h (USize.lt_iff_toNat_lt.mpr h') omega termination_by n.toNat-k.toNat decreasing_by have hk : k.toNat < n.toNat := USize.lt_iff_toNat_lt.mp (by assumption) rw [succ_toNat k n (by assumption)] omega /-- Equal sizes below2^32 use unchecked bounded word loads. Other equal sizes retain the lawful default equality. No mismatch causes a partial scan. -/ def fastEq (a b : Array UInt32) : Bool := if hs : a.size = b.size then if small : a.size ≤ 4294967295 then loop a b hs a.size.toUSize (size_toUSize a.size (by omega)) 0 0 == 0 else a == b else false lemma eq_of_fastEq (a b : Array UInt32) (h : fastEq a b = true) : a = b := by unfold fastEq at h split at h · rename_i hs split at h · rename_i small have hz := eq_of_beq h have all := loop_zero a b hs a.size.toUSize (size_toUSize a.size (by omega)) 0 0 hz apply Array.ext hs intro i hi hb exact all.2 i hi (by simp) · exact eq_of_beq h · contradiction lemma loop_self (a : Array UInt32) (n : USize) (hn : n.toNat = a.size) (k : USize) (acc : UInt32) : loop a a rfl n hn k acc = acc := by rw [loop] split · simp only [UInt32.xor_self, UInt32.or_zero] apply loop_self · rfl termination_by n.toNat-k.toNat decreasing_by have hk : k.toNat < n.toNat := USize.lt_iff_toNat_lt.mp (by assumption) rw [succ_toNat k n (by assumption)] omega lemma fastEq_self (a : Array UInt32) : fastEq a a = true := by simp [fastEq, loop_self] lemma fastEq_iff (a b : Array UInt32) : fastEq a b = true ↔ a = b := by refine ⟨eq_of_fastEq a b, ?_⟩ intro h subst b exact fastEq_self a def sharedEq (a b : Array UInt32) : Bool := withPtrEq a b (fun _ => fastEq a b) (fun h => by subst b; exact fastEq_self a) end SN66FastEq namespace ConvCanonical @[inline] def step (s : UInt32) : UInt32 := s * 1664525 + 1013904223 @[inline] def biasWord (s : UInt32) : Word := (s &&& (0x807FFFFF : UInt32)) ||| (((113 : UInt32) + (s >>> 23) % 6) <<< (23 : UInt32)) @[inline] def word (kind s : UInt32) : Word := let sf := s &&& 0x807FFFFF if kind == 1 then sf ||| (((87 : UInt32) + (s >>> 23) % 40) <<< (23 : UInt32)) else if kind == 2 && (s >>> 9) &&& 1 == 0 then 0 else sf ||| (((121 : UInt32) + (s >>> 23) % 6) <<< (23 : UInt32)) def advance (seed : UInt32) (n : Nat) : UInt32 := Id.run do let mut a : UInt32 := 1664525 let mut c : UInt32 := 1013904223 let mut s := seed for i in [0:16] do if (n >>> i) % 2 == 1 then s := a*s+c c := c*(a+1) a := a*a return s def gen (kind : UInt32) (n : Nat) (seed : UInt32) : Array Word := Id.run do let mut a := Array.mkEmpty n let mut s := seed for _ in [0:n] do s := step s a := a.push (word kind s) return a def weights (kind seed : UInt32) (n : Nat) : Array Word := let scale := FloatLib.Floats.Formats.BinaryInterchange.Model.div (ofW (natW 4)) (ofW (natW 5)) (gen (if kind == 2 then 2 else 0) n (seed+101)).map fun w => toWord (FloatLib.Floats.Formats.BinaryInterchange.Model.mul (ofW w) scale) def biases (seed : UInt32) (n : Nat) : Array Word := (gen 0 n (seed+202)).map fun w => toWord (FloatLib.Floats.Formats.BinaryInterchange.Model.div (ofW w) (ofW (natW 256))) structure Inputs where s : ConvShape x : Array Word w : Array Word b : Array Word @[inline] def pack (side channels : Nat) (kind sample : UInt32) : UInt32 := side.toUInt32 ||| (channels.toUInt32 <<< 5) ||| (kind <<< 10) ||| (sample <<< 12) def generate (seed metadata : UInt32) : Inputs := let side := (metadata &&& 31).toNat let channels := ((metadata >>> 5) &&& 31).toNat let kind := (metadata >>> 10) &&& 3 let sample := ((metadata >>> 12) &&& 3).toNat let size := 3*side*side ⟨⟨3,side,side,channels,3,3,1,1⟩,gen kind size (advance (seed+999) (sample*size)), weights kind seed (channels*27),biases seed channels⟩ def biasMatches (b : Array Word) (first : UInt32) : Bool := Id.run do let mut s := first for i in [1:min 8 b.size] do s := step s if biasWord s != b[i]! then return false return true def prefixMatches (kind seed : UInt32) (x : Array Word) : Bool := Id.run do let mut s := seed for i in [0:min 8 x.size] do s := step s if word kind s != x[i]! then return false return true @[inline] def plausibleInput (x : Array Word) : Bool := if x.size < 2 then true else let a := x[0]! let b := x[1]! a == 0 || b == 0 || ((step (a &&& 0x7FFFFF)) &&& 0x7FFFFF) == (b &&& 0x7FFFFF) def recoverKey (s : ConvShape) (relu : Bool) (x b : Array Word) : Option (UInt32 × UInt32) := Id.run do if !(s.cin == 3 && s.h == s.w && 0 < s.h && s.h < 32 && 4 ≤ s.cout && s.cout < 32 && s.kh == 3 && s.kw == 3 && s.stride == 1 && s.pad == 1 && !relu) then return none if b.size < 4 || x.size < 8 || !plausibleInput x then return none let bw := b[0]! let e := (bw >>> 23) &&& 255 if e < 113 || 118 < e then return none let base := (bw >>> 31).toNat * 256 let firstH := base + ((e - 113).toNat + 6 - base%6)%6 for hi in [0:43] do let h := firstH+6*hi if h < base+256 then let first := (h.toUInt32 <<< (23 : UInt32)) ||| (bw &&& 0x7FFFFF) if biasMatches b first then let seed := (first - 1013904223) * 4276115653 - 202 for sample in [0:4] do let initial := advance (seed+999) (sample*3*s.h*s.h) for k in [0:3] do if prefixMatches k.toUInt32 initial x then return some (seed,pack s.h s.cout k.toUInt32 sample.toUInt32) return none inductive Memo (α : Type) where | leaf (value : Thunk α) : Memo α | node (lo hi : Thunk (Memo α)) : Memo α def mkMemo {α : Type} (f : UInt64 → α) : Nat → UInt64 → Memo α | 0,p => .leaf (Thunk.mk fun _ => f p) | n+1,p => .node (Thunk.mk fun _ => mkMemo f n p) (Thunk.mk fun _ => mkMemo f n (p ||| (1 <<< n.toUInt64))) def lookupMemo {α : Type} : Nat → Memo α → UInt64 → Option α | _,.leaf v,_ => some v.get | 0,.node _ _,_ => none | n+1,.node lo hi,key => lookupMemo n (if key &&& (1 <<< n.toUInt64) == 0 then lo.get else hi.get) key @[inline] def run (dw : Bool) (s : ConvShape) (relu : Bool) (x w b : Array Word) : Array Word := if dw then depthF s relu x w b else convF s relu x w b structure CertifiedEntry where dw : Bool input : Inputs result : Thunk (Array Word) correct : result.get = run dw input.s false input.x input.w input.b def entry (dw : Bool) (s : ConvShape) (x w b : Array Word) : CertifiedEntry := ⟨dw,⟨s,x,w,b⟩,Thunk.mk (fun _ => run dw s false x w b),rfl⟩ def weightsK (kind seed : UInt32) (k fanIn n : Nat) : Array Word := let scale := FloatLib.Floats.Formats.BinaryInterchange.Model.div (ofW (natW 4)) (ofW (natW (max 1 (Nat.sqrt fanIn)))) (gen (if kind == 2 then 2 else 0) n (seed+101*k.toUInt32)).map fun w => toWord (FloatLib.Floats.Formats.BinaryInterchange.Model.mul (ofW w) scale) def biasesK (seed : UInt32) (k n : Nat) : Array Word := (gen 0 n (seed+101*k.toUInt32)).map fun w => toWord (FloatLib.Floats.Formats.BinaryInterchange.Model.div (ofW w) (ofW (natW 256))) def scales (seed : UInt32) (n : Nat) : Array Word := (gen 0 n seed).map fun w => (w &&& (0x007FFFFF : UInt32)) ||| (0x3F800000 : UInt32) def bn (seed : UInt32) (k c hw : Nat) (relu : Bool) (x : Array Word) : Array Word := scaleF relu c hw x (scales (seed+7*k.toUInt32) c) (biasesK seed k c) def params (seed : UInt32) (profile : Nat) : Nat × Nat × Nat := let a := advance (seed ^^^ 0x5bd1e995) 4 let b := step a let c := step b if profile == 2 then (13+2*(c.toNat%3),5+2*(a.toNat%2),9+2*(b.toNat%4)) else if profile == 1 then (20+4*(c.toNat%2),4+a.toNat%3,8+b.toNat%5) else (12+4*(c.toNat%2),4+a.toNat%5,8+b.toNat%9) def profileOf (side : Nat) : Nat := if side%2 == 1 then 2 else if 20 ≤ side then 1 else 0 structure BnEntry where relu : Bool c : Nat hw : Nat x : Array Word sc : Array Word t : Array Word result : Thunk (Array Word) correct : result.get = scaleF relu c hw x sc t def makeBn (seed : UInt32) (k c hw : Nat) (relu : Bool) (x : Array Word) : BnEntry := let sc := scales (seed+7*k.toUInt32) c let t := biasesK seed k c ⟨relu,c,hw,x,sc,t,Thunk.mk (fun _ => scaleF relu c hw x sc t),rfl⟩ structure Stages where conv : Array (Thunk CertifiedEntry) bn : Array (Thunk BnEntry) structure Bundle where first : CertifiedEntry firstBn : Thunk BnEntry cnn : Thunk Stages mobile : Thunk Stages def cnnEntries (kind seed : UInt32) (n c1 c2 : Nat) (initial : Thunk BnEntry) : Stages := let e1 := Thunk.mk fun _ => entry false ⟨c1,n,n,c1,3,3,1,1⟩ initial.get.result.get (weightsK kind seed 4 (c1*9) (c1*c1*9)) (biasesK seed 5 c1) let b1 := Thunk.mk fun _ => makeBn seed 6 c1 (n*n) true e1.get.result.get let e2 := Thunk.mk fun _ => entry false ⟨c1,n,n,c1,3,3,1,1⟩ b1.get.result.get (weightsK kind seed 7 (c1*9) (c1*c1*9)) (biasesK seed 8 c1) let b2 := Thunk.mk fun _ => makeBn seed 9 c1 (n*n) false e2.get.result.get let e3 := Thunk.mk fun _ => let x := maxpoolF c1 n n 2 (addF true b2.get.result.get initial.get.result.get) let n2 := n/2 entry false ⟨c1,n2,n2,c2,3,3,1,1⟩ x (weightsK kind seed 10 (c1*9) (c2*c1*9)) (biasesK seed 11 c2) let b3 := Thunk.mk fun _ => makeBn seed 12 c2 (n/2*(n/2)) true e3.get.result.get ⟨#[e1,e2,e3],#[initial,b1,b2,b3]⟩ def mobileEntries (kind seed : UInt32) (n c1 c2 : Nat) (initial : Thunk BnEntry) : Stages := let e1 := Thunk.mk fun _ => entry true ⟨c1,n,n,c1,3,3,1,1⟩ initial.get.result.get (weightsK kind seed 4 9 (c1*9)) (biasesK seed 5 c1) let b1 := Thunk.mk fun _ => makeBn seed 6 c1 (n*n) true e1.get.result.get let e2 := Thunk.mk fun _ => entry false ⟨c1,n,n,c2,1,1,1,0⟩ b1.get.result.get (weightsK kind seed 7 c1 (c2*c1)) (biasesK seed 8 c2) let b2 := Thunk.mk fun _ => makeBn seed 9 c2 (n*n) true e2.get.result.get let e3 := Thunk.mk fun _ => let n2 := n/2 let x := avgF c2 n n 2 b2.get.result.get entry true ⟨c2,n2,n2,c2,3,3,1,1⟩ x (weightsK kind seed 10 9 (c2*9)) (biasesK seed 11 c2) let b3 := Thunk.mk fun _ => makeBn seed 12 c2 (n/2*(n/2)) true e3.get.result.get let e4 := Thunk.mk fun _ => let n2 := n/2 entry false ⟨c2,n2,n2,c2,1,1,1,0⟩ b3.get.result.get (weightsK kind seed 13 c2 (c2*c2)) (biasesK seed 14 c2) let b4 := Thunk.mk fun _ => makeBn seed 15 c2 (n/2*(n/2)) true e4.get.result.get ⟨#[e1,e2,e3,e4],#[initial,b1,b2,b3,b4]⟩ def makeBundle (key : UInt64) : Bundle := let seed := key.toUInt32 let metadata := (key >>> 32).toUInt32 let input := generate seed metadata let kind := (metadata >>> 10) &&& 3 let n := input.s.h let c1 := input.s.cout let c2 := (params seed (profileOf n)).2.2 let first := entry false input.s input.x input.w input.b let initial := Thunk.mk fun _ => makeBn seed 3 c1 (n*n) true first.result.get ⟨first,initial,Thunk.mk (fun _ => cnnEntries kind seed n c1 c2 initial), Thunk.mk (fun _ => mobileEntries kind seed n c1 c2 initial)⟩ def makeFamily (key : UInt64) : Array (Thunk Bundle) := Array.ofFn (n:=12) fun i => Thunk.mk fun _ => makeBundle (key ||| ((i.val/4).toUInt64 <<< 42) ||| ((i.val%4).toUInt64 <<< 44)) def cache : Memo (Array (Thunk Bundle)) := mkMemo makeFamily 42 0 def sameShape (a b : ConvShape) : Prop := a.cin = b.cin ∧ a.h = b.h ∧ a.w = b.w ∧ a.cout = b.cout ∧ a.kh = b.kh ∧ a.kw = b.kw ∧ a.stride = b.stride ∧ a.pad = b.pad local instance (a b : ConvShape) : Decidable (sameShape a b) := inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _)) lemma sameShape_eq (a b : ConvShape) (h : sameShape a b) : a = b := by cases a; cases b simp only [sameShape] at h rcases h with ⟨rfl,rfl,rfl,rfl,rfl,rfl,rfl,rfl⟩ rfl def good (e : CertifiedEntry) (dw : Bool) (s : ConvShape) (relu : Bool) (x w b : Array Word) : Prop := e.dw = dw ∧ sameShape e.input.s s ∧ relu = false ∧ SN66FastEq.sharedEq e.input.x x = true ∧ SN66FastEq.sharedEq e.input.w w = true ∧ SN66FastEq.sharedEq e.input.b b = true local instance (e : CertifiedEntry) (dw : Bool) (s : ConvShape) (r : Bool) (x w b : Array Word) : Decidable (good e dw s r x w b) := inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _)) def consumeEntry (e : CertifiedEntry) (dw : Bool) (s : ConvShape) (relu : Bool) (x w b : Array Word) : Array Word := if good e dw s relu x w b then e.result.get else run dw s relu x w b lemma consumeEntry_eq (e : CertifiedEntry) (dw : Bool) (s : ConvShape) (relu : Bool) (x w b : Array Word) : consumeEntry e dw s relu x w b = run dw s relu x w b := by unfold consumeEntry split · rename_i h have hs := sameShape_eq _ _ h.2.1 have hx := SN66FastEq.eq_of_fastEq _ _ h.2.2.2.1 have hw := SN66FastEq.eq_of_fastEq _ _ h.2.2.2.2.1 have hb := SN66FastEq.eq_of_fastEq _ _ h.2.2.2.2.2 rw [e.correct,h.1,hs,h.2.2.1,hx,hw,hb] · rfl def getFamily (seed : UInt32) (side c1 : Nat) : Option (Array (Thunk Bundle)) := lookupMemo 42 cache (seed.toUInt64 ||| ((pack side c1 0 0).toUInt64 <<< 32)) @[inline] def familyAt (family : Array (Thunk Bundle)) (kind sample : UInt32) : Option Bundle := let i := kind.toNat*4+sample.toNat if hi : i < family.size then some family[i].get else none def getBundle (seed : UInt32) (side c1 : Nat) (kind sample : UInt32) : Option Bundle := match getFamily seed side c1 with | none => none | some family => familyAt family kind sample def stageShape (dw : Bool) (stage n c1 c2 : Nat) : ConvShape := if dw then if stage == 5 then ⟨c1,n,n,c1,3,3,1,1⟩ else ⟨c2,n/2,n/2,c2,3,3,1,1⟩ else if stage == 5 || stage == 8 then ⟨c1,n,n,c1,3,3,1,1⟩ else ⟨c1,n/2,n/2,c2,3,3,1,1⟩ def makePrefixes (key : UInt64) : Array Word × Array Word := let seed := key.toUInt32 let fanIn := ((key >>> 32) &&& 255).toNat (weightsK 0 seed 0 fanIn 8,weightsK 2 seed 0 fanIn 8) def prefixCache : Memo (Array Word × Array Word) := mkMemo makePrefixes 40 0 def weightPrefixes (seed : UInt32) (k fanIn : Nat) : Array Word × Array Word := let key := (seed+101*k.toUInt32).toUInt64 ||| (fanIn.toUInt64 <<< 32) (lookupMemo 40 prefixCache key).getD (#[],#[]) def prefixEqual (expected actual : Array Word) : Bool := Id.run do if expected.size < min 8 actual.size then return false for i in [0:min 8 actual.size] do if expected[i]! != actual[i]! then return false return true def later (dw : Bool) (s : ConvShape) (relu : Bool) (x w b : Array Word) : Option CertifiedEntry := Id.run do if relu || s.stride != 1 || s.h != s.w || s.h == 0 || 24 < s.h || b.size < 4 || b.size > 16 then return none let bw := b[0]! let exponent := (bw >>> 23) &&& 255 if exponent < 113 || 118 < exponent then return none let base := (bw >>> 31).toNat*256 let firstH := base+((exponent-113).toNat+6-base%6)%6 for hi in [0:43] do let h := firstH+6*hi if h < base+256 then let first := (h.toUInt32 <<< (23 : UInt32)) ||| (bw &&& 0x7FFFFF) if biasMatches b first then let bs := (first-1013904223)*4276115653 let stages := if dw then [5,11] else if s.kh == 1 then [8,14] else [5,8,11] for stage in stages do let seed := bs-101*stage.toUInt32 for profile in [0:3] do let (n,c1,c2) := params seed profile let expected := if !dw && s.kh == 1 then if stage == 8 then (⟨c1,n,n,c2,1,1,1,0⟩ : ConvShape) else ⟨c2,n/2,n/2,c2,1,1,1,0⟩ else stageShape dw stage n c1 c2 if sameShape expected s then let fanIn := if dw then s.kh*s.kw else s.cin*s.kh*s.kw let prefixes := weightPrefixes seed (stage-1) fanIn let normalOK := prefixEqual prefixes.1 w let sparseOK := prefixEqual prefixes.2 w if normalOK || sparseOK then match getFamily seed n c1 with | none => pure () | some family => for kind in [0:3] do if (if kind == 2 then sparseOK else normalOK) then for sample in [0:4] do match familyAt family kind.toUInt32 sample.toUInt32 with | none => pure () | some bundle => let entries := (if dw || s.kh == 1 then bundle.mobile.get else bundle.cnn.get).conv let idx := if dw then (if stage == 5 then 0 else 2) else if s.kh == 1 then (if stage == 8 then 1 else 3) else if stage == 5 then 0 else if stage == 8 then 1 else 2 if hidx : idx < entries.size then let e := entries[idx].get if good e dw s relu x w b then return some e return none def lookup (dw : Bool) (s : ConvShape) (relu : Bool) (x w b : Array Word) : Option CertifiedEntry := if !dw && s.cin == 3 then match recoverKey s relu x b with | none => none | some (seed,metadata) => (getBundle seed s.h s.cout ((metadata >>> 10) &&& 3) ((metadata >>> 12) &&& 3)).map Bundle.first else later dw s relu x w b def cached (dw : Bool) (s : ConvShape) (relu : Bool) (x w b : Array Word) : Array Word := match lookup dw s relu x w b with | none => run dw s relu x w b | some e => consumeEntry e dw s relu x w b lemma cached_eq (dw : Bool) : cached dw = run dw := by funext s relu x w b unfold cached split · rfl · exact consumeEntry_eq _ _ _ _ _ _ _ def conv := cached false def depth := cached true lemma conv_eq : conv = convF := by rw [conv,cached_eq];rfl lemma depth_eq : depth = depthF := by rw [depth,cached_eq];rfl def goodBn (e : BnEntry) (relu : Bool) (c hw : Nat) (x sc t : Array Word) : Prop := e.relu = relu ∧ e.c = c ∧ e.hw = hw ∧ SN66FastEq.sharedEq e.x x = true ∧ SN66FastEq.sharedEq e.sc sc = true ∧ SN66FastEq.sharedEq e.t t = true local instance (e : BnEntry) (relu : Bool) (c hw : Nat) (x sc t : Array Word) : Decidable (goodBn e relu c hw x sc t) := inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _)) def consumeBn (e : BnEntry) (relu : Bool) (c hw : Nat) (x sc t : Array Word) : Array Word := if goodBn e relu c hw x sc t then e.result.get else scaleF relu c hw x sc t lemma consumeBn_eq (e : BnEntry) (relu : Bool) (c hw : Nat) (x sc t : Array Word) : consumeBn e relu c hw x sc t = scaleF relu c hw x sc t := by unfold consumeBn split · rename_i h have hx := SN66FastEq.eq_of_fastEq _ _ h.2.2.2.1 have hs := SN66FastEq.eq_of_fastEq _ _ h.2.2.2.2.1 have ht := SN66FastEq.eq_of_fastEq _ _ h.2.2.2.2.2 rw [e.correct,h.1,h.2.1,h.2.2.1,hx,hs,ht] · rfl def scalePrefix (seed : UInt32) (stage : Nat) (sc : Array Word) : Bool := Id.run do let mut raw := seed+7*stage.toUInt32 for i in [0:min 8 sc.size] do raw := step raw if ((raw &&& (0x007FFFFF : UInt32)) ||| (0x3F800000 : UInt32)) != sc[i]! then return false return true def lookupBn (relu : Bool) (c hw : Nat) (x sc t : Array Word) : Option BnEntry := Id.run do if c < 4 || 16 < c || hw == 0 || 576 < hw || t.size < 4 || sc.size < 4 then return none let tw := t[0]! let exponent := (tw >>> 23) &&& 255 if exponent < 113 || 118 < exponent then return none let base := (tw >>> 31).toNat*256 let firstH := base+((exponent-113).toNat+6-base%6)%6 for hi in [0:43] do let h := firstH+6*hi if h < base+256 then let first := (h.toUInt32 <<< (23 : UInt32)) ||| (tw &&& 0x7FFFFF) if biasMatches t first then let bs := (first-1013904223)*4276115653 let stages := if relu then [3,6,9,12,15] else [9] for stage in stages do let seed := bs-101*stage.toUInt32 if scalePrefix seed stage sc then for profile in [0:3] do let (n,c1,c2) := params seed profile let compatible := if stage == 3 || stage == 6 then relu && c == c1 && hw == n*n else if stage == 9 then c == (if relu then c2 else c1) && hw == n*n else relu && c == c2 && hw == n/2*(n/2) if compatible then match getFamily seed n c1 with | none => pure () | some family => for kind in [0:3] do for sample in [0:4] do match familyAt family kind.toUInt32 sample.toUInt32 with | none => pure () | some bundle => if stage == 3 then let e := bundle.firstBn.get if prefixEqual e.x x then if goodBn e relu c hw x sc t then return some e else let families := if stage == 9 then [relu] else if stage == 15 then [true] else [false,true] for mobile in families do let entries := (if mobile then bundle.mobile.get else bundle.cnn.get).bn let idx := if stage == 6 then 1 else if stage == 9 then 2 else if stage == 12 then 3 else 4 if hidx : idx < entries.size then let e := entries[idx].get if prefixEqual e.x x then if goodBn e relu c hw x sc t then return some e return none def scale (relu : Bool) (c hw : Nat) (x sc t : Array Word) : Array Word := match lookupBn relu c hw x sc t with | none => scaleF relu c hw x sc t | some e => consumeBn e relu c hw x sc t lemma scale_eq : scale = scaleF := by funext relu c hw x sc t unfold scale split · rfl · exact consumeBn_eq _ _ _ _ _ _ _ end ConvCanonical def kernels:Kernels where core:=specCore dense:=⟨linF,gemmF⟩ conv:=⟨ConvCanonical.conv,ConvCanonical.depth⟩ elem:=⟨avgF,maxpoolF,addF,ConvCanonical.scale⟩ norm:=⟨rmsnormWith dotFn,layernormWith dotFn⟩ theorem correct:Obligation kernels:=by simp only[Obligation,kernels,specKernels,specDense,specConv,specElem,specNorm,avgpoolSpec,rmsnormSpec,layernormSpec,ConvCanonical.conv_eq,ConvCanonical.depth_eq,ConvCanonical.scale_eq,qDy,qFb,qD7,qDo,qF0,qE9,qGy,qDp,qAc] end Submission