Scaling Laws

Dive into Deep Learning · §11.6

Neural Scaling Laws and Compute-Optimal Training (Chinchilla)
N ≈ 12Ld² & C ≈ 6ND · 15-model scaling grid · IsoFLOP contours · Chinchilla optimum · over-training for inference

Why a 10²⁵-FLOP Run Must Be Planned Before It Starts

Motivation

  • Pretraining a frontier language model is a single, non-repeatable capital expenditure spanning thousands of GPUs for months (3.8 \times 10^{25}\text{ FLOPs} for Llama 3 405B).
  • The fundamental allocation question: Given a fixed training compute budget C \approx 6 N D, how should we split C between model parameters N and training tokens D to minimize validation loss \mathcal{L}(N, D)?

Parameter Census: Blocks (12Ld²) vs. Embeddings (Vd)

Parameter Accounting

In an L-block dense Transformer of width d with H_{\text{kv}} = H and SwiGLU (d_{\text{ff}} \approx \frac{8}{3}d), each block holds 4d^2 attention weights and 8d^2 FFN weights:

N_{\mathrm{blocks}} = 12 L d^2, \qquad N_{\mathrm{total}} = 12 L d^2 + V d + \text{norms/pos}

  • Small vocabulary (V = 256 bytes): The embedding table is only 1.4\% of our 4.79\text{M}-parameter model (N_{\text{blocks}} = 12.01\,L d^2).
  • Large BPE vocabulary (V = 50{,}257 in GPT-2 Small): The embedding and position tables hold 39.4\text{M} (31.6\%) of the 124.4\text{M} parameters, while the 12 blocks hold 85.0\text{M} (12.02\,L d^2).

Implementing the Parameter Census

Parameter Accounting

def census(model):
    L, d = len(model.blks), model.num_hiddens
    emb = d2l.num_params(model.token_emb) + (
        d2l.num_params(model.pos_emb) if model.pos == 'learned' else 0)
    total = d2l.num_params(model)
    print(f'embedding {emb / 1e6:6.2f}M ({emb / total:5.1%}),  '
          f'blocks {(total - emb) / 1e6:6.2f}M '
          f'= {(total - emb) / (L * d ** 2):.2f} L d^2,  '
          f'total {total / 1e6:.2f}M')

census(gpt(256))
census(gpt(50257, num_hiddens=768, num_heads=12, num_blks=12,
           pos='learned', norm='layer', act='gelu', bias=True))
embedding   0.07M ( 1.4%),  blocks   4.72M = 12.01 L d^2,  total 4.79M
embedding  39.38M (31.6%),  blocks  85.06M = 12.02 L d^2,  total 124.44M

Why Training Costs Six FLOPs per Parameter and Token

The C ≈ 6ND Law

  • For each linear projection \mathbf{Y} = \mathbf{X}\mathbf{W} with P = d_{\text{in}} d_{\text{out}} parameters across D tokens: C \approx \underbrace{2 N D}_{\text{forward}} + \underbrace{4 N D}_{\text{backward}} = 6 N D
  • Forward (2P FLOPs/token): \mathbf{Y} = \mathbf{X}\mathbf{W}.
  • Backward (4P FLOPs/token): Activation gradient \nabla_{\mathbf{X}}\mathcal{L} = (\nabla_{\mathbf{Y}}\mathcal{L})\mathbf{W}^\top (2P) + weight gradient \nabla_{\mathbf{W}}\mathcal{L} = \mathbf{X}^\top(\nabla_{\mathbf{Y}}\mathcal{L}) (2P).

Accounting for Attention Score Dot Products

The Attention FLOP Term

  • Inside each block, attention also computes \mathbf{Q}\mathbf{K}^\top and \mathbf{A}\mathbf{V} (4Td forward FLOPs/token vs. 24d^2 linear projection FLOPs/token), giving a relative correction of T / (6d) (or T / (12d) with causal tile skipping).
  • Short context (T \ll 6d): Only \approx +8\% in our byte-level GPT (T = 128, d = 256).
  • Long-context crossover (T \ge 6d): Equals projection FLOPs at T = 6d = 24{,}576 tokens in Llama 3 8B (d = 4{,}096).

Verifying C ≈ 6ND Against Exact Hardware Counters

Empirical FLOP Audit

Measuring a full forward + backward step of gpt(256, act='gelu') on batch (64, 128) (D_{\text{step}} = 8{,}192 tokens) with FlopCounterMode confirms the analytical derivation:

forward 8.483e+10 FLOPs, 1.082 x 2ND
step    2.545e+11 FLOPs, 1.082 x 6ND
backward / forward = 2.00

The exact matrix-multiply FLOP count lands at 1.082 \times 6 N D, matching 1 + \frac{4 L T d}{2 N} = 1.082 to three decimal places, and the backward-to-forward ratio is \mathbf{2.00} (4 N D vs. 2 N D).

A 15-Model Empirical Scaling Study on WikiText-103

Scaling Grid Design

To observe neural scaling laws firsthand, we train a 5 \times 3 grid of 15 byte-level GPTs spanning a 43\times range in non-embedding parameters N and three data budgets D:

Experimental Axis Values in Our 15-Run Grid Scope & Purpose
5 Backbone Sizes (d, L, H) (96, 3, 4), (128, 3, 4), (192, 4, 6), (256, 6, 8), (384, 8, 8) N \in [0.33\text{M},\; 14.16\text{M}] non-embedding params (43\times span)
3 Step Budgets (s steps) 500, 1{,}000, 2{,}000 steps (8{,}192 bytes/step) D \in \{4.1\text{M},\; 8.2\text{M},\; 16.4\text{M}\} bytes (\le 6\% of 270\text{M} corpus)
Width-Scaled Peak LR \eta_{\max}(d) = 2 \times 10^{-3} \cdot (256 / d) Optimal AdamW learning rate scales inversely with width d
Matched Cosine Horizon Warmup \max(100, s/10), cosine decays to 0 at step s Every run is evaluated at the end of its own annealed schedule

Pitfall: Fixed Horizons Distort Scaling Exponents

Experimental Methodology Pitfall

  • Hoffmann et al. (2022)’s correction to Kaplan et al. (2020): In a single 2{,}000-step cosine schedule, the learning rate at step 500 is still near its peak (\eta_{500} \approx 0.9\,\eta_{\max}).
  • Systematic small-budget bias: Reading intermediate checkpoints of an un-annealed schedule artificially inflates the loss of short runs, making extra tokens D look less valuable than extra parameters N.

Grid Results: Diminishing Returns in N Alone

Empirical Scaling Surface

  • Increasing N at fixed tokens D: Tripling model size from 0.59\text{M} \to 1.77\text{M} drops loss by 0.05\text{--}0.09\text{ nats}, whereas tripling from 4.72\text{M} \to 14.16\text{M} gains only 0.02\text{--}0.04\text{ nats} because 500\text{--}2{,}000 steps starve the 14.16\text{M} model of data.
  • Increasing D at fixed size N: Quadrupling tokens (4.1\text{M} \to 16.4\text{M}) lowers validation loss by 0.20\text{--}0.27\text{ nats} across every model size.

IsoFLOP Trade-Off: Smaller Models Trained Longer Win

The Pareto Compute Frontier

  • Plotting all 15 runs against total FLOPs C = 6 N D: Where curves for different model sizes overlap in compute C, the smaller model trained on more tokens consistently achieves lower validation loss.
  • Concrete IsoFLOP comparison:
    • N = 14.16\text{M} on D = 4.1\text{M} (C = 3.5 \times 10^{14}): 1.42\text{ nats}
    • N = 4.72\text{M} on D = 8.2\text{M} (C = 2.3 \times 10^{14}): 1.34\text{ nats}
    • N = 1.77\text{M} on D = 16.4\text{M} (C = 1.7 \times 10^{14}): 1.29\text{ nats} (lowest loss at half the FLOPs).

N =  1.77M, D = 16.4M tokens: C = 1.7e+14 FLOPs, validation 1.29
N =  4.72M, D =  8.2M tokens: C = 2.3e+14 FLOPs, validation 1.34
N = 14.16M, D =  4.1M tokens: C = 3.5e+14 FLOPs, validation 1.42

The Chinchilla Parametric Scaling Law

