Linear algebra for LLMsPart 6: The rest of the transformer
Count a model
8 min
One classic block:
Multi-head attention has four matrices; a MLP has two ones. Biases and norm gains add only a few thousand per block, small enough to ignore.
Write
The whole model
blocks, plus the embedding table, plus a separate unembedding when the weights aren’t tied. (GPT-2 also has a learned position table, .)
Write
Check it on GPT-2 small
, , tied weights, 1,024 positions. GPT-2 small is known as the 124M model; counting every bias and gain exactly gives 124,439,808.
Llama 3 8B, one layer
and are ; with 8 KV heads of 128, and are (GQA, Part 5). No biases.
Llama 3 8B, the whole model
32 layers, an embedding table and a separate output matrix (not tied), and a final norm: 8.03 billion parameters, which is why it’s called 8B. About 13% of it is the two vocabulary matrices.
Write
FLOPs per token
From Part 2: one multiply-add (2 FLOPs) per weight per token. Llama 3 8B: about 16 GFLOPs per token. Attention adds the and work, each per layer: at that’s GFLOPs more.
Write
✏️ Your turn
Size a model
Estimate with , and the forward FLOPs per token (ignore attention).
Work it out on paper, then go to the next card to check.
Answer: about 1.3B
📌 Recap
Count a model in one card
- Classic block: . Model: (tied).
- GPT-2 small: about 124M. Llama 3 8B: 8,030,261,248, counted matrix by matrix.
- Forward pass: FLOPs per token, plus for attention.
Write