Simple Algorithm
Deep Learning · Activation · 18 min

Deep Learning at Scale

Scaling laws, massive training runs and making giant models fast and useful.

In 2020 GPT-3, a language model with 175 billion parameters, was trained with about 3×10233\times10^{23} arithmetic operations. Its loss landed almost exactly on the straight line traced by far smaller models. That predictability turned building large models into engineering: choose a budget, compute the best model for it, then find a way to run the arithmetic on thousands of chips at once.

What you’ll learn

  • Explain what a scaling law is, and read a power law as a straight line on log–log axes.
  • Estimate the compute of a training run with C≈6NDC\approx6ND, and split a fixed budget between model size and training data.
  • Explain why large models are trained on many GPUs, with 16-bit (or smaller) numbers and several kinds of parallelism.
  • Describe how a pretrained model becomes a useful assistant, and how compression makes it cheaper to run.

Bigger, predictably better

Since about 2018 the most capable language models have come from scaling up one simple recipe: a transformer trained by next-token prediction on an enormous amount of text. Three numbers describe a training run:

  • NN, the number of parameters (weights) in the model;
  • DD, the number of training tokens, the word pieces the model reads;
  • CC, the training compute, counted in FLOPs: floating-point operations, each one addition or multiplication.

In 2020 Jared Kaplan and colleagues trained many language models of different sizes and found that the test loss fell as a power law in each of the three, smoothly, across more than six orders of magnitude, as long as the other two did not hold it back. The exact shape of the network (deep and narrow or shallow and wide) mattered far less than its size. A law like this is called a scaling law.

L(N)=(NcN)αN,αN≈0.076,Nc≈8.8×1013L(N)=\Big(\frac{N_c}{N}\Big)^{\alpha_N},\qquad\alpha_N\approx0.076,\quad N_c\approx8.8\times10^{13}
Kaplan et al.'s fit of the loss LL (in nats per token) against NN, counting the parameters outside the embedding tables, for models trained close to convergence on plenty of data. Take logarithms: log⁡L=αNlog⁡Nc−αNlog⁡N\log L=\alpha_N\log N_c-\alpha_N\log N. On log–log axes a power law is a straight line, here with slope −0.076-0.076.
Predict before you look

Under this fit, each doubling of the model multiplies the loss by 2−0.076≈0.952^{-0.076}\approx0.95, a 5% cut. You make a model 1,0241{,}024 times bigger: ten doublings. What fraction of the original loss is left?

Show the answer

0.59

Ten factors of 0.950.95 give 0.9510≈0.600.95^{10}\approx0.60; exactly, 1024−0.076≈0.591024^{-0.076}\approx0.59. A thousandfold bigger model removes about 40% of the loss: progress is slow but relentless, and the same 5% arrives with every doubling. Real curves cannot fall forever, though. Text has an irreducible loss, the randomness no model can predict, so later fits add a floor EE that the loss approaches.

Counting compute: C ≈ 6ND

How much arithmetic does training take? Almost all of a transformer's work is multiplying activations by weight matrices. In the forward pass each parameter takes part in one multiplication and one addition per token: 2 FLOPs. The backward pass computes two things for every layer, the error sent back to the layer's input and the gradient of its weights, each about as costly as the forward product: 4 FLOPs. In total:

C≈2ND⏟forward+4ND⏟backward=6NDC\approx\underbrace{2ND}_{\text{forward}}+\underbrace{4ND}_{\text{backward}}=6ND
This ignores the attention scores, which add a term proportional to the context length; it is small unless the context is long compared with the model's width (the Representation lesson Neural Scaling Laws counts it).
Predict before you look

How many FLOPs does it take to train a model with one billion parameters on 20 billion tokens? Answer in units of 102010^{20}.

Show the answer

1.2 × 10²⁰

