8. Training in Practice
From theory to GPU clusters
The previous article covered what training is — gradient descent, backprop, the loss function. This one is about actually doing it at scale. When you go from a toy model on a laptop to a multi-billion parameter model on hundreds of GPUs, a lot of things break in ways that theory doesn't warn you about. The following are the practical techniques that make large-scale training work.
Getting Data Into the Model
Before a single gradient is computed, you need to turn raw text into something a GPU can crunch.
The tokenizer converts raw Unicode text into integer IDs from a fixed vocabulary (typically 32k–128k tokens). That gives you a single enormous flat list of integers representing your entire training corpus — trillions of tokens for a large model.
You then slice this list into fixed-length sequences (often 2048 or 4096 tokens) and pack them end-to-end. These sequences are shuffled and grouped into batches. A data loader runs on CPU, prefetching the next batch into pinned memory while the GPU is working on the current one, keeping the pipeline saturated.
AdamW: The Optimizer That Actually Works
Vanilla gradient descent updates weights by subtracting a fraction of the gradient. That's it. It works, but it's painfully slow and sensitive to the learning rate.
Adam adds two improvements:
- Momentum — instead of using the raw gradient, you use an exponential moving average of past gradients. This smooths out noisy updates and keeps you moving in a consistent direction.
- Adaptive rates — each parameter gets its own effective learning rate, scaled by how large its recent gradients have been. Parameters with consistently large gradients get dampened; parameters with small gradients get amplified.
AdamW adds weight decay correctly. The original Adam applied weight decay through the gradient (which interacts badly with the adaptive scaling), while AdamW applies it directly to the weights, separate from the gradient update. This turns out to matter a lot for generalization.
In practice, almost every large language model is trained with AdamW. The hyperparameters beta1=0.9, beta2=0.95 (or 0.999), epsilon=1e-8, and weight_decay=0.1 are near-universal starting points.
Learning Rate Schedules
The learning rate is the single most important hyperparameter. Too high and training diverges; too low and it crawls.
The standard recipe for LLMs is linear warmup followed by cosine decay:
Why warmup? At the start of training, the model weights are random. The gradients are large and chaotic — the optimizer hasn't yet built up reliable momentum estimates. If you slam it with a full learning rate immediately, early steps can send weights into bad regions that take thousands of steps to recover from. A linear ramp over 1–2% of total steps lets the optimizer warm up its internal statistics before trusting large updates.
Why cosine decay? As training converges, you want increasingly fine-grained updates. Cosine decay is smooth and ends near zero, which has been found empirically to give slightly better final performance than linear decay or step schedules.
Mixed Precision Training
Modern GPUs have specialized hardware for 16-bit floating point operations that runs roughly 2x faster than 32-bit. Mixed precision training exploits this:
- Store a master copy of weights in FP32.
- Before each forward pass, cast weights to FP16.
- Run the forward pass and compute loss in FP16 — fast matrix multiplies.
- Scale the loss upward by a large constant (e.g. 1024) before backprop, to prevent gradients from underflowing to zero in FP16.
- Run backprop in FP16, then unscale the gradients.
- Apply gradients to the FP32 master weights — full precision update.
The loss scaling step is critical. FP16 has a much smaller range than FP32, and tiny gradients (common in deep networks) quietly vanish to zero without it. The scaler automatically adjusts the scale factor up or down based on whether gradients overflow or underflow.
Gradient Accumulation and Clipping
Gradient accumulation lets you simulate large batch sizes when you can't fit them in memory. Instead of updating weights after every batch, you accumulate gradients over N batches, then do one optimizer step. A model that can only fit 8 samples per GPU can simulate a batch size of 512 this way.
Gradient clipping addresses exploding gradients — moments when a bad batch causes the gradient norm to spike to enormous values, sending weights flying. The fix is simple: before the optimizer step, compute the global norm of all gradients. If it exceeds a threshold (typically 1.0), rescale all gradients proportionally so the norm equals the threshold. The direction is preserved; only the magnitude is clipped.
Gradient clipping sounds like a hack, but it's a standard part of every serious training run. Without it, a single unlucky batch can corrupt a model that took weeks to train.
Distributed Training
A model with billions of parameters doesn't fit on a single GPU, and even if it did, training would be unacceptably slow. The standard approaches:
Data parallelism — the simplest form. Each GPU holds a complete copy of the model and processes a different batch. After backprop, gradients are averaged across all GPUs (via an all-reduce operation) before the optimizer step. This scales throughput linearly with the number of GPUs, as long as the model fits on one.
Model parallelism (tensor parallelism) — split the model itself across GPUs. The weight matrices of each layer are partitioned column-wise or row-wise, with each GPU computing a slice of each matrix multiply. GPUs communicate mid-layer via all-reduce. This is necessary when the model is too large for a single GPU.
Pipeline parallelism — split the model by layers, with each GPU holding a contiguous slice of the network. A batch is split into micro-batches that flow through the pipeline. This reduces the communication overhead compared to tensor parallelism but introduces pipeline "bubbles" where GPUs sit idle waiting for the previous stage to finish.
In practice, large training runs use all three simultaneously — each node handles a different data shard, each GPU in a node holds a tensor-parallel slice, and layers are pipeline-parallel across nodes.
Key takeaways:
- Data pipelines run on CPU, tokenizing and batching to keep the GPU saturated
- AdamW is the standard optimizer: momentum + adaptive rates + correct weight decay
- Always use warmup + cosine decay; coldstarting at full LR causes early instability
- Mixed precision gives 2x speed with FP16 forward/backward and FP32 master weights
- Gradient accumulation fakes larger batches; gradient clipping prevents catastrophic spikes
- Large models combine data, tensor, and pipeline parallelism simultaneously
With the mechanics of training in hand, the next question is: once you have a trained model, how do you actually use it? How does it decide what token to generate next, and how do randomness and temperature control the outputs? That's the subject of the sampling article.