Large Language Models from the Beginning

From a model that predicts the next character by counting to the research frontier: tokens, attention and the Transformer; pretraining, data and scaling; the machines a run needs; post-training, evaluation and serving; agents and the environments they learn in; and what is inside. A real Transformer runs in the page, and every number about compute is worked out in front of you.

31 volumes · 308 plates · 3080 questions · English and Chinese

Part One · The Model
Vol. IWhat a Language Model IsWhat does it mean to predict the next token, and why is that enough to learn so much?
  1. Predicting the Next Token
  2. Models From Counting
  3. The Probability of a Text
  4. Perplexity and Bits
  5. Generating Text
  6. Why Counting Fails
  7. A Neural Language Model
  8. What Prediction Teaches
  9. The Life of a Model
  10. Drills: Language Models
Vol. IITokensWhat does a model actually read?
  1. Why Not Characters or Words
  2. Bytes and Unicode
  3. Byte-Pair Encoding
  4. Encoding With BPE
  5. The Unigram Tokeniser
  6. Choosing a Vocabulary Size
  7. The Cost of a Language
  8. When Tokens Get in the Way
  9. Special Tokens and Templates
  10. Drills: Tokens
Vol. IIILearning MachineryHow does a network learn from a loss, at the scale a language model needs?
  1. Embeddings
  2. Softmax and Cross-Entropy
  3. Backpropagation
  4. From SGD to AdamW
  5. Initialisation
  6. Normalisation
  7. Residual Connections
  8. The MLP
  9. Memorising and Generalising
  10. Drills: Learning Machinery
Vol. IVAttentionHow can each token use the tokens before it?
  1. The Bottleneck
  2. Queries, Keys and Values
  3. Scaled Dot-Product Attention
  4. The Causal Mask
  5. Many Heads
  6. Reading Attention Patterns
  7. What Attention Costs
  8. What Attention Weights Do Not Say
  9. Drills: Attention
Vol. VThe TransformerWhat is in a block, and why is it built that way?
  1. The Transformer Block
  2. The Residual Stream
  3. Pre-Norm and Post-Norm
  4. Decoder-Only, Encoder-Decoder
  5. Counting Parameters
  6. FLOPs per Token
  7. The Output Head
  8. A GPT in the Page
  9. Training It
  10. Drills: The Transformer
Vol. VIPositions and Long ContextHow does a model know where a token is, and how far can it see?
  1. Attention Is Order-Blind
  2. Absolute Positions
  3. Rotary Position Embedding
  4. Linear Biases
  5. Beyond the Training Length
  6. Stretching the Context
  7. Windows and Sinks
  8. Using a Long Context
  9. What a Long Context Costs
  10. Drills: Positions
Vol. VIIModern ArchitecturesWhat has changed in the Transformer since 2017, and why?
  1. Gated MLPs
  2. Fewer Keys and Values
  3. Latent Attention
  4. Mixture of Experts
  5. Keeping Experts Busy
  6. Total and Active Parameters
  7. Small Changes That Stabilise
  8. Beyond Quadratic Attention
  9. Wide or Deep
  10. Drills: Architectures
Part Two · Pretraining
Vol. VIIIThe Pretraining RunWhat happens when a model reads a trillion tokens?
  1. The Objective
  2. Packing Sequences
  3. Reading a Loss Curve
  4. What a Loss Value Means
  5. Abilities Over Training
  6. Emergence, Measured
  7. Memorisation
  8. Where Knowledge Lives
  9. What a Base Model Is
  10. Drills: Pretraining
Vol. IXDataWhat goes into a model, and who decides?
  1. The Web as Corpus
  2. From HTML to Text
  3. Which Language
  4. Deduplication
  5. Quality Filters
  6. Safety and Privacy Filters
  7. Beyond the Web
  8. Data Mixtures
  9. Synthetic Data
  10. Contamination
  11. Data, Consent and Law
  12. Drills: Data
Vol. XScaling LawsHow much better does a model get with more parameters, data and compute?
  1. Power Laws
  2. Model Size and Data
  3. Compute-Optimal Training
  4. Predicting From Small Runs
  5. Training Past Optimal
  6. When Data Runs Out
  7. From Loss to Skills
  8. Where Scaling Laws Break
  9. Drills: Scaling
