Documentation

LeanMlir.Proofs.Codegen.RenderKit

The renderers' shared optimizer tail #

Every batched train-step renderer ends the same way: fold a per-parameter optimizer step over the parameter list, so the θ/m/v outputs come out in signature order. This file holds that step once.

stepused byops per parameter
adamOneMobileNetV2, MobileNetV4, EfficientNetall-reduce (DP only) + the AdamW triple
rmsOneMobileNetV2, EfficientNetall-reduce + coupled L2 + mean-square + buffer + SGD
adamOneEmaViT, ConvNeXtadamOne + the model-EMA shadow, with the clip's preAvg

and the train step's packed interface — the one positional contract the driver's blob walks: packedTrainSig (arguments) and packedTrainRetTys (results), used by every batched renderer. Last, the precision-switched constructors XAt bf16 rnd … (convAt, convBackBatchedAt, …): one if bf16 then .XBf16 rnd … else .X … per op instead of one per call site.

ResNet34RenderB.optOne is the multi-optimizer step (AdamW / LAMB / heavy-ball / accumulation / EMA) that ResNet-34 and ResNet-50 fold; it reads the same PGrad.

At replicas ≤ 1 the all-reduce emits nothing and threads the raw gradient, so a single-device render is unchanged by it. The AdamW triple consumes the averaged gradient as an .operand, exactly as it consumed the raw one, so the den side does not shift.

A trainable parameter: emitted name (no %), gradient SSA name, and shape. The optimizer tail is a fold over this list, so the θ/m/v output order cannot drift from the signature order.

Instances For

    (θ', m', v') for one parameter under AdamW: the replica mean of its gradient (prettyAllReduceMean, pretty of the allReduceMeanF node) and then the proven adamMNextF/adamVNextF/adamWParamF triple (prettyAdamW).

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For

      (θ', b', s') for one parameter under RMSProp with momentum — the adamOne peer. Only ONE of the four ops is new. Reading the reference (jax/Jax/Codegen.lean, the .rmsprop branch) top to bottom:

      reference lineemitted here
      grads = g + WD * pmomVNextF at (μ := wd, v := θ) — Proofs.momVNext_as_coupled_l2
      sq = RHO*s + (1-RHO)*g*gadamVNextF at β₂ := ρ — Proofs.rmsSqNext_eq_adamVNext
      buf = MOMENTUM*b + g/sqrt(sq+EPS)rmsBufNextF — the new op, ε INSIDE the root
      params = p - lr*bufsgdParamF on the buffer's SSA

      ⚠ The weight decay is COUPLED and goes FIRST, so the accumulator sees the decayed gradient. Reversing that order — decaying after the accumulator, AdamW-style — is a different optimizer and would not show up as an arity or type error anywhere.

      ⚠ EfficientNet's ε is 1e-3, where MobileNetV2's is 1.0 — the placement's sensitive end: at a collapsed mean-square the textbook spelling takes a step 31.6× larger (Proofs.rmsBufNext_eps_placement_at_zero). A green MobileNetV2 tie does not license the EfficientNet render; rms-tie efficientnet is its own gate.

      Slot mapping: the packed [θ|m|v] signature is reused verbatim with m carrying the momentum buffer and v the running mean-square, the same slot reinterpretation the Nesterov render does for its velocity. That is why the driver and the interface do not move.

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For
        def Proofs.StableHLO.adamOneEma (B replicas : ℕ) (g : PGrad) (ema : Bool := false) (wdName : String := "%wd") (preAvg : Bool := false) :

        (θ', m', v', e') for one parameter under AdamW with the model-EMA shadow — ViT's and ConvNeXt's step. e' is "" at ema := false.

        • wdName — "%wd", or the zero constant for timm's no_weight_decay parameters. The AdamW ops take the decay as an OPERAND NAME, so excluding a parameter binds that name to a zero.
        • preAvg — the caller has ALREADY averaged (and clipped) this gradient, so the collective is not emitted a second time. Under data parallelism the clip must come AFTER the all_reduce (the reference clips the combined gradient; clipping per replica clips PARTIAL gradients — a different function that still trains and descends), and the clip needs every gradient at once while this step is per parameter, so at clip := true the caller hoists both (planning/archive/grad_clip.md §4).
        • ema — the shadow e' = d·e + (1−d)·θ' is adamMNextF at (β₁ := d, m := e, g := θ'): Proofs.adamMNext IS the reference's ema_update (ema_update in jax/Jax/Codegen.lean), so it needs no new op and adamMNextF_faithful closes the den side by rfl. ⚠ It reads nT, the UPDATED parameter — the shadow averages weights after the optimizer moves them. ⚠ %emad/%oemad are function ARGS, not constants: the reference's decay is warmup-corrected, d = min(decay, (1+t)/(10+t)) (planning/archive/ema.md §2 — without it a shadow held 12.8% of the random init and scored 0.00%). At ema := false no pretty call happens, so the fresh-name counter does not move and the non-EMA renders are unchanged by the flag.

        ⚠ The shadow's SSA name is %{nm}e here; ResNet34RenderB.optOne uses %{nm}ema because at suffix e the stem BN gamma %sg collides with select_and_scatter's block-local %sge. ViT and ConvNeXt have no max-pool, so e is safe for them.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For
          def Proofs.StableHLO.packedTrainSig (ps : List (String × String)) (acc ema : Bool := false) (emaSuf : String := "e") :

          The packed train step's argument list after %x: the parameter regions in the order the driver packs them — θ, m, v, then the accumulator G (<p>a, only under gradient accumulation) and the model-EMA shadow E (<p>{emaSuf}, only under ema) — followed by the runtime scalars %lr, %bc1, %bc2, %aup, %akeep (accumulation) and %emad, %oemad (EMA). ps is (%name, type) per parameter, in signature order.

          ⚠ G precedes E and never follows it: [θ|m|v|G|E] is the order that leaves both single-axis layouts at the index they already occupy. ⚠ emaSuf is "e" except on the ResNet family, whose stem BN gamma %sg + e would be %sge — select_and_scatter's block-local name in the max-pool backward — so there it is "ema" (ResNet34RenderB.optOne).

          Equations
          • One or more equations did not get rendered due to their size.
          Instances For

            The packed train step's leading result types, in packedTrainSig's order: the updated regions θ', m', v'[, G'][, E'], then %loss, %bc1, %bc2, then the handed-back accumulation / EMA scalars. pTy is the parameter types in signature order.

            Equations
            • One or more equations did not get rendered due to their size.
            Instances For
              def Proofs.StableHLO.dropMaskSig (B : ℕ) (sd : Bool) (idxs : List ℕ) :

              The stochastic-depth mask arguments , %dp<i>: tensor<Bxf32> for the sites idxs, in the order given — the order the driver's dropScales writes them into the blob. Empty when sd is off, which keeps every non-SD render byte-identical. Each net passes its own site list (vitDropSig, cnxDropSig, enetDropSig, r50DropSig).

              Equations
              • One or more equations did not get rendered due to their size.
              Instances For

                Every bf16 op is its f32 peer with one extra leading argument, the rounding rnd, and every renderer used to spell the choice out (if bf16 then .convBf16 (h := h) zrnd … else .conv (h := h) …, 248 times). XAt bf16 rnd … is that if, once per constructor. pretty evaluates it, so the emitted text is exactly the chosen branch's — every artifact renders byte-identically.

                @[reducible]
                def Proofs.StableHLO.BatchableOp.convAt (bf16 : Bool) {ic oc h w kH kW : ℕ} (rnd : ℝ → ℝ) (wName bName : String) (W : Kernel4 oc ic kH kW) (bias : Vec oc) :
                BatchableOp (ic * h * w) (oc * h * w)

                conv, or its bf16 peer at rounding rnd when bf16.

                Equations
                • One or more equations did not get rendered due to their size.
                Instances For
                  @[reducible]
                  def Proofs.StableHLO.SHlo.convBackBatchedAt (bf16 : Bool) {N ic oc h w kH kW : ℕ} (rnd : ℝ → ℝ) (wName : String) (W : Kernel4 oc ic kH kW) (b : Vec oc) :
                  SHlo (N * (oc * h * w)) → SHlo (N * (ic * h * w))

                  convBackBatched, or its bf16 peer at rounding rnd when bf16.

                  Equations
                  Instances For
                    @[reducible]
                    def Proofs.StableHLO.BatchableOp.convStride4At (bf16 : Bool) {ic oc h w kH kW : ℕ} (rnd : ℝ → ℝ) (wName bName : String) (W : Kernel4 oc ic kH kW) (bias : Vec oc) :
                    BatchableOp (ic * (2 * (2 * h)) * (2 * (2 * w))) (oc * h * w)

                    convStride4, or its bf16 peer at rounding rnd when bf16.

                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For
                      @[reducible]
                      def Proofs.StableHLO.SHlo.convStride4WeightGradBAt (bf16 : Bool) {N ic oc h w kH kW : ℕ} (rnd : ℝ → ℝ) (xName : String) (b : Vec oc) (x : Vec (N * (ic * (2 * (2 * h)) * (2 * (2 * w))))) (W : Kernel4 oc ic kH kW) :
                      SHlo (N * (oc * h * w)) → SHlo (oc * ic * kH * kW)

                      convStride4WeightGradB, or its bf16 peer at rounding rnd when bf16.

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        @[reducible]
                        def Proofs.StableHLO.BatchableOp.convStridedAt (bf16 : Bool) {ic oc h w kH kW : ℕ} (rnd : ℝ → ℝ) (wName bName : String) (W : Kernel4 oc ic kH kW) (bias : Vec oc) :
                        BatchableOp (ic * (2 * h) * (2 * w)) (oc * h * w)

                        convStrided, or its bf16 peer at rounding rnd when bf16.

                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For
                          @[reducible]
                          def Proofs.StableHLO.SHlo.convStridedBackBatchedAt (bf16 : Bool) {N ic oc h w kH kW : ℕ} (rnd : ℝ → ℝ) (wName : String) (W : Kernel4 oc ic kH kW) (b : Vec oc) :
                          SHlo (N * (oc * h * w)) → SHlo (N * (ic * (2 * h) * (2 * w)))

                          convStridedBackBatched, or its bf16 peer at rounding rnd when bf16.

                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For
                            @[reducible]
                            def Proofs.StableHLO.SHlo.convStridedWeightGradBAt (bf16 : Bool) {N ic oc h w kH kW : ℕ} (rnd : ℝ → ℝ) (xName : String) (b : Vec oc) (x : Vec (N * (ic * (2 * h) * (2 * w)))) (W : Kernel4 oc ic kH kW) :
                            SHlo (N * (oc * h * w)) → SHlo (oc * ic * kH * kW)

                            convStridedWeightGradB, or its bf16 peer at rounding rnd when bf16.

                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For
                              @[reducible]
                              def Proofs.StableHLO.BatchableOp.convStridedXlaAt (bf16 : Bool) {ic oc h w kH kW : ℕ} (rnd : ℝ → ℝ) (wName bName : String) (W : Kernel4 oc ic kH kW) (bias : Vec oc) :
                              BatchableOp (ic * (2 * h) * (2 * w)) (oc * h * w)

                              convStridedXla, or its bf16 peer at rounding rnd when bf16.

                              Equations
                              • One or more equations did not get rendered due to their size.
                              Instances For
                                @[reducible]
                                def Proofs.StableHLO.SHlo.convStridedXlaWeightGradBAt (bf16 : Bool) {N ic oc h w kH kW : ℕ} (rnd : ℝ → ℝ) (xName : String) (b : Vec oc) (x : Vec (N * (ic * (2 * h) * (2 * w)))) (W : Kernel4 oc ic kH kW) :
                                SHlo (N * (oc * h * w)) → SHlo (oc * ic * kH * kW)

                                convStridedXlaWeightGradB, or its bf16 peer at rounding rnd when bf16.

                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For
                                  @[reducible]
                                  def Proofs.StableHLO.SHlo.convWeightGradBAt (bf16 : Bool) {N ic oc h w kH kW : ℕ} (rnd : ℝ → ℝ) (xName : String) (b : Vec oc) (x : Vec (N * (ic * h * w))) (W : Kernel4 oc ic kH kW) :
                                  SHlo (N * (oc * h * w)) → SHlo (oc * ic * kH * kW)

                                  convWeightGradB, or its bf16 peer at rounding rnd when bf16.

                                  Equations
                                  Instances For
                                    @[reducible]
                                    def Proofs.StableHLO.BatchableOp.denseRowAt (bf16 : Bool) {N a c : ℕ} (rnd : ℝ → ℝ) (wName bName : String) (W : Mat a c) (b : Vec c) :
                                    BatchableOp (N * a) (N * c)

                                    denseRow, or its bf16 peer at rounding rnd when bf16.

                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For
                                      @[reducible]
                                      def Proofs.StableHLO.BatchableOp.denseRowBackAt (bf16 : Bool) {rows a c : ℕ} (rnd : ℝ → ℝ) (wName : String) (W : Mat a c) :
                                      BatchableOp (rows * c) (rows * a)

                                      denseRowBack, or its bf16 peer at rounding rnd when bf16.

                                      Equations
                                      Instances For
                                        @[reducible]
                                        def Proofs.StableHLO.BatchableOp.depthwiseAt (bf16 : Bool) {c h w kH kW : ℕ} (rnd : ℝ → ℝ) (wName bName : String) (W : DepthwiseKernel c kH kW) (bias : Vec c) :
                                        BatchableOp (c * h * w) (c * h * w)

                                        depthwise, or its bf16 peer at rounding rnd when bf16.

                                        Equations
                                        • One or more equations did not get rendered due to their size.
                                        Instances For
                                          @[reducible]
                                          def Proofs.StableHLO.SHlo.depthwiseBackBatchedAt (bf16 : Bool) {N c h w kH kW : ℕ} (rnd : ℝ → ℝ) (wName : String) (W : DepthwiseKernel c kH kW) (b : Vec c) :
                                          SHlo (N * (c * h * w)) → SHlo (N * (c * h * w))

                                          depthwiseBackBatched, or its bf16 peer at rounding rnd when bf16.

                                          Equations
                                          • One or more equations did not get rendered due to their size.
                                          Instances For
                                            @[reducible]
                                            def Proofs.StableHLO.BatchableOp.depthwiseStridedAt (bf16 : Bool) {c h w kH kW : ℕ} (rnd : ℝ → ℝ) (wName bName : String) (W : DepthwiseKernel c kH kW) (bias : Vec c) :
                                            BatchableOp (c * (2 * h) * (2 * w)) (c * h * w)

                                            depthwiseStrided, or its bf16 peer at rounding rnd when bf16.

                                            Equations
                                            • One or more equations did not get rendered due to their size.
                                            Instances For
                                              @[reducible]
                                              def Proofs.StableHLO.SHlo.depthwiseStridedBackBatchedAt (bf16 : Bool) {N c h w kH kW : ℕ} (rnd : ℝ → ℝ) (wName : String) (W : DepthwiseKernel c kH kW) (b : Vec c) :
                                              SHlo (N * (c * h * w)) → SHlo (N * (c * (2 * h) * (2 * w)))

                                              depthwiseStridedBackBatched, or its bf16 peer at rounding rnd when bf16.

                                              Equations
                                              • One or more equations did not get rendered due to their size.
                                              Instances For
                                                @[reducible]
                                                def Proofs.StableHLO.SHlo.depthwiseStridedWeightGradBAt (bf16 : Bool) {N c h w kH kW : ℕ} (rnd : ℝ → ℝ) (xName : String) (b : Vec c) (x : Vec (N * (c * (2 * h) * (2 * w)))) (W : DepthwiseKernel c kH kW) :
                                                SHlo (N * (c * h * w)) → SHlo (c * kH * kW)

                                                depthwiseStridedWeightGradB, or its bf16 peer at rounding rnd when bf16.

                                                Equations
                                                • One or more equations did not get rendered due to their size.
                                                Instances For
                                                  @[reducible]
                                                  def Proofs.StableHLO.BatchableOp.depthwiseStridedXlaAt (bf16 : Bool) {c h w kH kW : ℕ} (rnd : ℝ → ℝ) (wName bName : String) (W : DepthwiseKernel c kH kW) (bias : Vec c) :
                                                  BatchableOp (c * (2 * h) * (2 * w)) (c * h * w)

                                                  depthwiseStridedXla, or its bf16 peer at rounding rnd when bf16.

                                                  Equations
                                                  • One or more equations did not get rendered due to their size.
                                                  Instances For
                                                    @[reducible]
                                                    def Proofs.StableHLO.SHlo.depthwiseStridedXlaBackBatchedAt (bf16 : Bool) {N c h w kH kW : ℕ} (rnd : ℝ → ℝ) (wName : String) (W : DepthwiseKernel c kH kW) (b : Vec c) :
                                                    SHlo (N * (c * h * w)) → SHlo (N * (c * (2 * h) * (2 * w)))

                                                    depthwiseStridedXlaBackBatched, or its bf16 peer at rounding rnd when bf16.

                                                    Equations
                                                    • One or more equations did not get rendered due to their size.
                                                    Instances For
                                                      @[reducible]
                                                      def Proofs.StableHLO.SHlo.depthwiseStridedXlaWeightGradBAt (bf16 : Bool) {N c h w kH kW : ℕ} (rnd : ℝ → ℝ) (xName : String) (b : Vec c) (x : Vec (N * (c * (2 * h) * (2 * w)))) (W : DepthwiseKernel c kH kW) :
                                                      SHlo (N * (c * h * w)) → SHlo (c * kH * kW)

                                                      depthwiseStridedXlaWeightGradB, or its bf16 peer at rounding rnd when bf16.

                                                      Equations
                                                      • One or more equations did not get rendered due to their size.
                                                      Instances For
                                                        @[reducible]
                                                        def Proofs.StableHLO.SHlo.depthwiseWeightGradBAt (bf16 : Bool) {N c h w kH kW : ℕ} (rnd : ℝ → ℝ) (xName : String) (b : Vec c) (x : Vec (N * (c * h * w))) (W : DepthwiseKernel c kH kW) :
                                                        SHlo (N * (c * h * w)) → SHlo (c * kH * kW)

                                                        depthwiseWeightGradB, or its bf16 peer at rounding rnd when bf16.

                                                        Equations
                                                        • One or more equations did not get rendered due to their size.
                                                        Instances For
                                                          @[reducible]
                                                          def Proofs.StableHLO.SHlo.flatConvFAt (bf16 : Bool) {ic oc h w kH kW : ℕ} (rnd : ℝ → ℝ) (wName bName : String) (W : Kernel4 oc ic kH kW) (b : Vec oc) :
                                                          SHlo (ic * h * w) → SHlo (oc * h * w)

                                                          flatConvF, or its bf16 peer at rounding rnd when bf16.

                                                          Equations
                                                          Instances For
                                                            @[reducible]
                                                            def Proofs.StableHLO.SHlo.matmulFBAt (bf16 : Bool) {N m k n : ℕ} (rnd : ℝ → ℝ) :
                                                            SHlo (N * (m * k)) → SHlo (N * (k * n)) → SHlo (N * (m * n))

                                                            matmulFB, or its bf16 peer at rounding rnd when bf16.

                                                            Equations
                                                            Instances For
                                                              @[reducible]
                                                              def Proofs.StableHLO.SHlo.rowDenseWeightGradBAt (bf16 : Bool) {N tk a c : ℕ} (rnd : ℝ → ℝ) (xName : String) (x : Vec (N * (tk * a))) :
                                                              SHlo (N * (tk * c)) → SHlo (a * c)

                                                              rowDenseWeightGradB, or its bf16 peer at rounding rnd when bf16.

                                                              Equations
                                                              Instances For
                                                                def Proofs.StableHLO.bnEvalSite (B oc hh ww : ℕ) (epsStr gName btName statP xin : String) :

                                                                One frozen-statistic BN site at the per-example index — bnPerChannelEvalF on xin, with the running statistics arriving as graph inputs %{statP}mu / %{statP}var. The BN site of the per-example eval chains (r34FwdChain, r50FwdChain, mnv2FwdChain), which write @resnet34_fwd_eval, @resnet50in_fwd_eval and @mobilenetv2_fwd_eval: frozen-stat affine BN performs no reduction, so these forwards are class-batch-independent and partner the batch-BN train steps whose EMA'd μ/σ² they read. There is deliberately no training arm — the per-example training BN those chains once also emitted (bnPerChannelF) is not the BN any shipped train step uses.

                                                                Equations
                                                                • One or more equations did not get rendered due to their size.
                                                                Instances For