Models
From random weights to a model
One training step, and what it costs to take millions of them
The lesson Which token comes next followed a prompt through a finished model. This one asks where the 32.8 billion weights came from. They start as random numbers, and a model with random weights writes noise. Pre-training turns it into something that can continue any text, and it does so with one small move repeated millions of times. Show the model some text. Let it guess the next token at every position. Measure how wrong it was. Work out, for every weight, which way to nudge it to be a little less wrong. Nudge. This is the first and by far the most expensive stage of making a model, and it is where nearly everything the model knows is learned.
- Starts as
- 32.8 billion random numbers
- The task
- guess the next token, at every position
- One step
- guess, measure, work backwards, nudge
- Comes out
- a base model: it continues text, nothing more
Read the whole lesson as text
1. The text marks its own homework
Most machine learning needs people to label examples. A language model does not, and that is what lets it learn from a large part of everything ever written. Take any sentence. Hide everything after the first token and ask for the second. Then show two and ask for the third. Every position in every document is a question, and the answer is the token that actually comes next. So the training text is both the exam and the answer sheet. The model reads a window of text, predicts at every position at once, and the real next tokens are set aside to be compared with its guesses.
- A batch of text is read, and the token that follows each position is kept as the right answer.
2. A model that knows nothing
Run the first batch through the model with its random weights. The scores that come out mean nothing, so after softmax every token gets about the same share: one in 151,936. The measure of how wrong the model is, the loss, is minus the natural logarithm of the share it gave the right token. For a share of one in 151,936 that is 11.9. Engineers check this number before anything else. If the first loss is close to the logarithm of the vocabulary size, the model and the data are wired up correctly. If it is far above, something is broken, and no amount of training will fix it.
- With random weights the model spreads its guess evenly, and the loss comes out near 11.9.
3. One number for how wrong
Move on a few thousand steps, to a model that has learned something. For the prompt about France it scores Paris 2, London 1, Rome 0 and banana minus 1, which softmax turns into shares of 64, 24, 9 and 3 percent. The right answer is Paris, so the loss for this position is minus the logarithm of 0.64, which is 0.44. The loss only looks at the share given to the right token. It is near zero when the model was sure and right, and it climbs fast when the model was sure and wrong. Averaged over every position in the batch it gives the one number a training run tries to push down. Raise e to that number and you get perplexity: how many tokens the model is in effect still choosing between.
- The model gave the right token a share of 64 percent, so the loss for this position is 0.44.
4. Which way to nudge
A loss is only useful if it says what to change. For every number in the model there is a slope: how much the loss would move if that number moved a little. All the slopes together are the gradient. At the output the slope has a very plain form. For each token it is the share the model gave it, minus one if it was the right token. Paris got 0.64 and should have had 1, so its slope is minus 0.36: raise this score. London got 0.24 and should have had 0, so its slope is 0.24: lower it. Each wrong token is pushed down in proportion to the share it took. Take one step of size one against those slopes and Paris goes from 64 to 76 percent, and the loss from 0.44 to 0.28.
- The loss hands back one slope per score: push the right token up and every other one down.
5. Working backwards through 64 blocks
The scores are not weights. They were computed from the last block, which was computed from the one before, all the way down to the embedding table. Backpropagation carries the slopes back the same way, one stage at a time, using a rule from school calculus: if one gear turns another twice as fast, and that one turns a third three times as fast, the first turns the third six times as fast. A small example shows the whole idea. Take y = wx + b with x = 2, w = 3 and b = 1, so y is 7, and say the target is 10. The loss is the gap squared, 9. The slope for w is minus 12 and for b minus 6. Move each a hundredth of its slope and the loss falls to 7.29. A model does this for 32.8 billion weights in one backward sweep, for about twice the cost of the forward pass.
- The slopes travel backwards through each block, and every weight on the way gets one of its own.
- To go backwards the model needs what each stage produced on the way forward, so all of it was kept in memory.
6. Every weight moves a little
Now each weight has a slope, and the optimizer decides how far to move it. The plain rule is to step against the slope by a small fixed fraction, the learning rate. On its own that zigzags, because each batch is a noisy sample of the data. So the optimizer keeps a running average of recent slopes for each weight and follows that, which cancels the zigzag. It also keeps a running average of the squared slopes, and divides by its root, so a weight whose slope is always large takes smaller steps and a quiet one takes larger steps. That is Adam. AdamW adds one more thing, a small steady pull of every weight towards zero, applied separately from the slopes. Nearly every large language model is trained with it. The price is two more numbers stored for every weight.
- The optimizer receives a slope for every one of the 32.8 billion weights.
- Every weight is moved a small step, sized by its own history of slopes.
7. How big a step, and how big a batch
The learning rate is not held constant. At the start the weights are random and the slopes are wild, so a full-size step would throw the model around. Training begins with tiny steps and raises them over the first few thousand, then lets them fall slowly along a cosine curve for the rest of the run. LLaMA 2 warmed up for 2,000 steps and decayed to a tenth of its peak. Its peak was 0.0003 for the smaller models and half that for the larger ones: bigger models take smaller steps. The batch is large for the same reason of noise. LLaMA 2 used about 4 million tokens per step, and the largest GPT-3 about 3.2 million. A batch that does not fit in memory is split into smaller ones whose slopes are added up before the weights move, which comes to the same thing.
- The next batch of about four million tokens is read, and the step repeats with a slightly different step size.
8. Sixteen bytes for every weight
The factory serves this model from about 32 GB of GPU memory, one byte per weight. Training it is another matter. The forward pass uses a 2-byte copy of each weight for speed. Small updates would be lost in that precision, so a 4-byte master copy is kept and updated. The slope takes 2 bytes. AdamW's two running averages take 4 bytes each. That is 16 bytes per weight before a single intermediate result is stored. For 32.8 billion weights it comes to 525 GB, more than six GPUs of 80 GB can hold, and the results kept for the backward pass come on top. No single GPU trains a model of this size. The work has to be spread over many, and from that moment the links between them matter as much as the GPUs.
- Training holds 16 bytes for every weight, which for this model is 525 GB before anything else.
9. How much text, how much compute
The last question is how long to keep going. Loss falls in a predictable way as the model, the data and the compute grow, so a few small runs can be extended into a forecast for a large one. Two rules of thumb come out of that work. The compute for a run is about six times the number of weights times the number of training tokens: two parts for the forward pass and four for the backward. And for a fixed compute budget, the best loss comes from about 20 tokens of text per weight. That is the ratio of the Chinchilla model itself, 1.4 trillion tokens for 70 billion weights. Before that rule, models were too big for the text they were given. A model of 70 billion weights trained on 1.4 trillion tokens beat one of 280 billion trained on 300 billion, for about the same compute. Many models are now trained far past 20 on purpose, because training is paid once and every token served is paid forever.
- By the rule of 20 tokens per weight, a model this size calls for about 656 billion tokens of text.
Summary
Pre-training starts from random weights and repeats one step. The model guesses the next token at every position of a batch of text, and the text itself supplies the answers. The loss is minus the logarithm of the share given to the right token, averaged into one number. Backpropagation carries that number back through every block and leaves each weight with a slope. AdamW moves each weight against its slope, by a step that warms up and then decays. Holding all of this takes 16 bytes per weight, so a model that is served from one GPU needs many to be trained. How much text and compute to spend follows from scaling laws: about six operations per weight per token, and about 20 tokens per weight for the best use of a budget.
Next lesson: Teaching a model one more thing