Documentation

LeanMlir.Proofs.Nets.Small.Cifar8StepTie

PoC: the cifar8 (Chapter 4 deeper, 8-conv no-BN) §1a TIE — tied through the real forward #

The 4-stage peer of CifarFold's §1a tie (cifar_conv_tied_certified). cifar8 is cifar (ch5) with four conv→conv→pool stages instead of two, so its conv backward chain is the cifar chain repeated: within each stage the second conv is the maxpool-back layer (cnnChainCotW2 for the very last, then cifarChainCotW2's cross-pool move) and the first conv is the conv-back layer (cnnChainCotW1). Every chain cotangent reuses an existing constructor (cnnChainCotW2 / cnnChainCotW1 / cifarChainCotW2) at the 4-stage dims — no new constructor, no new ops, no new bridges. The conv ties are CifarPoC.convW_den/convB_den (generic in the cotangent); the dense head

Spatial bookkeeping (the 2-stage (h,w) convention nested two levels deeper): final pooled (h,w); stage 4 (conv₇/conv₈) at (2h,2w); stage 3 (conv₅/conv₆) at (2(2h),2(2w)); stage 2 (conv₃/conv₄) at (2(2(2h)),…); stage 1 (conv₁/conv₂) at (2(2(2(2h))),…).

Honest residual (same as cifar) #

theorem Proofs.Cifar8PoC.cifar8LossCot_den {ic c1 c2 c3 c4 h w d1 nClasses kH kW : } (nlogN ohN : String) (W₁ : Kernel4 c1 ic kH kW) (b₁ : Vec c1) (W₂ : Kernel4 c1 c1 kH kW) (b₂ : Vec c1) (W₃ : Kernel4 c2 c1 kH kW) (b₃ : Vec c2) (W₄ : Kernel4 c2 c2 kH kW) (b₄ : Vec c2) (W₅ : Kernel4 c3 c2 kH kW) (b₅ : Vec c3) (W₆ : Kernel4 c3 c3 kH kW) (b₆ : Vec c3) (W₇ : Kernel4 c4 c3 kH kW) (b₇ : Vec c4) (W₈ : Kernel4 c4 c4 kH kW) (b₈ : Vec c4) (W₉ : Mat (c4 * h * w) d1) (b₉ : Vec d1) (Wa : Mat d1 d1) (ba : Vec d1) (Wb : Mat d1 nClasses) (bb : Vec nClasses) (x : Vec (ic * (2 * (2 * (2 * (2 * h)))) * (2 * (2 * (2 * (2 * w)))))) (label : Fin nClasses) :
StableHLO.den ((StableHLO.SHlo.operand nlogN (cifarCnn8Forward W₁ b₁ W₂ b₂ W₃ b₃ W₄ b₄ W₅ b₅ W₆ b₆ W₇ b₇ W₈ b₈ W₉ b₉ Wa ba Wb bb x)).expe.softmaxDiv.sub (StableHLO.SHlo.operand ohN (oneHot nClasses label))) = fun (j : Fin nClasses) => softmax nClasses (cifarCnn8Forward W₁ b₁ W₂ b₂ W₃ b₃ W₄ b₄ W₅ b₅ W₆ b₆ W₇ b₇ W₈ b₈ W₉ b₉ Wa ba Wb bb x) j - oneHot nClasses label j

The emitted loss-cotangent graph denotes the softmax-CE gradient of the cifar8 forward.

