Documentation

LeanMlir.Proofs.Nets.ViT.ViTDataParallel

ViT-Tiny data-parallel: R replicas of N are one device at R·N, through the optimizer and the EMA #

ViTParamGrad proves, at one replica, that each of ViT-Tiny's 200 gradient nodes is the derivative of the smoothed loss of the whole net in its parameter slot (vit_net_lossGrad_smoothedCE). The vitin_*dp* artifacts, the book's vitin_emadp256x4wxdropeps0000001smxerfbf16 among them, run that program on R replicas: each gradient node feeds allReduceMeanF, the all-reduced gradient feeds the AdamW tail, and the updated weight feeds the EMA shadow. This file composes those three with the one-replica theorem.

ViT has no BatchNorm, so no op couples examples and the composition needs no synchronised statistic: the loss is a sum over examples, a shard's forward is the shard of the forward, and the mean of the replicas' losses is the single-device loss at the global batch.

A slot is any slot : Vec P → ViTTieWeights nC, the weights with one tensor replaced by a vector: fun θ => { w with bcls := θ }, fun θ => { w with b3 := { w.b3 with Wq := Mat.unflatten θ } }, and so on. ViTNetLossTiedGB's 200 conjuncts are exactly hypotheses hg of this shape, one per slot; vit_dp_bcls instantiates one end to end, the classifier bias, with no hypothesis left but 0 < ε and targets that sum to one.

What is not claimed #

That every replica holds the same weights, that the shards are cut from one host batch and that replica_groups names all R devices are the driver's (DataParallel.Basic, Verified.Train) and the *-dp-check gates'. The learning-rate, bias-correction and decay scalars are runtime inputs, binders here. The theorems are over ℝ.

