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.

How to read this chapter

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:

8B70B405B
Layers3280126
Model dimension4,0968,19216,384
FFN dimension14,33628,67253,248
Attention heads3264128
Key/value heads888
Peak learning rate$3\times10^{-4}$$1.5\times10^{-4}$$8\times10^{-5}$
ActivationSwiGLU
Vocabulary size128,000
Positional embeddingsRoPE, $\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.

InteractiveConfig comparisonswitch model size

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.

Intuition

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.

web crawldomain filter extractcustom parser de-dup ×3URL · MinHash doc· line (>6 in 30M) filtersheuristics + fastText+ Llama-2-labelled mix50/25/17/8 Reported mix: ~50% general knowledge, ~25% math and reasoning, ~17% code, ~8% multilingual (Section 3.1.3). Mix chosen with small-model scaling-law experiments, not by hand. Annealing at the end upsamples the highest-quality slices.
Figure 1. The reported web-data pipeline. Every stage is a decision that a smaller run can copy at smaller scale: the de-duplication levels and the idea of using a previous model as a quality labeller cost almost nothing compared to training.

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.

Worked example: the 6ND check

$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.

small models40M–16B params6e18–1e22 FLOPs step 1: power lawFLOPs → NLL of correctanswer on ARC-Challenge step 2: sigmoidNLL → accuracy(fit incl. Llama 2) 405Bpredicted Same two-step logic picks the data mix: train small models on candidate mixes, predict the large model's benchmark scores, choose the mix. Reported compute-optimal fit: D*(C) = 0.29 · C^0.53 → ~402B params on 16.55T tokens at 3.8e25 FLOPs (Section 3.2.1).
Figure 2. The reported two-step downstream prediction. Compute predicts a smooth quantity (negative log-likelihood of the right answer), and a separate fit maps that to the jumpy one (accuracy). The intermediate quantity is what makes extrapolation from small models workable.
InteractiveCompute calculator (preset: Llama 3 405B)adjust N, D, MFU, GPU count

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.

Common confusion

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.

InteractiveTraining-timeline stepperstep through the stages

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%)
STEP 1
Fix the budget
Pick $C$; fit $D^\star(C)$ on small models; choose $N, D$ near the (flat) optimum, leaning larger.
STEP 2
Build the data
De-dup at URL, document, line level; heuristic then classifier filters; choose the mix by small-model experiments.
STEP 3
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.
STEP 4
Post-train in rounds
Reward model, SFT on rejection-sampled data, DPO; repeat; average models.
what transfers to a small run copy these • de-dup ×3, classifier filtering • anneal on best data, average checkpoints • pre-norm, SwiGLU, RoPE, GQA, no tricks • warmup, cosine, batch ramp, scaled LR • small-model ladder before the real run skip these (for now) • 4-D parallelism, FSDP tuning • failure automation, 240 PB storage • 128k context extension • MMLU / GPQA as targets • six post-training rounds
Figure 2. The Llama 3 recipe sorted by what a single-GPU learner should copy now and what only matters at cluster scale. Everything in the left box was validated by the authors on small models first.

Practice

Exercise 1 — a Llama-3-shaped tiny model

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.

Exercise 2 — an annealing experiment

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).

Check yourself
According to the paper, why did Llama 3 use a dense transformer rather than a mixture of experts?
Section 3.2 reports the choice of a standard dense architecture over MoE explicitly for training stability, as part of the "managing complexity" theme.
The reported compute-optimal point for a 3.8e25 FLOP budget was about 402B parameters on 16.55T tokens. Meta chose 405B on about 15.6T. Which statement matches the paper's reasoning?
Section 3.2.1 notes the flatness of the compute-optimal curve near the optimum and the choice to lean toward a slightly larger model for robustness.
Which reported finding most directly supports "hold your best data for the annealing phase" for a small model?
Section 3.1.3 reports the annealing gains concentrated at the smaller size, which is exactly where a learner's model sits.
Over the reported 54-day snapshot, roughly what fraction of unexpected interruptions were attributed to hardware issues, and what was the reported effective training time?
Section 3.3.4 reports 419 unexpected interruptions, about 78% attributed to confirmed or suspected hardware issues, with over 90% effective training time thanks to automated recovery.

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