theorem Proofs.Cifar8PoC.cifar8_Wb_tied_totalloss {ic c1 c2 c3 c4 h w d1 nClasses kH kW : } (aN lrStr dyN : String) (W₁ : Kernel4 c1 ic kH kW) (b₁ : Vec c1) (W₂ : Kernel4 c1 c1 kH kW) (b₂ : Vec c1) (W₃ : Kernel4 c2 c1 kH kW) (b₃ : Vec c2) (W₄ : Kernel4 c2 c2 kH kW) (b₄ : Vec c2) (W₅ : Kernel4 c3 c2 kH kW) (b₅ : Vec c3) (W₆ : Kernel4 c3 c3 kH kW) (b₆ : Vec c3) (W₇ : Kernel4 c4 c3 kH kW) (b₇ : Vec c4) (W₈ : Kernel4 c4 c4 kH kW) (b₈ : Vec c4) (W₉ : Mat (c4 * h * w) d1) (b₉ : Vec d1) (Wa : Mat d1 d1) (ba : Vec d1) (Wb : Mat d1 nClasses) (bb : Vec nClasses) (x : Vec (ic * (2 * (2 * (2 * (2 * h)))) * (2 * (2 * (2 * (2 * w)))))) (a_head : Vec d1) (label : Fin nClasses) (lr : ) (i : Fin d1) (j : Fin nClasses) (hlog : cifarCnn8Forward W₁ b₁ W₂ b₂ W₃ b₃ W₄ b₄ W₅ b₅ W₆ b₆ W₇ b₇ W₈ b₈ W₉ b₉ Wa ba Wb bb x = mnistLinear Wb bb a_head) :
StableHLO.den (StableHLO.SHlo.weightSgd aN "%Wb" lrStr a_head Wb lr (StableHLO.SHlo.operand dyN fun (k : Fin nClasses) => softmax nClasses (cifarCnn8Forward W₁ b₁ W₂ b₂ W₃ b₃ W₄ b₄ W₅ b₅ W₆ b₆ W₇ b₇ W₈ b₈ W₉ b₉ Wa ba Wb bb x) k - oneHot nClasses label k)) (finProdFinEquiv (i, j)) = Wb i j - lr * pdiv (fun (v : Vec (d1 * nClasses)) (x : Fin 1) => crossEntropy nClasses (dense (Mat.unflatten v) bb a_head) label) Wb.flatten (finProdFinEquiv (i, j)) 0

Dense output weight Wb, tied to the WHOLE softmax-CE loss through the cifar8 forward. The dense head is the standard 3-layer MLP; given the forward logits = mnistLinear Wb bb a_head (true by Function.comp_apply, supplied as hlog), Wb folds to ∂CE/∂Wb.

