Explain a pretraining run end to end
Describe how a base model is produced: data pipeline, shifted-target objective, AdamW with warmup and cosine decay, gradient clipping, mixed precision, batch size in tokens. Diagnose a loss curve, including spikes and plateaus.
Builds on: Deduplication, memorization and contamination, AdamW and decoupled weight decay, Learning-rate warmup and decay schedules, Gradient clipping and loss spikes, Batch size in tokens and gradient accumulation, Mixed-precision training, Reading pretraining loss curves
What the animation says
The factory line
A pretraining run is a factory line: raw text in, base model out. Upstream work happens once: collect text, filter it, remove duplicates and any benchmark questions. Then tokenize with BPE, cut the tokens into rows, and shuffle them. Now the loop. Every step grabs a batch of rows, runs forward and backward, and nudges the weights. It logs the loss and saves checkpoints as it goes. After roughly one pass over the data, the weights that remain are the base model.
One step, precisely
Zoom into one step. Every position predicts the next token. So the targets are the same row, shifted left by one. The loss averages negative log probability of the true next token over all B times T positions. The gradient is clipped: if it's too long, it's scaled down to the threshold. AdamW steps at the scheduled rate, decaying weight matrices only. Matmuls run in bf16; master weights stay in fp32. Your turn: at the word 'cat', tap the token it's scored against.
Planning a 100B-token run
Plan a real run: a hundred billion tokens, rows of 2048, 512 rows per step. That's about one million tokens per step, so roughly 95,400 steps for one pass. Only 32 rows fit in memory, so each step accumulates 16 microbatches. Warmup climbs for 2,000 steps to a peak of three times ten to the minus four, then a cosine glides down to a tenth of that. Halfway down, cosine is zero, so the rate sits at the midpoint: 1.65 times ten to the minus four. The vocabulary has 50,000 tokens. What should the very first logged loss be?
Where the curve starts
Answer: about 10.8. An untrained model spreads its probability evenly, so each token costs the natural log of fifty thousand. So a healthy curve starts near ln V and falls fast. Stuck at ln V means nothing is learning: a zero learning rate, frozen weights, or a bug. Far too low, near zero nats, is bad news. Real language is never that predictable. It means the targets leak into the inputs: labels weren't shifted, or the causal mask lets positions see the future.
Spikes, NaN, val gap, late drop
Later in the run, other shapes appear. A brief spike that recovers is usually harmless; clipping kept those updates small. Repeated or unrecovered spikes: rewind to a checkpoint, skip those batches, lower the peak rate. NaN means something overflowed, like an fp16 activation past 65,504. Rewind and fix that operation. Validation far above training means memorizing, usually heavily duplicated documents. And a slow plateau, then a late drop? That's cosine decay working. Tap the run that needs a rewind.
A factory line, run about once over the data
One training step, precisely
Planning a 100B-token run
Diagnosing the curve: symptom → cause
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses