Documentation 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.
Block assembles a complete transformer block with pre-normalization (RMSNorm), multi-head causal self-attention, a residual connection, a second RMSNorm, a SwiGLU feed-forward network, and a final residual connection. It is the core repeating unit stacked to form a full GPT model.
Block()
*BlockT. The per-head dimension is derived automatically as headDim = embedDim / numOfHeads.
Hidden dimension used by all sub-layers: the RMSNorm scale vectors, all attention projection matrices, and the SwiGLU FFN weight matrices.
Number of attention heads passed through to
MultiHeadAttention. embedDim must be divisible by numOfHeads.Shared RoPE function constructed with
function.RoPE(theta, embedDim, contextLen). The same instance should be passed to every block in the model.Optional variadic flag forwarded to
MultiHeadAttention. Pass true to enable KV caching for autoregressive generation.Forward() — residual stream
First() is a convenience wrapper that returns only the first output variable.
x5 has the same shape as the input x[0]: (B, C, E).
The residual connections bypass the normalization layers, not just the sub-layer computations. The pre-norm position (applying norm before attention/FFN rather than after) is the convention used in modern LLMs and helps stabilize training at depth.
SwiGLU feed-forward network
Block. Constructs a gated feed-forward network with three Linear layers and a SiLU (sigmoid-weighted linear unit) gate.
Input and output dimension of the FFN, equal to
embedDim of the enclosing block.xDim * 8 / 3 (following the LLaMA convention for SwiGLU sizing):
output = (a ⊙ sigmoid(a)) ⊙ b, projected back to xDim by O. The SiLU gate a ⊙ sigmoid(a) replaces the simpler GELU gate used in the original GPT-2 FFN.
FFN (alternative)
Block by default, but available as an alternative FFN.
Input and output dimension.
Intermediate hidden dimension between the two linear layers. Typically set to
4 * xDim following the original transformer convention.ClearCache()
ClearCache(), resetting its KV cache and position offset:
useCache = true.
Params()
| Key | Description |
|---|---|
"norm1.gamma" | RMSNorm scale vector before attention |
"norm2.gamma" | RMSNorm scale vector before FFN |
"attn.Wq.w" | Query projection weight matrix |
"attn.Wk.w" | Key projection weight matrix |
"attn.Wv.w" | Value projection weight matrix |
"attn.Wo.w" | Output projection weight matrix |
"ffn.W.w" | SwiGLU gate branch weight |
"ffn.V.w" | SwiGLU value branch weight |
"ffn.O.w" | SwiGLU output projection weight |