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.
- 01Sample a mini-batch of data
- 02Forward pass: compute predictions and loss
- 03Backward pass: compute gradients via backpropagation
- 04Optimizer step: update all parameters using gradients
- 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:
| Precision | Bits | Range | Use in training |
|---|---|---|---|
| FP32 | 32 | ±3.4 × 10³⁸ | Master weights, loss scaling |
| FP16 | 16 | ±65,504 | Forward/backward pass (with loss scaling) |
| BF16 | 16 | ±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:
- 01Replicate model to all N GPUs
- 02Split each batch into N micro-batches, one per GPU
- 03Each GPU computes forward + backward on its micro-batch
- 04All-reduce: average gradients across all GPUs
- 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 Stage | What's partitioned | Memory per GPU |
|---|---|---|
| Stage 1 | Optimizer 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:
| Parallelism | What's split | Communication | Best for |
|---|---|---|---|
| Data | Batches | Gradient sync | Scaling to many GPUs |
| Tensor | Individual layers | Activations (intra-node) | Very large layers |
| Pipeline | Groups of layers | Activations (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:
| Phase | What happens | Why |
|---|---|---|
| Warmup | Linearly increase from 0 to peak LR | Stabilize early training when loss landscape is steep |
| Decay | Cosine decay to ~10% of peak | Fine-tune with smaller steps as training converges |
| Cooldown | Optional final drop | Squeeze 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:
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.
| Model | Parameters | Training tokens | Chinchilla-optimal? |
|---|---|---|---|
| GPT-3 | 175B | 300B | Undertrained |
| Chinchilla | 70B | 1.4T | Yes (by design) |
| LLaMA | 7-65B | 1-1.4T | Overtrained (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.