Layer Implementations #
Explicit-seed layer constructors behind the public nn.* builders in NN.API.Seeded.
Only NN.API.Seeded imports this module. Every constructor here takes its initialization seeds as
ordinary arguments; the public builders draw those seeds from nn.Builder and are the supported
way to construct layers. The bodies stay exposed so that downstream proofs can unfold a built model
through the public builders.
Leading dimensions #
Reshape arbitrary leading dimensions into the single outer dimension expected by layer.
For an input of shape leading.concat σ, the layer receives shape
[leading.size].concat σ; its output is then reshaped from [leading.size].concat τ to
leading.concat τ. The adapter reuses the layer's parameters and buffer-update function.
Instances For
Share a single-sequence recurrent core over every index of batchShape.
Instances For
Affine and recurrent layers #
Linear layer on the last axis (prefix-shape preserving).
PyTorch analogue: torch.nn.Linear.
See https://pytorch.org/docs/stable/generated/torch.nn.Linear.html.
If input has shape [..., inputWidth], linear inputWidth outputWidth returns a model of shape
[..., outputWidth]. The leading dimensions are treated as a batch: they are flattened to
(numel(prefix), inputWidth), the affine map is applied once, and the result is reshaped back.
Instances For
Vanilla RNN layer (time-major sequence, no batch axis).
Semantics:
$$ h_t=\tanh\!\left(W[x_t;h_{t-1}]+b\right),\qquad h_{-1}=0. $$
This is implemented by unrolling sequenceLength steps using existing TorchLean ops, so it runs on
both CPU and CUDA backends.
PyTorch analogy: torch.nn.RNN(inputWidth, hiddenWidth, nonlinearity="tanh") with
batch_first=false, specialized to a single batch element.
Instances For
GRU layer (time-major sequence, no batch axis).
This is implemented by unrolling sequenceLength Cho-style steps using existing TorchLean ops, so
it runs on both CPU and CUDA backends. PyTorch uses a different reset-after candidate
parameterization; its GRU checkpoints are not directly compatible with this constructor.
Instances For
Trainable selective Mamba layer.
The input has shape (sequenceLength × inputWidth) and the output has shape
(sequenceLength × hiddenWidth). Each token passes through a causal depthwise convolution and
produces its own time steps, input coefficients, and readout coefficients for a diagonal state
update. options controls the expanded channels, states per channel, and convolution width.
The recurrence is unrolled with differentiable tensor operations.
Instances For
LSTM layer (time-major sequence, no batch axis).
This is implemented by unrolling sequenceLength steps using existing TorchLean ops, so it runs on
both CPU and CUDA backends.
PyTorch analogy: torch.nn.LSTM(inputWidth, hiddenWidth) with batch_first=false, specialized to
a single batch element.
Instances For
Shape and reduction layers #
Softmax over a tensor dimension, rejected by model validation when the axis is out of bounds.
Instances For
Stable log-softmax over a tensor dimension, rejected by model validation when the axis is out of bounds.
Instances For
Reduce-sum to a scalar. PyTorch analogue: torch.sum.
Instances For
Flatten any tensor into a 1D vector of length size s. PyTorch analogue: torch.flatten.
Instances For
View a tensor with a new shape containing the same number of scalar entries.
This is the shape-typed counterpart of torch.reshape. A size mismatch is represented as an
invalid model configuration and rejected by the ordinary validation path.
Instances For
Flatten each tensor after an arbitrary batch shape.
For batchShape = [batch], this is the typed counterpart of
torch.flatten(x, start_dim=1). Multiple batch dimensions are preserved without introducing a
separate batched tensor type.
Instances For
Dropout layer (active in train mode, identity in eval mode).
PyTorch analogue: torch.nn.Dropout.
Instances For
Convolution and pooling #
Apply an arbitrary-dimensional convolution to the channel and spatial suffix of a tensor.
The input suffix is (inputChannels, spatial...). Any axes in batchShape are preserved;
internally they are flattened into one runtime batch and restored after the convolution.
Instances For
Apply an arbitrary-dimensional transpose convolution to the channel and spatial suffix.
The input suffix is (inputChannels, spatial...). Any axes in batchShape are mapped independently
and restored after the operation.
Instances For
Apply max pooling to the channel and spatial suffix of a tensor.
Instances For
Apply average pooling to the channel and spatial suffix of a tensor.
Instances For
Global average pooling over every spatial axis, preserving the batch axes and channels.
Instances For
Normalization #
Layer normalization over the final axis of a tensor.
Every index in batchShape selects one vector of length width. Its entries share a mean and
variance, while scale and bias are shared across all leading indices. batchShape := [] describes
a single vector, and zero-sized leading axes are also allowed. Only width must be positive.
eps is added to the variance before the square root. Setting bias := false keeps only the
learned scale; setting affine := false removes both learned parameters.
Instances For
Divide each final-axis vector by sqrt(mean(x * x) + eps), then apply a learned scale.
The scale has shape [width] and is shared across all leading indices. Setting affine := false
removes it from model state. The default eps is 1e-5, independent of the scalar type.
Instances For
Batch normalization over (batchShape..., channels, spatial...) for any spatial rank.
All leading batch axes and spatial axes contribute to each channel's training statistics.
Evaluation uses the running mean and variance. momentum controls their moving-average updates,
and eps is added to the variance before taking the square root.
Instances For
Instance normalization over (batchShape..., channels, spatial...) for any spatial rank.
Each sample and channel uses its own spatial mean and variance in both training and evaluation.
Scale and bias are shared across samples. Setting bias := false keeps only the scale, while
affine := false removes both parameters.
Instances For
Group normalization over (batchShape..., channels, spatial...) for any spatial rank.
Within each sample, each group shares a mean and variance across its channels and spatial
positions. Scale and bias have one entry per channel. Setting bias := false keeps only the
scale; setting affine := false removes both parameters.
Instances For
Attention #
Multi-head self-attention over a trailing (sequenceLength × modelWidth) shape.
If mask is provided, it is a boolean attention mask of shape (n × n) (e.g. causal masking).
Instances For
Positional encodings #
Add learned positional embeddings to the (sequenceLength × embeddingWidth) suffix of a tensor.
PyTorch analogue: x + position[:sequenceLength] where position is a parameter table.
Instances For
Add sinusoidal positional encodings to the (sequenceLength × embeddingWidth) suffix of a tensor.
Implementation:
- precompute
PE : (sequenceLength × embeddingWidth)at initialization time (stored as a non-trainable buffer), - broadcast it across
batchShapeand add it to the input.
Instances For
Apply RoPE to the (sequenceLength × headWidth) suffix of a tensor.
This matches the standard identity:
$$ \operatorname{rope}(x) = x \odot \cos + \operatorname{rotatePairs}(x) \odot \sin $$
where cos and sin depend only on (pos, dim) and broadcast across batchShape.
Notes:
- This layer is differentiable (gradients flow through the rotation), but it has no trainable
parameters; the precomputed
cos/sintables are stored as non-trainable buffers. - The pure spec version is in
NN.Spec.Layers.PositionalEncoding(Spec.ropeApplyHeadsSpec).
Instances For
Embeddings #
Linear projection for one-hot or soft token-distribution inputs.
Input shape: [..., vocabularySize]
Output shape: [..., embeddingWidth]
This is not an indexed embedding: it multiplies the final input axis by a trainable table. Use
embedding for bounded token ids.
Instances For
Build a trainable table for bounded token ids.
Instances For
Blocks and heads #
Build a rank-polymorphic convolution/activation block.
Instances For
Transformer encoder block.
With post-normalization (the default), this follows:
LayerNorm(x + MHA(x)) -> LayerNorm(x + FFN(x)).
With normalizeFirst := true, each branch is normalized before its learned transform:
x + MHA(LayerNorm(x)) -> x + FFN(LayerNorm(x)).
PyTorch analogue:
torch.nn.TransformerEncoderLayer(https://pytorch.org/docs/stable/generated/torch.nn.TransformerEncoderLayer.html)
Instances For
Flatten the feature suffix and apply an affine map, preserving every batch axis.