6×109×2×1010=1.2×10206\times10^{9}\times2\times10^{10}=1.2\times10^{20} FLOPs, the figure Hoffmann et al. (2022) list for this model. A chip sustaining 101410^{14} FLOPs per second would need 1.2×1061.2\times10^{6} seconds, about two weeks. GPT-3, with 175175 billion parameters on 300300 billion tokens, needed 6×1.75×1011×3×1011≈3.2×10236\times1.75\times10^{11}\times3\times10^{11}\approx3.2\times10^{23}, about 2,600 times more; its authors report 3.14×10233.14\times10^{23}.

Think first Why does generating text with a trained model cost far less per token than training on it?

Generation needs only the forward pass: about 2N2N FLOPs per token instead of 6N6N, and no gradients or optimizer to store. But training happens once while a deployed model may generate trillions of tokens, so over its lifetime the inference bill can exceed the training bill. That changes how big a model you should train, as the next step shows.

Spending a fixed budget

Suppose your budget is CC FLOPs. Since C≈6NDC\approx6ND, a bigger model leaves fewer tokens to train on. Too small a model runs out of capacity; too large a model is starved of data. Somewhere between lies the compute-optimal size.

In 2022 Jordan Hoffmann and colleagues at DeepMind trained over 400 models, from 70 million to over 16 billion parameters on 5 to 500 billion tokens, and concluded that model size and data should grow in equal proportion: double the model, double the tokens. They tested it head to head. Gopher had 280 billion parameters trained on 300 billion tokens, about one token per parameter. Chinchilla, with the same budget, had 70 billion parameters trained on 1.4 trillion tokens, and beat Gopher on nearly every task they measured. Chinchilla's ratio of 20 tokens per parameter became the rule of thumb.

Predict before you look

Hoffmann et al. put Gopher's training budget at about 5.8×10235.8\times10^{23} FLOPs. At 20 tokens per parameter, C=6N⋅20N=120N2C=6N\cdot20N=120N^2. What model size NN does that budget buy, in billions of parameters?

Show the answer

69 B

N=5.8×1023/120≈6.9×1010N=\sqrt{5.8\times10^{23}/120}\approx6.9\times10^{10}, about 69 billion parameters trained on 20N≈1.420N\approx1.4 trillion tokens. That is Chinchilla. Gopher spent the same compute on a model four times larger that saw a fifth as much data.

Compute-optimal is not cost-optimal. The rule minimizes the training bill. Every token a deployed model generates costs about 2N2N FLOPs, so a smaller model is cheaper forever after. The LLaMA authors noted that although Hoffmann et al. recommend training a 10-billion-parameter model on 200 billion tokens, their 7-billion-parameter model kept improving past 1 trillion tokens, about 140 per parameter (Touvron et al., 2023). As of the mid-2020s, models meant for heavy use are routinely trained far past 20 tokens per parameter: some training compute is wasted, and inference compute is saved.

Where the arithmetic happens

A GPU has thousands of simple arithmetic units, and large models spend most of their time on matrix multiplications, which split into many independent dot products. Special units called tensor cores multiply small blocks of matrices in a single instruction. Feeding them is the hard part: weights and activations live in a few tens of gigabytes of high-bandwidth memory beside the chip, while only a few tens of megabytes of fast on-chip memory sit next to the arithmetic units. Moving data is often slower than computing with it.

Memory also caps what fits on one GPU. Training with the Adam optimizer in mixed precision stores about 16 bytes per parameter: 2 for the 16-bit weights, 2 for their gradients and 12 for a full-precision copy of the weights and Adam's two running averages. A 70-billion-parameter model needs 70×109×16≈1.170\times10^9\times16\approx1.1 terabytes before counting activations, more than a dozen 80 GB GPUs.

To save memory and time, numbers are stored in fewer bits. A floating-point number has a sign, an exponent, which sets its range, and a mantissa, which sets its precision. Standard FP32 uses 32 bits. FP16 uses 16: its largest value is 65,504, its smallest positive value is about 6×10−86\times10^{-8}, and anything much smaller rounds to zero. BF16 keeps FP32's 8 exponent bits, and so its range, but keeps only 7 mantissa bits, about two to three significant digits.

One model, thousands of chips

