Papa Programmer
Lesson 09 of 20

Parameters and model size

9 min read

Optional

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.

python
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:

text
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%
Share of GPT-2's parameters in each part, from the count above

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:

python
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 parameterMemory for the weights
32-bit432 GB
16-bit216 GB
8-bit18 GB
4-bit0.54 GB
Memory for the weights of an 8 billion parameter model

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.

Count from the shapeVocabulary, vector length and block count set the parameter count.
Memory is count times bytes8 billion parameters at 16 bits need about 16 GB for the weights alone.
Work grows with sizeEach token costs about 2 operations for each parameter.

Your reading position is saved

0%

0 of 20 complete

Contents

Learning tools