TorchLean API

NN.Runtime.Autograd.Torch.LinkedSession.Core

LinkedSession #

Proof-linked imperative Session (eager-style API, proved IR under the hood).

Background:

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

Practical note:

@[reducible, inline]

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
    @[reducible, inline]

    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).

      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.

          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
                def Runtime.Autograd.Torch.Internal.SessionIR.param {α : Type} (s : SessionIR α) {sh : Spec.Shape} (init : Spec.Tensor α sh) (name : Option String := none) (requiresGrad : Option Bool := none) :
                IO (Param α sh)

                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
                        def Runtime.Autograd.Torch.Internal.SessionIR.input {α : Type} (s : SessionIR α) {sh : Spec.Shape} [DecidableEq Spec.Shape] (v : Spec.Tensor α sh) (name : Option String := none) (requiresGrad : Bool := false) :
                        IO (TensorRef α sh)

                        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

                            Read a previously recorded NatRef.

                            Instances For

                              Overwrite a previously recorded NatRef.

                              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.

                                          Instances For