TorchLean NN: Sequential Models #
Sequential models #
Sequential composition of Layers, indexed by input/output shape.
This is the builder-layer analogue of torch.nn.Sequential: a Seq σ τ represents a model that
takes an input of shape σ and produces an output of shape τ by running layers left-to-right.
Seq lives in Type 1, and no lower. Each Layer stores its forward pass as an
execution-polymorphic Program, which quantifies over the scalar type α : Type and the
interpreter monad m : Type → Type, so Layer σ τ : Type 1 and any type that stores a Layer
is at least Type 1. Consequently IO (Seq σ τ) is ill-typed; build models purely with
nn.build seed builder, draw a seed in IO with nn.buildIO, or pass the model to a
continuation with nn.withModel.
- id
(s : Spec.Shape)
: Seq s s
The empty sequence, which leaves a tensor unchanged.
- cons
{σ τ υ : Spec.Shape}
: Layer σ τ → Seq τ υ → Seq σ υ
Run one layer, then the remaining sequence.
Instances For
Lift one layer into a sequential model.
Instances For
Collect the parameter and persistent-buffer shapes owned by a sequential model.
This concatenates each layer's stateShapes in order.
Instances For
Collect the gradient flags for all parameters and buffers in a sequential model.
This concatenates each layer's requiresGrad in order. Persistent buffers carry false.
Instances For
Validate every layer's static value-level configuration.
Instances For
Initial parameter and persistent-buffer values for a sequential model.
This concatenates each layer's initState into the flat state list expected by forward and
the supervised module constructors.
Instances For
Collect a storage-first initializer plan when every parameterized layer supplies one.
Parameter-free layers need no annotation and contribute the empty plan. If any parameterized layer has only tensor-valued initializers, the whole model falls back to the ordinary initialization path.
Instances For
Whether any layer in the sequence owns mode-dependent mutable buffers.
Instances For
Internal evaluator that splits the flat model state as it walks the model.
This is the reference-level forward pass used to implement forward.
Instances For
The differentiable forward computation of a sequential model.
The result is operation-polymorphic: eager execution records an autograd tape, while typed-graph
execution records shape-indexed SSA data. mode controls layers such as dropout and batch
normalization; it does not enable or disable gradient tracking.
Instances For
Forward and inference helpers #
Mode.train and Mode.eval choose how layers such as dropout and BatchNorm behave.
forwardNoGrad takes live parameters and a concrete input, runs eagerly without recording
gradients, and returns the output tensor. predict selects evaluation mode for that same
operation. Decoding and sampling loops can use these helpers to inspect logits directly.
For repeated graph execution, call lowerToTypedGraph once and evaluate the result with
TypedGraph.forward. The recorded graph keeps the layer mode selected during lowering.
Run an eager forward pass for one concrete input under an explicit mode.
This uses the eager runtime so CUDA kernels stay available, reads back the concrete output, and then releases ephemeral CUDA tape buffers because no backward pass will follow. Use this for validation, decoding, diffusion sampling, and other inference loops.
Instances For
Run eval-mode eager inference for one concrete input.
This is the eval-mode convenience wrapper around forwardNoGrad.
Instances For
Lower a sequential model into a reusable TypedGraph.
The model is recorded once as a typed SSA graph and can then be evaluated repeatedly.
Instances For
Update per-layer buffers across a sequential model.
This explicit reference replay walks the model left-to-right, updating each layer's state from its reference activation. It does not observe a previous runtime execution or reproduce an eager session's random stream. Live modules and trainers instead use the buffer hooks in their actual forward pass.
PyTorch analogy: updating running_mean / running_var buffers during a forward pass in train
mode.
Instances For
Scalar objectives #
Pair an immutable sequential model with a scalar loss.
The resulting definition initializes the model's complete parameter-and-buffer state and computes
the loss from one (input, target) pair. Training mode is the default; pass
mode := .eval when evaluating a mode-sensitive model.
Instances For
Pair a model with mean-squared error.
Instances For
Pair a model with one-hot cross entropy.