Documentation

LeanMlir.Proofs.Nets.MobileNet.MobileNetV2

A two-block inverted-residual net — end-to-end VJP (flattened Vec space) #

Defines mobilenetv2Forward — stem conv → bn → relu6, one inverted-residual block with a skip, one without (channel change), global average pool, dense head — and proves its vector–Jacobian product correct at a smooth point (mobilenetv2HasVJPAt, mobilenetv2HasVJPAt_correct), as cnnHasVJPAt does for the two-residual-block CNN. Every "bn" in this file is bnForward: one normalisation over the whole flattened tensor with scalar γ β : ℝ, not per-channel BatchNorm. Every conv is stride 1. The seventeen-block, batch-BatchNorm net the MobileNetV2 artifacts train is mobilenetv2ForwardBFull (MobileNetV2FullB).

Everything lives in flattened Vec space and reuses the foundation rules from CNN.lean, Depthwise.lean, BatchNorm.lean, MLP.lean, and Residual.lean through vjpCompAt chaining.

What's new here #

Padding convention #

The strided pieces of the batched chain (MobileNetV2BackB0.lean on) read flatConvStride2Xla / depthwiseStride2FlatXla, the XLA-SAME (odd) phase every MobileNetV2 artifact emits. The 2-block generic mobilenetv2Forward here has a stride-1 stem.

All new defs/theorems certify to exactly [propext, Classical.choice, Quot.sound].

References #

@[reducible]
noncomputable def Proofs.ivExpand {ic mid h w kHe kWe : ℕ} (We : Kernel4 mid ic kHe kWe) (be : Vec mid) (εe γe βe : ℝ) :
Vec (ic * h * w) → Vec (mid * h * w)

The expand stage as a flat map.

Equations
Instances For
    @[reducible]
    noncomputable def Proofs.ivDepthwise {mid h w kHd kWd : ℕ} (Wd : DepthwiseKernel mid kHd kWd) (bd : Vec mid) (εd γd βd : ℝ) :
    Vec (mid * h * w) → Vec (mid * h * w)

    The depthwise stage as a flat map.

    Equations
    Instances For
      @[reducible]
      noncomputable def Proofs.ivProject {mid oc h w kHp kWp : ℕ} (Wp : Kernel4 oc mid kHp kWp) (bp : Vec oc) (εp γp βp : ℝ) :
      Vec (mid * h * w) → Vec (oc * h * w)

      The project (linear bottleneck) stage as a flat map.

      Equations
      Instances For
        @[reducible]
        noncomputable def Proofs.invresBody {ic mid oc h w kHe kWe kHd kWd kHp kWp : ℕ} (We : Kernel4 mid ic kHe kWe) (be : Vec mid) (εe γe βe : ℝ) (Wd : DepthwiseKernel mid kHd kWd) (bd : Vec mid) (εd γd βd : ℝ) (Wp : Kernel4 oc mid kHp kWp) (bp : Vec oc) (εp γp βp : ℝ) :
        Vec (ic * h * w) → Vec (oc * h * w)

        Inverted-residual body = project ∘ depthwise ∘ expand. Flat Vec (ic*h*w) → Vec (oc*h*w).

        Equations
        Instances For
          noncomputable def Proofs.mobilenetv2Forward {ic c mid₁ oc mid₂ h w kHs kWs kHe₁ kWe₁ kHd₁ kWd₁ kHp₁ kWp₁ kHe₂ kWe₂ kHd₂ kWd₂ kHp₂ kWp₂ nClasses : ℕ} (Ws : Kernel4 c ic kHs kWs) (bs : Vec c) (εs γs βs : ℝ) (We₁ : Kernel4 mid₁ c kHe₁ kWe₁) (be₁ : Vec mid₁) (e₁ ge₁ be1 : ℝ) (Wd₁ : DepthwiseKernel mid₁ kHd₁ kWd₁) (bd₁ : Vec mid₁) (d₁ gd₁ bd1 : ℝ) (Wp₁ : Kernel4 c mid₁ kHp₁ kWp₁) (bp₁ : Vec c) (p₁ gp₁ bp1 : ℝ) (We₂ : Kernel4 mid₂ c kHe₂ kWe₂) (be₂ : Vec mid₂) (e₂ ge₂ be2 : ℝ) (Wd₂ : DepthwiseKernel mid₂ kHd₂ kWd₂) (bd₂ : Vec mid₂) (d₂ gd₂ bd2 : ℝ) (Wp₂ : Kernel4 oc mid₂ kHp₂ kWp₂) (bp₂ : Vec oc) (p₂ gp₂ bp2 : ℝ) (Wh : Mat oc nClasses) (bh : Vec nClasses) :
          Vec (ic * h * w) → Vec nClasses

          The forward MobileNetV2.

          Equations
          • One or more equations did not get rendered due to their size.
          Instances For
            noncomputable def Proofs.mobilenetv2HasVJPAt {ic c mid₁ oc mid₂ h w kHs kWs kHe₁ kWe₁ kHd₁ kWd₁ kHp₁ kWp₁ kHe₂ kWe₂ kHd₂ kWd₂ kHp₂ kWp₂ nClasses : ℕ} (Ws : Kernel4 c ic kHs kWs) (bs : Vec c) (εs γs βs : ℝ) (hεs : 0 < εs) (We₁ : Kernel4 mid₁ c kHe₁ kWe₁) (be₁ : Vec mid₁) (e₁ ge₁ be1 : ℝ) (he₁ : 0 < e₁) (Wd₁ : DepthwiseKernel mid₁ kHd₁ kWd₁) (bd₁ : Vec mid₁) (d₁ gd₁ bd1 : ℝ) (hd₁ : 0 < d₁) (Wp₁ : Kernel4 c mid₁ kHp₁ kWp₁) (bp₁ : Vec c) (p₁ gp₁ bp1 : ℝ) (hp₁ : 0 < p₁) (We₂ : Kernel4 mid₂ c kHe₂ kWe₂) (be₂ : Vec mid₂) (e₂ ge₂ be2 : ℝ) (he₂ : 0 < e₂) (Wd₂ : DepthwiseKernel mid₂ kHd₂ kWd₂) (bd₂ : Vec mid₂) (d₂ gd₂ bd2 : ℝ) (hd₂ : 0 < d₂) (Wp₂ : Kernel4 oc mid₂ kHp₂ kWp₂) (bp₂ : Vec oc) (p₂ gp₂ bp2 : ℝ) (hp₂ : 0 < p₂) (Wh : Mat oc nClasses) (bh : Vec nClasses) (x : Vec (ic * h * w)) (h_stem : ∀ (k : Fin (c * h * w)), bnForward (c * h * w) εs γs βs (flatConv Ws bs x) k ≠ 0 ∧ bnForward (c * h * w) εs γs βs (flatConv Ws bs x) k ≠ 6) (h_b1e : ∀ (k : Fin (mid₁ * h * w)), bnForward (mid₁ * h * w) e₁ ge₁ be1 (flatConv We₁ be₁ ((relu6 (c * h * w) ∘ bnForward (c * h * w) εs γs βs ∘ flatConv Ws bs) x)) k ≠ 0 ∧ bnForward (mid₁ * h * w) e₁ ge₁ be1 (flatConv We₁ be₁ ((relu6 (c * h * w) ∘ bnForward (c * h * w) εs γs βs ∘ flatConv Ws bs) x)) k ≠ 6) (h_b1d : ∀ (k : Fin (mid₁ * h * w)), bnForward (mid₁ * h * w) d₁ gd₁ bd1 (depthwiseFlat Wd₁ bd₁ (ivExpand We₁ be₁ e₁ ge₁ be1 ((relu6 (c * h * w) ∘ bnForward (c * h * w) εs γs βs ∘ flatConv Ws bs) x))) k ≠ 0 ∧ bnForward (mid₁ * h * w) d₁ gd₁ bd1 (depthwiseFlat Wd₁ bd₁ (ivExpand We₁ be₁ e₁ ge₁ be1 ((relu6 (c * h * w) ∘ bnForward (c * h * w) εs γs βs ∘ flatConv Ws bs) x))) k ≠ 6) (h_b2e : ∀ (k : Fin (mid₂ * h * w)), bnForward (mid₂ * h * w) e₂ ge₂ be2 (flatConv We₂ be₂ (residual (invresBody We₁ be₁ e₁ ge₁ be1 Wd₁ bd₁ d₁ gd₁ bd1 Wp₁ bp₁ p₁ gp₁ bp1) ((relu6 (c * h * w) ∘ bnForward (c * h * w) εs γs βs ∘ flatConv Ws bs) x))) k ≠ 0 ∧ bnForward (mid₂ * h * w) e₂ ge₂ be2 (flatConv We₂ be₂ (residual (invresBody We₁ be₁ e₁ ge₁ be1 Wd₁ bd₁ d₁ gd₁ bd1 Wp₁ bp₁ p₁ gp₁ bp1) ((relu6 (c * h * w) ∘ bnForward (c * h * w) εs γs βs ∘ flatConv Ws bs) x))) k ≠ 6) (h_b2d : ∀ (k : Fin (mid₂ * h * w)), bnForward (mid₂ * h * w) d₂ gd₂ bd2 (depthwiseFlat Wd₂ bd₂ (ivExpand We₂ be₂ e₂ ge₂ be2 (residual (invresBody We₁ be₁ e₁ ge₁ be1 Wd₁ bd₁ d₁ gd₁ bd1 Wp₁ bp₁ p₁ gp₁ bp1) ((relu6 (c * h * w) ∘ bnForward (c * h * w) εs γs βs ∘ flatConv Ws bs) x)))) k ≠ 0 ∧ bnForward (mid₂ * h * w) d₂ gd₂ bd2 (depthwiseFlat Wd₂ bd₂ (ivExpand We₂ be₂ e₂ ge₂ be2 (residual (invresBody We₁ be₁ e₁ ge₁ be1 Wd₁ bd₁ d₁ gd₁ bd1 Wp₁ bp₁ p₁ gp₁ bp1) ((relu6 (c * h * w) ∘ bnForward (c * h * w) εs γs βs ∘ flatConv Ws bs) x)))) k ≠ 6) :
            HasVJPAt (mobilenetv2Forward Ws bs εs γs βs We₁ be₁ e₁ ge₁ be1 Wd₁ bd₁ d₁ gd₁ bd1 Wp₁ bp₁ p₁ gp₁ bp1 We₂ be₂ e₂ ge₂ be2 Wd₂ bd₂ d₂ gd₂ bd2 Wp₂ bp₂ p₂ gp₂ bp2 Wh bh) x

            MobileNetV2 end-to-end VJP at a smooth point. Chains the stem, a skip inverted-residual, a no-skip inverted-residual, global avg pool, and dense head with vjpCompAt under one bundled smoothness family (one ≠0∧≠6 hypothesis per relu6 site, evaluated at the running activation).

            Equations
            • One or more equations did not get rendered due to their size.
            Instances For
              theorem Proofs.mobilenetv2HasVJPAt_correct {ic c mid₁ oc mid₂ h w kHs kWs kHe₁ kWe₁ kHd₁ kWd₁ kHp₁ kWp₁ kHe₂ kWe₂ kHd₂ kWd₂ kHp₂ kWp₂ nClasses : ℕ} (Ws : Kernel4 c ic kHs kWs) (bs : Vec c) (εs γs βs : ℝ) (hεs : 0 < εs) (We₁ : Kernel4 mid₁ c kHe₁ kWe₁) (be₁ : Vec mid₁) (e₁ ge₁ be1 : ℝ) (he₁ : 0 < e₁) (Wd₁ : DepthwiseKernel mid₁ kHd₁ kWd₁) (bd₁ : Vec mid₁) (d₁ gd₁ bd1 : ℝ) (hd₁ : 0 < d₁) (Wp₁ : Kernel4 c mid₁ kHp₁ kWp₁) (bp₁ : Vec c) (p₁ gp₁ bp1 : ℝ) (hp₁ : 0 < p₁) (We₂ : Kernel4 mid₂ c kHe₂ kWe₂) (be₂ : Vec mid₂) (e₂ ge₂ be2 : ℝ) (he₂ : 0 < e₂) (Wd₂ : DepthwiseKernel mid₂ kHd₂ kWd₂) (bd₂ : Vec mid₂) (d₂ gd₂ bd2 : ℝ) (hd₂ : 0 < d₂) (Wp₂ : Kernel4 oc mid₂ kHp₂ kWp₂) (bp₂ : Vec oc) (p₂ gp₂ bp2 : ℝ) (hp₂ : 0 < p₂) (Wh : Mat oc nClasses) (bh : Vec nClasses) (x : Vec (ic * h * w)) (h_stem : ∀ (k : Fin (c * h * w)), bnForward (c * h * w) εs γs βs (flatConv Ws bs x) k ≠ 0 ∧ bnForward (c * h * w) εs γs βs (flatConv Ws bs x) k ≠ 6) (h_b1e : ∀ (k : Fin (mid₁ * h * w)), bnForward (mid₁ * h * w) e₁ ge₁ be1 (flatConv We₁ be₁ ((relu6 (c * h * w) ∘ bnForward (c * h * w) εs γs βs ∘ flatConv Ws bs) x)) k ≠ 0 ∧ bnForward (mid₁ * h * w) e₁ ge₁ be1 (flatConv We₁ be₁ ((relu6 (c * h * w) ∘ bnForward (c * h * w) εs γs βs ∘ flatConv Ws bs) x)) k ≠ 6) (h_b1d : ∀ (k : Fin (mid₁ * h * w)), bnForward (mid₁ * h * w) d₁ gd₁ bd1 (depthwiseFlat Wd₁ bd₁ (ivExpand We₁ be₁ e₁ ge₁ be1 ((relu6 (c * h * w) ∘ bnForward (c * h * w) εs γs βs ∘ flatConv Ws bs) x))) k ≠ 0 ∧ bnForward (mid₁ * h * w) d₁ gd₁ bd1 (depthwiseFlat Wd₁ bd₁ (ivExpand We₁ be₁ e₁ ge₁ be1 ((relu6 (c * h * w) ∘ bnForward (c * h * w) εs γs βs ∘ flatConv Ws bs) x))) k ≠ 6) (h_b2e : ∀ (k : Fin (mid₂ * h * w)), bnForward (mid₂ * h * w) e₂ ge₂ be2 (flatConv We₂ be₂ (residual (invresBody We₁ be₁ e₁ ge₁ be1 Wd₁ bd₁ d₁ gd₁ bd1 Wp₁ bp₁ p₁ gp₁ bp1) ((relu6 (c * h * w) ∘ bnForward (c * h * w) εs γs βs ∘ flatConv Ws bs) x))) k ≠ 0 ∧ bnForward (mid₂ * h * w) e₂ ge₂ be2 (flatConv We₂ be₂ (residual (invresBody We₁ be₁ e₁ ge₁ be1 Wd₁ bd₁ d₁ gd₁ bd1 Wp₁ bp₁ p₁ gp₁ bp1) ((relu6 (c * h * w) ∘ bnForward (c * h * w) εs γs βs ∘ flatConv Ws bs) x))) k ≠ 6) (h_b2d : ∀ (k : Fin (mid₂ * h * w)), bnForward (mid₂ * h * w) d₂ gd₂ bd2 (depthwiseFlat Wd₂ bd₂ (ivExpand We₂ be₂ e₂ ge₂ be2 (residual (invresBody We₁ be₁ e₁ ge₁ be1 Wd₁ bd₁ d₁ gd₁ bd1 Wp₁ bp₁ p₁ gp₁ bp1) ((relu6 (c * h * w) ∘ bnForward (c * h * w) εs γs βs ∘ flatConv Ws bs) x)))) k ≠ 0 ∧ bnForward (mid₂ * h * w) d₂ gd₂ bd2 (depthwiseFlat Wd₂ bd₂ (ivExpand We₂ be₂ e₂ ge₂ be2 (residual (invresBody We₁ be₁ e₁ ge₁ be1 Wd₁ bd₁ d₁ gd₁ bd1 Wp₁ bp₁ p₁ gp₁ bp1) ((relu6 (c * h * w) ∘ bnForward (c * h * w) εs γs βs ∘ flatConv Ws bs) x)))) k ≠ 6) (dy : Vec nClasses) (i : Fin (ic * h * w)) :
              (mobilenetv2HasVJPAt Ws bs εs γs βs hεs We₁ be₁ e₁ ge₁ be1 he₁ Wd₁ bd₁ d₁ gd₁ bd1 hd₁ Wp₁ bp₁ p₁ gp₁ bp1 hp₁ We₂ be₂ e₂ ge₂ be2 he₂ Wd₂ bd₂ d₂ gd₂ bd2 hd₂ Wp₂ bp₂ p₂ gp₂ bp2 hp₂ Wh bh x h_stem h_b1e h_b1d h_b2e h_b2d).backward dy i = ∑ j : Fin nClasses, pdiv (mobilenetv2Forward Ws bs εs γs βs We₁ be₁ e₁ ge₁ be1 Wd₁ bd₁ d₁ gd₁ bd1 Wp₁ bp₁ p₁ gp₁ bp1 We₂ be₂ e₂ ge₂ be2 Wd₂ bd₂ d₂ gd₂ bd2 Wp₂ bp₂ p₂ gp₂ bp2 Wh bh) x i j * dy j

              Public correctness theorem for mobilenetv2HasVJPAt — exposes the witness's .correct field: at a point where all five relu6 sites (stem, and the expand and depthwise stages of both blocks: h_stem, h_b1e, h_b1d, h_b2e, h_b2d) are away from 0 and 6, the backward of the two-block mobilenetv2Forward equals the pdiv-contracted Jacobian (Jacobian-transpose applied to the cotangent). Analogue of cnnHasVJPAt_correct.