Causal Transformer Architecture #
Configuration, shape families, and model constructors for GPT-style causal Transformers.
Configuration shared by TorchLean's GPT-style causal language models.
The model has the common GPT-2 “shape”:
embedding → learned positional embedding → (masked self-attention + FFN)×layerCount → LayerNorm
→ linear
The configuration is independent of how token ids enter the model. One-hot, bounded-token, and pre-embedded constructors reuse the same Transformer width, depth, and output vocabulary.
- sequenceLength : ℕ
Number of token positions processed in one model invocation. Must be positive.
- vocabularySize : ℕ
Number of token categories accepted by the input and vocabulary head. Must be positive.
- headCount : ℕ
Number of parallel self-attention heads in each Transformer block. Must be positive.
- headWidth : ℕ
Width of each attention head. Must be positive.
- feedForwardWidth : ℕ
Hidden width of each block's position-wise feed-forward network.
- layerCount : ℕ
Number of stacked causal Transformer blocks.
- activation : Activation.Kind
Feed-forward activation used in every Transformer block.
Dropout probability for attention and feed-forward outputs.
Drop attention probabilities before value aggregation, independently of residual dropout.
Drop activated FFN hidden units before their output projection.
- attentionInputBias : Bool
Enable separate query, key, and value biases;
falsekeeps the existing parameter layout. - normalizeFirst : Bool
Use pre-normalized Transformer blocks, as in GPT-2.
- attentionOutputBias : Bool
Add a trainable bias after each attention output projection, as in GPT-2.
- parameterInitialization? : Option Init.Scheme
Shared initialization for embedding and projection weights.
nonekeeps layer defaults. - residualProjectionInitialization? : Option Init.Scheme
Initialization for projections whose outputs are added to residual streams.
GPT-2 scales these weights by network depth. Keeping the setting explicit lets other causal Transformers use their own residual initialization without changing the block implementation.
Instances For
Instances For
Shared token and positional embedding initialization, including the layer default.
Instances For
Block settings shared by validation and construction of the hidden stack.
Instances For
GPT-style causal Transformer body with an independent affine vocabulary head.
Use hidden when the caller needs hidden states or a tied
token-embedding/output matrix.
Instances For
Build a GPT-2-style causal language model over one-hot tokens.
This is the shared constructor used by the runnable GPT-2 examples. It stays in nn.Builder so it
composes with the rest of the API-layer model-building interface.