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 Γ ss → RevGraph 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 Γ ss → FwdGraphScale 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]

                Runtime evaluation agrees with the unannotated reverse graph.

                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 epsIn → scaleCtx toSpec xS xR bIn → scaleCtx 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.

                    Backprop scale bounds (analogous to backpropBounds) #

                    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 epsIn → scaleCtx toSpec xS xR bIn → scaleCtx toSpec seedS seedR bSeed → scaleCtx 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 supplied with addSound: its output must bound both the exact and interpreted runtime sums. Adding the two input bounds is enough for exact addition, but a rounded carrier may require another rounding allowance.