Vol. XIOptimisation at ScaleHow do you train a model with hundreds of billions of parameters without it diverging?
  1. Learning-Rate Schedules
  2. Batch Size
  3. AdamW at Scale
  4. Gradient Clipping
  5. Loss Spikes
  6. Mixed Precision
  7. Eight-Bit Training
  8. Transferring Hyperparameters
  9. New Optimisers
  10. Drills: Optimisation
Vol. XIIMid-TrainingWhat happens between pretraining on the web and fine-tuning for chat?
  1. What Mid-Training Is
  2. Annealing on Good Data
  3. Upsampling Domains
  4. The Long-Context Phase
  5. Continued Pretraining
  6. Forgetting
  7. Learning From a Larger Model
  8. Averaging and Merging
  9. The Knowledge Cutoff
  10. Drills: Mid-Training
Part Three · Infrastructure
Vol. XIIIHardwareWhat does a training chip actually do, and what limits it?
  1. Accelerators
  2. The Memory Hierarchy
  3. The Roofline
  4. Arithmetic Intensity in a Transformer
  5. Interconnect
  6. Model FLOPs Utilisation
  7. Number Formats in Silicon
  8. Reading a Spec Sheet
  9. Drills: Hardware
Vol. XIVParallelismHow is one model spread over thousands of chips?
  1. Where the Memory Goes
  2. Recomputing Activations
  3. Collective Communication
  4. Data Parallelism
  5. Sharding the States
  6. Splitting Every Layer
  7. Pipelines and Bubbles
  8. Splitting the Sequence
  9. Spreading the Experts
  10. Putting It Together
  11. Drills: Parallelism
Vol. XVRunning a Training ClusterWhat keeps a run of ten thousand GPUs going for months?
  1. Failures at Scale
  2. Checkpointing
  3. Stragglers
  4. Silent Data Corruption
  5. Feeding the Machine
  6. Watching a Run
  7. Cost and Energy
  8. Reading a Training Report
  9. Drills: Clusters
Vol. XVIKernels and EfficiencyWhere does the time inside a step go, and how is it won back?
  1. Profiling a Step
  2. Kernel Fusion
  3. The Online Softmax
  4. FlashAttention
  5. Compilers and Kernel Languages
  6. Low-Precision Matrix Multiply
  7. Predicting a Kernel’s Time
  8. Drills: Kernels
Part Four · Post-Training
Vol. XVIISupervised Fine-TuningHow does a text predictor become an assistant?
  1. From Base to Chat
  2. Chat Templates
  3. Learning Only the Answers
  4. Instruction Data
  5. Quality Over Quantity
  6. LoRA
  7. Fine-Tuning a Quantised Model
  8. What Fine-Tuning Costs
  9. Distilling a Teacher
  10. Drills: Fine-Tuning
Vol. XVIIILearning From PreferencesHow is a model taught what people prefer when no answer is simply correct?
  1. Comparisons, Not Labels
  2. The Bradley–Terry Model
  3. Reward Models
  4. RLHF With PPO
  5. Staying Close
  6. Over-Optimising a Reward
  7. Direct Preference Optimisation
  8. The DPO Family
  9. Feedback From Models
  10. Pleasing Instead of Helping
  11. Drills: Preferences
Vol. XIXReinforcement Learning for ReasoningHow does a model learn to reason from rewards it can check?
  1. Policies, Rewards and Returns
  2. The Policy Gradient
  3. Rewards You Can Check
  4. PPO in Detail
  5. Group-Relative Advantages
  6. Keeping the Good Samples
  7. Thinking Out Loud
  8. Rewarding the Steps
  9. When Exploration Dies
  10. Spending Compute at Test Time
  11. Distilling Reasoning
  12. Drills: RL for Reasoning
Vol. XXSafety TrainingHow is a model trained to be helpful, honest and harmless, and how would we know?
  1. What Alignment Means
  2. Writing the Rules Down
  3. Refusal and Over-Refusal
  4. Training for Robustness
  5. Honesty and Calibration
  6. Hallucination
  7. Specification Gaming
  8. Oversight That Scales
  9. Evaluating Safety
  10. Drills: Safety Training
