Runtime context #
This module defines the runtime environment for executing TorchLean computations.
The main challenge is that tensors are dependently typed by their Shape, but at runtime we want
to store a heterogeneous map from names to values. We solve this by storing an existential wrapper
AnyTensor that carries the Shape alongside the tensor. Lookup functions then check the shape
and use the resulting equality proof to cast the stored tensor to the requested shape.
Reading map #
AnyTensoris the shape-erased wrapper used by the registries.RuntimeContextis the mutable-looking record that stores values, gradients, and a fresh-id counter.register_variable/get_variableare the core value registry operations.register_gradient/get_gradientdo the same on the gradient side.
Existential wrapper for tensors of arbitrary shape.
This is the core trick that lets a runtime registry store tensors of different shapes in one container.
- s : Spec.Shape
The shape carried alongside the tensor value.
- t : Spec.Tensor α self.s
The tensor value, indexed by
s.
Instances For
Runtime context for tracking named variables and their gradients.
We keep two registries:
var_registryfor values, andgradientsfor accumulated gradients (typically produced by backprop).
Registry of named variable values.
Implementation note: this is a simple list, so lookup uses the first match (most-recent wins).
Registry of named gradients.
As with
var_registry, this is a simple list; callers are responsible for deciding whether to prepend new gradients or update/accumulate existing ones.- next_id : ℕ
Fresh id counter for allocating new variables/parameters.
This is incremented by
register_variableand used by higher-level runtime layers.
Instances For
An empty context with no variables and no gradients.
Instances For
Register a new variable in the context.
The value is wrapped as an AnyTensor so we can store it in a heterogeneous registry.
Instances For
Lookup a variable by name and requested shape.
If the name exists and the stored Shape matches s, we cast the stored tensor from
Tensor α any_tensor.s to Tensor α s using the equality proof any_tensor.s = s.
Instances For
Update the value of an existing variable (or do nothing if the name is absent).
This performs a simple list-map update of the registry.
Instances For
Register (prepend) a gradient entry in the context.
If you want to accumulate gradients under the same name, do that at a higher layer before calling this helper.
Instances For
Lookup a gradient by name and requested shape (with the same casting trick as get_variable).
Instances For
Update a gradient entry in the context (simple list-map update).
Instances For
Remove all gradient entries (analogue of PyTorch optimizer.zero_grad()).
Instances For
Return true iff the context contains a variable named name.
Instances For
List all variable names stored in ctx.
Instances For
List all gradient names stored in ctx.
Instances For
Number of registered variables.
Instances For
Number of registered gradient entries.
Instances For
Check a simple invariant: every gradient entry refers to an existing variable name.
This does not check shapes; it only checks name presence.
Instances For
Render the context contents as a string (for debugging).