TorchLean API

NN.Proofs.Autograd.Runtime.Link.FDeriv

Analytic upgrade of the runtime link: reverse mode computes (fderiv eval)† #

Two independent developments meet in this file.

The two developments are stated on different graph types: the algebraic Algebra.Graph (α := α) Δ Γ ss versus the -monomorphic, environment-free Proofs.Autograd.Graph Γ ss. Their contexts already coincide (TorchLean.TensorPack ℝ Γ is by definition Algebra.TorchLean.TensorPack ℝ Γ), and their eval/jvpCtx/backpropCtx recursions mirror each other node for node; what has been missing is the formal connection. This file supplies it:

Throughout, "reverse pass" means the exact tape model: the Tape.backwardDenseFrom program of Runtime/Autograd instantiated at the exact carrier α := ℝ. Nothing in this file is a statement about the native Float evaluation or the CUDA execution path; relating those to the exact model is a separate (approximation) concern.

The analytic context inner product (Spec.dot-based) agrees with the algebraic one (TensorAlgebra.dot-based) at .

Both recursions are the same sum of per-entry tensor dots; the entries agree by dot_eq_tensorAlgebra_dot.

flattenCtx commutes with casting a context along a shape-list equality.

Vectorization is additive: tensorToVec maps addSpec to vector addition.

flattenCtx maps context addition to vector addition.

The vectorized forward map, evaluated on a flattened context.

theorem Proofs.Autograd.Node.jvpVec_flattenCtx {Γ : List Spec.Shape} {τ : Spec.Shape} (node : Node Γ τ) (x dx : TorchLean.TensorPack Γ) :
node.jvpVec (flattenCtx x) (flattenCtx dx) = tensorToVec (node.jvp x dx)

The vectorized JVP, evaluated on flattened contexts.

The vectorized VJP, evaluated on a flattened context and a vectorized cotangent.

The Euclidean graph evaluation is the flattening of the TensorPack evaluation.

The Euclidean graph JVP is the flattening of the TensorPack JVP.

The Euclidean reverse pass is the flattening of the TensorPack reverse pass.

Embed an analytic node as an algebraic node over with a trivial environment.

Instances For

    Embed an analytic graph as an algebraic graph over with a trivial environment.

    Instances For
      def Proofs.Autograd.Algebra.Node.toReal {Δ : Type} {Γ : List Spec.Shape} {τ : Spec.Shape} (node : Node Δ Γ τ) (d : Δ) :

      Specialize an algebraic node at carrier and a fixed environment d : Δ to an analytic node. The adjointness law transports along dot_eq_tensorAlgebra_dot.

      Instances For
        def Proofs.Autograd.Algebra.Graph.toReal {Δ : Type} {Γ ss : List Spec.Shape} :
        Graph Δ Γ ssΔAutograd.Graph Γ ss

        Specialize an algebraic graph at carrier and a fixed environment to an analytic graph.

        Instances For
          @[simp]

          The round trip through the algebraic model is the identity on analytic nodes.

          @[simp]

          Specializing an algebraic node with a trivial environment and embedding it back is the identity.

          @[simp]

          The round trip through the algebraic model is the identity on analytic graphs.

          @[simp]

          Specializing an algebraic graph with a trivial environment and embedding it back is the identity.

          theorem Proofs.Autograd.Algebra.Graph.toReal_eval {Δ : Type} {Γ ss : List Spec.Shape} (g : Graph Δ Γ ss) (x : TorchLean.TensorPack Γ) (d : Δ) :
          (g.toReal d).eval x = g.eval x d

          Specialization preserves evaluation.

          theorem Proofs.Autograd.Algebra.Graph.toReal_jvpCtx {Δ : Type} {Γ ss : List Spec.Shape} (g : Graph Δ Γ ss) (x dx : TorchLean.TensorPack Γ) (d : Δ) :
          (g.toReal d).jvpCtx x dx = g.jvpCtx x dx d

          Specialization preserves the JVP.

          theorem Proofs.Autograd.Algebra.Graph.toReal_backpropCtx {Δ : Type} {Γ ss : List Spec.Shape} (g : Graph Δ Γ ss) (x : TorchLean.TensorPack Γ) (d : Δ) (seed : TorchLean.TensorPack (Γ ++ ss)) :
          (g.toReal d).backpropCtx x seed = g.backpropCtx x d seed

          Specialization preserves the reverse pass.

          Read the input (Γ-prefix) block out of a full context over Γ ++ ss.

          Instances For

            The proof context's input projection is the public tensor-pack split.

            Push a cast along a cons cell.

            On a context with no intermediates, takeLeft is the cast along Γ ++ [] = Γ.

            takeLeft ignores a snoc-ed final block (after reassociating the context).

            theorem Proofs.Autograd.Algebra.Graph.takeLeft_backpropAllCtx {α : Type} [TorchLean.Storage α] [CommSemiring α] {Δ : Type} {Γ ss : List Spec.Shape} (g : Graph Δ Γ ss) (x : TorchLean.TensorPack α Γ) (d : Δ) (seed : TorchLean.TensorPack α (Γ ++ ss)) :

            The input (Γ-prefix) block of the full backpropagation is the inputs-only backpropagation.

            This identifies the link-facing backpropAllCtx (which retains a cotangent for every value, mirroring the tape engine's dense reverse pass) with the proof-facing backpropCtx on the input block.

            theorem Proofs.Autograd.Algebra.GraphData.takeLeft_backpropAllCtx {α : Type} [TorchLean.Storage α] [Add α] {Δ : Type} {Γ ss : List Spec.Shape} (g : GraphData α Δ Γ ss) (x : TorchLean.TensorPack α Γ) (d : Δ) (seed : TorchLean.TensorPack α (Γ ++ ss)) :

            GraphData version of Graph.takeLeft_backpropAllCtx.

            Analytic upgrade of the algebraic reverse pass. At carrier and a fixed environment, the inputs-only backpropagation computes the adjoint of the Fréchet derivative of the graph's (vectorized) forward evaluation.

            Pointwise variant of backpropCtx_eq_adjoint_fderiv: differentiability is assumed only at the values actually encountered, admitting non-smooth primitives away from their kinks.

            theorem Proofs.Autograd.Algebra.Graph.toReal_evalVec {Δ : Type} {Γ ss : List Spec.Shape} (g : Graph Δ Γ ss) (d : Δ) (x : TorchLean.TensorPack Γ) :

            The function differentiated in the endpoints is the graph's own forward evaluation.

            Tape-model reverse pass = adjoint of the Fréchet derivative. Running the tape engine's dense reverse pass (Tape.backwardDenseFrom) on a lowered graph (the exact tape model instantiated at α := ℝ, not the native Float or CUDA execution path) succeeds and returns the full backpropagation context, whose input (Γ-prefix) block is exactly the adjoint of the Fréchet derivative of the graph's forward evaluation applied to the seed.

            This composes the runtime link (backwardDenseFrom_lowerGraphToTape_eq_backpropAllCtx) with the analytic upgrade above.