Documentation

LeanMlir.Proofs.Nets.ViT.ViTWholeBackCertifiedTieB

vitInputGradKB is the certified whole-net ViT gradient at a batch #

ViTWholeBackCertifiedTie.lean proves for one image that vitInputGradK, the reverse of vitForwardKV over that image's N + 1 tokens, is the certified gradient. The batched step tie (ViTStepTieGB.lean) states every activation as StableHLO.batchMap B of the per-example prefix and every cotangent as batchMapAux B of the per-example chain, because no ViT op couples examples. This file proves the same at that index: the five-stage batched chain vitInputGradKB (ViTBackChains.lean) is the certified gradient of batchMap B vitForwardKV at every batch x, for every B. The forward vitForwardKV has no drop-path and is in exact arithmetic.

ViT is smooth everywhere: every stage has a global HasVJP, so its batched witness is batchMapHasVJPAt (the field-by-field lift, BatchMapVJPAt.lean) over HasVJP.toHasVJPAt at each row. The only hypothesis is 0 < ε; there is no smooth-point condition.

  1. batchMap_comp (BatchMapVJPAt.lean, shared with ConvNeXt's batched tie) — batchMap B (g ∘ f) = batchMap B g ∘ batchMap B f, the lemma the shape check needs and the reason the chain saves its activations stage by stage: the two spellings agree only up to finProdFinEquiv.symm_apply_apply, which is not rfl.
  2. The batched leaf ties. The patch-embed stage needs none (its backward is rfl); the other three are batchMapAux_eq_batchMapHasVJPAt (the head: its batchMap form, after batchMap_comp) over the per-example tie at each row (vitTowerBackK_eq_vjp, vitFinalLNBack_eq_vjp, vitHeadBack_eq_classifier_vjp).
  3. vitKVBHasVJPAt — the four-stage apex, three vjpCompDiffAts over the batched stage witnesses — and vitInputGradKB_eq_vitKVB_vjp, the tie: three leaf rewrites, then rfl.
  4. vitForwardKVB_eq_chain — the shape check: the four batched stages compose to batchMap B vitForwardKV, by vitForwardKV_eq_chain and three batchMap_comps — and vitInputGradKB_eq_batchMap_vitForwardKV_vjp, the tie carried to the committed GLOBAL witness batchMapHasVJP (vitForwardKV …) through HasVJPAt.backward_unique_of_eq (batchMapHasVJP is ▸-transported, so its .backward does not reduce), plus the ∑ pdiv reading.
  5. vitTinyInputGradB_eq_vitTiny_vjp — the capstone at ViT-Tiny's literal dims, the batch B and the class count nCls binders.
noncomputable def Proofs.vitTowerBAt (gf : GeluForm) (B Np1 heads d_head mlpDim : ℕ) (ε : ℝ) (hε : 0 < ε) (k : ℕ) (ps : Fin k → BlockParamsV (heads * d_head) mlpDim) (v : Vec (B * (Np1 * (heads * d_head)))) :
HasVJPAt (StableHLO.batchMap B (vitBodyKVFlat gf Np1 heads d_head mlpDim ε k ps)) v

The batched tower witness at v.

Equations
  • One or more equations did not get rendered due to their size.
Instances For
    noncomputable def Proofs.vitLNBAt (B n D : ℕ) (ε : ℝ) (hε : 0 < ε) (γF βF : Vec D) (v : Vec (B * (n * D))) :
    HasVJPAt (StableHLO.batchMap B fun (v : Vec (n * D)) => Mat.flatten fun (r : Fin n) => layerNormVec D ε γF βF (Mat.unflatten v r)) v

    The batched final-LayerNorm witness at v.

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      noncomputable def Proofs.vitHeadBAt (B N D nClasses : ℕ) (Wcls : Mat D nClasses) (bcls : Vec nClasses) (v : Vec (B * ((N + 1) * D))) :
      HasVJPAt (StableHLO.batchMap B (classifierFlat N D nClasses Wcls bcls)) v

      The batched classifier-head witness at v.

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For
        theorem Proofs.vitTowerBackB_eq_vjp {gf : GeluForm} (B Np1 heads d_head mlpDim : ℕ) (ε : ℝ) (hε : 0 < ε) (k : ℕ) (ps : Fin k → BlockParamsV (heads * d_head) mlpDim) (v : Vec (B * (Np1 * (heads * d_head)))) :
        StableHLO.batchMapAux B (vitTowerBackK gf Np1 heads d_head mlpDim ε k ps) v = (vitTowerBAt gf B Np1 heads d_head mlpDim ε hε k ps v).backward

        The batched tower tie. batchMapAux B of the depth-k tower backward at the batched saved input IS the lift's backward: vitTowerBackK_eq_vjp at each row.

        theorem Proofs.vitLNBackB_eq_vjp (B n D : ℕ) (ε : ℝ) (hε : 0 < ε) (γF βF : Vec D) (v : Vec (B * (n * D))) :
        StableHLO.batchMapAux B (rowLNVecFlatBack n D ε γF) v = (vitLNBAt B n D ε hε γF βF v).backward

        The batched final-LayerNorm tie. batchMapAux B of rowLNVecFlatBack at the batched tower output IS the lift's backward — vitFinalLNBack_eq_vjp at one example's row.

        theorem Proofs.vitHeadBackB_eq_vjp (B N D nClasses : ℕ) (Wcls : Mat D nClasses) (bcls : Vec nClasses) (v : Vec (B * ((N + 1) * D))) :
        StableHLO.batchMap B (clsScatter N D) ∘ StableHLO.batchMap B (dense Wcls.transpose 0) = (vitHeadBAt B N D nClasses Wcls bcls v).backward

        The batched head tie. batchMap B of the CLS scatter after batchMap B of the free dense backward IS the lift's backward at any saved v (the head is linear): fuse the two lifts by batchMap_comp, then vitHeadBack_eq_classifier_vjp at one example's row.

        noncomputable def Proofs.vitKVBHasVJPAt (gf : GeluForm) (B ic H W patchSize N mlpDim heads d_head nClasses k : ℕ) (W_conv : Kernel4 (heads * d_head) ic patchSize patchSize) (b_conv cls_token : Vec (heads * d_head)) (pos_embed : Mat (N + 1) (heads * d_head)) (ε : ℝ) (hε : 0 < ε) (ps : Fin k → BlockParamsV (heads * d_head) mlpDim) (γF βF : Vec (heads * d_head)) (Wcls : Mat (heads * d_head) nClasses) (bcls : Vec nClasses) (x : Vec (B * (ic * H * W))) :
        HasVJPAt (StableHLO.batchMap B (classifierFlat N (heads * d_head) nClasses Wcls bcls) ∘ (StableHLO.batchMap B fun (v : Vec ((N + 1) * (heads * d_head))) => Mat.flatten fun (r : Fin (N + 1)) => layerNormVec (heads * d_head) ε γF βF (Mat.unflatten v r)) ∘ StableHLO.batchMap B (vitBodyKVFlat gf (N + 1) heads d_head mlpDim ε k ps) ∘ StableHLO.batchMap B (patchEmbedFlat ic H W patchSize N (heads * d_head) W_conv b_conv cls_token pos_embed)) x

        The batched whole-net witness, four batched stages composed by vjpCompDiffAt, each at the batched saved activation the chain uses (vitSavedPEB, vitSavedBodyB).

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For
          theorem Proofs.vitInputGradKB_eq_vitKVB_vjp {gf : GeluForm} (B ic H W patchSize N mlpDim heads d_head nClasses k : ℕ) (W_conv : Kernel4 (heads * d_head) ic patchSize patchSize) (b_conv cls_token : Vec (heads * d_head)) (pos_embed : Mat (N + 1) (heads * d_head)) (ε : ℝ) (hε : 0 < ε) (ps : Fin k → BlockParamsV (heads * d_head) mlpDim) (γF βF : Vec (heads * d_head)) (Wcls : Mat (heads * d_head) nClasses) (bcls : Vec nClasses) (x : Vec (B * (ic * H * W))) :
          vitInputGradKB gf B ic H W patchSize N mlpDim heads d_head nClasses k W_conv b_conv cls_token pos_embed ε ps γF Wcls x = (vitKVBHasVJPAt gf B ic H W patchSize N mlpDim heads d_head nClasses k W_conv b_conv cls_token pos_embed ε hε ps γF βF Wcls bcls x).backward

          The batched tie. vitInputGradKB — the five-stage batched chain, every slot a lift of the per-example backward at the batched saved activation — is the backward of vitKVBHasVJPAt. Three leaf rewrites (tower, final LN, head), then rfl: the patch-embed leaf is definitional.

          theorem Proofs.vitForwardKVB_eq_chain {gf : GeluForm} (B ic H W patchSize N mlpDim heads d_head nClasses k : ℕ) (W_conv : Kernel4 (heads * d_head) ic patchSize patchSize) (b_conv cls_token : Vec (heads * d_head)) (pos_embed : Mat (N + 1) (heads * d_head)) (ε : ℝ) (ps : Fin k → BlockParamsV (heads * d_head) mlpDim) (γF βF : Vec (heads * d_head)) (Wcls : Mat (heads * d_head) nClasses) (bcls : Vec nClasses) :
          StableHLO.batchMap B (vitForwardKV gf ic H W patchSize N mlpDim heads d_head nClasses k W_conv b_conv cls_token pos_embed ε ps γF βF Wcls bcls) = StableHLO.batchMap B (classifierFlat N (heads * d_head) nClasses Wcls bcls) ∘ (StableHLO.batchMap B fun (v : Vec ((N + 1) * (heads * d_head))) => Mat.flatten fun (r : Fin (N + 1)) => layerNormVec (heads * d_head) ε γF βF (Mat.unflatten v r)) ∘ StableHLO.batchMap B (vitBodyKVFlat gf (N + 1) heads d_head mlpDim ε k ps) ∘ StableHLO.batchMap B (patchEmbedFlat ic H W patchSize N (heads * d_head) W_conv b_conv cls_token pos_embed)

          The shape check. The four batched stages the apex is stated at compose to batchMap B vitForwardKV, the committed per-example forward lifted whole: the per-example shape check vitForwardKV_eq_chain and three batchMap_comps.

          theorem Proofs.vitForwardKV_differentiable {gf : GeluForm} (ic H W patchSize N mlpDim heads d_head nClasses k : ℕ) (W_conv : Kernel4 (heads * d_head) ic patchSize patchSize) (b_conv cls_token : Vec (heads * d_head)) (pos_embed : Mat (N + 1) (heads * d_head)) (ε : ℝ) (hε : 0 < ε) (ps : Fin k → BlockParamsV (heads * d_head) mlpDim) (γF βF : Vec (heads * d_head)) (Wcls : Mat (heads * d_head) nClasses) (bcls : Vec nClasses) :
          Differentiable ℝ (vitForwardKV gf ic H W patchSize N mlpDim heads d_head nClasses k W_conv b_conv cls_token pos_embed ε ps γF βF Wcls bcls)

          vitForwardKV is differentiable everywhere (only 0 < ε): the four stage lemmas composed.

          theorem Proofs.vitInputGradKB_eq_batchMap_vitForwardKV_vjp {gf : GeluForm} (B ic H W patchSize N mlpDim heads d_head nClasses k : ℕ) (W_conv : Kernel4 (heads * d_head) ic patchSize patchSize) (b_conv cls_token : Vec (heads * d_head)) (pos_embed : Mat (N + 1) (heads * d_head)) (ε : ℝ) (hε : 0 < ε) (ps : Fin k → BlockParamsV (heads * d_head) mlpDim) (γF βF : Vec (heads * d_head)) (Wcls : Mat (heads * d_head) nClasses) (bcls : Vec nClasses) (x : Vec (B * (ic * H * W))) :
          vitInputGradKB gf B ic H W patchSize N mlpDim heads d_head nClasses k W_conv b_conv cls_token pos_embed ε ps γF Wcls x = (batchMapHasVJP (vitForwardKV gf ic H W patchSize N mlpDim heads d_head nClasses k W_conv b_conv cls_token pos_embed ε ps γF βF Wcls bcls) (vitForwardKVHasVJP gf ic H W patchSize N mlpDim heads d_head nClasses k W_conv b_conv cls_token pos_embed ε hε ps γF βF Wcls bcls) ⋯).backward x

          The batched apex at the global witness. vitInputGradKB is (batchMapHasVJP (vitForwardKV …) …).backward x — the certified gradient of the drop-free per-example forward lifted over B examples, under 0 < ε. Carried from the chain-shaped apex by HasVJPAt.backward_unique_of_eq along the shape check.

          theorem Proofs.vitInputGradKB_correct {gf : GeluForm} (B ic H W patchSize N mlpDim heads d_head nClasses k : ℕ) (W_conv : Kernel4 (heads * d_head) ic patchSize patchSize) (b_conv cls_token : Vec (heads * d_head)) (pos_embed : Mat (N + 1) (heads * d_head)) (ε : ℝ) (hε : 0 < ε) (ps : Fin k → BlockParamsV (heads * d_head) mlpDim) (γF βF : Vec (heads * d_head)) (Wcls : Mat (heads * d_head) nClasses) (bcls : Vec nClasses) (x : Vec (B * (ic * H * W))) (dy : Vec (B * nClasses)) (i : Fin (B * (ic * H * W))) :
          vitInputGradKB gf B ic H W patchSize N mlpDim heads d_head nClasses k W_conv b_conv cls_token pos_embed ε ps γF Wcls x dy i = ∑ j : Fin (B * nClasses), pdiv (StableHLO.batchMap B (vitForwardKV gf ic H W patchSize N mlpDim heads d_head nClasses k W_conv b_conv cls_token pos_embed ε ps γF βF Wcls bcls)) x i j * dy j

          The batched apex, read as the Jacobian. vitInputGradKB is the pdiv-contracted Jacobian transpose of batchMap B vitForwardKV, at EVERY batch and EVERY cotangent. Only 0 < ε.

          theorem Proofs.vitTinyInputGradB_eq_vitTiny_vjp {gf : GeluForm} (B : ℕ) {nCls : ℕ} (W_conv : Kernel4 (3 * 64) 3 16 16) (b_conv cls_token : Vec (3 * 64)) (pos_embed : Mat (196 + 1) (3 * 64)) (ε : ℝ) (hε : 0 < ε) (ps : Fin 12 → BlockParamsV (3 * 64) 768) (γF βF : Vec (3 * 64)) (Wcls : Mat (3 * 64) nCls) (bcls : Vec nCls) (x : Vec (B * (3 * 224 * 224))) :
          vitInputGradKB gf B 3 224 224 16 196 768 3 64 nCls 12 W_conv b_conv cls_token pos_embed ε ps γF Wcls x = (batchMapHasVJP (vitForwardKV gf 3 224 224 16 196 768 3 64 nCls 12 W_conv b_conv cls_token pos_embed ε ps γF βF Wcls bcls) (vitForwardKVHasVJP gf 3 224 224 16 196 768 3 64 nCls 12 W_conv b_conv cls_token pos_embed ε hε ps γF βF Wcls bcls) ⋯).backward x

          ViT-Tiny's batched whole-net backward tie. vitInputGradKB_eq_batchMap_vitForwardKV_vjp at the exact vitTiny spec (3×224×224, 16×16 patches, 196 + CLS tokens, D = 192 = 3 × 64, MLP 768, 12 distinct blocks, vector-[D] LayerNorm), at any class count nCls (10 for the Imagenette spec, 1000 for the vitin_* ImageNet artifacts) and a variable batch B (the vitin_* artifacts run 32, 128 or 256 per device; 512 is the global batch of the 128x4 runs). The forward is the drop-free vitForwardKV in exact arithmetic; the *drop* artifacts compute another function. The batched peers of the other nets include r34InputGradB_eq_r34B_full_vjp and mnv2InputGradB_eq_mobilenetv2B_full_vjp.