theorem Proofs.Cifar8PoC.cifar8_convs_tied_certified {ic c1 c2 c3 c4 h w d1 nClasses kH kW : } (xN wN bN lrStr cotN : String) (W₁ : Kernel4 c1 ic kH kW) (b₁ : Vec c1) (W₂ : Kernel4 c1 c1 kH kW) (b₂ : Vec c1) (W₃ : Kernel4 c2 c1 kH kW) (b₃ : Vec c2) (W₄ : Kernel4 c2 c2 kH kW) (b₄ : Vec c2) (W₅ : Kernel4 c3 c2 kH kW) (b₅ : Vec c3) (W₆ : Kernel4 c3 c3 kH kW) (b₆ : Vec c3) (W₇ : Kernel4 c4 c3 kH kW) (b₇ : Vec c4) (W₈ : Kernel4 c4 c4 kH kW) (b₈ : Vec c4) (W₉ : Mat (c4 * h * w) d1) (b₉ : Vec d1) (Wa : Mat d1 d1) (ba : Vec d1) (Wb : Mat d1 nClasses) (bb : Vec nClasses) (x : Tensor3 ic (2 * (2 * (2 * (2 * h)))) (2 * (2 * (2 * (2 * w))))) (label : Fin nClasses) (lr : ) :
have xv := x.flatten; have cc1 := flatConv W₁ b₁ xv; have r1 := relu (c1 * (2 * (2 * (2 * (2 * h)))) * (2 * (2 * (2 * (2 * w))))) cc1; have r1t := Tensor3.unflatten r1; have cc2 := flatConv W₂ b₂ r1; have r2 := relu (c1 * (2 * (2 * (2 * (2 * h)))) * (2 * (2 * (2 * (2 * w))))) cc2; have r2t := Tensor3.unflatten r2; have zp1 := maxPoolFlat c1 (2 * (2 * (2 * h))) (2 * (2 * (2 * w))) r2; have zp1t := Tensor3.unflatten zp1; have cc3 := flatConv W₃ b₃ zp1; have r3 := relu (c2 * (2 * (2 * (2 * h))) * (2 * (2 * (2 * w)))) cc3; have r3t := Tensor3.unflatten r3; have cc4 := flatConv W₄ b₄ r3; have r4 := relu (c2 * (2 * (2 * (2 * h))) * (2 * (2 * (2 * w)))) cc4; have r4t := Tensor3.unflatten r4; have zp2 := maxPoolFlat c2 (2 * (2 * h)) (2 * (2 * w)) r4; have zp2t := Tensor3.unflatten zp2; have cc5 := flatConv W₅ b₅ zp2; have r5 := relu (c3 * (2 * (2 * h)) * (2 * (2 * w))) cc5; have r5t := Tensor3.unflatten r5; have cc6 := flatConv W₆ b₆ r5; have r6 := relu (c3 * (2 * (2 * h)) * (2 * (2 * w))) cc6; have r6t := Tensor3.unflatten r6; have zp3 := maxPoolFlat c3 (2 * h) (2 * w) r6; have zp3t := Tensor3.unflatten zp3; have cc7 := flatConv W₇ b₇ zp3; have r7 := relu (c4 * (2 * h) * (2 * w)) cc7; have r7t := Tensor3.unflatten r7; have cc8 := flatConv W₈ b₈ r7; have r8 := relu (c4 * (2 * h) * (2 * w)) cc8; have r8t := Tensor3.unflatten r8; have zp4 := maxPoolFlat c4 h w r8; have h9 := dense W₉ b₉ zp4; have ha := dense Wa ba (relu d1 h9); have g := fun (k : Fin nClasses) => softmax nClasses (cifarCnn8Forward W₁ b₁ W₂ b₂ W₃ b₃ W₄ b₄ W₅ b₅ W₆ b₆ W₇ b₇ W₈ b₈ W₉ b₉ Wa ba Wb bb xv) k - oneHot nClasses label k; have cotC8 := cnnChainCotW2 W₉ Wa Wb h9 ha r8t cc8 g; have cotC7 := cnnChainCotW1 W₈ cc7 cotC8; have cotC6 := CifarPoC.cifarChainCotW2 W₇ r6t cc6 cotC7; have cotC5 := cnnChainCotW1 W₆ cc5 cotC6; have cotC4 := CifarPoC.cifarChainCotW2 W₅ r4t cc4 cotC5; have cotC3 := cnnChainCotW1 W₄ cc3 cotC4; have cotC2 := CifarPoC.cifarChainCotW2 W₃ r2t cc2 cotC3; have cotC1 := cnnChainCotW1 W₂ cc1 cotC2; ConvWSgdTied xN wN lrStr cotN b₁ x W₁ cotC1 lr ConvBSgdTied bN lrStr cotN W₁ x b₁ cotC1 lr ConvWSgdTied xN wN lrStr cotN b₂ r1t W₂ cotC2 lr ConvBSgdTied bN lrStr cotN W₂ r1t b₂ cotC2 lr ConvWSgdTied xN wN lrStr cotN b₃ zp1t W₃ cotC3 lr ConvBSgdTied bN lrStr cotN W₃ zp1t b₃ cotC3 lr ConvWSgdTied xN wN lrStr cotN b₄ r3t W₄ cotC4 lr

Whole cifar8 conv tail, tied. All 16 conv params (8 conv W+b), at the real cifar8 forward and the composed softmax-CE cotangent, denote the certified loss-descent step. Each conv op is fed the cotangent the 4-stage backward chain delivers: cnnChainCotW2 (conv₈, the last before pool₄), cnnChainCotW1 (conv₇/₅/₃/₁, the within-stage conv-back), cifarChainCotW2 (conv₆/₄/₂, the cross-pool move). Together with the dense head (cifar8_Wb_tied_totalloss + the generic denseW_den/denseB_den at g) the WHOLE cifar8 train step is den-composed forward→loss→backward.