theorem Proofs.HasGradAt.at_allReduceMeanF {P : ℕ} (R : ℕ) (hR : 0 < R) (t : String) (ds : List ℕ) (g : Fin R → StableHLO.SHlo P) (G : Fin R → Vec P → Vec 1) (θ : Vec P) (hg : ∀ (r : Fin R), HasGradAt (G r) θ (StableHLO.den (g r))) :
HasGradAt (fun (θ' : Vec P) (x : Fin 1) => meanLoss (fun (r : Fin R) (θ'' : Vec P) => G r θ'' 0) θ') θ (StableHLO.den (StableHLO.SHlo.allReduceMeanF R hR t ds g))

The collective at a slot. If each replica's node is the gradient at θ of that replica's loss G r, the all-reduced node is the gradient at θ of the mean of the R losses. dpMeanGrad_eq_grad_meanLoss in HasGradAt's vocabulary, at the SHlo node.

theorem Proofs.smoothedBatchLossDiv_shard (R N K : ℕ) (hR : 0 < R) (α B : ℝ) (t z : Vec (R * N * K)) :
smoothedBatchLossDiv (R * N) K α (↑R * B) t z = fun (x : Fin 1) => 1 / ↑R * ∑ r : Fin R, smoothedBatchLossDiv N K α B (batchShard R N K t r) (batchShard R N K z r) 0

The smoothed batch loss shards. The loss is a sum over examples divided by a constant, so the batch-R·N loss at divisor R·B is the mean over the R replicas of the batch-N losses at divisor B, each at its shard of the targets and logits.

def Proofs.ViTTieGB.sdShard (R N : ℕ) (sd : Option (Fin 12 → Vec (R * N) × Vec (R * N))) (r : Fin R) :
Option (Fin 12 → Vec N × Vec N)

Replica r's shard of the stochastic-depth masks: example n of replica r reads the global batch's entries at (r, n), at both sites of every block. none stays none.

Equations
  • One or more equations did not get rendered due to their size.
Instances For
    theorem Proofs.ViTTieGB.exampleSite_vitSdA_shard (R N : ℕ) (sd : Option (Fin 12 → Vec (R * N) × Vec (R * N))) (r : Fin R) (k : Fin 12) (n : Fin N) :
    theorem Proofs.ViTTieGB.exampleSite_vitSdM_shard (R N : ℕ) (sd : Option (Fin 12 → Vec (R * N) × Vec (R * N))) (r : Fin R) (k : Fin 12) (n : Fin N) :
    theorem Proofs.ViTTieGB.vitNetB_shard {gf : GeluForm} (R N : ℕ) {nC : ℕ} (ε : ℝ) (w : ViTTie.ViTTieWeights nC) (sd : Option (Fin 12 → Vec (R * N) × Vec (R * N))) (img : Vec (R * N * (3 * 224 * 224))) (r : Fin R) :
    batchShard R N nC (vitNetB gf (R * N) ε w sd img) r = vitNetB gf N ε w (sdShard R N sd r) (batchShard R N (3 * 224 * 224) img r)

    Replica r's forward is its shard of the batch-R·N forward, at its shard of the images and of the masks. No ViT op couples examples, so every stage commutes with the cut (batchShard_batchMap, batchShard_batchMapIdx).

    noncomputable def Proofs.ViTTieGB.vitReplicaLoss (gf : GeluForm) (R N : ℕ) {nC : ℕ} (ε α B : ℝ) (sd : Option (Fin 12 → Vec (R * N) × Vec (R * N))) (img : Vec (R * N * (3 * 224 * 224))) (t : Vec (R * N * nC)) (r : Fin R) (w : ViTTie.ViTTieWeights nC) :
    Vec 1

    Replica r's loss, as a function of the weights: the smoothed loss at divisor B of the batch-N net on replica r's shard of the images, masks and targets.

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      noncomputable def Proofs.ViTTieGB.vitGlobalLoss (gf : GeluForm) (R N : ℕ) {nC : ℕ} (ε α B : ℝ) (sd : Option (Fin 12 → Vec (R * N) × Vec (R * N))) (img : Vec (R * N * (3 * 224 * 224))) (t : Vec (R * N * nC)) (w : ViTTie.ViTTieWeights nC) :
      Vec 1

      The single-device loss at the global batch, as a function of the weights: the smoothed loss at divisor R·B of the batch-R·N net.

      Equations
      Instances For
        theorem Proofs.ViTTieGB.vitGlobalLoss_eq_mean {gf : GeluForm} (R N : ℕ) (hR : 0 < R) {nC : ℕ} (ε α B : ℝ) (sd : Option (Fin 12 → Vec (R * N) × Vec (R * N))) (img : Vec (R * N * (3 * 224 * 224))) (t : Vec (R * N * nC)) (w : ViTTie.ViTTieWeights nC) :
        vitGlobalLoss gf R N ε α B sd img t w = fun (x : Fin 1) => 1 / ↑R * ∑ r : Fin R, vitReplicaLoss gf R N ε α B sd img t r w 0

        The mean of the replicas' losses is the single-device loss at the global batch, as functions of the weights: smoothedBatchLossDiv_shard at vitNetB_shard.

        theorem Proofs.ViTTieGB.vit_dp_node_lossGrad {gf : GeluForm} (R N : ℕ) (hR : 0 < R) {nC : ℕ} (ε α B : ℝ) (sd : Option (Fin 12 → Vec (R * N) × Vec (R * N))) (img : Vec (R * N * (3 * 224 * 224))) (t : Vec (R * N * nC)) {P : ℕ} (slot : Vec P → ViTTie.ViTTieWeights nC) (θ : Vec P) (tn : String) (ds : List ℕ) (g : Fin R → StableHLO.SHlo P) (hg : ∀ (r : Fin R), HasGradAt (fun (θ' : Vec P) => vitReplicaLoss gf R N ε α B sd img t r (slot θ')) θ (StableHLO.den (g r))) :
        HasGradAt (fun (θ' : Vec P) => vitGlobalLoss gf R N ε α B sd img t (slot θ')) θ (StableHLO.den (StableHLO.SHlo.allReduceMeanF R hR tn ds g))

        Every all-reduced ViT-Tiny gradient node is the single-device gradient at the global batch R·N. For any parameter slot slot and any family g of per-replica gradient nodes: if each replica's node is the gradient, in the slot, of that replica's loss on its own shard (vit_net_lossGrad_smoothedCE at batch N, for each of the 200 nodes), then the node the render all-reduces them into is the gradient, in the slot, of the smoothed loss of the batch-R·N net at divisor R·B.

        theorem Proofs.ViTTieGB.vit_dp_node_eq_global {gf : GeluForm} (R N : ℕ) (hR : 0 < R) {nC : ℕ} (ε α B : ℝ) (sd : Option (Fin 12 → Vec (R * N) × Vec (R * N))) (img : Vec (R * N * (3 * 224 * 224))) (t : Vec (R * N * nC)) {P : ℕ} (slot : Vec P → ViTTie.ViTTieWeights nC) (θ : Vec P) (tn : String) (ds : List ℕ) (g : Fin R → StableHLO.SHlo P) (gG : StableHLO.SHlo P) (hg : ∀ (r : Fin R), HasGradAt (fun (θ' : Vec P) => vitReplicaLoss gf R N ε α B sd img t r (slot θ')) θ (StableHLO.den (g r))) (hG : HasGradAt (fun (θ' : Vec P) => vitGlobalLoss gf R N ε α B sd img t (slot θ')) θ (StableHLO.den gG)) :

        …and so equals the batch-R·N node. Whatever single-device node gG is the gradient of the global-batch loss in the slot (vit_net_lossGrad_smoothedCE at batch R·N), the all-reduced node denotes the same vector.

        theorem Proofs.ViTTieGB.vit_dp_bcls {gf : GeluForm} (R N : ℕ) (hR : 0 < R) {nC : ℕ} (ε α B : ℝ) (hε : 0 < ε) (w : ViTTie.ViTTieWeights nC) (sd : Option (Fin 12 → Vec (R * N) × Vec (R * N))) (img : Vec (R * N * (3 * 224 * 224))) (t : Vec (R * N * nC)) (ht : ∀ (k : Fin (R * N)), ∑ c : Fin nC, StableHLO.batchSlice (R * N) nC t k c = 1) (cotN aStr negAK bStr logN ohN tn : String) (ds : List ℕ) :
        HasGradAt (fun (θ' : Vec nC) => vitGlobalLoss gf R N ε α B sd img t { Wc := w.Wc, bc := w.bc, cls := w.cls, pos := w.pos, b1 := w.b1, b2 := w.b2, b3 := w.b3, b4 := w.b4, b5 := w.b5, b6 := w.b6, b7 := w.b7, b8 := w.b8, b9 := w.b9, b10 := w.b10, b11 := w.b11, b12 := w.b12, γF := w.γF, βF := w.βF, Wcls := w.Wcls, bcls := θ' }) w.bcls (StableHLO.den (StableHLO.SHlo.allReduceMeanF R hR tn ds fun (r : Fin R) => (StableHLO.SHlo.operand cotN (StableHLO.den (smoothedLossCotGraphDiv N nC α B aStr negAK bStr logN ohN (vitNetB gf N ε w (sdShard R N sd r) (batchShard R N (3 * 224 * 224) img r)) (batchShard R N nC t r)))).biasGradB))

        One slot end to end: the classifier bias. The all-reduced biasGradB node of the data-parallel render, each replica's at the smoothed-loss cotangent it emits on its own shard, is the gradient in bcls of the single-device loss at the global batch R·N. The hypothesis of vit_dp_node_lossGrad is discharged by vit_net_lossGrad_smoothedCE at each replica; the other 199 slots discharge the same way from their conjuncts.

        theorem Proofs.ViTTieGB.vit_dp_step {gf : GeluForm} (R N : ℕ) (hR : 0 < R) {nC : ℕ} (ε α B : ℝ) (sd : Option (Fin 12 → Vec (R * N) × Vec (R * N))) (img : Vec (R * N * (3 * 224 * 224))) (t : Vec (R * N * nC)) {P : ℕ} (slot : Vec P → ViTTie.ViTTieWeights nC) (θ : Vec P) (tn : String) (ds' : List ℕ) (g : Fin R → StableHLO.SHlo P) (hg : ∀ (r : Fin R), HasGradAt (fun (θ' : Vec P) => vitReplicaLoss gf R N ε α B sd img t r (slot θ')) θ (StableHLO.den (g r))) (θN mN vN b1N ob1N b2N ob2N bc1N bc2N lrN epsN wdN eN dN odN θN' : String) (ds : List ℕ) (β₁ β₂ εA lr wd bc₁ bc₂ d : ℝ) (m v e : Vec P) :
        have grad := fun (i : Fin P) => pdiv (fun (θ' : Vec P) => vitGlobalLoss gf R N ε α B sd img t (slot θ')) θ i 0; have θnode := StableHLO.SHlo.adamWParamF θN mN vN b1N ob1N b2N ob2N bc1N bc2N lrN epsN wdN ds β₁ β₂ εA lr wd bc₁ bc₂ θ m v (StableHLO.SHlo.allReduceMeanF R hR tn ds' g); (StableHLO.den θnode, StableHLO.den (StableHLO.SHlo.adamMNextF mN b1N ob1N ds β₁ m (StableHLO.SHlo.allReduceMeanF R hR tn ds' g)), StableHLO.den (StableHLO.SHlo.adamVNextF vN b2N ob2N ds β₂ v (StableHLO.SHlo.allReduceMeanF R hR tn ds' g))) = adamWStep β₁ β₂ εA lr wd bc₁ bc₂ θ m v grad ∧ StableHLO.den (StableHLO.SHlo.adamMNextF eN dN odN ds d e (StableHLO.SHlo.operand θN' (StableHLO.den θnode))) = emaNext d e (adamWStep β₁ β₂ εA lr wd bc₁ bc₂ θ m v grad).1

        The data-parallel ViT-Tiny update, through the optimizer and the EMA shadow. Under vit_dp_node_lossGrad's hypothesis, for any slot:

        • the AdamW triple the render emits at the all-reduced node is adamWStep at ∇, the gradient in the slot of the single-device loss at the global batch R·N (adamW_at_allReduceMeanF; wd = 0 is the wx renders' excluded slots);
        • the EMA node over the updated weight is emaNext d e θ', i.e. d·e + (1 − d)·θ', with θ' that step's parameter (emaShadow_faithful).

        The rate, the bias corrections and the decay are runtime scalars, binders here.