TorchLean API

NN.Runtime.Autograd.Engine.Core.Base

CPU tape values #

The CPU tape is Tape (Spec.SomeTensor α): the one tape of NN.Runtime.Autograd.Engine.Tape, storing shape-erased CPU tensors. This module gives those values their GradValue instance and the typed constructors that every CPU operation is built from.

Design goals:

This module defines and executes the tape; it does not by itself prove every registered VJP. Analytic correctness theorems for supported operations live under NN.Proofs.Autograd, while runtime checks cover the integration between those operations and the tape. For the imperative frontend over this engine, see NN.Runtime.Autograd.Torch.Core.

References (PyTorch / background reading):

Accumulate two packed tensors by elementwise addition, with a dynamic shape check.

This is the heart of DAG support: if two different paths contribute gradients to the same parent, we sum the contributions.

Instances For
    @[instance_reducible]

    CPU gradients are shape-erased tensors; ownership needs no bookkeeping.

    @[instance_reducible]

    The CPU zero gradient of a value.

    @[simp]
    theorem Runtime.Autograd.GradValue.drop_someTensor {α β : Type} [TorchLean.Storage α] [Add α] (g : Spec.SomeTensor α) (rest : β) :
    drop g rest = rest

    Tape Construction #

    The Tape namespace provides pure constructors for building a recorded computation graph. Each op appends exactly one node and returns the updated tape plus the new node id.

    def Runtime.Autograd.Tape.leaf {α : Type} [TorchLean.Storage α] {s : Spec.Shape} (t : Tape (Spec.SomeTensor α)) (value : TorchLean.Tensor α s) (name : Option String := none) (requiresGrad : Bool := true) :

    Add a leaf node (no parents).

    PyTorch comparison: a tensor that enters the graph as a leaf (e.g. input or parameter value).

    Instances For

      Read a typed tensor value from a tape node id.

      This is the main "dynamic check" boundary in the eager runtime:

      • fails if the id is invalid, or
      • fails if the stored runtime shape doesn't match the expected dependent shape s.
      Instances For

        Read a typed upstream gradient tensor from a packed tensor.

        This is the backward analogue of Tape.requireValue: it checks that the upstream gradient has the expected shape τ and then performs the dependent cast.

        Instances For
          @[inline]
          def Runtime.Autograd.Tape.unary {α : Type} [TorchLean.Storage α] {σ τ : Spec.Shape} (t : Tape (Spec.SomeTensor α)) (opName : String) (xId : ℕ) (forward : TorchLean.Tensor α σ → TorchLean.Tensor α τ) (backward : TorchLean.Tensor α σ → TorchLean.Tensor α τ → TorchLean.Tensor α σ) :

          Generic constructor for unary ops.

          You provide:

          • forward : Tensor α σ → Tensor α τ
          • backward : Tensor α σ → Tensor α τ → Tensor α σ (a VJP rule; note it may depend on the input)

          The returned node inherits the parent's gradient flag and stores the forward value and a backward closure that checks the upstream gradient's shape and returns the parent contribution.

          Instances For
            @[inline]
            def Runtime.Autograd.Tape.binary {α : Type} [TorchLean.Storage α] {σ₁ σ₂ τ : Spec.Shape} (t : Tape (Spec.SomeTensor α)) (opName : String) (aId bId : ℕ) (forward : TorchLean.Tensor α σ₁ → TorchLean.Tensor α σ₂ → TorchLean.Tensor α τ) (backward : TorchLean.Tensor α σ₁ → TorchLean.Tensor α σ₂ → TorchLean.Tensor α τ → TorchLean.Tensor α σ₁ × TorchLean.Tensor α σ₂) :

            Record a binary operation with typed inputs and cotangents.

            Input lookup, parent metadata, and backward contributions all follow the order a, then b. The backward function runs after the upstream shape check and receives both saved inputs.

            Instances For