chapter 07 / llm-pretraining · estimated time 150-180 min

Large Language Models I
Pretraining: Compressing the Entire Internet

AUDIO // Chapter Audio Guide
Chapter Contents
  1. The Pretraining Objective: The Deeper Meaning of Next-Token Prediction
  2. Tokenization: BPE and the Blame It Takes
  3. Scaling Laws: From Kaplan to Chinchilla to "Overtraining"
  4. Interactive Lab: The Scaling Calculator
  5. Data Engineering: Where Do Trillions of Tokens Come From
  6. Distributed Training: Slicing One Model Across Ten Thousand GPUs
  7. Dense vs. MoE: Trade-offs Between Two Ways of Scaling Up
  8. Linear / Hybrid Attention: The Revolt Against O(n²)
  9. Training in Practice: Hyperparameters, Loss Curves, and Black Magic
  10. Chapter Quiz

The Pretraining Objective: The Deeper Meaning of Next-Token Prediction

Formally, pretraining is disappointingly simple—the softmax cross-entropy from Chapter 2, with about 100,000 token classes, maximizing the log-probability of the next token at every position in the corpus:

$$L = -\frac{1}{T}\sum_{t=1}^{T} \log P_\theta\big(x_t \mid x_{<t}\big)$$

The depth lies in "what kind of text appears in the corpus." Predicting "Paris is the ____ of France" requires facts; predicting "therefore x = ____" requires having done the preceding algebra; predicting "the murderer is ____" on the last page of a detective novel requires digesting the whole book's clues. Next-token prediction is a superset of tasks: push the loss low enough and grammar, facts, and reasoning are all squeezed out together—not because the model "wants" to learn them, but because without them the loss won't go down.

An information-theoretic view (the rigorous version of Karpathy's "training is compression"): $L$ measured in nats, converted to bits/token, is exactly the code length for compressing the corpus with arithmetic coding using this model. "Lower loss = better compression = a better predictive model of the world." GPT-3 achieves about 0.7 bits/character, far below gzip's ~2.5—its "understanding" of text can be rigorously measured by compression ratio. This also explains why a 0.01 difference in loss is visibly noticeable in capability: it is 0.01 in exponent space.

Tokenization: BPE and the Blame It Takes

The model doesn't read characters, it reads tokens. The BPE (Byte Pair Encoding) algorithm is as naive as a homework problem: start from 256 bytes, repeatedly find the most frequent adjacent pair in the corpus and merge it into a new token, until the vocabulary reaches the target size (GPT-4 is about 100,000):

Corpus: "low lower lowest"  vocab starts from bytes
Round 1: most frequent pair = (l,o) → merge "lo"     vocab +1
Round 2: most frequent pair = (lo,w) → merge "low"   vocab +1
Round 3: (low,e) → "lowe" …… until |V| = target

Vocabulary size is a three-way trade-off: a large vocab → shorter sequences (saving $O(n^2)$ attention) but a larger embedding matrix and undertrained rare tokens; a small vocab is the reverse. Many of an LLM's "stupid moments" are actually the tokenizer taking the blame: miscounting how many r's are in "strawberry" (it sees the three atoms [st][raw][berry], not 10 letters), unstable arithmetic ("1234" might be split into [12][34], making digit-aligned addition impossible), and Chinese costing 1-2 tokens per character versus ~1.3 tokens per word for English (the same context window holds less Chinese information and the API bills more). Karpathy's blunt claim is worth remembering: "Every quirk of an LLM, traced to its root, is half the tokenizer's fault."

VIDEO 01
Let's build the GPT Tokenizer (writing GPT's tokenizer from scratch)
Andrej Karpathy · Zero to Hero Episode 8 2:13:35
Viewing Guide · Watch and code along (the first hour has the best value)
  • 00:00 A full catalog of the strange behaviors tokenizers cause—see the case studies before learning the anatomy.
  • 14:00 Line-by-line implementation of the BPE merge algorithm—corresponds to this chapter's pseudocode.
  • 57:00 The evolution of the regex splitting rules from GPT-2 → GPT-4 vocabularies—industrial details.

Scaling Laws: From Kaplan to Chinchilla to "Overtraining"

In 2020, Kaplan et al. found that loss follows a power law in parameter count $N$, data size $D$, and compute $C$, holding across seven orders of magnitude:

$$L(N) \propto N^{-0.076}, \qquad L(D) \propto D^{-0.095}, \qquad C \approx 6ND$$