A training run of 102410^{24} FLOPs would keep a single GPU busy for decades, and the model may not even fit in its memory. Large runs therefore combine several ways of splitting the work:

StrategyWhat each GPU holdsWhat the GPUs exchange
Data parallelisma full copy of the model and its own slice of each batchgradients, averaged after every backward pass by an all-reduce
Sharding (ZeRO, FSDP)a slice of the weights, gradients and optimizer stateeach layer's weights, gathered just before they are needed, and the gradients, summed so that each GPU receives its own slice
Tensor parallelisma slice of every weight matrixpartial results inside every layer, so it stays within one server's fast links
Pipeline parallelisma contiguous block of layers (a stage)activations passed from stage to stage, like an assembly line
Real runs nest these: tensor parallelism inside a server, pipeline stages across servers, and data parallelism across copies of the whole pipeline.

A pipeline has a catch. The last stage cannot start until the first stage has finished its part, and at the end of a step the first stage waits for the gradients to come back. The idle time is the pipeline bubble. Splitting each batch into many micro-batches keeps every stage busy most of the time.

Check your understanding

In plain data-parallel training on 64 GPUs, what do the GPUs exchange after each backward pass?

  1. Their training examples, so that every GPU sees the whole batch
  2. Their gradients, which are averaged so that every copy of the model takes the same step
  3. Nothing: each GPU trains its own model, and the best one is kept
  4. The activations of every layer
Show the answer

B. Their gradients, which are averaged so that every copy of the model takes the same step

Each GPU computes gradients on its own slice of the batch; an all-reduce averages them, so all 64 copies apply the same update and stay identical. The average equals the gradient of the whole batch, which is why data parallelism behaves like training with a 64-times-larger batch.

From raw model to useful assistant

Pretraining produces a base model: it continues text, but it does not reliably follow instructions. Post-training, which uses far less compute, changes its behavior:

  • Instruction tuning: supervised fine-tuning on demonstrations, prompts paired with good responses.
  • Learning from preferences: people compare pairs of responses, a reward model learns to predict their choices, and the model is optimized to score well while staying close to where it started (RLHF; Ouyang et al., 2022). Direct preference optimization learns from the comparisons without a separate reward model.
  • Reinforcement learning for reasoning: on math and code an automatic checker can supply a verifiable reward. Models trained this way come to produce long chains of reasoning before answering, spending more compute at test time.

Then the model has to be served cheaply. Quantization stores weights in 8 or 4 bits instead of 16. Distillation trains a small student to imitate a large teacher's predictions. Pruning removes weights that matter little. Serving systems reuse each sequence's keys and values in a KV cache, batch many users together and, with speculative decoding, let a small model draft several tokens that the large model checks in one pass.

In the Representation lessons you'll take each piece apart:

  • Neural Scaling Laws: power laws in parameters, data and compute, deriving C≈6NDC\approx6ND, Kaplan's and Chinchilla's allocations and why they differed, and scaling beyond language.
  • Hardware, Precision & Parallelism: GPUs and their memory, mixed-precision training, the 16 bytes per parameter, and data, tensor and pipeline parallelism.
  • Fine-Tuning & Aligning Large Models: instruction tuning, policy gradients from REINFORCE to PPO, and learning from preferences.
  • Model Compression & Efficient Inference: quantization, pruning, distillation and fast serving.

The Emergence lessons then reach the research frontier: hyperparameters that transfer across width, where scaling laws come from, 4-bit number formats, the roofline model, reinforcement learning for reasoning and more.

Key takeaways

  • Test loss falls as a power law in parameters NN, data DD and compute CC: a straight line on log–log axes over many orders of magnitude, which lets small runs predict large ones.
  • Training costs about C≈6NDC\approx6ND FLOPs: 2 per parameter per token forward and 4 backward. Generating a token costs about 2N2N.
  • For a fixed budget, grow the model and the data together: Chinchilla's 20 tokens per parameter. Models meant for heavy use are often trained smaller and longer, to be cheaper to serve.
  • Large runs need GPUs with fast memory, 16-bit or lower number formats used with care for range and precision, and data, tensor and pipeline parallelism with sharded memory.
  • Post-training (instruction tuning, preference learning, reinforcement learning with verifiable rewards) turns a base model into an assistant; quantization, distillation and pruning make it cheaper to run.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

