One page to come back to. Every component of a decoder-only transformer with its formula, its shape, its parameter count and the lesson that explains it, plus GPT-2 small and Llama 3 8B side by side and a tracer for every shape in a forward pass.
Key idea
Notation, every component with formula, shape and parameters, two real models compared, and a shape tracer
Real world
The exploded diagram at the back of a bicycle manual: every part, its size and its part number, so you can find it again without reading the whole manual.
In an LLM
A paper says “d_model 4096, 32 layers, GQA with 8 KV heads, d_ff 14336”. With this page you can work out every matrix shape and that the model has about 8 billion parameters.
Notation
Symbol
Meaning
GPT-2 small
Llama 3 8B
T
tokens in the sequence (context length used)
up to 1,024
up to 8,192
dmodel
width of the residual stream: numbers per token
768
4,096
h
attention heads per layer
12
32
hkv
key/value heads (fewer than h with GQA)
12
8
dhead
numbers per head, usually dmodel/h
64
128
dff
hidden width of the MLP
3,072
14,336
L
number of blocks (layers)
12
32
V
vocabulary size
50,257
128,256
N
total parameters
124M
8.03B
Matrices act on rows: X has one row per token, so X is T×dmodel and a linear layer is XW with W of shape din×dout.
Every component
Parameter counts are for GPT-2 small, including biases; per block where marked.
Totals. GPT-2 small: 38,597,376+786,432+12×7,087,872+1,536=124,439,808. Llama 3 8B: two 128,256×4,096 matrices (embedding and unembedding), 32 blocks of 218,112,000 and a final norm: 8,030,261,248.
GPT-2 small and Llama 3 8B
GPT-2 small (2019)
Llama 3 8B (2024)
Parameters
124M
8.03B
Blocks L
12
32
dmodel
768
4,096
Heads h / KV heads
12 / 12
32 / 8 (grouped-query attention)
dhead
64
128
dff
3,072 (4×)
14,336 (3.5×, three matrices)
Vocabulary V
50,257
128,256
Context
1,024
8,192 (128K in Llama 3.1)
Positions
learned, added to the input
RoPE, θ = 500,000
Normalisation
LayerNorm, before each sublayer
RMSNorm, before each sublayer
MLP activation
GELU
SwiGLU
Biases
yes
no
Embedding and unembedding
tied (shared)
separate
Parameters per block
7.09M
218M
KV cache per token (16-bit)
2×12×12×64×2 bytes = 36.9 kB
2×32×8×128×2 bytes = 131 kB
Forward pass per token
≈ 2N = 0.25 GFLOPs
≈ 2N = 16 GFLOPs
The modern recipe explains each of the changes in the right-hand column, and what is new what came after.
Trace every shape
Pick a model and a number of tokens, then step through one forward pass. Click any row to jump to it.
token ids
tokenizer
[T] = [5] · 5 numbers
parameters per block7.1M
blocks12
total parameters124,439,808
The text as 5 integers, each between 0 and 50,256.
Every step of one forward pass. Shapes use the notation table above: T tokens, d_model, h heads of d_head, d_ff, vocabulary V. Rows marked with the amber bar repeat once per block. Parameter counts are exact and add up to the published totals.
Rules of thumb
Parameters per block ≈ 12dmodel2 for a GPT-style block (4d2 attention + 8d2 MLP). GPT-2 small: 12×7682=7.08M, against 7.09M exactly.
Compute ≈ 2N FLOPs per token for a forward pass and 6N for training (forward plus backward), plus attention’s T2 term for long contexts. See cost.
Memory for weights = N × bytes per parameter: 8B parameters in 16-bit is 16 GB; see model memory.
KV cache = 2×L×hkv×dhead× bytes, per token. It is what limits long context and many users at once.
Attention scores grow with T2; everything else grows with T.
Vocabulary in one line each
Token: a chunk of text with an ID; the model only ever sees IDs.
Embedding: the learned vector for each token ID.
Residual stream: the running vector per token that every layer reads from and adds to.
Attention head: moves information between positions, weighted by query–key matches.
MLP: processes each position on its own; stores much of the model’s knowledge.
Logits: one raw score per vocabulary token; softmax turns them into probabilities.
KV cache: saved keys and values, so generation doesn’t recompute the past.
Induction head: a head that continues repeated patterns (interpretability).
Related
map - A transformer language model is a short pipeline. Text becomes token IDs, IDs become vectors, a stack of identical blocks refines the vectors, and the last vector is turned into a probability for every token. Here is the whole thing on one diagram, with the shape of the data at every step.
the-block - Put the pieces together: LayerNorm, attention, add; LayerNorm, MLP, add. That is one transformer block, and a model is a stack of identical blocks between an embedding table and an output layer. Here is one block computed number by number, and an exact parameter count for real models.
cost - A transformer does about 2 FLOPs per parameter for every token it reads, 6 when training, plus an attention term that grows with the context. But when generating for one user, arithmetic is not the bottleneck. Every new token has to read every weight from memory, so speed is set by memory bandwidth, and batching many users together is almost free.
modern-recipe - A 2026 open model is still GPT-2’s skeleton: embed, a stack of attention and feed-forward blocks on a residual stream, then predict the next token. Almost every part inside has been swapped: RMSNorm, SwiGLU, RoPE, shared or compressed keys and values, mixtures of experts, no biases, QK-norm, bigger vocabularies and contexts a thousand times longer. Here is each change, model by model.