LinkedSession #
Proof-linked imperative Session (eager-style API, proved IR under the hood).
Background:
Runtime.Autograd.TorchLean.Sessionprovides a unified imperative API for training/debugging (eager) and verification-friendly execution (compiled).Proofs.Autograd.Algebra.GraphDatais the proved/typed SSA(DAG) IR used by the proof-compiled pipeline (Proofs.Autograd.Algebra.Graph.compileAuxData), andNN/Proofs/Autograd/Runtime/Link.leanproves that running the runtime reverse-mode loop on the compiled tape matchesGraphData.backpropAllCtx.
This file provides a session-style API that records a GraphData (well-typed IR) as you call
ops imperatively, and then runs the standard runtime tape loop on the compiled tape.
Key guarantee (pure theorem, no IO reasoning needed):
- If the session snapshot is
(g, x), thenTape.backwardDenseFrom (compileAuxData g x)equalsGraphData.backpropAllCtx g x(viabackwardDenseFrom_compileAuxData_eq_backpropAllCtx).
Practical note:
- This session enforces a simple invariant: all leaf tensors are created before any op node. This matches the standard training pattern (reset → add leaves → forward → backward).
constis available as a graph node, so you can still introduce literal constants mid-graph.- This is the fully proof-linked variant used by
TorchLean.Sessionwhenopts.backend := .compiled.
Convenience: turn a Result α into IO α by throwing IO.userError on .error.
This mirrors the common pattern in the eager runtime front-end (Torch.Core).
Instances For
Non-differentiable external environment for the proved graph: a small array of Nat inputs.
Instances For
Internal proof-linked session state (a well-typed GraphData plus its leaf values).
- Γ : List Spec.Shape
Leaf shapes (inputs/parameters), in creation order.
- x : Proofs.Autograd.Algebra.TList α self.Γ
Leaf values, aligned with
Γ. - nat : NatEnv
Non-differentiable external inputs (e.g. class labels/indices).
- ss : List Spec.Shape
Internal node shapes, in creation order.
- g : Proofs.Autograd.Algebra.GraphData α NatEnv self.Γ self.ss
SSA/DAG graph nodes (one per entry in
ss).
Instances For
Empty session state: no leaves, no nodes, empty nat-environment.
Instances For
SessionIR is an imperative session that records a GraphData (proved IR) as it runs.
It is "eager-style" (you call ops imperatively), but it is proof-linked: the recorded graph can be
compiled and then the runtime tape backward loop is provably equal to GraphData.backpropAllCtx.
- opts : Options
Session options shared with the eager front-end.
- st : IO.Ref (SessionIRState α)
Mutable proof-linked graph snapshot.
- paramsByLeaf : IO.Ref (Std.HashMap ℕ (AnyParam α))
Map from graph leaf ids to mutable parameter objects.
Instances For
Create a new proof-linked session.
This allocates IO.Refs for the session snapshot (SessionIRState) and the leaf-id→parameter map.
Call resetTape to start a new "graph recording" phase.
Instances For
Reset the session to an empty snapshot.
Important invariant: this session requires that all leaves are created before any op node.
resetTape is the intended boundary between training steps/forwards.
Instances For
Create a mutable parameter object (not yet part of the recorded graph).
To use the parameter in the recorded graph, call use, which reads its current value and records
it as a leaf in Γ.
PyTorch comparison: analogous to creating a torch.nn.Parameter and then using it in a forward.
Instances For
Enforce the session invariant: leaves must be created before any op node.
This matches the usual training pattern: resetTape → add leaves → forward ops → backward.
Instances For
Record a new differentiable leaf tensor in the session context Γ.
This is the primitive used by use (parameters) and input (external inputs).
Instances For
Use a Param in the recorded graph by reading its current value and recording it as a leaf.
The returned TensorRef is the graph handle you pass to subsequent ops. The session also remembers
which leaf-id corresponds to which parameter, so sgdStepAll can update parameters after backward.
PyTorch comparison: like referencing a torch.nn.Parameter in the forward; the parameter's value
is treated as a leaf for autograd.
Instances For
Record an external differentiable input tensor as a leaf.
name and requiresGrad are accepted for API parity with the eager session, but this proof-linked
session always records the input in Γ (a leaf) and uses typing/invariants to determine what
gradients are meaningful.
Instances For
Record a non-differentiable Nat input in the external environment.
This is used for "index-like" inputs (labels, gather indices, etc.) that should not receive gradients. PyTorch comparison: like passing an integer tensor / index to an op; indices are not differentiable.
Instances For
Convert a small Tensor Nat (.dim k .scalar) into an Array Nat.
This is used to stage NatVecRef inputs into the session nat-environment.
Instances For
Record a non-differentiable vector of Nat inputs.
Returns a NatVecRef k which points into the nat-environment. This is useful for "runtime gather"
style ops where indices are supplied externally (and are not differentiable).
Instances For
Read back the k-vector stored at a NatVecRef k.
Instances For
Overwrite the nat-environment segment referenced by NatVecRef k.
Instances For
Build a typed index into the current context Γ ++ ss from a raw numeric id and expected shape.
This is the main "dynamic check" used by getValue (and by a few index-driven nodes): it ensures
that the Nat id points to an existing tensor in the session context and that the shape matches.
Instances For
Evaluate the recorded graph and return the value of a TensorRef.
This is a pure graph evaluation (GraphData.eval) using the recorded leaf values and
nat-environment. It does not run the runtime tape or mutate session state.