Step 5 of 6

Training dynamics & scaling laws

Learning rate schedules, Adam and its variants, mixed precision, data and model parallelism, and the scaling laws that predict how performance improves with compute.

16 min read

Training a large model is an engineering problem as much as a scientific one. A single GPU can't hold a 70-billion-parameter model, let alone train it. Distributed training, mixed precision, and memory optimization are what make modern AI possible - and understanding them explains why some models cost $100M+ to train.

Gradient descent at scale#

Training is iterative: forward pass → compute loss → backward pass → update weights. The speed of this loop determines how fast you can train, and the memory it requires determines how big a model you can fit.

  1. 01Sample a mini-batch of data
  2. 02Forward pass: compute predictions and loss
  3. 03Backward pass: compute gradients via backpropagation
  4. 04Optimizer step: update all parameters using gradients
  5. 05Repeat for millions of steps

Batch size is a critical hyperparameter. Small batches give noisy gradients (good for generalization, slow for convergence). Large batches give stable gradients (fast convergence, but can overfit and require learning rate warmup).

Mixed precision training#

Modern GPUs have specialized hardware for half-precision (FP16/BF16) arithmetic that runs 2-8× faster than full precision (FP32). Mixed precision training uses half precision for most operations while keeping a master copy of weights in FP32:

PrecisionBitsRangeUse in training
FP3232±3.4 × 10³⁸Master weights, loss scaling
FP1616±65,504Forward/backward pass (with loss scaling)
BF1616±3.4 × 10³⁸Forward/backward pass (no loss scaling needed)

BF16 (bfloat16) has become the standard for LLM training: it has the same exponent range as FP32 (so no overflow issues), just less mantissa precision. This eliminates the need for loss scaling, which FP16 requires to prevent underflow in gradients.

Data parallelism#

The simplest way to use multiple GPUs: each GPU holds a complete copy of the model and processes a different mini-batch. After the backward pass, gradients are averaged across GPUs:

  1. 01Replicate model to all N GPUs
  2. 02Split each batch into N micro-batches, one per GPU
  3. 03Each GPU computes forward + backward on its micro-batch
  4. 04All-reduce: average gradients across all GPUs
  5. 05Each GPU updates its local copy of the weights

The communication cost is the all-reduce operation: every GPU must send and receive the full gradient vector. For a 7B model, that's 28 GB of gradients (in FP32) synchronized every step.

ZeRO (Zero Redundancy Optimizer) from DeepSpeed reduces memory by partitioning optimizer states, gradients, and even parameters across GPUs instead of replicating them:

ZeRO StageWhat's partitionedMemory per GPU
Stage 1Optimizer states~4× reduction
Stage 2+ Gradients~8× reduction
Stage 3+ Parameters~N× reduction (N GPUs)

Model parallelism#

When a model is too large for one GPU (even with ZeRO), split the model itself across GPUs:

Tensor parallelism: split individual layers across GPUs. A matrix multiplication WX can be split column-wise: each GPU computes a portion of the columns and the results are combined. Used within a single node (fast interconnect required).

Pipeline parallelism: assign different layers to different GPUs. GPU 1 runs layers 1-10, GPU 2 runs layers 11-20, etc. Data flows through the pipeline, with micro-batching to keep all GPUs busy:

ParallelismWhat's splitCommunicationBest for
DataBatchesGradient syncScaling to many GPUs
TensorIndividual layersActivations (intra-node)Very large layers
PipelineGroups of layersActivations (inter-node)Very deep models

Modern training combines all three: 3D parallelism. A 1,000-GPU cluster might use 8-way tensor parallelism within each node, 4-way pipeline parallelism across nodes, and data parallelism across groups.

ZeRO / FSDP
Zero Redundancy Optimizer (ZeRO) and its PyTorch equivalent (Fully Sharded Data Parallel) - partition optimizer states, gradients, and parameters across GPUs instead of replicating them.
All-reduce
A communication operation where every GPU sends and receives gradient updates, so all copies stay synchronized - the main communication cost in data parallelism.
Tensor parallelism
Splitting individual matrix multiplications across GPUs within a single machine - each GPU computes part of the layer.
Pipeline parallelism
Assigning different layers to different GPUs - data flows through the pipeline, with micro-batching to keep all GPUs busy.

Gradient accumulation and checkpointing#

Two techniques that trade compute for memory:

Gradient accumulation: instead of one large batch, process several smaller micro-batches and sum the gradients before updating. This simulates a large batch size without the memory cost of holding a large batch in memory.

Gradient checkpointing (activation checkpointing): don't store intermediate activations during the forward pass. Instead, recompute them during the backward pass. This reduces memory from O(layers) to O(√layers) at the cost of one extra forward pass.

Learning rate schedules#

The learning rate is the most important hyperparameter. Modern training uses a schedule that changes the learning rate during training:

PhaseWhat happensWhy
WarmupLinearly increase from 0 to peak LRStabilize early training when loss landscape is steep
DecayCosine decay to ~10% of peakFine-tune with smaller steps as training converges
CooldownOptional final dropSqueeze out last bits of performance

The combination of AdamW optimizer + cosine learning rate schedule + linear warmup has become the near-universal recipe for transformer training.

Scaling laws#

Kaplan et al. (2020) and Chinchilla (Hoffmann et al., 2022) established that model performance follows power laws in three variables:

L(N,D)≈(NcN) ⁣αN+(DcD) ⁣αD+L∞L(N, D) \approx \left(\frac{N_c}{N}\right)^{\!\alpha_N} + \left(\frac{D_c}{D}\right)^{\!\alpha_D} + L_\infty

Scaling law: loss decreases as a power law in model parameters N, dataset size D, and compute budget C.

The Chinchilla scaling law showed that most models were undertrained - the optimal allocation of a fixed compute budget is roughly equal scaling of parameters and tokens. A 70B model should be trained on ~1.4T tokens, not the 300B tokens used for the original GPT-3.

Scaling laws
Empirical power-law relationships showing that model performance improves predictably as you increase parameters, data, and compute - used to plan training budgets.
Compute-optimal training
Allocating a fixed compute budget to balance model size and training data - Chinchilla showed most early LLMs used too many parameters and too little data.
Gradient checkpointing
Recomputes intermediate activations during the backward pass instead of storing them, trading ~30% more compute for 5-10× less memory.
ModelParametersTraining tokensChinchilla-optimal?
GPT-3175B300BUndertrained
Chinchilla70B1.4TYes (by design)
LLaMA7-65B1-1.4TOvertrained (intentionally, for inference efficiency)

Where it shows up in ML#

FSDP is PyTorch's ZeRO

Fully Sharded Data Parallel (FSDP) implements ZeRO-3 natively in PyTorch - shard parameters, gradients, and optimizer states across GPUs.

BF16 is the standard precision

Every major LLM training run (GPT-4, Claude, Gemini, LLaMA) uses BF16 mixed precision. FP16 is falling out of favor due to its limited range.

Chinchilla changed the game

After Chinchilla, the field shifted from making models bigger to training them longer on more data - the LLaMA family is the direct result.

Flash Attention reduces memory

Flash Attention fuses the attention computation to avoid materializing the N×N attention matrix, reducing memory from O(N²) to O(N) with no approximation.

Papers behind this guide

Further reading

Search Data Science After Dark

Search across prompts, models, papers, guides, and library topics.