All courses

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

▶ Watch this lesson Start the whole course

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.

What is inside a model's parameter count?

A model's size is the count of every learned number in its weight matrices, biases and norm gains. Suppose you know only the width $d$, the number of blocks $L$ and the vocabulary size $V$. Can you predict the total to within about 1%? You can, with one short formula. Deriving it also shows which parts of the network hold most of the weights.

Almost everything is a d-by-d matrix

Nearly all parameters sit in matrices that map width $d$ to $d$, or to a fixed multiple of $d$. Each one costs about $d^2$ numbers, so count "$d^2$ units" per block. Attention uses 4: query, key, value and output. The FFN uses 8: up to $4d$ is 4 units, and back down is 4 more. Biases and norm gains are vectors of length about $d$, which is tiny. Embeddings sit outside the blocks, with one row of $d$ per token.

The 12Ld² formula

**Attention:** each of $h$ heads has $W_Q, W_K, W_V \in \mathbb{R}^{d\times d_{head}}$. Since $h\,d_{head}=d$, that totals $3d^2$. Add $W_O \in \mathbb{R}^{d\times d}$ for $4d^2$. **FFN:** $d\times 4d$ plus $4d\times d$ gives $8d^2$. SwiGLU uses 3 matrices with $d_{ff}=8d/3$, which is also $8d^2$. $$N \approx 12\,L\,d^2$$ On top of this come $Vd$ for $W_E$ (twice if $W_U$ is not tied). Each block also has a few bias and norm vectors of length $d$ or $4d$, which are negligible.

Check: GPT-2 small (124M)

Settings: $d=768$, $L=12$, $V=50{,}257$, $T=1024$, with weight tying. - $d^2 = 589{,}824$ - Blocks: $12\cdot 12\cdot 589{,}824 \approx 84.9$M - Token embeddings: $50{,}257\cdot 768 \approx 38.6$M - Position embeddings: $1024\cdot 768 \approx 0.8$M The sum is about $124.3$M, against a published 124M. In a model this small, token embeddings alone are about 31% of the total.

Exact count, biases included

The approximation $12Ld^2 + Vd + Td$ gives 124,318,464. Adding every bias and LayerNorm vector adds only about 0.12M. The exact result matches GPT-2 small's official 124,439,808.
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

Each block holds about $12d^2$ parameters: $4d^2$ in attention and $8d^2$ in the FFN. So $N \approx 12Ld^2$, plus $Vd$ for embeddings. Parameters grow with the square of width but only linearly with depth. Embeddings matter mostly in small models.

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.