Runtime variables #
This module defines a small Variable record that bundles:
- a name/id,
- a value tensor,
- an optional gradient tensor, and
- a
requires_gradflag.
This matches the mental model of a PyTorch torch.Tensor with requires_grad, together with a
place to store an accumulated .grad. It is used by the runtime layers
that want an explicit "parameter object", while NN/Runtime/Context.lean provides a more generic
name-based registry.
A runtime variable with a fixed shape.
gradient is optional so we can represent "no gradient accumulated yet" (or variables that are
used only for inference).
- id : ℕ
Unique numeric id (allocated by the runtime context).
- name : String
Human-readable name (often a parameter name like
"layer1.weight"). - value : Spec.Tensor α s
The current tensor value.
- gradient : Option (Spec.Tensor α s)
Optional accumulated gradient.
This is
nonewhen no gradient has been accumulated yet (or whenrequires_grad = false). - requires_grad : Bool
Whether gradient-based optimizers may update this variable.
Instances For
Create a new variable from an id/name/value.
By default we mark variables as trainable (requires_grad := true), matching common ML defaults.
Instances For
Read the tensor value stored in a variable.
Instances For
Replace the tensor value stored in a variable.
Instances For
Read the optional gradient stored in a variable.
Instances For
Set (overwrite) the gradient stored in a variable.
Instances For
Clear the gradient stored in a variable.
Instances For
Check whether gradient-based optimizers may update this variable.
Instances For
Accumulate a gradient contribution into the variable.
If no gradient was set yet, this behaves like assignment. Otherwise it adds into the existing
gradient (analogue of PyTorch param.grad += ...).
Instances For
Render a variable for debugging (includes value and optional gradient).
Instances For
Check that the variable's shape equals the given shape.
Instances For
Compute the L2 norm of the variable value (Euclidean norm).
This is a small convenience helper for monitoring/debugging.
Instances For
Clone a variable under a new id, clearing the gradient.
Instances For
Mark a variable as non-trainable and clear its gradient (inference-only).
Instances For
Register this variable's value in a RuntimeContext under its name.
Instances For
Retrieve this variable's value from a RuntimeContext (with shape checking).
Instances For
If v.gradient is present, register it in the RuntimeContext under v.name.
Instances For
Create a sentinel scalar variable used to thread error messages through some pipelines.
This is mainly for demos / ergonomic error handling, not for proofs.
Instances For
Cast a variable to an equal shape.
This is the shape-indexed analogue of Eq.mp: given a proof s₁ = s₂, we can transport the
value (and optional gradient) across the equality.