(Where $C \approx 6ND$ comes from: the forward pass is about 2 FLOPs per parameter per token (one multiply, one add), the backward pass is about 2× the forward, totaling 6.) Kaplan's conclusion of "prioritize scaling N" led to the GPT-3 era's "big model, little data" (175B fed only 300B tokens). In 2022, Chinchilla corrected the experimental methodology (retuning the learning rate schedule at each compute point), and the conclusion flipped: for a given compute budget, N and D should be scaled in proportion, with an optimal ratio of about $D^* \approx 20N$. The Chinchilla model with 70B parameters × 1.4T tokens comprehensively beat the 4× larger Gopher—rendering half the field's training plans obsolete on the spot.

Practice after 2023 evolved one step further: deliberate "overtraining." Chinchilla-optimal only cares about "buying loss with training compute" and doesn't account for inference cost—once deployed, a model runs trillions of tokens per day, and a smaller model is far cheaper per token. So LLaMA-3 8B was fed 15T tokens (D/N = 1875, which is 90× the Chinchilla ratio): spend more money during training to get a small model whose capability far exceeds its size, and recoup it during deployment. This shift from "training-optimal → inference-optimal" is the key to understanding the post-2024 model lineage (why small models keep getting stronger). Go to the calculator and compare the D/N ratios of GPT-3 and LLaMA-3 8B yourself.

Interactive Lab: The Scaling Calculator

Required experiments: ① Click the five presets in order and watch the D/N verdict travel from "undertrained" (GPT-3, 1.7:1) to "inference-optimal" (LLaMA-3, 1875:1)—the historical trajectory; ② Pull N up to 1T and set D to 20N—see how many H100s and how many years it takes to train a trillion-parameter model Chinchilla-optimally, and understand why trillion-scale models are almost all MoE; ③ Verify by mental arithmetic at fixed compute: double N, halve D, C stays the same—but the loss differs, which is exactly the question scaling laws are meant to answer.

scaling.calculator(C = 6ND)

Sliders are log-scale · MFU assumed at 40% (real large clusters are 35-45%) · MoE models compute C from active parameters

Data Engineering: Where Do Trillions of Tokens Come From

