Transformer Blocks #
This module exposes attention, feed-forward, and Transformer-stack constructors used by sequence models and higher-level examples.
Config record for transformerEncoderBlock.
Separating the config as a structure makes it easier to write readable examples and keep seed management deterministic.
- numHeads : ℕ
Number of attention heads.
- headDim : ℕ
Per-head embedding dimension.
- ffnHidden : ℕ
Hidden dimension of the feed-forward network.
- activation : Activation
Activation used in the feed-forward network.
Optional dropout probability for examples;
nonemeans no dropout.- normFirst : Bool
Normalize before attention and feed-forward sublayers instead of after each residual.
- attentionOutputBias : Bool
Add a trainable bias after the attention output projection.
- weightInit? : Option Runtime.Autograd.Torch.Init.Scheme
Attention and feed-forward weight initialization.
nonekeeps each layer's default. - residualOutputInit? : Option Runtime.Autograd.Torch.Init.Scheme
Initializer for the attention and feed-forward projections that write to residual streams.
When omitted,
weightInit?is used. The separate field supports depth-scaled residual initialization without imposing that convention on every Transformer. - seedBase : ℕ
Base seed used to derive deterministic per-layer seeds inside the block.
Instances For
Transformer encoder block configuration.
This follows the familiar pattern:
(residual MHA) -> LayerNorm -> (residual FFN) -> LayerNorm.
PyTorch analogue:
torch.nn.TransformerEncoderLayer(https://pytorch.org/docs/stable/generated/torch.nn.TransformerEncoderLayer.html)
Instances For
Transformer encoder block.
This is transformerEncoderBlockWithMask; pass mask := ... to enable causal masking (or other
attention masks).
Instances For
Config record for transformerEncoderStack.
This builds layers copies of transformerEncoderBlock, allocating seeds in a fixed stride.
- layers : ℕ
Layer stack.
- block : TransformerEncoderBlock
Template config for each block (its
seedBaseis ignored; we allocate per-layer seeds). - seedBase : ℕ
Base seed for the whole stack.
- seedStride : ℕ
Seed stride between consecutive blocks (must exceed the per-block seed footprint).
Instances For
Internal recursion for transformerEncoderStack.
Builds remaining blocks starting at layerIdx, allocating each block's seedBase as
seedBase + layerIdx * seedStride.
Instances For
Internal recursion for transformerEncoderStack (unmasked).
This is transformerStackGoWithMask with mask := none.
Instances For
Stack cfg.layers copies of blocks.transformerEncoderBlock.
TorchLean analogue of composing torch.nn.TransformerEncoderLayer into a
torch.nn.TransformerEncoder, using Seq composition for the typed model.
Instances For
Stack cfg.layers copies of blocks.transformerEncoderBlock.
This is transformerEncoderStackWithMask; pass mask := ... to enable causal masking (or other
attention masks).
Instances For
Transformer encoder followed by a flatten+linear classification head.
PyTorch analogue (approximately): nn.TransformerEncoder(...) + pooling/flattening + nn.linear.