The transformer at a glance

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

SymbolMeaningGPT-2 smallLlama 3 8B
TTtokens in the sequence (context length used)up to 1,024up to 8,192
dmodeld_{\text{model}}width of the residual stream: numbers per token7684,096
hhattention heads per layer1232
hkvh_{\text{kv}}key/value heads (fewer than hh with GQA)128
dheadd_{\text{head}}numbers per head, usually dmodel/hd_{\text{model}} / h64128
dffd_{\text{ff}}hidden width of the MLP3,07214,336
LLnumber of blocks (layers)1232
VVvocabulary size50,257128,256
NNtotal parameters124M8.03B

Matrices act on rows: XX has one row per token, so XX is T×dmodelT \times d_{\text{model}} and a linear layer is XWX W with WW of shape din×doutd_{\text{in}} \times d_{\text{out}}.

Every component

Parameter counts are for GPT-2 small, including biases; per block where marked.

ComponentFormulaOutput shapeParameters (GPT-2 small)Lesson
Tokenizertext → idsTT0 (a merge table)BPE
Token embeddingX=WE[ids]X = W_E[\text{ids}]T×dmodelT \times d_{\text{model}}VdmodelV d_{\text{model}} = 38.6Membeddings
Learned positionsX+=Wpos[0..T−1]X \mathrel{+}= W_{\text{pos}}[0..T{-}1]T×dmodelT \times d_{\text{model}}1024 dmodel1024\, d_{\text{model}} = 786Kpositions
RoPE (Llama)rotate pairs of q,kq, k by angle ∝\propto positionunchanged0RoPE
LayerNorm / RMSNormγ⊙x−μσ+β\gamma \odot \frac{x - \mu}{\sigma} + \beta / γ⊙xrms(x)\gamma \odot \frac{x}{\text{rms}(x)}T×dmodelT \times d_{\text{model}}2dmodel2 d_{\text{model}} / dmodeld_{\text{model}}normalisation
Q, K, V projectionsQ=X^WQQ = \hat X W_Q, K=X^WKK = \hat X W_K, V=X^WVV = \hat X W_Vh×T×dheadh \times T \times d_{\text{head}}3dmodel2+3dmodel3 d_{\text{model}}^2 + 3 d_{\text{model}} = 1.77M per blockQ, K, V
ScoresS=QK⊤/dheadS = Q K^\top / \sqrt{d_{\text{head}}}h×T×Th \times T \times T0scaling
Causal mask + softmaxA=softmax⁡(S+M)A = \operatorname{softmax}(S + M)h×T×Th \times T \times T0causal mask
Mix valuesO=AVO = A Vh×T×dheadh \times T \times d_{\text{head}}0attention intuition
Output projectionconcat⁡(O) WO\operatorname{concat}(O)\, W_OT×dmodelT \times d_{\text{model}}dmodel2+dmodeld_{\text{model}}^2 + d_{\text{model}} = 591K per blockmulti-head
Residual addx←x+f(norm⁡(x))x \leftarrow x + f(\operatorname{norm}(x))T×dmodelT \times d_{\text{model}}0residual stream
MLP (GELU)GELU⁡(X^W1+b1)W2+b2\operatorname{GELU}(\hat X W_1 + b_1) W_2 + b_2T×dffT \times d_{\text{ff}} → T×dmodelT \times d_{\text{model}}2dmodeldff+dff+dmodel2 d_{\text{model}} d_{\text{ff}} + d_{\text{ff}} + d_{\text{model}} = 4.72M per blockMLP
MLP (SwiGLU, Llama)(SiLU⁡(X^Wg)⊙X^Wu)Wd(\operatorname{SiLU}(\hat X W_g) \odot \hat X W_u) W_dsame3dmodeldff3 d_{\text{model}} d_{\text{ff}}MLP
One blockattention + MLP, each with norm and residualT×dmodelT \times d_{\text{model}}7,087,872the block
Final normnorm⁡(x)\operatorname{norm}(x)T×dmodelT \times d_{\text{model}}2dmodel2 d_{\text{model}}normalisation
Unembeddinglogits=X^WU\text{logits} = \hat X W_UT×VT \times V0 in GPT-2 (tied: WU=WE⊤W_U = W_E^\top); dmodelVd_{\text{model}} V otherwiseunembedding
Next-token probabilitiesp=softmax⁡(logitsT)p = \operatorname{softmax}(\text{logits}_{T})VV0what an LLM does
Training loss−1T∑tlog⁡p(xt+1∣x≤t)-\frac{1}{T}\sum_t \log p(x_{t+1} \mid x_{\le t})scalar0training objective
KV cachestore K,VK, V for every layer2×L×hkv×T×dhead2 \times L \times h_{\text{kv}} \times T \times d_{\text{head}}0 (memory, not weights)KV cache
MoE routertop-⁡k(softmax⁡(x^Wr))\operatorname{top-}k(\operatorname{softmax}(\hat x W_r))EE scores per tokendmodelEd_{\text{model}} Emixture of experts

Totals. GPT-2 small: 38,597,376+786,432+12×7,087,872+1,536=124,439,80838{,}597{,}376 + 786{,}432 + 12 \times 7{,}087{,}872 + 1{,}536 = 124{,}439{,}808. Llama 3 8B: two 128,256×4,096128{,}256 \times 4{,}096 matrices (embedding and unembedding), 32 blocks of 218,112,000 and a final norm: 8,030,261,2488{,}030{,}261{,}248.

GPT-2 small and Llama 3 8B

GPT-2 small (2019)Llama 3 8B (2024)
Parameters124M8.03B
Blocks LL1232
dmodeld_{\text{model}}7684,096
Heads hh / KV heads12 / 1232 / 8 (grouped-query attention)
dheadd_{\text{head}}64128
dffd_{\text{ff}}3,072 (4×)14,336 (3.5×, three matrices)
Vocabulary VV50,257128,256
Context1,0248,192 (128K in Llama 3.1)
Positionslearned, added to the inputRoPE, θ\theta = 500,000
NormalisationLayerNorm, before each sublayerRMSNorm, before each sublayer
MLP activationGELUSwiGLU
Biasesyesno
Embedding and unembeddingtied (shared)separate
Parameters per block7.09M218M
KV cache per token (16-bit)2×12×12×64×22 \times 12 \times 12 \times 64 \times 2 bytes = 36.9 kB2×32×8×128×22 \times 32 \times 8 \times 128 \times 2 bytes = 131 kB
Forward pass per token≈ 2N2N = 0.25 GFLOPs≈ 2N2N = 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 ≈ 12 dmodel212\, d_{\text{model}}^2 for a GPT-style block (4d24 d^2 attention + 8d28 d^2 MLP). GPT-2 small: 12×7682=7.0812 \times 768^2 = 7.08M, against 7.09M exactly.
  • Compute ≈ 2N2N FLOPs per token for a forward pass and 6N6N for training (forward plus backward), plus attention’s T2T^2 term for long contexts. See cost.
  • Memory for weights = NN × bytes per parameter: 8B parameters in 16-bit is 16 GB; see model memory.
  • KV cache = 2×L×hkv×dhead×2 \times L \times h_{\text{kv}} \times d_{\text{head}} \times bytes, per token. It is what limits long context and many users at once.
  • Attention scores grow with T2T^2; everything else grows with TT.

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.