Using FineWeb (Hugging Face's openly reproduced pretraining data pipeline; the appendix sources have report links) as a template, going from Common Crawl's ~hundreds of PB of raw web pages to 15T clean tokens, every step is an order-of-magnitude cut:

  1. Text extraction: stripping the HTML shell (navigation bars/ads/template noise); extraction quality directly determines everything downstream.
  2. Language identification + quality filtering: rules (punctuation ratio, repeated lines) + model scoring ("does this passage look like a textbook"). Aggressive filtering actually hurts diversity—the quality threshold is a delicate object of tuning.
  3. Deduplication: MinHash fuzzy deduplication. Duplicate data is a major taboo—it wastes compute, amplifies memorization (privacy/copyright risk), and also makes loss artificially low.
  4. Benchmark decontamination: removing benchmark questions from the training set (the trillion-scale version of Chapter 1's discipline); do it poorly and all the scores are fake.
  5. Data mixing: web pages/code/books/papers/multilingual mixed in proportion. The code proportion is a widely acknowledged lever on reasoning ability—even for non-programming tasks, training on code significantly improves the quality of chains of logic.

The key discipline: train essentially only ~1 epoch (when there's enough data, the returns from seeing the same data again drop sharply and memorization worsens). Contrast Chapter 1's classical ML with hundreds of epochs—the main battleground for LLM overfitting is not the epoch count but data repetition and contamination. The exhaustion of high-quality data (the "data wall") is a real problem for 2025-2026, and the responses are: synthetic data (generated by a strong model + filtered), multimodal data, and the RL of Chapter 8 (trading compute for data efficiency).

Distributed Training: Slicing One Model Across Ten Thousand GPUs

70B parameters × (weights 2 bytes + gradients 2 + Adam's two moments 8) ≈ 840GB—a single 80GB GPU can't hold even a fraction. Four knives, distinguished by "what gets sliced":

StrategyWhat gets slicedCost
Data Parallel DPSlice the batch; each GPU holds a full model replicaAll-reduce gradients each step; no memory saved
ZeRO / FSDPShard optimizer states/gradients/weights across GPUs, temporarily gathering when neededTrades communication for memory; the memory cure for DP
Tensor Parallel TPSlice a single matrix multiply (by row/column); several GPUs jointly compute one layerTwo all-reduces per layer; needs NVLink-class bandwidth, generally kept within a node
Pipeline Parallel PPSlice into segments by layer; different GPUs handle different layersPipeline "bubbles" (head and tail idle time), filled with micro-batches

Ten-thousand-GPU training is 3D parallelism: TP within a node (eating NVLink bandwidth) × PP across nodes × overall DP/ZeRO. Plus two standard fixtures: mixed precision (compute in bf16, accumulate in fp32—bf16 has the same exponent bits as fp32, so it doesn't overflow easily, and it has largely retired fp16+loss scaling) and gradient checkpointing (don't store intermediate activations, recompute them in the backward pass—trading 1/3 extra compute for more than half the activation memory, enabled by default in almost all large-model training).

Dense vs. MoE: Trade-offs Between Two Ways of Scaling Up

MoE (Mixture of Experts) replicates each layer's FFN into $E$ "experts," with a router picking the top-$k$ (e.g., 1-2 out of 8) to activate for each token. The effect: parameter count ×E, while the compute per token is almost unchanged. DeepSeek-V3: 671B total parameters, only 37B activated per token—buying the knowledge capacity of 671B with the compute of 37B. But there's no free lunch; the trade-offs need careful accounting:

DenseMoE
Inference computeAll parameters participateOnly the top-k experts are activated; low FLOPs
Memory= parameter countAll experts must be loaded into memory (routing is per-token, and you can't predict which one the next token will use)—compute is saved, memory is not
Training engineeringMature, simpleA notch harder: load-balancing auxiliary loss (to prevent expert collapse—a few experts monopolizing traffic), all-to-all communication for expert parallelism, capacity-factor tuning
Fine-tuningWorks out of the boxRouting easily becomes imbalanced on small data; framework support is uneven
Practical corollary: for resource-constrained vertical scenarios (local deployment, single-machine fine-tuning), prefer dense—MoE's "cheap compute" only pays off when memory is ample and the framework's training support for it is mature; in a unified-memory Mac/single-GPU environment, the cost of cramming all 671B experts into memory far exceeds the entire cost of a 37B dense model. This is even more true on the training side (the complexity of load balancing and communication). The hands-on Chapter 13 will lay out this account with real numbers.

Linear / Hybrid Attention: The Revolt Against O(n²)

The landmine planted in Chapter 6: attention is $O(n^2)$ in sequence length—at 128K context, the attention matrix alone is an astronomical figure. An active line of research tries to compress it to $O(n)$: linear attention / state space models (the Mamba, RWKV, DeltaNet families), whose idea can be crudely summarized as "compress the KV history into a fixed-size recurrent state"—in a sense a return to the RNN (the ghost of Chapter 5) while keeping the Transformer's training parallelism. The retrieval ability ("needle in a haystack") of pure linear models still lags full attention, so the mainstream compromise of 2025-2026 is hybrid architectures: most layers use linear attention, with a full-attention layer inserted every few layers as a backstop (Qwen3.5, MiniMax, etc. all fall into this category).

Paper FLOPs ≠ actual throughput; the hidden cost of new architectures is framework maturity. A real controlled experiment (2026, Apple Silicon + mlx-lm training scenario): on the same machine, same data, same LoRA scale, a classic dense Transformer had 15× the throughput and used 3/4 less memory than a delta-net-family hybrid architecture—the gap was 100% due to the training framework's implementation maturity for the new operators (fused kernels and memory-layout optimizations hadn't caught up), not the architecture's theoretical properties. The engineering lesson: when choosing an architecture, audit "the target framework's support maturity for it"; theoretical complexity is only half the story. From a paper's new architecture to an ecosystem with well-polished kernels usually lags by 1-2 years.

Training in Practice: Hyperparameters, Loss Curves, and Black Magic

A typical LLM pretraining configuration, every item of which you derived in earlier chapters:

optimizer:  AdamW(β₁=0.9, β₂=0.95, weight_decay=0.1)   # Ch.3; β₂ lowered to handle gradient noise
lr:         warmup 2000 steps → cosine decay to 10% of peak  # Ch.1 cure for the SGD noise floor
grad_clip:  1.0                                          # Ch.5 gradient clipping
precision:  bf16 (fp32 master weights)                  # this chapter §6
batch:      ~4M tokens/step (millions)                  # Ch.1 mini-batch in its extreme form

The closing video: Karpathy's 3.5-hour overview ties this chapter (pretraining) and the next (post-training) into a complete picture; after watching it you'll have an engineering-level understanding of "how ChatGPT is made":

VIDEO 02 · The Grand Overview Feast
Deep Dive into LLMs like ChatGPT
Andrej Karpathy 3:31:24
Viewing Guide · Watch the first half for this chapter (pretraining); come back for the second half in Chapter 8
  • 00:00 A hands-on walkthrough of the FineWeb data pipeline—the visualized version of §5.
  • 14:30 Tokenization revisited—reinforcing §2.
  • 31:00 The training process, loss, and "the base model is an internet-document simulator"—what you get when pretraining is done (and what it is not yet).
  • 1:20:00 After this it enters SFT/RLHF—a trailer for Chapter 8.

Chapter Quiz