Counting the parameters
Count one block, then multiply.
One block holds 12C² weights. Where are most of them?
With C numbers per token:
| Piece | Weights | Biases and norms | At C = 32 |
|---|---|---|---|
ln1 and ln2 | 4C | 128 | |
attention qkv | C × 3C | 3C | 3,168 |
attention proj | C × C | C | 1,056 |
| MLP up | C × 4C | 4C | 4,224 |
| MLP down | 4C × C | C | 4,128 |
| One block | 12C² | 13C | 12,704 |
The whole model is the two tables, L blocks and the final norm. The token table is V × C, and it counts once, because it is also the head. The position table is T_max × C, where T_max is block_size. The final norm holds a scale and a shift, C numbers each.
For big models, the quick estimate is 12 L C²: the blocks' weights, without the tables, the biases and the norms.
To check your count in code: sum(p.numel() for p in model.parameters()). A shared tensor appears once in parameters(), so the tied table counts once.
That count matches numel() on the practice model and on GPT-2 small exactly. Switch between the two models, and untie the head:
The quick estimate is for big models. At C = 32 the biases and norms are not small next to 12C², so it's 3.5% short of the blocks and the final norm, against 0.14% for GPT-2 small.
Untied, GPT-2 small has 163 million parameters. The GPT-2 paper lists this size as 117M, but counting gives 124M, which is why you'll hear it called GPT-2 124M.
Write n_params(V, T_max, C, L) from the table and the pieces in this step: set block, one block's weights, biases and norms, then return the whole model's count. The last two lines print the practice model and GPT-2 small.
- 1
In
n_params, setblock: one block's weights, biases and norms. - 2
Return the whole model: the token table once, the position table,
Lblocks and the final norm.
Run your code to check it. Output shows here.