Parameters and model size
9 min read
Counting GPT-2's parameters
The parameters are also called the model's Model weightsThe numbers inside a model. Training sets them, and everything the model knows is stored in them.. Every table in the model adds to the count, and the size of each table follows from four numbers: the vocabulary size, the longest text, the vector length and the number of blocks.
The program below counts the parameters of GPT-2's smallest version from those four numbers.
vocab, context, d, layers = 50257, 1024, 768, 12
embeddings = vocab * d + context * d
attention = (d * 3 * d + 3 * d) + (d * d + d)
feed_forward = (d * 4 * d + 4 * d) + (4 * d * d + d)
norms = 2 * (2 * d)
total = embeddings + layers * (attention + feed_forward + norms) + 2 * d
print(f"total: {total:,}")
for name, count in [("embeddings", embeddings), ("attention", layers * attention), ("feed-forward", layers * feed_forward)]:
print(f"{name:13} {count / total:.0%}")It prints:
total: 124,439,808
embeddings 32%
attention 23%
feed-forward 46%The total matches the 124,439,808 parameters that the published GPT-2 model reports. The + 3 * d and + d terms are biases, a small extra number for each output.
- Feed-forward46%
- Embeddings32%
- Attention23%
In a small model, the embedding tables take a large share. In a large model, the blocks take almost all of it, because the block count and the vector length grow while the vocabulary stays about the same.
Memory for the weights
Each parameter is stored as a number, and the format sets its size in bytes. Training usually stores 32-bit or 16-bit numbers. For running a model, the weights can be squeezed to 8 or 4 bits with a small loss of quality. This squeezing is called quantisation.
The memory for the weights is the parameter count times the bytes per parameter. For a model with 8 billion parameters:
for name, size in [("32-bit", 4), ("16-bit", 2), ("8-bit", 1), ("4-bit", 0.5)]:
print(f"8B model, {name}: {8e9 * size / 1e9:.0f} GB")| Bytes per parameter | Memory for the weights | |
|---|---|---|
| 32-bit | 4 | 32 GB |
| 16-bit | 2 | 16 GB |
| 8-bit | 1 | 8 GB |
| 4-bit | 0.5 | 4 GB |
The weights are not the whole bill. Running a model also needs memory for the stored keys and values of the text so far. Lesson 10 shows how that part grows with the length of the text.
Work for each token
A common estimate from OpenAI's 2020 scaling paper by Kaplan and others is that a model does about 2 operations for each parameter to produce one token. A model with 8 billion parameters does about 16 billion operations for every token it reads or writes. That is why a bigger model is slower and costs more for each token.
Total and active parameters
Some models switch on only part of their parameters for each token. These are called mixture-of-experts models. The feed-forward step is split into several expert networks, and a small router picks a few experts for each token. Mistral's Mixtral 8x7B has 46.7 billion parameters in total, but uses about 12.9 billion for each token. It needs memory for all 46.7 billion, while the work for each token is closer to that of a 13 billion parameter model.
Bigger is not always better
More parameters let a model store more patterns, and Scaling lawsMeasured rules that predict how a model's loss falls as you add parameters, training tokens and compute. They are used to plan how big a model should be for a given budget. show that loss keeps falling as models grow. Size is still not the only factor. The amount and quality of training text matter as much, and a smaller model trained on more text can beat a larger one. Lesson 15 covers this trade-off, and Lesson 20 shows how to pick a size for your task.
How much memory do the weights need?
A model has 7 billion parameters, stored at 16 bits each.
16 bits is 2 bytes. 7 billion parameters times 2 bytes is about 14 GB, before any memory for the cache.
Which model does less work for each token?
Model A has 46.7 billion parameters and uses 12.9 billion for each token. Model B has 30 billion parameters and uses all of them.
Work for each token follows the parameters in use. Model A is a mixture of experts and uses 12.9 billion, fewer than B's 30 billion.
What does 4-bit quantisation change?
You load an 8 billion parameter model at 4 bits instead of 16 bits.
Quantisation stores each parameter in fewer bits. 8 billion parameters drop from about 16 GB to about 4 GB.
Your reading position is saved
0 of 20 complete
Lesson complete
Parameters and model size
Up next