TorchLean API

NN.Proofs.RuntimeApprox.Graph.BackwardApprox

BackwardApprox #

Reverse-mode (backward) runtime→spec approximation framework.

This is the backward analogue of NN/Proofs/RuntimeApprox/Graph/ForwardApprox.lean.

It models a tape/SSA-style DAG where each node provides:

The global theorem RevGraph.backprop_approx composes these local bounds over the whole graph, analogously to FwdGraph.eval_approx.

Note: reverse-mode accumulation uses context-wise addition; the corresponding bound/soundness for accumulation is an explicit parameter to the theorem (addBound, addSound).

What you get #

Reading guide #

  1. RevNode / RevGraph: nodes carry both forward and VJP approximation data.
  2. RevGraph.eval_*: reuse the forward theory by forgetting VJP data.
  3. RevGraph.backprop*: reverse-mode accumulation and its explicit bound propagation.
  4. RevGraph.backprop_approx: composes node-local VJP bounds and accumulation bounds over the whole graph (induction over the snoc-list).

PyTorch correspondence / citations #

This mirrors the structure of reverse-mode AD in PyTorch Autograd: a forward pass produces intermediate values, and a backward pass propagates cotangents/gradients in reverse topological order while accumulating contributions at shared nodes. https://pytorch.org/docs/stable/autograd.html

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

A node with forward and VJP approximation data.

vjpSound relates the two supplied reverse maps; it does not itself prove that either map is the derivative of the forward map. Derivative correctness requires a separate node contract.

Instances For
    inductive Proofs.RuntimeApprox.RevGraph {α : Type} (toSpec : α → Spec.SpecScalar) (Γ : List Spec.Shape) :

    Tape/SSA DAG with reverse-mode metadata (nodes appended in topological order).

    Compared to FwdGraph, each node additionally carries a local VJP rule (RevNode.vjp*) plus an explicit error transformer vjpBound and its soundness lemma vjpSound.

    Instances For
      def Proofs.RuntimeApprox.RevGraph.toFwdGraph {α : Type} {toSpec : α → Spec.SpecScalar} {Γ ss : List Spec.Shape} :
      RevGraph toSpec Γ ss → FwdGraph toSpec Γ ss

      Forget the VJP metadata of a RevGraph, yielding a FwdGraph with the same node ordering.

      This lets us reuse the forward approximation theorem (FwdGraph.eval_approx) for free.

      Instances For

        Spec-level evaluation of a RevGraph (delegates to toFwdGraph).

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

          Runtime-level evaluation of a RevGraph (delegates to toFwdGraph).

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

            Forward bound propagation for a RevGraph (delegates to toFwdGraph).

            Instances For
              theorem Proofs.RuntimeApprox.RevGraph.eval_approx {α : Type} {toSpec : α → Spec.SpecScalar} {Γ ss : List Spec.Shape} (g : RevGraph toSpec Γ ss) (xS : TorchLean.TensorPack Spec.SpecScalar Γ) (xR : TorchLean.TensorPack α Γ) (epsIn : EList Γ) :
              approxCtx toSpec xS xR epsIn → approxCtx toSpec (g.evalSpec xS) (g.evalRuntime xR) (g.evalBounds epsIn xR)

              Forward approximation theorem for RevGraph (just FwdGraph.eval_approx via toFwdGraph).

              Spec-level reverse-mode evaluation.

              Informally: run the forward pass to get node contexts, then traverse nodes in reverse topological order, applying each node’s VJP and accumulating the resulting context-cotangent into the running seed.

              Instances For
                def Proofs.RuntimeApprox.RevGraph.backpropRuntime {α : Type} {toSpec : α → Spec.SpecScalar} {Γ ss : List Spec.Shape} (g : RevGraph toSpec Γ ss) [Add α] (x : TorchLean.TensorPack α Γ) (seed : TorchLean.TensorPack α (Γ ++ ss)) :

                Runtime-level reverse-mode accumulation.

                This mirrors backpropSpec, but uses runtime tensors and relies on an Add α instance to accumulate contributions at shared nodes.

                Instances For
                  def Proofs.RuntimeApprox.RevGraph.backpropBounds {α : Type} {toSpec : α → Spec.SpecScalar} {Γ ss : List Spec.Shape} (g : RevGraph toSpec Γ ss) [Add α] (epsIn : EList Γ) (xR : TorchLean.TensorPack α Γ) (epsSeed : EList (Γ ++ ss)) (seedR : TorchLean.TensorPack α (Γ ++ ss)) (addBound : {Δ : List Spec.Shape} → EList Δ → EList Δ → TorchLean.TensorPack α Δ → TorchLean.TensorPack α Δ → EList Δ) :

                  Backward error propagation for backprop*.

                  This is parameterized by:

                  • addBound: how to compute the (per-entry) error list for context-wise addition.

                  addBound may depend on the runtime addends, to account for rounding models.

                  Instances For
                    theorem Proofs.RuntimeApprox.RevGraph.backprop_approx {α : Type} {toSpec : α → Spec.SpecScalar} {Γ ss : List Spec.Shape} (g : RevGraph toSpec Γ ss) [Add α] (addBound : {Δ : List Spec.Shape} → EList Δ → EList Δ → TorchLean.TensorPack α Δ → TorchLean.TensorPack α Δ → EList Δ) (addSound : ∀ {Δ : List Spec.Shape} (xS yS : TorchLean.TensorPack Spec.SpecScalar Δ) (xR yR : TorchLean.TensorPack α Δ) (epsx epsy : EList Δ), approxCtx toSpec xS xR epsx → approxCtx toSpec yS yR epsy → approxCtx toSpec (xS.add yS) (xR.add yR) (addBound epsx epsy xR yR)) (xS : TorchLean.TensorPack Spec.SpecScalar Γ) (xR : TorchLean.TensorPack α Γ) (epsIn : EList Γ) (seedS : TorchLean.TensorPack Spec.SpecScalar (Γ ++ ss)) (seedR : TorchLean.TensorPack α (Γ ++ ss)) (epsSeed : EList (Γ ++ ss)) :
                    approxCtx toSpec xS xR epsIn → approxCtx toSpec seedS seedR epsSeed → approxCtx toSpec (g.backpropSpec xS seedS) (g.backpropRuntime xR seedR) (g.backpropBounds epsIn xR epsSeed seedR fun {Δ : List Spec.Shape} => addBound)

                    End-to-end reverse-mode approximation theorem for RevGraph.backprop*.

                    Informally: assume (1) the runtime inputs xR approximate the spec inputs xS with bounds epsIn, and (2) the runtime seed cotangents seedR approximate the spec seeds seedS with bounds epsSeed. Then the whole backprop result backpropRuntime g xR seedR approximates the spec backprop result backpropSpec g xS seedS, with an explicit bound computed by backpropBounds.

                    The only "extra" ingredient beyond per-node VJP approximation is how we accumulate contributions: addBound describes how addition affects error bounds, and addSound is the theorem justifying it (e.g. for exact reals it is trivial; for rounding models it carries the rounding-error analysis).