Part Five · Evaluation and Inference
Vol. XXIEvaluationHow do we know what a model can do?
  1. Benchmarks
  2. Error Bars
  3. One Prompt, Many Scores
  4. Pass@k
  5. Models as Judges
  6. Head-to-Head
  7. Saturation and Leakage
  8. Measuring Real Tasks
  9. Designing an Evaluation
  10. Drills: Evaluation
Vol. XXIIDecodingHow is a probability distribution turned into text?
  1. Greedy and Beam Search
  2. Temperature
  3. Truncation
  4. Repetition and Degeneration
  5. Structured Output
  6. Calibration
  7. Stopping
  8. Drills: Decoding
Vol. XXIIIServingHow does one model answer millions of requests?
  1. Prefill and Decode
  2. The KV Cache
  3. Running Out of Cache
  4. Continuous Batching
  5. Latency or Throughput
  6. Reusing Prefixes
  7. Splitting Prefill From Decode
  8. The Cost of a Token
  9. Drills: Serving
Vol. XXIVMaking Inference CheapHow is the same answer produced with less memory, time and energy?
  1. Quantising Weights
  2. Smarter Quantisation
  3. Quantising the Cache
  4. Speculative Decoding
  5. Drafting Without a Draft Model
  6. Smaller Students
  7. Pruning and Sparsity
  8. Serving a Mixture of Experts
  9. Models on Devices
  10. Drills: Cheap Inference
Part Six · Agents and Environments
Vol. XXVTools and AgentsWhat changes when a model acts instead of only answering?
  1. Calling Tools
  2. The Agent Loop
  3. Retrieval
  4. Managing the Context
  5. Errors Compound
  6. Using a Computer
  7. Many Agents
  8. Agents and Risk
  9. Drills: Agents
Vol. XXVITraining AgentsHow is a model trained to complete long, multi-step tasks?
  1. Trajectories as Data
  2. Learning From Demonstrations
  3. Multi-Turn RL
  4. Which Step Mattered
  5. Training on Actions, Not Observations
  6. Learning From Its Own Attempts
  7. Long Horizons
  8. RL Infrastructure
  9. What Goes Wrong
  10. Drills: Training Agents
Vol. XXVIIRL EnvironmentsWhat makes a good place for a model to learn?
  1. What an Environment Is
  2. Graders and Verifiers
  3. Code Environments
  4. Generating Tasks
  5. Curricula
  6. Reward Hacking
  7. Hardening a Grader
  8. Breadth of Environments
  9. Training and Test Environments
  10. Simulated Users and Tools
  11. Drills: Environments
Part Seven · Inside the Model and the Frontier
Vol. XXVIIIInterpretabilityWhat is going on inside?
  1. The Logit Lens
  2. Linear Probes
  3. Activation Patching
  4. Induction Heads
  5. Superposition
  6. Sparse Autoencoders
  7. Steering
  8. Do Explanations Explain
  9. Drills: Interpretability
Vol. XXIXMultimodal ModelsHow do language models see, hear and speak?
  1. Images as Tokens
  2. Training to See
  3. Audio and Speech
  4. One Model for Everything
  5. Generating Images and Sound
  6. What Pictures Cost
  7. Drills: Multimodal
Vol. XXXThe Research FrontierWhat is the field working on now, and what is still unknown?
  1. Reasoning Models
  2. Scaling Reinforcement Learning
  3. Memory Beyond the Context
  4. After the Transformer
  5. Learning From Itself
  6. Running Out of Data
  7. Doing More With Less
  8. World Models and Embodiment
  9. Open Problems in Safety
  10. What We Do Not Know
  11. Drills: The Frontier
Part Eight · History and Projects
Vol. XXXIHistory and ProjectsHow did the field get here, and can the reader build each stage?
  1. From Shannon to n-grams
  2. Neural Language Models
  3. Attention and the Transformer
  4. The Age of Scale
  5. Assistants and Reasoning
  6. Project: A Tokeniser
  7. Project: Pretrain a Small Model
  8. Project: Plan a Training Run
  9. Project: Align a Small Model
  10. Project: RL With a Verifier
  11. Project: Build an Environment
  12. Project: Serve a Model
  13. The Misreadings Met Again
  14. The Journey Map
Settings: