Documentation

LeanMlir.Proofs.Nets.ResNet.ResNet34BackB0

Backward-graph faithfulness for the VERIFIED ResNet-34 basic block #

The ResNet-34 peer of EfficientNetBackB0.lean / MobileNetV2BackB0.lean: a backward StableHLO graph that denotes the proven VJP of the batched ResNet-34 basic block.

The basic block is relu ∘ residual(F) (identity block), with body F = (conv-bn) ∘ (conv-bn-relu) — a 3×3 conv → bn → relu, then a 3×3 conv → bn (no activation), and an identity skip, followed by an outer relu after the residual add. This last fact is the structural difference from the MobileNetV2/EfficientNet residual blocks (whose residual add is the block output): r34 wraps the residual add in one more relu.

The relu wrinkle (vs relu6 / swish) #

r34 uses relu (one kink, at 0): its VJP is only the pointwise reluHasVJPAt, conditioned on the smoothness hypothesis ∀ k, x k ≠ 0 at the pre-activation — simpler than relu6's two-sided x k ≠ 0 ∧ x k ≠ 6, but the same _at machinery (vjpCompAt + HasVJP.toHasVJPAt). Its per-op backward token is .selectPos (the mask if x>0 then dy else 0), whose denotation faithfulness is the already-proven (rfl) StableHLO.selectPos_faithful.

Because there are TWO relu kinks (the body's mid-relu AND the outer post-residual relu), the whole-block VJP and its backward-graph faithfulness are _at / hypothesis-threaded: one smoothness family at the body's mid-relu pre-activation, one at the outer relu's pre-activation residual(F)(x).

Structure #

The stages are defined in Foundation, not here: cbReluB (batched conv → bn → relu, the relu sibling of cbrB), cbReluStridedB and projStridedB, with their _at VJPs, the backward graph cbReluBackBatchedGraph + cbReluBackBatchedGraph_faithful (chaining selectPos_faithful + bnBatchLABack_faithful + convBackBatched_faithful), and the four CertLayers cbReluLayer / projLayer / cbReluStridedLayer / projStridedLayer, are in Foundation/Batched/StageLayers.lean; projB (conv → bn, no activation, the body's cbB) is in Foundation/Batched/Stages.lean; projBackBatchedGraph / projBackBatchedGraph_faithful are in Foundation/Batched/BackLinks.lean. This file holds the ResNet-34 assemblies:

The strided/downsample block (relu ∘ residualProj(proj, F_s)) #

The downsample-block capstone (r34DownBlockBackBatchedGraph_faithful) reuses the strided batched-conv backward primitive convStridedBackBatched (StableHLO.Basic, the stride-2 analog of convBackBatched; convStridedBackBatched_faithful is in Foundation/Batched/BackLinks.lean). The body F_s = projB ∘ cbReluStridedB has a stride-2 conv1 (cbReluStridedB, the strided sibling of cbReluB) and a stride-1 conv2 (projB); the projection skip projStridedB is a stride-2 conv-bn. The whole block composes vjpCompAt(residualProjHasVJPAt(proj, F_s), relu) exactly like the identity block, but with the projection skip (residualProj, both paths nontrivial) instead of the identity skip (residual), and the strided convs in the body+skip.

noncomputable def Proofs.StableHLO.r34BasicBlockLayer (N : ℕ) {c h w kH₁ kW₁ kH₂ kW₂ : ℕ} (W₁ : Kernel4 c c kH₁ kW₁) (b₁ : Vec c) (ε₁ : ℝ) (hε₁ : 0 < ε₁) (γ₁ β₁ : Vec c) (W₂ : Kernel4 c c kH₂ kW₂) (b₂ : Vec c) (ε₂ : ℝ) (hε₂ : 0 < ε₂) (γ₂ β₂ : Vec c) :
CertLayer (N * (c * h * w)) (N * (c * h * w))

The batched R34 identity basic block as a CertLayer: residual (cbReluLayer ; projLayer), then reluOut. Its ok is the body's mid-relu and the OUTER post-residual relu — the extra factor R34 has over the MBConv/inverted-residual blocks (projLayer contributes True). An endomorphism, so chain iterates it into a stage tail.

Equations
  • One or more equations did not get rendered due to their size.
Instances For
    noncomputable def Proofs.StableHLO.r34BasicBlockBHasVJPAt (N : ℕ) {c h w kH₁ kW₁ kH₂ kW₂ : ℕ} (W₁ : Kernel4 c c kH₁ kW₁) (b₁ : Vec c) (ε₁ : ℝ) (hε₁ : 0 < ε₁) (γ₁ β₁ : Vec c) (W₂ : Kernel4 c c kH₂ kW₂) (b₂ : Vec c) (ε₂ : ℝ) (hε₂ : 0 < ε₂) (γ₂ β₂ : Vec c) (x : Vec (N * (c * h * w))) (h_s1 : ∀ (k : Fin (N * (c * h * w))), bnBatchLA N c h w ε₁ γ₁ β₁ (batchMap N (flatConv W₁ b₁) x) k ≠ 0) (h_out : ∀ (k : Fin (N * (c * h * w))), residual (projB N W₂ b₂ ε₂ γ₂ β₂ ∘ cbReluB N W₁ b₁ ε₁ γ₁ β₁) x k ≠ 0) :
    HasVJPAt (relu (N * (c * h * w)) ∘ residual (projB N W₂ b₂ ε₂ γ₂ β₂ ∘ cbReluB N W₁ b₁ ε₁ γ₁ β₁)) x

    The batched ResNet-34 identity basic block's VJP at a smooth point — relu ∘ residual(F) with body F = projB ∘ cbReluB: the residual fan-in VJP of the body, then the OUTER relu's pointwise VJP at the pre-relu activation residual(F)(x) (r34BasicBlockLayer's VJP).

    h_s1 is the body's mid-relu smoothness; h_out is the outer-relu smoothness (at residual(F)(x)).

    Equations
    Instances For
      noncomputable def Proofs.StableHLO.r34BasicBlockBackBatchedGraph {N c h w kH₁ kW₁ kH₂ kW₂ : ℕ} (W₁ : Kernel4 c c kH₁ kW₁) (b₁ : Vec c) (ε₁ : ℝ) (γ₁ β₁ : Vec c) (W₂ : Kernel4 c c kH₂ kW₂) (b₂ : Vec c) (ε₂ : ℝ) (γ₂ β₂ : Vec c) (x : Vec (N * (c * h * w))) (ecot : SHlo (N * (c * h * w))) :
      SHlo (N * (c * h * w))

      The whole batched ResNet-34 identity basic block backward graph: selectPos (outer relu) ∘ residual fan-in (body backward + identity skip). The outer relu is the LAST forward op, so its .selectPos backward is the OUTERMOST backward op; inside, the residual addV sums the body's input-cotangent (r34BodyBackBatchedGraph fed the relu-masked cotangent) and the identity skip's verbatim cotangent (%dy).

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For
        theorem Proofs.StableHLO.r34BasicBlockBackBatchedGraph_faithful {N c h w kH₁ kW₁ kH₂ kW₂ : ℕ} (W₁ : Kernel4 c c kH₁ kW₁) (b₁ : Vec c) (ε₁ : ℝ) (hε₁ : 0 < ε₁) (γ₁ β₁ : Vec c) (W₂ : Kernel4 c c kH₂ kW₂) (b₂ : Vec c) (ε₂ : ℝ) (hε₂ : 0 < ε₂) (γ₂ β₂ : Vec c) (x : Vec (N * (c * h * w))) (ecot : SHlo (N * (c * h * w))) (h_s1 : ∀ (k : Fin (N * (c * h * w))), bnBatchLA N c h w ε₁ γ₁ β₁ (batchMap N (flatConv W₁ b₁) x) k ≠ 0) (h_out : ∀ (k : Fin (N * (c * h * w))), residual (projB N W₂ b₂ ε₂ γ₂ β₂ ∘ cbReluB N W₁ b₁ ε₁ γ₁ β₁) x k ≠ 0) :
        den (r34BasicBlockBackBatchedGraph W₁ b₁ ε₁ γ₁ β₁ W₂ b₂ ε₂ γ₂ β₂ x ecot) = (r34BasicBlockBHasVJPAt N W₁ b₁ ε₁ hε₁ γ₁ β₁ W₂ b₂ ε₂ hε₂ γ₂ β₂ x h_s1 h_out).backward (den ecot)

        CAPSTONE — the whole batched ResNet-34 identity basic block: backward graph ↔ the proven VJP. The two batched stage backward graphs (cbReluB/projB) chained at their forward activations, wrapped in the residual additive fan-in (body cotangent + identity skip) and the OUTER post-residual relu, proven equal to r34BasicBlockBHasVJPAt (= vjpCompAt(residualHasVJPAt(F), relu)). The ResNet-34 analogue of mbResidBlockBackBatchedGraph_faithful / mnv2ResidBlockBackBatchedGraph_faithful, with the extra outer-relu factor, threaded through both relu smoothness hypotheses. It is r34BasicBlockLayer's faithful.

        Key fact: the outer relu's .selectPos mask is applied ONCE to the incoming dy (giving masked = reluHasVJPAt.backward (den ecot)), and that masked cotangent is what the residual fan-in (r34BodyBackBatchedGraph + identity skip) sees — exactly matching vjpCompAt(residual, relu)'s structure: first apply relu's backward, then residual's backward to the result.

        noncomputable def Proofs.StableHLO.r34DownBlockLayer (N : ℕ) {ic oc h w kH₁ kW₁ kH₂ kW₂ kHp kWp : ℕ} (W₁ : Kernel4 oc ic kH₁ kW₁) (b₁ : Vec oc) (ε₁ : ℝ) (hε₁ : 0 < ε₁) (γ₁ β₁ : Vec oc) (W₂ : Kernel4 oc oc kH₂ kW₂) (b₂ : Vec oc) (ε₂ : ℝ) (hε₂ : 0 < ε₂) (γ₂ β₂ : Vec oc) (Wp : Kernel4 oc ic kHp kWp) (bp : Vec oc) (εp : ℝ) (hεp : 0 < εp) (γp βp : Vec oc) :
        CertLayer (N * (ic * (2 * h) * (2 * w))) (N * (oc * h * w))

        The batched R34 downsample basic block as a CertLayer: residualProj (projStridedLayer) (cbReluStridedLayer ; projLayer), then reluOut. Halves resolution (hence the 2*h in the input type), with a strided conv1 and a strided projection skip.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For
          noncomputable def Proofs.StableHLO.r34DownBlockBHasVJPAt (N : ℕ) {ic oc h w kH₁ kW₁ kH₂ kW₂ kHp kWp : ℕ} (W₁ : Kernel4 oc ic kH₁ kW₁) (b₁ : Vec oc) (ε₁ : ℝ) (hε₁ : 0 < ε₁) (γ₁ β₁ : Vec oc) (W₂ : Kernel4 oc oc kH₂ kW₂) (b₂ : Vec oc) (ε₂ : ℝ) (hε₂ : 0 < ε₂) (γ₂ β₂ : Vec oc) (Wp : Kernel4 oc ic kHp kWp) (bp : Vec oc) (εp : ℝ) (hεp : 0 < εp) (γp βp : Vec oc) (x : Vec (N * (ic * (2 * h) * (2 * w)))) (h_s1 : ∀ (k : Fin (N * (oc * h * w))), bnBatchLA N oc h w ε₁ γ₁ β₁ (batchMap N (flatConvStride2 W₁ b₁) x) k ≠ 0) (h_out : ∀ (k : Fin (N * (oc * h * w))), residualProj (projStridedB N Wp bp εp γp βp) (projB N W₂ b₂ ε₂ γ₂ β₂ ∘ cbReluStridedB N W₁ b₁ ε₁ γ₁ β₁) x k ≠ 0) :
          HasVJPAt (relu (N * (oc * h * w)) ∘ residualProj (projStridedB N Wp bp εp γp βp) (projB N W₂ b₂ ε₂ γ₂ β₂ ∘ cbReluStridedB N W₁ b₁ ε₁ γ₁ β₁)) x

          The batched ResNet-34 downsample basic block's VJP at a smooth point — relu ∘ residualProj(proj, F_s) with body F_s = projB ∘ cbReluStridedB and projection skip proj = projStridedB: the projected residual fan-in VJP (skip + body), then the OUTER relu's pointwise VJP at the pre-relu activation residualProj(proj, F_s)(x) (r34DownBlockLayer's VJP).

          The strided sibling of r34BasicBlockBHasVJPAt: residualProj (BOTH paths nontrivial) for residual (identity skip), strided convs in body+skip.

          h_s1 is the body's mid-relu smoothness; h_out is the outer-relu smoothness.

          Equations
          • One or more equations did not get rendered due to their size.
          Instances For
            noncomputable def Proofs.StableHLO.r34DownBlockBackBatchedGraph {N ic oc h w kH₁ kW₁ kH₂ kW₂ kHp kWp : ℕ} (W₁ : Kernel4 oc ic kH₁ kW₁) (b₁ : Vec oc) (ε₁ : ℝ) (γ₁ β₁ : Vec oc) (W₂ : Kernel4 oc oc kH₂ kW₂) (b₂ : Vec oc) (ε₂ : ℝ) (γ₂ β₂ : Vec oc) (Wp : Kernel4 oc ic kHp kWp) (bp : Vec oc) (εp : ℝ) (γp βp : Vec oc) (x : Vec (N * (ic * (2 * h) * (2 * w)))) (ecot : SHlo (N * (oc * h * w))) :
            SHlo (N * (ic * (2 * h) * (2 * w)))

            The whole batched ResNet-34 downsample basic block backward graph: selectPos (outer relu) ∘ projected-residual fan-in (body backward + PROJECTION skip backward). The outer relu is the LAST forward op, so its .selectPos backward is the OUTERMOST backward op; inside, the residualProj addV sums the projection skip's input-cotangent (projStridedBackBatchedGraph fed the relu-masked cotangent) and the body's input-cotangent (r34DownBodyBackBatchedGraph, same masked cotangent). Unlike the identity block, BOTH operands are nontrivial backward subgraphs (the skip is a strided conv-bn, not a verbatim %dy passthrough).

            Equations
            • One or more equations did not get rendered due to their size.
            Instances For
              theorem Proofs.StableHLO.r34DownBlockBackBatchedGraph_faithful {N ic oc h w kH₁ kW₁ kH₂ kW₂ kHp kWp : ℕ} (W₁ : Kernel4 oc ic kH₁ kW₁) (b₁ : Vec oc) (ε₁ : ℝ) (hε₁ : 0 < ε₁) (γ₁ β₁ : Vec oc) (W₂ : Kernel4 oc oc kH₂ kW₂) (b₂ : Vec oc) (ε₂ : ℝ) (hε₂ : 0 < ε₂) (γ₂ β₂ : Vec oc) (Wp : Kernel4 oc ic kHp kWp) (bp : Vec oc) (εp : ℝ) (hεp : 0 < εp) (γp βp : Vec oc) (x : Vec (N * (ic * (2 * h) * (2 * w)))) (ecot : SHlo (N * (oc * h * w))) (h_s1 : ∀ (k : Fin (N * (oc * h * w))), bnBatchLA N oc h w ε₁ γ₁ β₁ (batchMap N (flatConvStride2 W₁ b₁) x) k ≠ 0) (h_out : ∀ (k : Fin (N * (oc * h * w))), residualProj (projStridedB N Wp bp εp γp βp) (projB N W₂ b₂ ε₂ γ₂ β₂ ∘ cbReluStridedB N W₁ b₁ ε₁ γ₁ β₁) x k ≠ 0) :
              den (r34DownBlockBackBatchedGraph W₁ b₁ ε₁ γ₁ β₁ W₂ b₂ ε₂ γ₂ β₂ Wp bp εp γp βp x ecot) = (r34DownBlockBHasVJPAt N W₁ b₁ ε₁ hε₁ γ₁ β₁ W₂ b₂ ε₂ hε₂ γ₂ β₂ Wp bp εp hεp γp βp x h_s1 h_out).backward (den ecot)

              CAPSTONE — the whole batched ResNet-34 DOWNSAMPLE basic block: backward graph ↔ the proven VJP. The two batched stage backward graphs of the body (cbReluStridedB/projB) chained at their forward activations, wrapped in the PROJECTED-residual additive fan-in (body cotangent + STRIDED projection-skip cotangent) and the OUTER post-residual relu, proven equal to r34DownBlockBHasVJPAt (= vjpCompAt(residualProjHasVJPAt(proj, F_s), relu)). The strided sibling of r34BasicBlockBackBatchedGraph_faithful: residualProj (both backward paths nontrivial) for residual (identity skip), convStridedBackBatched in the body's conv1 and the whole projection skip. It is r34DownBlockLayer's faithful.

              Key fact: the outer relu's .selectPos mask is applied ONCE to the incoming dy (giving masked = reluHasVJPAt.backward (den ecot)), and that masked cotangent is what BOTH residualProj fan-in operands see — matching vjpCompAt(residualProj, relu)'s structure: first relu's backward, then residualProj's backward (= proj.backward + body.backward at the masked cotangent).