PJ's Guide to Large Language Models

Chapter 3: Pre-Training Objectives, Optimization & Neural Scaling Laws

3.1 Autoregressive Likelihood & Perplexity

Pre-training fits parameters θ by maximizing the factorized log-likelihood over a multi-trillion-token corpus &calig; via the chain rule of probability. Equivalently, we minimize the expected token-level cross-entropy loss (equivalent to KL divergence from the empirical data distribution up to the irreducible entropy H(pdata)):

PJ investigating high-entropy peanut butter distributions
Fig 3.1 — Over-Training Regime
C ≈ 6 N D  FLOPs

Prof. PJ empirically verifying that 15T tokens of crunchy peanut butter significantly lowers downstream perplexity.

ℒ(θ) = − 1S ∑t=1S log pθ(xt | x<t),     PPL(θ) = exp(ℒ(θ))
(3.1)

3.2 AdamW, Decoupled Weight Decay & Precision Stability

Standard L2 regularization coupled inside Adam's adaptive denominator distorts effective regularization across parameters with differing gradient magnitudes. AdamW (Loshchilov & Hutter, 2019) decouples weight decay λ directly from the momentum and second-moment variance estimates (mt, vt):

mt = β1mt−1 + (1−β1)gt,    vt = β2vt−1 + (1−β2)gt2,    θt = (1 − ηtλ)θt−1 − ηt m̂t√v̂t + ε
(3.2)

Large-scale runs typically set β1 = 0.9, β2 = 0.95 (faster adaptation to sharp loss spikes than 0.999), global gradient norm clipping ||g||2 ≤ 1.0, and WSD (Warmup–Stable–Decay) or cosine learning rate schedules. Computations execute in BF16 (8 exponent bits matching FP32 dynamic range, eliminating FP16 loss-scaling underflow) or FP8 (E4M3/E5M2) with FP32 master weights and optimizer states.

3.3 Chinchilla Compute-Optimal Scaling Laws (Lagrange Derivation)

Empirical scaling analyses (Kaplan et al., 2020; Hoffmann et al., 2022 “Chinchilla”) model pre-training cross-entropy loss as a parametric power law over non-embedding parameter count N and training tokens D, where E ≈ 1.69 nats represents the irreducible Shannon entropy of natural text:

L(N, D) = E + ANα + BDβ,     subject to   C ≈ 6 N D
(3.3)

Here the factor 6 arises from 2 FLOPs per multiply-accumulate (forward pass: ~2ND) plus 4 FLOPs per parameter-token in the backward pass (input gradients + weight gradients: ~4ND). Forming the Lagrangian Λ(N, D, μ) = L(N, D) + μ(6ND − C) and setting ∂Λ/∂N = ∂Λ/∂D = 0 yields αA N−α = βB D−β, which solves in closed form to:

Nopt(C) = G (C/6)a,    Dopt(C) = G−1 (C/6)b,    where   a = βα+β ≈ 0.50,   b = αα+β ≈ 0.50
(3.4)

With Hoffmann et al.’s empirical fits (α ≈ 0.34, β ≈ 0.28), compute-optimal training scales N and D roughly equally (~20 tokens per parameter). Modern inference-optimal over-training (e.g., Llama-3 8B trained on 15T tokens, ~1,875 tokens/param!) intentionally departs right of the Chinchilla frontier: spending extra one-time training FLOPs to pack frontier intelligence into smaller weights drastically reduces lifetime inference serving costs.

Model Parameters N (log scale) Validation Loss L C = 10^21 FLOPs C = 10^23 FLOPs C = 10^25 FLOPs Chinchilla Compute-Optimal Frontier (D ≈ 20N)

Fig 3.2 — IsoFLOP validation loss valleys across parameter budgets; minima trace the compute-optimal scaling envelope.

3.4 Distributed 3D Parallelism & Memory Math

Training a 70B–405B parameter model in mixed-precision AdamW requires 16 bytes per parameter for model state alone (2B BF16 weights + 2B BF16 gradients + 4B FP32 master weights + 8B FP32 Adam moments m, v → ~1.12 TB for 70B before activations!). Clusters therefore compose three orthogonal parallelism axes:

Prof. PJ's Bench Note: Pipeline bubbles are completely unacceptable. When I request a belly rub across four human limbs, I expect zero idle cycles and full NVLink-bandwidth scratching.
« Previous: Ch. 2 — Attention & FlashAttention [ Table of Contents ] Next: Ch. 4 — Post-Training, RLHF, DPO & GRPO »