8.3. Autograd and Execution
The runtime has a dynamic tape for eager execution and a typed SSA builder for compiled programs.
Both sit below the layer API. A separate bridge executes the supported fragment of NN.IR.Graph;
its correctness theorem states the fragment explicitly.
The operation interface describes programs over shape-indexed tensors and scalar operations without choosing eager or compiled execution at the call site.
Lean code for Definition8.3.1●1 definition
Associated Lean declarations
-
Runtime.Autograd.TorchLean.Program[complete]
-
Runtime.Autograd.TorchLean.Program[complete]
-
abbrevdefined in NN/Runtime/Autograd/TorchLean/Backend.leancomplete
abbrev Runtime.Autograd.TorchLean.Program (α : Type) [Context α] [DecidableEq Spec.Shape] (ss : List Spec.Shape) (τ : Spec.Shape) : Type 1
abbrev Runtime.Autograd.TorchLean.Program (α : Type) [Context α] [DecidableEq Spec.Shape] (ss : List Spec.Shape) (τ : Spec.Shape) : Type 1
A TorchLean program is backend-polymorphic: it can run in any `m` that implements `Ops`. In practice: - `m := Runtime.Autograd.Session` gives you eager execution (and an autograd tape), - `m := Runtime.Autograd.Compiled.M` records an SSA/DAG suitable for compilation/verification.
The CPU tape is a dynamic computation DAG. It stores shape-erased values and accumulates reverse-mode gradients by node index.
Lean code for Definition8.3.2●1 definition
Associated Lean declarations
-
Runtime.Autograd.Tape[complete]
-
Runtime.Autograd.Tape[complete]
-
structuredefined in NN/Runtime/Autograd/Engine/Core/Core.leancomplete
structure Runtime.Autograd.Tape (α : Type) : Type
structure Runtime.Autograd.Tape (α : Type) : Type
Autograd tape: a grow-only array of nodes. Node ids are array indices (`Nat`). All ops append exactly one node and return its id. This makes it easy to implement reverse-mode by traversing ids in reverse order.
Fields
nodes : Array (Runtime.Autograd.Node α)
Tape nodes in evaluation order. Node ids are array indices (`Nat`). Each tape op appends exactly one node and returns its id.
compile takes executable typed graph data and its input context, then returns a
runtime tape together with the typed list of inputs and intermediate
values.
Lean code for Definition8.3.3●1 definition
Associated Lean declarations
-
Runtime.Autograd.Compiled.compile[complete]
-
Runtime.Autograd.Compiled.compile[complete]
-
defdefined in NN/Runtime/Autograd/Compiled/Core.leancomplete
def Runtime.Autograd.Compiled.compile {α : Type} [DecidableEq Spec.Shape] {Γ ss : List Spec.Shape} (g : Runtime.Autograd.Compiled.GraphData α Γ ss) (x : Runtime.Autograd.Compiled.TList α Γ) : Runtime.Autograd.Tape α × Runtime.Autograd.Compiled.TList α (Γ ++ ss)
def Runtime.Autograd.Compiled.compile {α : Type} [DecidableEq Spec.Shape] {Γ ss : List Spec.Shape} (g : Runtime.Autograd.Compiled.GraphData α Γ ss) (x : Runtime.Autograd.Compiled.TList α Γ) : Runtime.Autograd.Tape α × Runtime.Autograd.Compiled.TList α (Γ ++ ss)
Compile an executable `GraphData` into a runtime eager tape. This is the bridge from the proof-compiled SSA representation to the runtime tape engine: `Graph.compileAuxData` emits a `Runtime.Autograd.Tape` whose nodes replay the graph and whose backward closures implement the graph's VJP rules. PyTorch comparison: conceptually similar to the front half of `torch.compile` / TorchDynamo (tracing a computation to an IR), except our target is an explicit autograd tape for which we also maintain proof links.
For executable typed graph data over a commutative semiring, dense reverse accumulation on the compiled tape equals proof-level graph backpropagation after both contexts are converted to the tape's value array.
Lean code for Theorem8.3.4●1 theorem
Associated Lean declarations
-
theoremdefined in NN/Proofs/Autograd/Runtime/Link/BackwardGraphData.leancomplete
theorem Proofs.Autograd.Algebra.Graph.backwardDenseFrom_compileAuxData_eq_backpropAllCtx {α Δ : Type} [DecidableEq Spec.Shape] [CommSemiring α] {Γ ss : List Spec.Shape} (g : Proofs.Autograd.Algebra.GraphData α Δ Γ ss) (x : Proofs.Autograd.Algebra.TList α Γ) (d0 : Δ) (seed : Proofs.Autograd.Algebra.TList α (Γ ++ ss)) : (Proofs.Autograd.Algebra.Graph.compileAuxData g x d0).1.backwardDenseFrom seed.toAnyArray = Except.ok (g.backpropAllCtx x d0 seed).toAnyArray
theorem Proofs.Autograd.Algebra.Graph.backwardDenseFrom_compileAuxData_eq_backpropAllCtx {α Δ : Type} [DecidableEq Spec.Shape] [CommSemiring α] {Γ ss : List Spec.Shape} (g : Proofs.Autograd.Algebra.GraphData α Δ Γ ss) (x : Proofs.Autograd.Algebra.TList α Γ) (d0 : Δ) (seed : Proofs.Autograd.Algebra.TList α (Γ ++ ss)) : (Proofs.Autograd.Algebra.Graph.compileAuxData g x d0).1.backwardDenseFrom seed.toAnyArray = Except.ok (g.backpropAllCtx x d0 seed).toAnyArray
Variant of `backwardDenseFrom_compileAux_eq_backpropAllCtx` for the `GraphData` interface. This is useful when a graph carries extra payload `Δ` (e.g. parameters/config) through forward and backward closures.
Induction over the typed graph keeps the forward context and tape indices aligned while the reverse loop accumulates each node's contribution.
For a linked-session snapshot, dense reverse accumulation on the compiled tape agrees with proof-level graph backpropagation.
Lean code for Theorem8.3.5●1 theorem
Associated Lean declarations
-
theoremdefined in NN/Runtime/Autograd/Torch/LinkedSession/Autograd.leancomplete
theorem Runtime.Autograd.Torch.Internal.SessionIR.backwardDenseFrom_compileAuxData_eq_backpropAllCtx {α : Type} [DecidableEq Spec.Shape] [CommSemiring α] (st : Runtime.Autograd.Torch.Internal.SessionIRState α) (seed : Proofs.Autograd.Algebra.TList α (st.Γ ++ st.ss)) : (Proofs.Autograd.Algebra.Graph.compileAuxData st.g st.x st.nat).1.backwardDenseFrom seed.toAnyArray = Except.ok (st.g.backpropAllCtx st.x st.nat seed).toAnyArray
theorem Runtime.Autograd.Torch.Internal.SessionIR.backwardDenseFrom_compileAuxData_eq_backpropAllCtx {α : Type} [DecidableEq Spec.Shape] [CommSemiring α] (st : Runtime.Autograd.Torch.Internal.SessionIRState α) (seed : Proofs.Autograd.Algebra.TList α (st.Γ ++ st.ss)) : (Proofs.Autograd.Algebra.Graph.compileAuxData st.g st.x st.nat).1.backwardDenseFrom seed.toAnyArray = Except.ok (st.g.backpropAllCtx st.x st.nat seed).toAnyArray
Core proof-link: running the runtime reverse-mode loop on the compiled tape equals proved backprop. This theorem is the "hook" that lets a session-style API be backed by the proved IR: `compileAuxData` produces a tape, and `Tape.backwardDenseFrom` is shown equal to `GraphData.backpropAllCtx` (up to the `TList.toAnyArray` representation change).
The linked-session hook specializes the graph-data backward theorem to the session's graph, inputs, and auxiliary index environment.
Layer definitions carry parameter and buffer initialization, training or evaluation mode, and shape-checked sequential composition. They build their computations through the runtime operation interface.
Lean code for Definition8.3.6●1 definition
Associated Lean declarations
-
Runtime.Autograd.TorchLean.NN.Seq[complete]
-
Runtime.Autograd.TorchLean.NN.Seq[complete]
-
inductivedefined in NN/Runtime/Autograd/TorchLean/NN/Seq.leancomplete
inductive Runtime.Autograd.TorchLean.NN.Seq : Spec.Shape → Spec.Shape → Type 2
inductive Runtime.Autograd.TorchLean.NN.Seq : Spec.Shape → Spec.Shape → Type 2
Sequential composition of `LayerDef`s, indexed by input/output shape. This is the builder-layer analogue of `torch.nn.Sequential`: a `Seq σ τ` represents a model that takes an input of shape `σ` and produces an output of shape `τ` by running layers left-to-right.
Constructors
Runtime.Autograd.TorchLean.NN.Seq.id (s : Spec.Shape) : Runtime.Autograd.TorchLean.NN.Seq s s
The empty sequence, which leaves a tensor unchanged.
Runtime.Autograd.TorchLean.NN.Seq.cons {σ τ υ : Spec.Shape} : Runtime.Autograd.TorchLean.NN.LayerDef σ τ → Runtime.Autograd.TorchLean.NN.Seq τ υ → Runtime.Autograd.TorchLean.NN.Seq σ υ
Run one layer, then the remaining sequence.
Lean code for Definition8.3.7●1 definition
Associated Lean declarations
-
Runtime.Autograd.Compiled.execGraphOfIR[complete]
-
Runtime.Autograd.Compiled.execGraphOfIR[complete]