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 (TList Γ is by definition Algebra.TList ℝ Γ), 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.

theorem Proofs.Autograd.flattenCtx_cast {Γ₁ Γ₂ : List Spec.Shape} (h : Γ₁ = Γ₂) (xs : TList Γ₁) :

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

Vectorization is additive: toVecT maps addSpec to vector addition.

flattenCtx maps context addition to vector addition.

flattenCtx maps TList.snoc to snocCtx.

flattenCtx maps TList.unsnoc to unsnocCtx.

theorem Proofs.Autograd.Node.forwardVec_flattenCtx {Γ : List Spec.Shape} {τ : Spec.Shape} (node : Node Γ τ) (x : TList Γ) :

The vectorized forward map, evaluated on a flattened context.

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

The vectorized JVP, evaluated on flattened contexts.

theorem Proofs.Autograd.Node.vjpVec_flattenCtx {Γ : List Spec.Shape} {τ : Spec.Shape} (node : Node Γ τ) (x : TList Γ) (δ : Spec.Tensor τ) :
node.vjpVec (flattenCtx x) (toVecT δ) = flattenCtx (node.vjp x δ)

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

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

theorem Proofs.Autograd.Graph.jvpVec_flattenCtx {Γ ss : List Spec.Shape} (g : Graph Γ ss) (x dx : TList Γ) :

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

theorem Proofs.Autograd.Graph.backpropVec_flattenCtx {Γ ss : List Spec.Shape} (g : Graph Γ ss) (x : TList Γ) (seed : TList (Γ ++ ss)) :

The Euclidean reverse pass is the flattening of the TList 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 : TList Γ) (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 : TList Γ) (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 : TList Γ) (d : Δ) (seed : TList (Γ ++ ss)) :
          (g.toReal d).backpropCtx x seed = g.backpropCtx x d seed

          Specialization preserves the reverse pass.

          def Proofs.Autograd.Algebra.TList.takeLeft {α : Type} {Γ ss : List Spec.Shape} :
          TList α (Γ ++ ss)TList α Γ

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

          Instances For
            theorem Proofs.Autograd.Algebra.TList.cast_cons {α : Type} {s : Spec.Shape} {ss₁ ss₂ : List Spec.Shape} (h : s :: ss₁ = s :: ss₂) (h' : ss₁ = ss₂) (x : Spec.Tensor α s) (xs : TList α ss₁) :
            cast h (cons x xs) = cons x (cast h' xs)

            Push a cast along a cons cell.

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

            theorem Proofs.Autograd.Algebra.TList.takeLeft_cast_snoc {α : Type} {Γ ss : List Spec.Shape} {τ : Spec.Shape} (h : Γ ++ ss ++ [τ] = Γ ++ (ss ++ [τ])) (w : TList α (Γ ++ ss)) (y : Spec.Tensor α τ) :

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

            theorem Proofs.Autograd.Algebra.Graph.takeLeft_backpropAllCtx {α : Type} [CommSemiring α] {Δ : Type} {Γ ss : List Spec.Shape} (g : Graph Δ Γ ss) (x : TList α Γ) (d : Δ) (seed : TList α (Γ ++ ss)) :
            (g.backpropAllCtx x d seed).takeLeft = g.backpropCtx x d seed

            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} [Add α] {Δ : Type} {Γ ss : List Spec.Shape} (g : GraphData α Δ Γ ss) (x : TList α Γ) (d : Δ) (seed : TList α (Γ ++ ss)) :
            (g.backpropAllCtx x d seed).takeLeft = g.backpropCtx x d seed

            GraphData version of Graph.takeLeft_backpropAllCtx.

            theorem Proofs.Autograd.Algebra.Graph.backpropCtx_eq_adjoint_fderiv {Δ : Type} {Γ ss : List Spec.Shape} (g : Graph Δ Γ ss) (d : Δ) (hg : GraphFDerivCorrect (g.toReal d)) (x : TList Γ) (seed : TList (Γ ++ ss)) :

            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 : TList Γ) :

            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 compiled 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_compileAux_eq_backpropAllCtx) with the analytic upgrade above.