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:
- Works for arbitrary
Tensor α sshapes usingSpec.SomeTensorvalues. - Supports DAGs (shared subexpressions): gradients are accumulated by summation.
- Preserves typed tensors at operator boundaries and reports dynamic shape mismatches as errors.
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):
- PyTorch docs:
torch.autogradand "Autograd mechanics": https://pytorch.org/docs/stable/autograd.html https://pytorch.org/docs/stable/notes/autograd.html - "micrograd" (small autograd engine, useful for intuition): https://github.com/karpathy/micrograd
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
CPU gradients are shape-erased tensors; ownership needs no bookkeeping.
The CPU zero gradient of a value.
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.
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
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
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.