Each transformer block inDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/itsubaki/gpt/llms.txt
Use this file to discover all available pages before exploring further.
itsubaki/gpt follows a pre-normalization design: the residual stream is normalized with RMSNorm before entering each sub-layer, causal self-attention is applied and the result is added back to the stream, then a second RMSNorm feeds a SwiGLU feed-forward network whose output is added via another residual connection. This pre-norm arrangement stabilizes training without requiring careful learning-rate warmup.
Block constructor and struct
headDim is derived by integer division: embedDim / numOfHeads. The RoPEFunc closure is passed in from the parent GPT model and shared across all blocks so the cos/sin tables are computed once.
Forward pass — the residual stream
F.Add calls implement skip connections: x[0] (the original input) is added to the attention output, and x2 (after the first residual) is added to the FFN output. Both norms operate on the residual stream before it enters a sub-layer, not after.
RMSNorm instead of LayerNorm
itsubaki/gpt uses Root Mean Square normalization in place of the standard LayerNorm.
Why RMSNorm
LayerNorm subtracts the per-token mean and divides by the standard deviation, then scales and shifts with learnablegamma and beta. RMSNorm omits both the mean subtraction and the beta shift:
beta parameter entirely, making RMSNorm slightly faster and fewer parameters to tune while empirically matching LayerNorm quality in autoregressive language models.
RMSNorm implementation
gamma vector is initialized to ones and learned during training. The eps value 1e-5 guards against division by zero. The forward pass computes:
F.Reshape with keepdims-style shape ensures correct broadcasting when dividing the (B, C, EmbedDim) tensor by the scalar RMS computed over the last dimension.
SwiGLU feed-forward network
Constructor
hiddenDim = xDim * 8 / 3. For xDim = 192 that gives hiddenDim = 512; for xDim = 394 it gives 1050. This 8/3 ratio approximately matches the parameter count of a standard 4× FFN while fitting the three-matrix SwiGLU design.
Gated activation
W·x ⊙ sigmoid(W·x) is the SiLU (Swish) activation applied to the first linear branch, and the result is element-wise multiplied by the second linear branch V·x. The gated output is then projected back to xDim by O. This gating mechanism lets the network learn to suppress or amplify individual hidden units based on the input, often outperforming GELU-based FFNs.
Alternative FFN with GELU
The repository also provides a classic two-layer FFN for reference:FFN uses a standard expand-then-contract shape with GELU activation and only two weight matrices compared to SwiGLU’s three. The default Block constructor uses SwiGLU; swap in FFN if you want a simpler baseline.
Cache propagation
BlockT.ClearCache() delegates directly to the attention layer inside the block:GPT.ClearCache() walks every block[i] and calls this method, resetting the KV cache and token-offset counter in each attention layer.