Chapter 1 · Book pages 39–110
LLM Architecture and Optimization Methods
The big picture
Everything an LLM does — chat, code, reasoning — is one loop: text is chopped into tokens, tokens become vectors of numbers, a stack of transformer layers processes them, the model outputs a probability for every possible next token, one gets picked, and the loop runs again with the new token appended. Watch it below.
Interactive · The autoregressive loop
Press play to watch a sentence being generated token by token: tokenize → process → predict → append → repeat.
Stop 1 — Tokenization: how the model reads
The model never sees letters or words. It reads tokens: word pieces from a fixed menu (the vocabulary, 32K–128K entries). Common words are one piece; rare words get assembled — like LEGO for text. The menu is learned by BPE: start from characters, repeatedly merge the most frequent adjacent pair, stop when the menu is full.
Go deeper: other tokenizers, best practices
WordPiece (BERT) merges by likelihood instead of frequency; Unigram (SentencePiece/T5) starts big and prunes down; byte-level BPE (GPT-2 onward) works on raw bytes so no input is ever "unknown". Llama-3 uses a 128K vocabulary for better multilingual and code coverage, and tokenizes runs of spaces as single tokens so indented code stays cheap.
Practice rules from the book: never let a tokenizer split special tokens; mask loss on structural tokens during SFT; keep role markers as dedicated tokens rather than plain text. Book §1.2, p. 39–43.
Stop 2 — Embeddings: how pieces get meaning
Each token ID looks up a row in a big table: its embedding, a vector of ~4096 numbers. These coordinates place every token in a "meaning space" where similar concepts sit close together — and even arithmetic works: king − man + woman ≈ queen. Nobody designs this geometry; it emerges purely from next-token prediction, because words used in similar contexts end up with similar vectors.
Go deeper: anisotropy and whitening
Pretrained embedding spaces are anisotropic: vectors occupy a narrow cone, so cosine similarity between any two texts comes out high regardless of content — which quietly breaks retrieval, clustering, and recommenders. A one-time linear fix called whitening (rotate and rescale so all directions have equal variance) restores meaningful similarity, and can shrink dimensionality at the same time. Alternatives: contrastive fine-tuning (SimCSE-style). Book §1.3.4, p. 46–48.
Stop 3 — Attention & the transformer: how it thinks
Attention lets each token pull in information from the tokens before it. Every token makes three vectors: a query ("what am I looking for?"), a key ("what do I contain?"), and a value ("what do I hand over if picked?"). Query·key scores go through softmax into weights, and the token's output is the weighted mix of values. The causal mask hides the future. Step through it:
Interactive · Attention, one query at a time
Step through how the word "it" finds what it refers to: query vs keys → scores → softmax weights → value mix.
A transformer layer is attention (tokens talk) followed by an FFN (each token thinks alone — research suggests the FFN acts as stored knowledge). Both steps are wrapped in residual connections and preceded by RMSNorm. Stack 32–126 of these layers and you have the model.
Go deeper: multi-head, GQA/MLA, positional encodings, pathologies, interpretability
Multi-head attention runs many small attentions in parallel; heads specialize (syntax, coreference, "induction heads" that copy patterns — the mechanism behind in-context learning). GQA shares one K/V head across several query heads (Llama-3: 32 Q, 8 KV → 4× smaller cache); MLA (DeepSeek) compresses all K/V into one small latent vector per token (~93% cache reduction).
Positional encoding history: fixed sinusoids (2017) → learned positions (GPT-2, hard length cap) → RoPE (relative, extendable) and ALiBi (distance penalty, great extrapolation). Long contexts also need continued pretraining on long documents — supporting a length positionally is not the same as using it.
Interpretability ladder: attention maps (cheap, least faithful — "attention is not explanation") → probing → sparse autoencoders (decompose activations into single-concept features, scaled to 34M features on Claude 3 Sonnet) → causal tracing (most faithful, priciest). Book §1.3.5–1.3.12, p. 48–59.
Stop 4 — Output & decoding: how it speaks
After the last layer, a projection turns each position's vector into logits — raw scores for every vocabulary token — and softmax makes them probabilities. Picking the actual token is decoding, and the strategy changes everything: greedy is precise but repetitive, temperature dials randomness, top-p adapts the candidate pool to the model's confidence. Try it:
Interactive · Sampling playground
Drag the temperature and top-p sliders to reshape a real next-token distribution, then sample from it.
Go deeper: beam search, min-p, contrastive and constrained decoding
Beam search tracks several candidate sequences (great for translation, bland for open-ended prose). Min-p keeps tokens at least X% as likely as the best token — scales with confidence even better than top-p. Contrastive decoding subtracts a weak model's scores from a strong model's, stripping the generic signal both share. Repetition penalties push down already-used tokens.
Constrained decoding compiles a JSON schema or grammar into a state machine; at every step, tokens that would break the grammar get probability zero. Libraries: Outlines, Guidance, XGrammar (used inside vLLM). Book §1.4 p. 59–62, §1.12 p. 88–94.
Stop 5 — Training: how it learns
Training is hill-descending in the dark. The loss measures how wrong the next-token predictions are (its human-readable cousin is perplexity); the gradient tells each weight which way to nudge; the learning rate sets the step size. Repeat over trillions of tokens. Plain SGD fails at LLM scale because different layers have wildly different gradient sizes — AdamW gives every weight its own adaptive step plus clean weight decay.
Go deeper: Adam mechanics, Muon, precision frontier, failure modes
Adam keeps two running averages per weight: recent gradient direction (momentum) and magnitude (for the per-weight step). AdamW applies weight decay outside the adaptive scaling — always AdamW, never plain Adam. Muon (2025) orthogonalizes momentum for ~2× compute efficiency; adopted by GLM, DeepSeek-V4, and Kimi K2 (15.5T tokens, zero loss spikes) while AdamW stays the fine-tuning default.
Precision frontier: BF16 beats FP16 (same range as FP32, no loss-scaling gymnastics); DeepSeek-V3 trained 671B mostly in FP8 for ≈$5.6M; NVFP4 (4-bit) training already works with ~15% of layers kept in BF16. Pretraining pitfalls: loss spikes (roll back, skip the batch), memorization (deduplicate), short-context training that fails at long context. Book §1.5 p. 63–72, §1.7 p. 76–78.
Stop 6 — Fine-tuning & LoRA: becoming an assistant
A pretrained model completes text; it doesn't follow instructions. SFT fixes that with curated prompt→response pairs — mechanically identical to pretraining except loss is computed only on response tokens. That single masking change turns a text predictor into an assistant. Quality beats quantity: 1,000 excellent examples can match 50,000 noisy ones (the LIMA principle).
Full fine-tuning of a 70B model needs ~560 GB of GPU memory. LoRA sidesteps it: freeze the weights, learn a thin low-rank correction W′ = W + (α/r)·B·A. Watch what the rank buys:
Interactive · LoRA visualizer
Drag the rank slider and watch the adapter matrices grow while the trainable-parameter count stays a rounding error next to full fine-tuning.
Go deeper: LoRA variants and efficiency stacks
DoRA splits weight direction from magnitude and adapts only direction (+1–3% on reasoning). LoRA+ trains B with ~16× the LR of A (free ~2%). rsLoRA stabilizes high ranks (≥64). VeRA freezes shared random A/B and trains only tiny scaling vectors (~10× fewer parameters). Training stacks: Unsloth (fastest single-GPU QLoRA), Liger Kernel (fused Triton kernels, +20% throughput), torchtune (hackable pure PyTorch). Book §1.8–1.9, p. 78–84.
Stop 7 — Mixture of Experts: capacity without the bill
A MoE layer replaces one big FFN with many parallel "expert" FFNs plus a router that picks the top-K per token — a hospital with specialists instead of one generalist brain. Total knowledge is huge; per-token compute stays small. Mixtral 8x7B: 47B total, 13B active, matching a dense 70B.
Go deeper: balancing losses, noisy top-K, notable models
The classic fix adds an auxiliary loss pushing routing toward uniform use; DeepSeek showed the loss fights specialization and replaced it with a per-expert bias adjusted outside the gradient. Noisy top-K gating adds learned Gaussian noise before selection (removed at inference); Gumbel-softmax is the differentiable alternative. Aggregating balance statistics over the global batch (not micro-batches) is what preserves expert specialization. Book §1.10, p. 85–87.
Stop 8 — Running fast & small
Serving is where architecture meets physics. A GPU has a big slow warehouse (HBM) and a tiny fast desk (SRAM) — and standard attention wastes its time walking between them. Flash Attention computes the exact same result in desk-sized tiles using a running-softmax trick, never materializing the giant score matrix: identical math, unchanged FLOP count, 2–4× faster, memory O(n²) → O(n).
For the model itself: quantization stores weights in 4–8 bits (70B: 140 GB → 35 GB at ~2–3% quality cost — the book's rule: always quantize for serving, W4A16 is the sweet spot). Distillation trains a small student on a big teacher's full probability distribution — "dark knowledge": Paris 90%, Lyon 5%, banana ~0% teaches which mistakes are reasonable.
And speculative decoding attacks generation latency itself: a small drafter proposes ~5 tokens, the big model verifies them in one parallel pass, and a clever acceptance rule keeps the output distribution exactly the big model's. Pure speedup, zero quality change.
Go deeper: FA versions, quantization methods, pruning, distillation modes
Flash Attention by GPU generation: FA2 (A100 — better parallelism, skips masked blocks), FA3 (H100 — async memory engine, warp specialization, 75% of peak), FA4 (B200 — the exponential became the bottleneck, so it's emulated with polynomials on tensor cores). Lesson: each hardware generation moves the bottleneck and demands new algorithms.
Quantization named methods: GPTQ, AWQ (protects the ~1% of weights that matter most), GGUF (llama.cpp local formats), SmoothQuant (W8A8), QAT (highest quality, expensive). Pruning: 2:4 structured sparsity gets exact 2× on NVIDIA tensor cores; SparseGPT/Wanda reach 50% one-shot. Distillation modes: offline (stored logits), online (co-training), black-box (API text only — degrades to imitation SFT), self-distillation. Speculative variants: Medusa (extra heads), Eagle (drafts from hidden states, 85–95% acceptance), n-gram lookup (free, great for code). Book §1.6 p. 73–76, §1.14–1.15 p. 101–107.
Stop 9 — Failures & safety
A hallucination is confident, fluent, wrong output — contradicting the given context, inventing facts, or ignoring instructions. Model-level detectors look for uncertainty: high entropy on output tokens, or self-disagreement (ask several times and compare meanings; correct knowledge is stable, confabulation varies). The honest caveat: these detect uncertainty, not incorrectness — a model can be confidently and consistently wrong. Reliable factuality needs retrieval (chapter 16).
Go deeper: detection methods and the safety pipeline
Semantic entropy clusters sampled answers by meaning and measures disagreement; SelfCheckGPT verifies claims against the model's own alternative generations; DoLA contrasts late-layer vs early-layer logits (factual knowledge lives deeper) at decode time. Threat taxonomy: harmful content, bias, privacy leaks, jailbreaks, misinformation, dual-use. Helpfulness–safety balancing is a constrained optimization with separate reward models; over-refusal target <5%. Book §1.16–1.17, p. 108–110.