Case Study: Llama 3
A guided reading of one of the most complete public descriptions of a frontier training run. You will see every decision from the previous seven chapters made for real, with the reasons the authors gave, and come away knowing which of those decisions transfer to a model you can train yourself.
Most frontier models are described in a blog post. "The Llama 3 Herd of Models" (Grattafiori et al. 2024) is a 92-page paper that says what was decided and, unusually, why. That makes it the best available textbook example of the whole pretraining pipeline, and the reason this part of the course ends with it.
We will read it as a sequence of decisions, one per course topic: architecture, data, scaling laws, infrastructure, training stages, post-training, evaluation. For each: what did they do, what did they say the reason was, and what would you do at your scale? Every specific number below is as reported in the paper, with the section it comes from; where the paper gives an approximation, so do we.
Have the paper open (arXiv 2407.21783). Section numbers refer to it. This chapter covers the pre-training half in depth (their Section 3) and summarizes post-training (Section 4); the post-training part of the course picks up from there.
The model family and the central bet
The paper describes three dense decoder-only transformers of 8B, 70B, and 405B parameters, each pre-trained and then post-trained into an instruct model, with 128k-token context and support for eight languages. The 405B model is the flagship; the 8B and 70B are, in their words, "the best models at their scale" at release, and they were trained on the same data and recipe so that lessons transfer across sizes.
The authors frame their approach around three levers: data, scale, and managing complexity. On the third one they are explicit: they chose a standard dense architecture with minor changes over a mixture-of-experts model in order to maximize training stability, and a relatively simple post-training procedure over more complex reinforcement-learning algorithms for the same reason. "Managing complexity" is a theme worth carrying through the whole chapter, because it is the one that most transfers to a small run.
The configuration table
Table 3 of the paper (Section 3.2) gives the shapes. Reported values:
| 8B | 70B | 405B | |
|---|---|---|---|
| Layers | 32 | 80 | 126 |
| Model dimension | 4,096 | 8,192 | 16,384 |
| FFN dimension | 14,336 | 28,672 | 53,248 |
| Attention heads | 32 | 64 | 128 |
| Key/value heads | 8 | 8 | 8 |
| Peak learning rate | $3\times10^{-4}$ | $1.5\times10^{-4}$ | $8\times10^{-5}$ |
| Activation | SwiGLU | ||
| Vocabulary size | 128,000 | ||
| Positional embeddings | RoPE, $\theta = 500{,}000$ | ||
Notice three regularities. The head dimension is 128 in every model (dimension divided by heads). The FFN dimension is 3.5× the model dimension throughout, the usual SwiGLU choice that keeps parameter count near the 4× of a classic MLP with its three matrices. And the peak learning rate falls as the model grows, roughly as the inverse square root of the width, which is the pattern the scaling-laws chapter predicts.
Parameters per component, computed from the reported shapes (assuming untied input and output embeddings and a 128,256-entry vocabulary). Watch how the MLP share grows with size while the embedding share shrinks.
Architecture: what they chose and what they refused
Section 3.2 says the architecture is essentially Llama 2's, and lists the changes. Grouped-query attention with 8 key/value heads on all three sizes, for inference speed and a smaller KV cache (the attention chapter). A 128k-token vocabulary, combining 100k tokens from the tiktoken tokenizer with 28k extra tokens for non-English languages; they report this improved compression from 3.17 to 3.94 characters per token on English, so the same compute reads more text. The RoPE base frequency raised to 500,000 to support long context (positional encoding chapter). And an attention mask that stops tokens attending across document boundaries within a packed sequence, which they report had limited effect at 8k but mattered for the long-context stage.
Everything else is the standard recipe of the objectives-and-architecture chapter: pre-norm with RMSNorm, SwiGLU MLPs, no biases, dense attention over every layer.
What they explicitly did not do
No mixture of experts. The paper says they opted for a standard dense transformer rather than MoE "to maximize training stability". At the time, several competitors had gone sparse; Meta chose the option with fewer failure modes when training for months on 16,000 GPUs. No exotic attention variants, no novel optimizers, no learned positional schemes. The architectural bet was to spend the novelty budget on data and scale and keep the model boring.
A 405B run costs tens of millions of dollars of compute and cannot be restarted casually. Every unfamiliar component is a chance for a loss spike at step 800,000 that nobody knows how to diagnose. "Boring architecture" is the engineering decision that lets you take risks elsewhere.
The data pipeline
Section 3.1 is where the effort went. The headline: roughly 15 trillion multilingual tokens, versus 1.8 trillion for Llama 2, with a knowledge cutoff at the end of 2023. The data engineering chapter described the general toolkit; here is how it was applied.
Cleaning and de-duplication
The web pipeline reported in Section 3.1.1 starts with filtering out domains known to contain personal data or adult content, then extracts text with a custom HTML parser tuned to keep math and code, which they say beat generic extractors. De-duplication runs at three levels: URL-level (keep the newest crawl of each page), document-level (MinHash near-duplicate detection across the corpus), and line-level, removing lines that appeared more than six times in buckets of 30 million documents, which strips navigation menus, cookie banners, and boilerplate. They also apply heuristics: duplicated-n-gram ratios to catch logging and error text, "dirty word" counting, and a token-distribution divergence check against the training-corpus distribution to drop outliers.
Quality classifiers
On top of the heuristics, model-based filters. A fastText classifier trained to recognise text that Wikipedia references. A more expensive RoBERTa-based classifier trained on quality labels produced by Llama 2, prompted to judge whether a document met quality requirements. Separate pipelines for code and for reasoning-heavy pages, with classifiers trained to find "math deduction, reasoning in STEM areas, and code interleaved with natural language". The lesson for the small-scale learner: quality classification is cheap relative to training, and the classifiers can be bootstrapped from a previous-generation model.
The mix
Section 3.1.3 reports the final data mix as roughly 50% general-knowledge tokens, 25% mathematical and reasoning tokens, 17% code tokens, and 8% multilingual tokens. Two things stand out. Math and reasoning at a quarter of the mix is high compared to earlier open models. And the mix was not chosen by intuition: they report knowledge classification of the web data (to downsample over-represented categories like entertainment) and scaling-law experiments in which small models were trained on candidate mixes to predict the large model's benchmark performance on each.
Annealing on high-quality data
Section 3.1.3 (annealing data) reports that upsampling small amounts of high-quality code and mathematical data while annealing the learning rate at the end of training boosted the 8B model's GSM8K and MATH scores substantially, with negligible effect on the 405B model. They also turned this into a tool: to judge whether a new data source is valuable, anneal a partially trained 8B model on it for 40 billion tokens with 30% of the mix from the candidate, and see what moves. That is a cheap experiment any lab, or student, can copy.
Scaling laws: choosing 405B and 15.6T
Section 3.2.1 is a compact worked example of the scaling-laws chapter. Given a compute budget of about $3.8\times10^{25}$ FLOPs, how big should the model be and how many tokens should it see?
They report training models from 40M to 16B parameters over compute budgets from $6\times10^{18}$ to $10^{22}$ FLOPs, each with a range of token counts, and fitting the compute-optimal token count as a power law in compute: $A^\star(C) = A\,C^{\alpha}$ with reported fit $(\alpha, A) = (0.53, 0.29)$. Extrapolated to $3.8\times10^{25}$ FLOPs, this gave a compute-optimal model of about 402B parameters on 16.55T tokens. They chose 405B and about 15.6T tokens, and note the flatness of the optimum: modest changes in size cost little, so they leaned toward a slightly larger model for robustness to the extrapolation.
$6 \times 405\times10^{9} \times 15.6\times10^{12} = 3.79\times10^{25}$ FLOPs, which matches the reported budget. The $6ND$ rule is not an approximation they made; it is what made the budget and the model choice consistent with each other.
Predicting the benchmarks in advance
The same section describes the two-step downstream prediction discussed in the evaluation chapter: fit the negative log-likelihood of the correct answer on a benchmark against training FLOPs using the small models, then fit a sigmoid from that log-likelihood to accuracy using the small models plus Llama 2 models, and chain them to predict the 405B model's ARC-Challenge accuracy. They report the prediction was accurate. This is the practice that lets a lab commit tens of millions of dollars with some confidence about the outcome.
Training FLOPs from 6ND, then GPU-days and wall-clock days at a given MFU on H100s (989 TFLOP/s bf16 dense peak). The preset uses the reported 405B, 15.6T tokens, 16,384 GPUs and a 40% MFU in the reported 38–43% band.
Compute and infrastructure
Section 3.3 is the concrete version of the systems chapter. Reported facts: the 405B model was trained on up to 16,384 H100 GPUs (80 GB HBM3, 700 W), in Meta's production clusters, with servers of eight GPUs connected by NVLink; the cluster network was RoCE Ethernet at 400 Gbps per GPU; and a distributed storage system offering 240 PB with sustained throughput of 2 TB/s (7 TB/s peak) absorbed checkpoints, which are trillions of bytes per checkpoint for the 405B model, so that checkpoint writes did not stall training.
4-D parallelism
Section 3.3.2 describes tensor, context, pipeline, and data parallelism stacked in that order from innermost to outermost. For 8k context on 16,384 GPUs the reported degrees are TP 8, CP 1, PP 16, DP 128; for the 128k stage they report TP 8, CP 16, PP 16, DP 8. Data parallelism used FSDP with weights sharded but not resharded after forward, to avoid an extra all-gather during backward. They describe several pipeline-schedule fixes (flexible micro-batch counts per stage, balancing the first and last stages that carry the embedding and output layers, and asynchronous send/receive) that together they report improved 405B throughput by around 10%.
Utilization and reliability
Reported BF16 MFU across their configurations: 38 to 43%, with per-GPU throughput of 380 to 430 TFLOP/s. And the numbers on failures, from Section 3.3.4: over a 54-day snapshot, 466 job interruptions, 419 of them unexpected; about 78% of the unexpected ones attributed to hardware, with GPU faults the largest category at 58.7%; effective training time over 90%; only three interruptions needing significant manual work. They also describe silent data corruption, thermal throttling that varied throughput by 1 to 2% over the day, and power swings of tens of megawatts when thousands of GPUs idled and resumed simultaneously.
People read "38 to 43% MFU" as a low number and assume waste. It is a good number: attention, normalization, communication and pipeline bubbles cannot use tensor cores at full rate, and large dense runs above 50% are essentially unheard of. If your small run is at 30% you are doing well; if it is at 10%, profile.
The staged training recipe
Section 3.4 describes three pretraining stages followed by post-training. The reported numbers, and what changes at each stage, are worth having as a timeline.
Each stage changes something specific: the batch size, the context length, the learning rate, or the data. The bar lengths are proportional to the reported token counts on a log scale.
Initial pre-training
Two details from Section 3.4.1 that matter beyond the numbers. First, the batch-size ramp: starting at 4M tokens and 4k context, then 8M tokens and 8k context, then 16M tokens, each step taken after the model had stabilized. They report very few loss spikes and no need for interventions to correct divergence. Second, they changed the data mix during training based on what evaluations showed, which tells you that the mix is not sacred and that evaluation during training (the evaluation chapter) is an input to decisions, not just a report card.
Long context and annealing
Both stages are described in the advanced objectives chapter; the timeline above carries the reported numbers. The design principle they share is "do the expensive or delicate thing late": long context costs quadratic attention and heavy communication, so it is trained last on a small fraction of tokens; the highest-quality data has most effect when the learning rate is small, so it is concentrated in the final 40M tokens.
Post-training in summary
Section 4 describes turning the base models into Llama 3 Instruct. The reported approach is iterative: six rounds, each of reward modeling on human preference annotations, supervised fine-tuning on a mix of human-written and synthetic examples (much of it generated by the previous round's model and filtered by the reward model, i.e. rejection sampling), and direct preference optimization on preference pairs. They report choosing DPO over on-policy RL such as PPO because it needed less compute and was easier to keep stable, and they averaged models obtained with different data or hyperparameters at each stage. Specific capabilities such as coding, multilinguality, math, long context, tool use, factuality, and steerability each got dedicated data work. The SFT chapter and the preference optimization chapter take these apart.
Evaluation highlights, with caveats
Section 5 reports the pre-trained and post-trained models on the standard benchmarks. A few reported post-trained numbers to anchor the scale: MMLU (5-shot) of about 69 for 8B, 84 for 70B, and 87 for 405B; GSM8K in the mid-80s for 8B and mid-90s for the larger two; HumanEval in the 70s for 8B and around 89 for 405B; GPQA around 51 for 405B. The paper positions the 405B model as comparable to the leading closed models of mid-2024 across these tasks.
The honest caveats, most of which the paper itself raises. The numbers are from Meta's own harness and prompts, so they are not directly comparable with other papers' numbers for the same benchmarks (Section 5.1 discusses this and reports contamination analysis with per-benchmark rates and estimated gains). Several benchmarks are near saturation for the 405B model, so differences at the top are within noise. Human evaluations, which they report as pairwise comparisons against other models, depend heavily on the prompt distribution. And "reported" is the operative word throughout: these are the authors' measurements of their own models.
Lessons for a learner training a small model
You will not train on 16,000 GPUs. So which of these decisions transfer to a 100M-parameter model on one card? More than you would think, because the authors' own smaller experiments are what justified most of the choices.
What transfers down
Data quality over data quantity. The three-level de-duplication and the classifier-based filtering are the highest-leverage part of the recipe, and they are cheap. For a small model with a small corpus, a day spent de-duplicating and filtering beats a day spent on architecture.
Annealing on the best data. The 8B model gained substantially from annealing on high-quality math and code; the 405B did not. The effect is strongest at small scale, which is exactly where you are. Hold back your best data for the end, decay the learning rate to zero over it, and average the last few checkpoints.
A stable, simple architecture. Pre-norm RMSNorm, SwiGLU, RoPE, GQA, no biases, no tricks. It is what code/lumen/gpt2.py and code/lumen/block.py already give you, and it is what a frontier lab chose when stability mattered most.
A careful learning rate. Warmup, a peak scaled down as the model grows, cosine decay to a small floor, and a batch-size ramp early. Llama 3 reports almost no loss spikes at 405B with this recipe. code/lumen/train.py implements the schedule.
Predict before you spend. Run a ladder of tiny models, fit the loss trend, and use annealing experiments to test data sources, before committing your compute to the final run.
What does not transfer
The parallelism strategy, the checkpointing infrastructure, and the failure-handling automation are solutions to problems you do not have at one GPU. Long-context extension to 128k is a large-model concern. And absolute benchmark targets are meaningless at small scale: a 100M model will score at chance on MMLU no matter how well it is trained, so evaluate with loss, bits per byte, and easy log-likelihood benchmarks as the evaluation chapter advises.
Symbols
$N$ = parameters (405B)$D$ = tokens (~15.6T)
$C = 6ND$ = compute (~3.8e25)
$\theta$ = RoPE base (500,000)
MFU = model FLOPs utilization (38–43%)
Fix the budget
Pick $C$; fit $D^\star(C)$ on small models; choose $N, D$ near the (flat) optimum, leaning larger.Build the data
De-dup at URL, document, line level; heuristic then classifier filters; choose the mix by small-model experiments.Train in stages
Short context on the full mix with a batch ramp; extend context late on long documents; anneal on the best data; average checkpoints.Post-train in rounds
Reward model, SFT on rejection-sampled data, DPO; repeat; average models.Practice
Using code/lumen/block.py and code/lumen/gpt2.py, build a model that keeps the Llama 3 proportions at small scale: head dimension 128, FFN dimension 3.5× the model dimension, 4 query heads per KV head, RoPE base 500,000, RMSNorm pre-norm, SwiGLU. Choose the width and depth so that it has about 50M non-embedding parameters, and write down the resulting config in the style of the paper's Table 3.
Solution sketch
Non-embedding parameters per layer ≈ $2d^2 + 2d\cdot d_{kv} + 3\cdot d \cdot 3.5d$. With $d = 512$, 4 heads of 128, 1 KV head: attention ≈ 0.65M, MLP ≈ 2.75M, so about 3.4M per layer and 15 layers gives 51M. The Llama 3 ratio of dimension to layers is about 128 (405B: 16,384 / 126); at this scale a narrower, shallower shape is fine, but keep the head dimension at 128 so the RoPE frequency table is realistic.
Copy Llama 3's data-evaluation trick at toy scale. Train a model on the corpus from code/lumen/data.py until the learning rate is at 30% of peak. Save the checkpoint. Then anneal to zero twice: once on the original mix, once with a candidate data source (for example, arithmetic strings) making up 30% of the mix. Compare held-out loss on the original distribution and on the candidate distribution.
Solution sketch
The candidate anneal should improve loss on the candidate distribution a lot and hurt loss on the original slightly; the size of the hurt tells you whether the source is worth including. Use code/lumen/eval.py for the loss and keep the number of annealing tokens identical between the two runs, as the paper does (40B tokens at their scale).
Key takeaways
- Llama 3's central bet was "manage complexity": a boring dense architecture (GQA, RoPE base 500k, 128k vocab, SwiGLU, RMSNorm), no MoE, and simple post-training, so that effort could go into data and scale.
- The data pipeline (three-level de-duplication, heuristic plus model-based quality filters, a mix chosen by small-model experiments, ~50/25/17/8 general/math/code/multilingual as reported) is where most of the work went.
- Scaling laws fit on 40M–16B models picked ~405B on ~15.6T tokens for ~3.8e25 FLOPs, and a two-step fit predicted benchmark accuracy before training.
- Training ran on up to 16,384 H100s with 4-D parallelism at a reported 38–43% MFU, with hundreds of interruptions in 54 days handled almost entirely by automation.
- The recipe is staged: ~15T tokens at 8k with a batch ramp, ~800B tokens extending to 128k in six steps, a 40M-token anneal on the best data with checkpoint averaging, then six post-training rounds of SFT, rejection sampling, and DPO.
- For a small model, copy the data hygiene, the annealing, the simple architecture, and the learning-rate discipline; skip the cluster engineering and the frontier benchmarks.
Further reading
- Grattafiori et al. (2024). The Llama 3 Herd of Models. The paper this chapter reads; Sections 3 and 4 are the relevant ones for this course.
- Touvron et al. (2023). Llama 2: Open Foundation and Fine-Tuned Chat Models. The previous generation, useful for seeing what changed.
- Touvron et al. (2023). LLaMA: Open and Efficient Foundation Language Models. Where the architecture recipe (pre-norm, SwiGLU, RoPE) was assembled.
- Hoffmann et al. (2022). Training Compute-Optimal Large Language Models (Chinchilla). The scaling-law methodology Llama 3 adapts.
- Ainslie et al. (2023). GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints.
- Rafailov et al. (2023). Direct Preference Optimization. The post-training algorithm they chose for stability.
- DeepSeek-AI (2024). DeepSeek-V3 Technical Report. A contemporaneous report that made the opposite architectural bet (MoE, fp8), good to read side by side.