Parametric Loss Surface

  • Hoffmann et al. (2022) decompose validation loss \mathcal{L}(N, D) into three additive terms: \mathcal{L}(N, D) = L_\infty + \frac{A}{N^\alpha} + \frac{B}{D^\beta}
  • Entropy floor (L_\infty \approx 1.7\text{--}1.8): Irreducible entropy of natural language.
  • Model capacity (A / N^\alpha) & Data sample (B / D^\beta): Power-law penalties with \alpha \approx 0.35, \beta \approx 0.37.

Deriving the Compute-Optimal Split N(C) and D(C)

Constrained Optimization

To minimize \mathcal{L}(N, D) subject to a fixed compute budget C = 6 N D, substitute D = C / (6N) into \mathcal{L}(N, C/(6N)) and set the derivative with respect to N to zero:

\frac{d}{dN}\left(L_\infty + A N^{-\alpha} + B (C/6)^{-\beta} N^\beta\right) = -\alpha A N^{-\alpha-1} + \beta B (C/6)^{-\beta} N^{\beta-1} = 0

  • Equating marginal returns (\alpha A (N^*)^{-\alpha} = \beta B (D^*)^{-\beta}): At the compute optimum, the finite-model penalty and finite-data penalty shrink at proportional rates: N^*(C) = G\left(\frac{C}{6}\right)^a, \qquad D^*(C) = G^{-1}\left(\frac{C}{6}\right)^b where a = \frac{\beta}{\alpha + \beta}, b = \frac{\alpha}{\alpha + \beta}, and G = \left(\frac{\alpha A}{\beta B}\right)^{\frac{1}{\alpha + \beta}}.
  • Equal scaling (a \approx b \approx 0.5): Because \alpha \approx \beta, scaling compute C by 100\times scales both N^* and D^* by \sim 10\times.

IsoFLOP Contours and the Compute-Optimal Path

Geometric Intuition

  • IsoFLOP budget lines (dashed, slope -1 on log–log axes): Lines of constant training compute C = 6 N D.
  • Compute-optimal ridge (orange line, slope \approx +1): Connects the tangency points (N^*(C), D^*(C)) where each budget line touches the lowest possible loss contour.
  • The 20-tokens-per-parameter rule: Along the orange path, D^* / N^* \approx 20 tokens per parameter (Chinchilla 70B on 1.4\text{T} tokens).
image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/

IsoFLOP Contours and the Compute-Optimal Path (cont.)

Geometric Intuition

Evaluating the closed-form optimum N^*(C), D^*(C) for two published fits at Chinchilla’s budget C = 5.76 \times 10^{23} FLOPs:

fits = {'Hoffmann et al.': (1.69, 406.4, 410.7, 0.34, 0.28),
        'Besiroglu et al.': (1.82, 482.0, 2085.4, 0.348, 0.366)}

def optimum(C, L_inf, A, B, alpha, beta):
    G = (alpha * A / (beta * B)) ** (1 / (alpha + beta))
    N = G * (C / 6) ** (beta / (alpha + beta))
    return N, C / (6 * N)

for name, fit in fits.items():
    N, D = optimum(5.76e23, *fit)
    print(f'{name:17s} N* = {N / 1e9:3.0f}B, D* = {D / 1e12:.2f}T, '
          f'{D / N:3.0f} tokens per parameter')
Hoffmann et al.   N* =  32B, D* = 2.98T,  93 tokens per parameter
Besiroglu et al.  N* =  72B, D* = 1.33T,  18 tokens per parameter

Reconciling Kaplan (2020) vs. Chinchilla (2022)

Why Exponents Shifted

  • Kaplan et al. (2020): Found N^* \propto C^{0.73}, D^* \propto C^{0.27} by reading intermediate losses from un-annealed LR schedules and omitting embedding parameters V d.
  • Chinchilla (2022) & replications: Matching the cosine decay horizon to each budget s and accounting for total parameters/FLOPs yields N^* \propto C^{0.50}, D^* \propto C^{0.50} (D^* / N^* \approx 20).

Why Deployed Models Train Far Past the Chinchilla Ratio

Inference-Aware Scaling

Look at where recent open-weights models sit relative to their Chinchilla pretraining optimum:

