Documentation

LeanMlir.Proofs.Nets.ResNet.ResNet34SyncStepTieB

ResNet-34's data-parallel step at SYNCHRONISED BatchNorm IS the single-device step at R·N #

ResNet34StepTieB.lean threads the label-smoothed loss cotangent down the batch-BN backward chain on ONE device and ties every parameter gradient node to the certified per-op gradient at the cotangent that chain delivers. This is its data-parallel twin, for the render ResNet34RenderB emits at replicas > 1: R replicas at batch N, every BatchNorm synchronised (bnFwdSite / bnBackSite / bnGammaSite), every parameter gradient all-reduced by its mean. The capstone r34_net_syncTiedB says that, for every parameter,

mean over the R replicas of replica r's gradient node, loss divided by B
  = the single-device gradient node at the global batch R·N, loss divided by R·B

— the gradient node r34_net_tiedB at N := R·N ties to the certified per-op gradient. So the sentence the DP render header carries ("this step IS the single-device step at the global batch") is a theorem, and the spec it is stated against has not moved: the right-hand side is the existing single-device chain at N := R·N.

Four steps #

The per-op facts behind each step are in DataParallel.SyncKit; this file states ResNet-34's block chains and ties over them.

  1. Sharding — each replica's backward chain, handed its shard of a global cotangent, computes the shard of the global chain. Every non-BN link (relu mask, conv and strided-conv input-VJP, the 3×3/s2 pool's scatter, the head) is a per-example map and commutes with sharding by definition; the BN link is bnSyncInB, whose shard lemma bnSyncInB_shard is P2 on the graph (DataParallel.Sync.den_bnSyncBack_allReduce) carried across the mul_assoc seam.
  2. The collectives — the mean over replicas of each replica's gradient node is 1/R of the global node at the global cotangent (the P4 lemmas of DataParallel.Sync and DataParallel.SyncKit). The γ node is the sync one, bnSyncGammaGradB, reading the forward's all-reduced statistics — the one parameter gradient sync-BN changes.
  3. Homogeneity — the single-device chain and its gradient nodes are linear in the loss cotangent (*_smul): R × the cotangent gives R × every node.
  4. The divisor — replica r divides its loss by B and the global step by R·B, so replica r's loss cotangent is R × its shard of the global one (replicaLossCot_eq). Steps 1–3 carry that R down the chain and it cancels the collective's 1/R.

What is NOT claimed #

The replicas' saved forward activations enter as the shards of the single-device forward's (batchShard r (r34Pre_k (R*N) w X)); that the sync forward graph computes exactly those is ResNet34SyncB.resnet34FwdGraphSyncFull_shard, the forward half. That the replicas' inputs are the shards of one batch is the driver's. The emitted artifacts run convBias := false, so the conv-bias nodes are not emitted and are not tied here (r34_net_tiedB keeps them for the flag). The lowerer's all_reduce is trusted as every other op's lowering is.

Precision is a flag on the statement. The ImageNet run the book reports trains from resnet34in_momdp64bf16, whose conv weight gradients are the bf16 kinds. Every conv weight collective below is stated on the renderers' switch — ConvWSyncAt bf16 / ConvStridedWSyncAt bf16 (Foundation.GradNodesBAt): the replicas' nodes and the batch-R·N node are the f32 kind at false and the bf16 kind at true — and the capstone takes bf16, so it covers that run's step read over ℝ at the identity rounding (Bf16Erasure), where each bf16 collective is 1/R of the batch-R·N bf16 node exactly as at f32 (DataParallel.SyncBf16's …_shard_id). At a real rounding the bf16 weight-gradient collective differs from the single-device node by the per-replica rounding of each partial sum, which SyncBf16 states and this file does not model, as the f32 tie does not model f32 rounding.

The block cotangents, each one line from the previous link's.

theorem Proofs.ResNet34SyncTieB.r34IdCotIn_smul (N h w : ℕ) {c : ℕ} (p : R34IdW c) (xin : Vec (N * (c * h * w))) :
theorem Proofs.ResNet34SyncTieB.r34DownCotIn_smul (N h w : ℕ) {ic oc : ℕ} (p : R34DownW ic oc) (xin : Vec (N * (ic * (2 * h) * (2 * w)))) :

The gradient nodes.