The test loss of a family of models follows L∝N−0.076L\propto N^{-0.076}. You plot LL against NN with logarithmic scales on both axes. What do you see?

  1. A curve that drops to zero after a few doublings
  2. A horizontal line, since 0.0760.076 is so small
  3. A straight line sloping down, with slope −0.076-0.076
  4. An exponential decay
Show the answer

C. A straight line sloping down, with slope −0.076-0.076

log⁡L=const−0.076log⁡N\log L=\text{const}-0.076\log N: a power law is a straight line on log–log axes, and its slope is the exponent. A small exponent means a gentle slope, about 5% less loss per doubling, but no flattening within the measured range.

Question 2

You double the number of parameters and also double the number of training tokens. By what factor does the training compute grow?

  1. 2
  2. 4
  3. 6
  4. 12
Show the answer

B. 4

C≈6NDC\approx6ND is proportional to the product NDND, so doubling both multiplies it by 2×2=42\times2=4. That is why compute-optimal scaling grows NN and DD each like the square root of the budget.

Question 3

What did Hoffmann et al. (2022) conclude about spending a fixed compute budget?

  1. Train the largest model that fits in memory, on whatever data is at hand
  2. Grow the model much faster than the data, as Kaplan et al. (2020) had recommended
  3. Keep the number of training tokens fixed and spend all extra compute on parameters
  4. Grow model size and training tokens in equal proportion, about 20 tokens per parameter at Chinchilla's scale
Show the answer

D. Grow model size and training tokens in equal proportion, about 20 tokens per parameter at Chinchilla's scale

Their three estimation methods put the exponents of Nopt∝CaN_{\text{opt}}\propto C^a and Dopt∝CbD_{\text{opt}}\propto C^b both near 0.50.5 (between 0.46 and 0.54), and Chinchilla, 70 billion parameters on 1.4 trillion tokens, beat the four-times-larger Gopher trained with the same compute.

Question 4

Why are many widely used models trained on far more than 20 tokens per parameter?

  1. A smaller model trained longer reaches a given quality with a cheaper model to serve, and serving costs about 2N2N FLOPs per generated token
  2. More tokens per parameter always gives the lowest loss for a fixed training budget
  3. The 20-token rule applies only to images
  4. Training a smaller model longer needs less training compute
Show the answer

A. A smaller model trained longer reaches a given quality with a cheaper model to serve, and serving costs about 2N2N FLOPs per generated token

Compute-optimal training minimizes the training bill only. If the model will generate trillions of tokens, a smaller model that costs somewhat more to train but less to run is cheaper overall.

Question 5

Why do many training runs use BF16 rather than FP16 for their 16-bit numbers?

  1. BF16 is more precise: it keeps more significant digits
  2. BF16 uses fewer bits, so it halves memory relative to FP16
  3. BF16 has FP32's 8-bit exponent, so small gradients and large activations stay in range without loss scaling
  4. GPUs cannot multiply FP16 numbers
Show the answer

C. BF16 has FP32's 8-bit exponent, so small gradients and large activations stay in range without loss scaling

Both formats use 16 bits. FP16 spends 5 bits on the exponent and 10 on the mantissa; BF16 spends 8 and 7. BF16 gives up precision for FP32's range, so values from about 10−3810^{-38} to 103810^{38} survive.

Go deeper

Further reading

  • J. Kaplan et al., "Scaling laws for neural language models," arXiv:2001.08361 (2020).
  • J. Hoffmann et al., "Training compute-optimal large language models," NeurIPS (2022).
  • R. Sutton, "The bitter lesson", essay (2019).
  • S. J. D. Prince, Understanding Deep Learning (MIT Press, 2023), ch. 12 on transformers and large language models, free at udlbook.github.io.