Skip to content

B.7 Pass 5: The Transformer

This is the pass where the course reaches the architecture behind every modern language model. It builds the 2017 transformer from its parts (scaled dot-product attention, masks, multi-head attention, positional encodings) and trains it to add three-digit numbers exactly, then shows why pre-LN trains where post-LN does not. It then applies one architecture to several pretraining objectives: GPT’s next-token prediction, BERT’s masked tokens, and ELECTRA’s replaced-token detection, with LoRA fine-tuning and an evaluation harness that puts every model the course has trained on one report.

Concepts from other sections (40)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

The last third of the pass covers what changed between 2017 and a Llama-family model: RMSNorm, gated MLPs, RoPE and context extension, grouped-query and multi-head latent attention, sliding windows and sinks, and mixture of experts. It ends with a Llama-family model that loads SmolLM2-135M exactly as Hugging Face saves it and matches Hugging Face’s logits and greedy continuation token for token. From here on, the engine can serve real weights.

Learning objectives

  • Derive scaled dot-product attention’s forward and backward passes and explain each mask and positional encoding.
  • Explain why pre-LN trains without warmup where post-LN does not.
  • Explain the GPT, BERT, and ELECTRA objectives and how LoRA adapts a model through under 5% of its parameters.
  • Count a model’s parameters, FLOPs, and KV-cache bytes.
  • Explain each component of the modern decoder block and how a published Llama checkpoint is loaded and verified.

This pass needs Pass 4, whose attention, beam search, and sequence metrics the transformer reuses.

The pass opens with the statistics used to compare models, then builds the transformer, its objectives, and the modern block:

  1. Definite Integrals: Trapezoid and Simpson. Afterwards you can explain the error orders of the trapezoid and Simpson rules.
  2. Hypothesis tests: paired and two-sample permutation, McNemar, Holm. Afterwards you can explain how paired and two-sample permutation tests, McNemar, and Holm’s correction decide significance.
  3. Logistic regression (IRLS), ROC-AUC, calibration (ECE). Afterwards you can explain how IRLS fits logistic regression and how ROC-AUC and ECE measure ranking and calibration.
  4. Probability problem set, part d: hypothesis tests, regression, queueing, balls into bins. Afterwards you can work every problem in the set and explain each answer.
  5. Scaled dot-product attention (forward and backward). Afterwards you can derive scaled dot-product attention’s forward and backward and explain the 1/sqrt(d) scale.
  6. Masks: causal, padding, sliding window, additive. Afterwards you can explain how causal, padding, and sliding-window masks combine as additive masks.
  7. Multi-head attention. Afterwards you can explain how multi-head attention splits, attends, and merges heads.
  8. Positional encodings: sinusoidal, learned. Afterwards you can explain how sinusoidal and learned positional encodings inject position.
  9. Encoder-decoder Transformer: post-LN and pre-LN, Noam, label smoothing. Afterwards you can explain why pre-LN trains where post-LN needs warmup, and how the Noam schedule and label smoothing are applied.
  10. GPT: the decoder-only language model. Afterwards you can explain the decoder-only GPT, its shifted causal LM loss, and how GPT-2 weights are mapped in.
  11. BERT: the bidirectional encoder and masked language modeling. Afterwards you can explain the BERT encoder and the 80/10/10 MLM masking rule.
  12. ELECTRA: replaced-token detection. Afterwards you can explain how ELECTRA’s generator and discriminator are trained for replaced-token detection.
  13. LoRA: low-rank adaptation with PiSSA initialization and merging. Afterwards you can explain how LoRA adds a low-rank update, how PiSSA initializes it from the SVD, and how it merges back.
  14. Fine-tuning heads and the linear-head export. Afterwards you can explain how fine-tuning heads attach to the backbone and what the linear-head export writes.
  15. The evaluation harness and the model zoo. Afterwards you can explain how the evaluation harness scores the models in the zoo.
  16. Counting: parameters, FLOPs, KV bytes, and memory plans. Afterwards you can count a model’s parameters, FLOPs, and KV-cache bytes by hand.
  17. Pre-LN and RMSNorm. Afterwards you can explain how RMSNorm differs from LayerNorm and why the modern block is pre-LN.
  18. Gated MLPs: SwiGLU and GeGLU. Afterwards you can explain how SwiGLU and GeGLU gate the MLP and how the hidden size is chosen.
  19. Rotary Position Embeddings. Afterwards you can explain how RoPE rotates query and key pairs in the half and interleaved layouts, and how partial rotary works.
  20. Context Extension: PI, NTK, YaRN, Llama 3 Scaling, and ALiBi. Afterwards you can explain how PI, NTK, YaRN, and Llama-3 scaling stretch RoPE to longer contexts.
  21. Grouped-Query Attention with a Cache Hook, Windows, and Sinks. Afterwards you can explain how MQA and GQA share key-value heads and how the cache hook, window, and learned sinks plug in.
  22. Multi-Head Latent Attention and Weight Absorption. Afterwards you can explain how multi-head latent attention compresses the KV cache and how weight absorption avoids decompressing it.
  23. Sliding Windows and Attention Sinks. Afterwards you can explain how a sliding window bounds attention and why StreamingLLM keeps sink tokens.
  24. Mixture of Experts. Afterwards you can explain how MoE routes tokens to experts, dispatches them in sorted order, and balances load with the Switch aux loss or the aux-free bias.
  25. The Llama-Family Model: Config, Weight Loading, and Download. Afterwards you can trace how the Llama-family model reads an HF config, loads weights, and matches Hugging Face logits.
  26. Oracles, golden and differential tests, gradcheck as a test. Afterwards you can explain how oracles, golden and differential tests, and gradcheck verify numerical code.
  27. What the System Does After Pass 5. Afterwards you can explain why pre-LN trains where post-LN does not, and how the modern decoder loads SmolLM2-135M and matches Hugging Face.