Chinchilla      20 tokens per parameter, 0.96 x optimal size, loss +0.000
Llama 3 8B    1875 tokens per parameter, 0.10 x optimal size, loss +0.054
Llama 3 405B    39 tokens per parameter, 0.65 x optimal size, loss +0.001
OLMo 2 7B      579 tokens per parameter, 0.18 x optimal size, loss +0.037
Gemma 3 27B    519 tokens per parameter, 0.18 x optimal size, loss +0.023
  • Chinchilla optimizes pretraining cost (6 N D) alone: It assigns zero cost to the inference FLOPs (2 N per generated token) and VRAM spent serving the model after release.
  • Shallow loss contours near the optimum: Training Llama 3 8B on 15\text{T} tokens (1{,}875\text{ tokens/param}, 0.10\times the Chinchilla-optimal size for that budget) pays only +0.054\text{ nats} over a Chinchilla-optimal \sim 80\text{B} model while making every inference token \sim 10\times cheaper to generate.

Lifetime Compute: Balancing Pretraining and Serving

Total Cost of Ownership

  • Total lifetime compute across D training tokens and D_{\text{inf}} generated inference tokens is: C_{\mathrm{lifetime}}(N, D) = 6 N D + 2 N D_{\mathrm{inf}}
  • Payback threshold for Llama 3 8B (D = 15\text{T}) vs. an equal-loss 34B Chinchilla model: The extra 5.9 \times 10^{23} upfront training FLOPs save 5.2 \times 10^{10}\text{ FLOPs} per generated token, paying for themselves after D_{\text{inf}} \approx 11.3\text{T} inference tokens.

Concept Check: FLOP Accounting and Budget Allocation

Check Your Understanding

Test your grasp of scaling laws with three calculations:

  1. GPT-2 Small compute & attention correction: GPT-2 Small has N = 124\text{M} total parameters (d = 768) and was trained on roughly D = 10\text{B} tokens at context length T = 1{,}024. Estimate its 6ND training FLOPs and the relative attention score correction T / (6d).
  2. 100× compute budget expansion: If a lab increases its pretraining compute budget C by 100\times (from 10^{23} to 10^{25}\text{ FLOPs}), by what factors should it scale model parameters N^* and training tokens D^* to stay on the Chinchilla compute-optimal path (a \approx b \approx 0.5)?
  3. Activation checkpointing (8ND): Full block-level activation checkpointing recomputes the forward pass (2ND) once during backpropagation (4ND). How does this change the per-token training cost formula?

Answers:

  1. C \approx 6 \times (1.24 \times 10^8) \times 10^{10} \approx \mathbf{7.4 \times 10^{18}\text{ FLOPs}}, and the attention term adds T / (6d) = 1024 / (6 \times 768) \approx \mathbf{22.2\%} on top of the linear projections.
  2. Both N^* and D^* scale by 100^{0.5} = \mathbf{10\times}, keeping the ratio D^* / N^* \approx 20 tokens per parameter invariant.
  3. Total training compute increases by 33\% from C \approx 6 N D to C \approx (2 + 2 + 4) N D = \mathbf{8 N D} in exchange for O(1) per-layer activation memory.

Summary: Parameter, FLOP, and Chinchilla Scaling Laws

Key Takeaways

Law / Quantity Formula Practical Meaning & Rule of Thumb
Non-Embedding Parameters N \approx 12 L d^2 (4 L d^2 attn + 8 L d^2 FFN) Exact to <1\% for dense models; add V d for tied embeddings
Training FLOPs C \approx 6 N D \cdot \bigl(1 + \frac{T}{6d}\bigr) 2ND forward + 4ND backward; attention dominates only when T \gtrsim 6d
Chinchilla Loss Surface \mathcal{L}(N, D) = L_\infty + A N^{-\alpha} + B D^{-\beta} Additive decomposition into entropy floor, capacity error, and data error
Compute-Optimal Split N^*(C) \propto C^{\frac{\beta}{\alpha+\beta}} \approx C^{0.5},\; D^*(C) \propto C^{0.5} Scale N and D equally with \sqrt{C}; maintain D^* / N^* \approx 20\text{ tokens/param}
Inference-Optimal Shift Minimize 6 N D + 2 N D_{\text{inf}} along \mathcal{L}(N,D)=\mathcal{L}_0 Train smaller models to 200\text{--}2{,}000\text{ tokens/param} to slash serving cost