TorchLean API

NN.Proofs.RuntimeApprox.Scale.BackwardScale

BackwardScale #

Backward (reverse-mode) scale propagation.

This optional module mirrors NN.Proofs.RuntimeApprox.Graph.BackwardApprox, but for scale bounds (nonnegative bounds on linfNorm) rather than eps error bounds.

Use it alongside the backward approximation graph when you want to derive abs+rel tolerances for gradients/cotangents from both an eps error bound and a propagated magnitude bound.

def Proofs.RuntimeApprox.AddScaleSound {α : Type} (toSpec : αSpec.SpecScalar) [Add α] (addBound : {Δ : List Spec.Shape} → BList ΔBList ΔTorchLean.TensorPack α ΔTorchLean.TensorPack α ΔBList Δ) :

Soundness condition for accumulating scale bounds under addition in a context.

Instances For
    structure Proofs.RuntimeApprox.RevNodeScale {α : Type} (toSpec : αSpec.SpecScalar) (Γ : List Spec.Shape) (τ : Spec.Shape) extends Proofs.RuntimeApprox.RevNode toSpec Γ τ :

    A reverse node augmented with forward+VJP scale bounds.

    Instances For

      Reverse-mode graph with scale-aware nodes.

      Instances For
        def Proofs.RuntimeApprox.RevGraphScale.toRevGraph {α : Type} {toSpec : αSpec.SpecScalar} {Γ ss : List Spec.Shape} :
        RevGraphScale toSpec Γ ssRevGraph toSpec Γ ss

        Forget the scale annotations on nodes, producing an ordinary RevGraph.

        Instances For
          def Proofs.RuntimeApprox.RevGraphScale.toFwdGraphScale {α : Type} {toSpec : αSpec.SpecScalar} {Γ ss : List Spec.Shape} :
          RevGraphScale toSpec Γ ssFwdGraphScale toSpec Γ ss

          Convert a RevGraphScale into a FwdGraphScale by dropping the reverse-mode payload.

          Instances For

            Forgetting the scale annotations commutes with forgetting the reverse-mode structure.

            Both directions of the square end at the same plain forward graph, which is why the scale-annotated development can reuse the unannotated evaluation lemmas instead of repeating them.

            Evaluate the forward pass on spec values, returning the extended context Γ ++ ss.

            Instances For
              def Proofs.RuntimeApprox.RevGraphScale.evalRuntime {α : Type} {toSpec : αSpec.SpecScalar} {Γ ss : List Spec.Shape} (g : RevGraphScale toSpec Γ ss) (x : TorchLean.TensorPack α Γ) :

              Evaluate the forward pass on runtime values, returning the extended context Γ ++ ss.

              Instances For
                @[simp]

                Spec evaluation of a scale-annotated reverse graph agrees with the unannotated one.

                @[simp]

                And so does runtime evaluation. The annotations carry bounds only; they never change what is computed, which is exactly what these two lemmas record.

                def Proofs.RuntimeApprox.RevGraphScale.evalBounds {α : Type} {toSpec : αSpec.SpecScalar} {Γ ss : List Spec.Shape} (g : RevGraphScale toSpec Γ ss) (epsIn : EList Γ) (xR : TorchLean.TensorPack α Γ) :
                EList (Γ ++ ss)

                Forward-pass error bounds for all intermediate nodes, computed from input bounds epsIn.

                Instances For
                  def Proofs.RuntimeApprox.RevGraphScale.evalScales {α : Type} {toSpec : αSpec.SpecScalar} {Γ ss : List Spec.Shape} (g : RevGraphScale toSpec Γ ss) (bIn : BList Γ) (xR : TorchLean.TensorPack α Γ) :
                  BList (Γ ++ ss)

                  Forward-pass scale bounds for all intermediate nodes, computed from input bounds bIn.

                  Instances For
                    theorem Proofs.RuntimeApprox.RevGraphScale.eval_scale {α : Type} {toSpec : αSpec.SpecScalar} {Γ ss : List Spec.Shape} (g : RevGraphScale toSpec Γ ss) (xS : TorchLean.TensorPack Spec.SpecScalar Γ) (xR : TorchLean.TensorPack α Γ) (epsIn : EList Γ) (bIn : BList Γ) :
                    approxCtx toSpec xS xR epsInscaleCtx toSpec xS xR bInscaleCtx toSpec (g.evalSpec xS) (g.evalRuntime xR) (g.evalScales bIn xR)

                    Forward evaluation respects the scale bounds: if the inputs are within their bounds, every intermediate node is within the bound evalScales computes for it.

                    def Proofs.RuntimeApprox.RevGraphScale.backpropScales {α : Type} {toSpec : αSpec.SpecScalar} {Γ ss : List Spec.Shape} (g : RevGraphScale toSpec Γ ss) [Add α] (bIn : BList Γ) (xR : TorchLean.TensorPack α Γ) (bSeed : BList (Γ ++ ss)) (seedR : TorchLean.TensorPack α (Γ ++ ss)) (addBound : {Δ : List Spec.Shape} → BList ΔBList ΔTorchLean.TensorPack α ΔTorchLean.TensorPack α ΔBList Δ) :

                    Backpropagate scale bounds through a RevGraphScale, analogous to RevGraph.backpropRuntime.

                    Instances For
                      theorem Proofs.RuntimeApprox.RevGraphScale.backprop_scale {α : Type} {toSpec : αSpec.SpecScalar} {Γ ss : List Spec.Shape} (g : RevGraphScale toSpec Γ ss) [Add α] (addBound : {Δ : List Spec.Shape} → BList ΔBList ΔTorchLean.TensorPack α ΔTorchLean.TensorPack α ΔBList Δ) (addSound : AddScaleSound toSpec fun {Δ : List Spec.Shape} => addBound) (xS : TorchLean.TensorPack Spec.SpecScalar Γ) (xR : TorchLean.TensorPack α Γ) (epsIn : EList Γ) (bIn : BList Γ) (seedS : TorchLean.TensorPack Spec.SpecScalar (Γ ++ ss)) (seedR : TorchLean.TensorPack α (Γ ++ ss)) (bSeed : BList (Γ ++ ss)) :
                      approxCtx toSpec xS xR epsInscaleCtx toSpec xS xR bInscaleCtx toSpec seedS seedR bSeedscaleCtx toSpec (g.toRevGraph.backpropSpec xS seedS) (g.toRevGraph.backpropRuntime xR seedR) (g.backpropScales bIn xR bSeed seedR fun {Δ : List Spec.Shape} => addBound)

                      The same statement for the backward pass: propagated cotangents stay within the backpropagated scale bounds.

                      The addBound combiner is a parameter rather than a fixed choice, because how two accumulated gradients' bounds combine depends on the carrier: adding bounds is always sound, but a carrier with a sharper triangle inequality can do better. addSound is what pins down the requirement.