23. From tiny to large
Our models have about a quarter of a million weights, the paper's base model 65 million, published models billions. The design barely changes with size; the cost does, and the context length costs more than you might think.
- The GPT-style model 114,496 · decoder onlyCounted from its weights by the lab
- The paper's model 244,800 · encoder–decoderCounted from its weights by the lab
- Transformer base 65 million · encoder–decoderVaswani et al., Attention Is All You Need, Table 3, 2017
- BERT base 110M · encoder onlyDevlin et al., BERT, section 3, October 2018
- GPT-2, smallest 117M · decoder onlyRadford et al., Language Models are Unsupervised Multitask Learners, Table 2, February 2019
- Transformer big 213 million · encoder–decoderVaswani et al., Attention Is All You Need, Table 3, 2017
- GPT-2, largest 1542M · decoder onlyRadford et al., Language Models are Unsupervised Multitask Learners, Table 2, February 2019
- GPT-3 175 billion · decoder onlyBrown et al., Language Models are Few-Shot Learners, abstract, May 2020
The largest bar here is about 1,528,000 times the smallest: every step on the axis multiplies by ten.
Our two models, counted from their weights, against the paper's base and big models (Table 3) and sizes their publishers stated: BERT, GPT-2 and GPT-3, each with its source and date. Each step on the axis is ten times the one before; the bars and the numbers say the same in words.
The idea
A transformer's size is set by a handful of numbers: the layers N, the width d_model, the feed-forward width d_ff, the heads h and the vocabulary. Every weight sits in a matrix whose shape those numbers fix, so the weights can be counted without training anything. Scale them up and the same design holds billions of parameters.
Most of the weights grow with the square of the width: a layer holds four d_model × d_model attention matrices and two feed-forward matrices of d_model × 4·d_model, about 12·d_model² in all. Double the width and each layer holds four times as many weights.
The cost of reading also depends on the context window, the number of tokens the model reads at once. Attention compares every token with every token, so its work grows with the square of the context; the rest of a layer grows only in proportion.
One head computes 32 × 32 = 1,024 attention scores per layer.
attention, growing with n² the rest of the layer, growing with n
A rough count of one layer's multiplications: attention's scores and weighted sums grow with n², the four projections and the feed-forward network with n. Softmax, normalisation and biases are left out.
Slide the context length n. For one head, the attention scores are n × n. For a whole layer, the bars compare attention's work, which grows with n², with the rest of the layer, which grows with n, for our width and for GPT-2's smallest; beside each bar, the context length from which attention takes over.
The paper's design comes in three families, by which halves they keep. Encoder-only models such as BERT read a whole text at once and produce vectors for it, for search and classification; decoder-only models such as GPT-2 write; encoder–decoder models such as the paper's translate one sequence into another.
Encoder only
Reads a whole text at once and turns it into vectors: search, classification, embeddings.
Example: BERT (Devlin et al., 2018).
Decoder only
Writes text one token at a time, reading the prompt and its own answer as one sequence: chat and completion.
Example: GPT-2 (Radford et al., 2019), and our GPT-style model.
Encoder–decoder
Turns one sequence into another: translation, as in the paper.
Example: the paper's Transformer (2017), and our paper model.
The three families side by side: which tokens each attention may look at, drawn as a grid of five tokens, and what each family is used for, with an example whose publisher describes it so.
A worked example: counting and comparing
One encoder layer of ours holds 49,728 weights. With d_model = 64, 12·d_model² = 49,152; the difference is the biases and the LayerNorm gains. The whole paper model holds 244,800.
The paper's base model has 65 million weights, 266 times ours; its big model 213 million. GPT-2's report lists its largest at 1542M, 24 times the paper's base model; the GPT-3 paper gives 175 billion, about 715,000 times ours.
Our GPT-style model reads at most 32 tokens: 1,024 attention scores per head and layer. GPT-2's report gives a context of 1024 tokens: 1,048,576 scores per head, 1,024 times ours. Doubling the context multiplies them by 4.
Counting a layer's multiplications roughly, attention's n² part passes the rest of the layer at n = 6·d_model: 384 tokens for our width of 64, 4,608 for GPT-2's smallest, whose report gives d_model = 768. Below that, the matrices dominate the cost; above it, the context does.
The formula
- \(d\)
- d_model, the width of every vector between sub-layers
- \(d_{ff}\)
- the feed-forward network's inner width, 4·d in the paper and in our models
- \(n\)
- the number of tokens read at once, up to the context window
- \(4\,d^{2}\)
- the attention's four matrices W^Q, W^K, W^V and W^O
- \(2\,n^{2}d\)
- every query dotted with every key, and every weight times a value
Both counts leave out the small parts (biases, LayerNorm, the softmax) and the embedding matrix, which adds vocabulary × d_model weights once.
Why it matters
Every token you send passes through every layer's matrices, so a provider's cost per token grows with the model's size, and a model ten times bigger is roughly ten times the work per token. Long prompts add the n² of attention on top: at short contexts it is a small share, at long ones it is the main cost, which is why long-context models are engineered around it and why sending less context is often the cheapest optimisation.
When a vendor quotes a model's size or its context window, you now know what each number buys: weights for what the model can represent, context for how much of your text it can read at once, and the bill for both.
Where it fails
- Size is not quality. The paper's own Table 3 shows that bigger models are better on its task, but data, training and tuning matter as much; the lab makes no claim about which published model is best.
- Rough counts. The formula ignores biases, normalisation and the softmax, and real systems avoid some of the n² work with engineering the paper does not describe.
- Published numbers only. Many recent models do not state their size; the figure shows only sizes a publisher stated, with the date, and they age.
Exercises
Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.
The paper's base model against ours
How many times as many weights as our paper model does the paper's base model have (65 million)? Round to a whole number.
Scores in a long context
How many attention scores does one head compute over a context of 1024 tokens?
Where attention takes over
With the paper's width, d_model = 512, from how many tokens does attention's n² part outweigh the rest of a layer, by the rough count?
Which family is BERT?
Which family does BERT belong to?
In the paper
Section 6.2 varies the base model's sizes in Table 3, from fewer heads to the big model with 213 million weights, and concludes: “We further observe in rows (C) and (D) that, as expected, bigger models are better, and dropout is very helpful in avoiding over-fitting.”
In the paper: section 6.2