One optional section covers the third classic pretraining objective:

  1. T5: span corruption and relative position buckets. Afterwards you can explain T5’s span corruption and how relative position buckets are computed.

The stages are also in this pass’s manifest, path.tsv. Pass 6 makes this model fast to run without changing a single output token.

Generated from path.tsv.

StageReadDone when
1Definite integrals, trapezoid, Simpson
7.4 Definite Integrals: Trapezoid and Simpson
You can explain the error orders of the trapezoid and Simpson rules (M01.4).
2Hypothesis tests: paired and two-sample permutation, McNemar, Holm
13.6 Hypothesis tests: paired and two-sample permutation, McNemar, Holm
You can explain how paired and two-sample permutation tests, McNemar, and Holm’s correction decide significance (M07.5).
3Logistic regression (IRLS), ROC-AUC, calibration (ECE)
13.8 Logistic regression (IRLS), ROC-AUC, calibration (ECE)
You can explain how IRLS fits logistic regression and how ROC-AUC and ECE measure ranking and calibration (M07.7).
4Solve set: hypothesis tests, regression, queueing and Little’s law, balls into bins
13.12 Probability problem set, part d: hypothesis tests, regression, queueing, balls into bins
You can work every S-M07d problem and explain each answer.
5Scaled dot-product attention (forward and backward)
11.1 Scaled dot-product attention (forward and backward)
You can derive scaled dot-product attention’s forward and backward and explain the 1/sqrt(d) scale (L5.1).
6Masks: causal, padding, sliding window, additive
11.2 Masks: causal, padding, sliding window, additive
You can explain how causal, padding, and sliding-window masks combine as additive masks (L5.2).
7Multi-head attention
11.3 Multi-head attention
You can explain how multi-head attention splits, attends, and merges heads (L5.3).
8Positional encodings: sinusoidal, learned
11.4 Positional encodings: sinusoidal, learned
You can explain how sinusoidal and learned positional encodings inject position (L5.4).
9Encoder-decoder Transformer: post-LN and pre-LN, Noam, label smoothing
11.5 Encoder-decoder Transformer: post-LN and pre-LN, Noam, label smoothing
You can explain why pre-LN trains where post-LN needs warmup, and how the Noam schedule and label smoothing are applied (L5.5).
10GPT decoder-only, causal LM loss, GPT-2 weight loading
12.1 GPT: the decoder-only language model
You can explain the decoder-only GPT, its shifted causal LM loss, and how GPT-2 weights are mapped in (L6.1).
11BERT encoder and MLM masking
12.2 BERT: the bidirectional encoder and masked language modeling
You can explain the BERT encoder and the 80/10/10 MLM masking rule (L6.2).
12ELECTRA replaced-token detection
12.3 ELECTRA: replaced-token detection
You can explain how ELECTRA’s generator and discriminator are trained for replaced-token detection (L6.3).
13LoRA with PiSSA init and merge
12.6 LoRA: low-rank adaptation with PiSSA initialization and merging
You can explain how LoRA adds a low-rank update, how PiSSA initializes it from the SVD, and how it merges back (L6.6).
14Fine-tuning heads and the linear-head export
12.5 Fine-tuning heads and the linear-head export
You can explain how fine-tuning heads attach to the backbone and what the linear-head export writes (L6.5).
15LM evaluation harness and the model zoo
12.7 The evaluation harness and the model zoo
You can explain how the evaluation harness scores the models in the zoo (L6.7).
16Counting: params, FLOPs, KV bytes, memory plans
11.1 Counting: parameters, FLOPs, KV bytes, and memory plans
You can count a model’s parameters, FLOPs, and KV-cache bytes by hand (M05.1).
17Pre-LN, RMSNorm
13.1 Pre-LN and RMSNorm
You can explain how RMSNorm differs from LayerNorm and why the modern block is pre-LN (L7.1).
18Gated MLPs: SwiGLU, GeGLU
13.2 Gated MLPs: SwiGLU and GeGLU
You can explain how SwiGLU and GeGLU gate the MLP and how the hidden size is chosen (L7.2).
19RoPE (half and interleaved layouts, partial rotary)
13.3 Rotary Position Embeddings
You can explain how RoPE rotates query and key pairs in the half and interleaved layouts, and how partial rotary works (L7.3).
20Context extension: PI, NTK, YaRN, Llama-3 scaling; ALiBi (optional part)
13.4 Context Extension: PI, NTK, YaRN, Llama 3 Scaling, and ALiBi
You can explain how PI, NTK, YaRN, and Llama-3 scaling stretch RoPE to longer contexts (L7.4).
21MQA/GQA attention with cache hook, window, learned sinks
13.5 Grouped-Query Attention with a Cache Hook, Windows, and Sinks
You can explain how MQA and GQA share key-value heads and how the cache hook, window, and learned sinks plug in (L7.5).
22Multi-head latent attention (DeepSeek-V2/V3), weight absorption
13.6 Multi-Head Latent Attention and Weight Absorption
You can explain how multi-head latent attention compresses the KV cache and how weight absorption avoids decompressing it (L7.6).
23Sliding window, StreamingLLM sinks, learned sinks
13.7 Sliding Windows and Attention Sinks
You can explain how a sliding window bounds attention and why StreamingLLM keeps sink tokens (L7.7).
24Mixture of Experts: routing, sorted dispatch, Switch aux loss, aux-free bias
13.8 Mixture of Experts
You can explain how MoE routes tokens to experts, dispatches them in sorted order, and balances load with the Switch aux loss or the aux-free bias (L7.8).
25Llama-family model, HF config, weight loading, downloader
13.9 The Llama-Family Model: Config, Weight Loading, and Download
You can trace how the Llama-family model reads an HF config, loads weights, and matches Hugging Face logits (L7.9).
26Oracles, golden and differential tests, gradcheck as a test (R5)
3.3 Oracles, golden and differential tests, gradcheck as a test
You can explain how oracles, golden and differential tests, and gradcheck verify numerical code (craft.05).
27What the system does after Pass 5
B.7.1 What the System Does After Pass 5
You can explain why pre-LN trains where post-LN does not, and how the modern decoder loads SmolLM2-135M and matches Hugging Face.
28T5 span corruption and relative position buckets
12.4 T5: span corruption and relative position buckets
You can explain T5’s span corruption and how relative position buckets are computed (L6.4).