theorem Proofs.ResNet34SyncTieB.r34HeadCotBlk_shard {R N : ℕ} (h w : ℕ) {c nCls : ℕ} (Wd : Mat c nCls) (bd : Vec nCls) (XIN : Vec (R * N * (c * h * w))) (G : Vec (R * N * nCls)) (r : Fin R) :
ResNet34TieB.r34HeadCotBlk N h w Wd bd (batchShard R N (c * h * w) XIN r) (batchShard R N nCls G r) = batchShard R N (c * h * w) (ResNet34TieB.r34HeadCotBlk (R * N) h w Wd bd XIN G) r
noncomputable def Proofs.ResNet34SyncTieB.r34IdSyncCotIn (R : ℕ) (hR : 0 < R) (N h w : ℕ) {c : ℕ} (p : R34IdW c) (XIN : Vec (R * N * (c * h * w))) (dys : Fin R → Vec (N * (c * h * w))) (r : Fin R) :
Vec (N * (c * h * w))

The replica's block-INPUT cotangent: the residual fan-in, body plus identity skip.

Equations
  • One or more equations did not get rendered due to their size.
Instances For
    theorem Proofs.ResNet34SyncTieB.r34IdSyncCotIn_shard (R : ℕ) (hR : 0 < R) (N h w : ℕ) {c : ℕ} (hN : 0 < N) (hh : 0 < h) (hw : 0 < w) (p : R34IdW c) (XIN : Vec (R * N * (c * h * w))) (dys : Fin R → Vec (N * (c * h * w))) (DY : Vec (R * N * (c * h * w))) (hdys : ∀ (r : Fin R), dys r = batchShard R N (c * h * w) DY r) (r : Fin R) :
    r34IdSyncCotIn R hR N h w p XIN dys r = batchShard R N (c * h * w) (ResNet34TieB.r34IdCotIn (R * N) h w p XIN DY) r
    noncomputable def Proofs.ResNet34SyncTieB.r34DownSyncCotIn (R : ℕ) (hR : 0 < R) (N h w : ℕ) {ic oc : ℕ} (p : R34DownW ic oc) (XIN : Vec (R * N * (ic * (2 * h) * (2 * w)))) (dys : Fin R → Vec (N * (oc * h * w))) (r : Fin R) :
    Vec (N * (ic * (2 * h) * (2 * w)))

    The replica's block-INPUT cotangent: the projected-residual fan-in.

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      theorem Proofs.ResNet34SyncTieB.r34DownSyncCotIn_shard (R : ℕ) (hR : 0 < R) (N h w : ℕ) {ic oc : ℕ} (hN : 0 < N) (hh : 0 < h) (hw : 0 < w) (p : R34DownW ic oc) (XIN : Vec (R * N * (ic * (2 * h) * (2 * w)))) (dys : Fin R → Vec (N * (oc * h * w))) (DY : Vec (R * N * (oc * h * w))) (hdys : ∀ (r : Fin R), dys r = batchShard R N (oc * h * w) DY r) (r : Fin R) :
      r34DownSyncCotIn R hR N h w p XIN dys r = batchShard R N (ic * (2 * h) * (2 * w)) (ResNet34TieB.r34DownCotIn (R * N) h w p XIN DY) r
      noncomputable def Proofs.ResNet34SyncTieB.r34StemSyncCotC (R : ℕ) (hR : 0 < R) (N h w : ℕ) {ic oc : ℕ} (Ws : Kernel4 oc ic 7 7) (bs : Vec oc) (εs : ℝ) (γs βs : Vec oc) (X : Vec (R * N * (ic * (2 * (2 * h)) * (2 * (2 * w))))) (dys : Fin R → Vec (N * (oc * h * w))) (r : Fin R) :
      Vec (N * (oc * (2 * h) * (2 * w)))
      Equations
      • One or more equations did not get rendered due to their size.
      Instances For
        theorem Proofs.ResNet34SyncTieB.r34StemSyncCotC_shard (R : ℕ) (hR : 0 < R) (N h w : ℕ) {ic oc : ℕ} (hN : 0 < N) (hh : 0 < h) (hw : 0 < w) (Ws : Kernel4 oc ic 7 7) (bs : Vec oc) (εs : ℝ) (γs βs : Vec oc) (X : Vec (R * N * (ic * (2 * (2 * h)) * (2 * (2 * w))))) (dys : Fin R → Vec (N * (oc * h * w))) (DY : Vec (R * N * (oc * h * w))) (hdys : ∀ (r : Fin R), dys r = batchShard R N (oc * h * w) DY r) (r : Fin R) :
        r34StemSyncCotC R hR N h w Ws bs εs γs βs X dys r = batchShard R N (oc * (2 * h) * (2 * w)) (ResNet34TieB.r34StemCotC (R * N) h w Ws bs εs γs βs X DY) r
        def Proofs.ResNet34SyncTieB.r34StemSyncTiedB (R : ℕ) (hR : 0 < R) (N h w : ℕ) {ic oc : ℕ} (xN cotN vN epsStr : String) (Ws : Kernel4 oc ic 7 7) (bs : Vec oc) (εs : ℝ) (γs βs : Vec oc) (bf16 : Bool) (X : Vec (R * N * (ic * (2 * (2 * h)) * (2 * (2 * w))))) (dys : Fin R → Vec (N * (oc * h * w))) (DY : Vec (R * N * (oc * h * w))) :

        Stem, DP-tied — the 7×7/s2 conv weight and its BatchNorm's γ and β.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For
          theorem Proofs.ResNet34SyncTieB.r34_stem_syncTiedB (R : ℕ) (hR : 0 < R) (N h w : ℕ) {ic oc : ℕ} (hN : 0 < N) (hh : 0 < h) (hw : 0 < w) (xN cotN vN epsStr : String) (Ws : Kernel4 oc ic 7 7) (bs : Vec oc) (εs : ℝ) (γs βs : Vec oc) (bf16 : Bool) (X : Vec (R * N * (ic * (2 * (2 * h)) * (2 * (2 * w))))) (dys : Fin R → Vec (N * (oc * h * w))) (DY : Vec (R * N * (oc * h * w))) (hdys : ∀ (r : Fin R), dys r = batchShard R N (oc * h * w) (fun (i : Fin (R * N * (oc * h * w))) => ↑R * DY i) r) :
          r34StemSyncTiedB R hR N h w xN cotN vN epsStr Ws bs εs γs βs bf16 X dys DY
          def Proofs.ResNet34SyncTieB.r34HeadSyncTiedB (R : ℕ) (hR : 0 < R) (N h w : ℕ) {c nCls : ℕ} (xN cotN : String) (XIN : Vec (R * N * (c * h * w))) (gs : Fin R → Vec (N * nCls)) (G : Vec (R * N * nCls)) :

          Head, DP-tied — the classifier's weight and bias, at the GAP output.

          Equations
          • One or more equations did not get rendered due to their size.
          Instances For
            theorem Proofs.ResNet34SyncTieB.r34_head_syncTiedB (R : ℕ) (hR : 0 < R) (N h w : ℕ) {c nCls : ℕ} (xN cotN : String) (XIN : Vec (R * N * (c * h * w))) (gs : Fin R → Vec (N * nCls)) (G : Vec (R * N * nCls)) (hgs : ∀ (r : Fin R), gs r = batchShard R N nCls (fun (i : Fin (R * N * nCls)) => ↑R * G i) r) :
            r34HeadSyncTiedB R hR N h w xN cotN XIN gs G
            theorem Proofs.ResNet34SyncTieB.r34HeadCotBlk_scaled (R N h w : ℕ) {c nCls : ℕ} (Wd : Mat c nCls) (bd : Vec nCls) (XIN : Vec (R * N * (c * h * w))) (gs : Fin R → Vec (N * nCls)) (G : Vec (R * N * nCls)) (hgs : ∀ (r : Fin R), gs r = batchShard R N nCls (fun (i : Fin (R * N * nCls)) => ↑R * G i) r) (r : Fin R) :
            ResNet34TieB.r34HeadCotBlk N h w Wd bd (batchShard R N (c * h * w) XIN r) (gs r) = batchShard R N (c * h * w) (fun (i : Fin (R * N * (c * h * w))) => ↑R * ResNet34TieB.r34HeadCotBlk (R * N) h w Wd bd XIN G i) r

            The head's block-side cotangent on a replica, at R × the shards of G, is R × the shard of the single-device one.

            def Proofs.ResNet34SyncTieB.r34NetSyncTiedB (R : ℕ) (hR : 0 < R) (N : ℕ) {nCls : ℕ} (xN cotN vN epsStr : String) (w : R34BWeights nCls) (bf16 : Bool) (X : Vec (R * N * (3 * (2 * (2 * 56)) * (2 * (2 * 56))))) (G : Vec (R * N * nCls)) (gs : Fin R → Vec (N * nCls)) :

            The whole-net statement, named — so the capstone (cotangents bound) and its smoothed-CE corollary (cotangents instantiated) state exactly one thing. The first 16 lets are r34_net_tiedB's chain at N := R·N, driven by the global cotangent G; the rest are the replicas' sync-BN chain, driven by the family gs; the 18 conjuncts are one per stage, every emitted parameter collective against r34_net_tiedB's node at the global batch.

            Equations
            • One or more equations did not get rendered due to their size.
            Instances For
              theorem Proofs.ResNet34SyncTieB.r34_net_syncTiedB (R : ℕ) (hR : 0 < R) (N : ℕ) (hN : 0 < N) {nCls : ℕ} (xN cotN vN epsStr : String) (w : R34BWeights nCls) (bf16 : Bool) (X : Vec (R * N * (3 * (2 * (2 * 56)) * (2 * (2 * 56))))) (G : Vec (R * N * nCls)) (gs : Fin R → Vec (N * nCls)) (hgs : ∀ (r : Fin R), gs r = batchShard R N nCls (fun (i : Fin (R * N * nCls)) => ↑R * G i) r) :
              r34NetSyncTiedB R hR N xN cotN vN epsStr w bf16 X G gs

              The synchronised-BN data-parallel ResNet-34 step IS the single-device step at the global batch. R replicas at batch N, each running the render's sync-BN backward chain from its own cotangent gs r, with gs r the R-scaled shard of a global cotangent G; every parameter's all-reduced mean gradient — stem 3, thirteen identity blocks × 6, three downsample blocks × 9, dense 2: the 110 the render emits — equals the single-device batch-BN gradient node at batch R·N, at the cotangent r34_net_tiedB's chain delivers there from G.

              The left-hand chain is the replicas' own: sync-BN backward (bnSyncInB, a collective per BN layer), per-example conv / relu / pool / head links. The right-hand chain is r34_net_tiedB's at N := R·N, whose nodes that capstone ties to the certified per-op gradient at the chain cotangent — so this and it together say each all-reduced gradient equals the single-device node at the global batch. r34_net_syncTiedB_smoothedCE discharges the hypothesis for the label-smoothed chain the artifacts emit. bf16 selects the conv weight collectives' kind on both sides — true is resnet34in_momdp64bf16's, read at the identity rounding (Bf16Erasure).

              Note: with per-replica BatchNorm the mean of the replica gradients is not the global-batch gradient in general; DataParallel.dpMeanGrad_ne_globalBatchGrad is a two-replica, one-parameter counterexample.

              theorem Proofs.ResNet34SyncTieB.r34_net_syncTiedB_smoothedCE (R : ℕ) (hR : 0 < R) (N : ℕ) (hN : 0 < N) {nCls : ℕ} (xN cotN vN epsStr aStr negAK bStr logN ohN : String) (α B : ℝ) (w : R34BWeights nCls) (bf16 : Bool) (X : Vec (R * N * (3 * (2 * (2 * 56)) * (2 * (2 * 56))))) (T : Vec (R * N * (1 * nCls))) :
              r34NetSyncTiedB R hR N xN cotN vN epsStr w bf16 X (BackLinks.unrowB (R * N) nCls (StableHLO.den (smoothedLossCotGraph (R * N) nCls α (↑R * B) aStr negAK bStr logN ohN (BackLinks.rowB (R * N) nCls (resnet34ForwardBFull (R * N) w X)) T))) fun (r : Fin R) => BackLinks.unrowB N nCls (StableHLO.den (smoothedLossCotGraph N nCls α B aStr negAK bStr logN ohN (BackLinks.rowB N nCls (batchShard R N nCls (resnet34ForwardBFull (R * N) w X) r)) (batchShard R N (1 * nCls) T r)))

              …and at the loss the artifacts emit. r34_net_syncTiedB with its cotangent hypothesis discharged by replicaLossCot_eq: each replica runs the label-smoothed softmax chain (smoothedLossCotGraph) on its shard of the logits and targets with divisor B; the single-device step runs it on the whole R·N batch with divisor R·B. Then every all-reduced gradient the DP render emits IS the single-device node at batch R·N, loss divided by R·B.