Counting transformer parameters
Derive about 12d² parameters per block (4d² attention + 8d² FFN), so N ≈ 12Ld², plus V·d for embeddings. Check this against a known model size.
Builds on: The decoder-only transformer, end to end
What the animation says
Every learned number
A model's parameter count is every learned number it holds: weights, biases, and norm gains. Give me just three settings: the width, the number of blocks, and the vocabulary size. From those alone, we can predict the total to within about one percent. The trick is a unit: one square of width by width numbers. Call it one d squared. Most pieces cost whole squares. A few are only thin vectors. Tap the two pieces too small to matter.
Attention: four squares
Start with attention. Each head has a query, a key, and a value matrix. One head's matrix is width by head size: a thin slice of a square. Line up all the heads. Head size times the number of heads equals the width, so the slices fill one full square. So queries, keys and values make three squares, whatever the head count. The output matrix adds a fourth. Next, the feed-forward network goes up to four times the width, then back down. How many squares is that?
FFN: eight. Block: twelve
Eight. Going up, width by four times width, is four squares. Coming back down is four more. Newer models use SwiGLU: three matrices, each a bit narrower. They still add up to eight squares. So one block holds four squares in attention plus eight in the feed-forward. Twelve. Stack L blocks, and the total is about twelve, times L, times d squared. Now test the formula. Double the width and keep the depth. What happens to the block parameters?
Width squared, depth linear
Four times. Every square grows on both sides, so width enters squared. Doubling the depth only doubles the count. Depth is linear. Now a real model: GPT-2 small. Width seven hundred sixty-eight, twelve blocks. Width squared is about five hundred ninety thousand. Times twelve, times twelve: about eighty-five million. But GPT-2 small is published at one hundred twenty-four million. What holds the missing forty?
Embeddings close the gap
Token embeddings. Fifty thousand two hundred fifty-seven tokens, each a row of seven hundred sixty-eight numbers. That's about thirty-eight point six million. Positions add one row per slot, for a thousand twenty-four slots: about point eight million. GPT-2 ties its output matrix to this embedding, so it's counted once. Blocks plus embeddings: about one hundred twenty-four point three million. Embeddings alone are almost a third. Now add back every bias and norm vector we ignored. How much does the total grow?
Exact count, then remember
About a tenth of a million. Here's the exact count in code. This line counts attention: four squares plus four bias vectors. This line is the feed-forward: eight squares plus its biases. The norms are just gains and biases. The rough formula lands within a tenth of a percent of the exact official count. Remember: twelve squares per block, times the depth, plus vocabulary times width. Width counts squared, depth counts once, and embeddings matter most in small models.
Almost everything is a d-by-d matrix
The 12Ld² formula
Check: GPT-2 small (124M)
Exact count, biases included
d, L, V, T = 768, 12, 50257, 1024
attn = 4*d*d + 4*d # W_Q,W_K,W_V,W_O + biases
ffn = 8*d*d + 4*d + d # up/down matrices + biases
norms = 2 * 2*d # two LayerNorms: gain+bias
block = attn + ffn + norms
total = L*block + V*d + T*d + 2*d # + final norm
approx = 12*L*d*d + V*d + T*d
print(f"approx = {approx:,}") # 124,318,464
print(f"exact = {total:,}") # 124,439,808
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses