LLM inference and training internals
The techniques behind production language models, one at a time: sampling strategies, the KV cache, grouped-query attention, the SwiGLU block, the FlashAttention softmax trick, mixture-of-experts routing, speculative decoding, preference optimisation and quantisation.
12 problems · about 5.5 h · Advanced. You'll need: The Build a transformer path, or equivalent experience.
By the end you can
- Implement the decoding strategies and explain how each trades quality for diversity
- Explain where inference time and memory go, and how the KV cache, GQA and speculative decoding reduce them
- Write the DPO and GRPO objectives used to align models after pre-training
Steps
- Step 1: Temperature Scaling → Temperature: reshape the next-token distribution.
- Step 2: Nucleus (Top-p) Sampling → Nucleus sampling and its edge cases.
- Step 3: KV Cache for Efficient Generation → Cache keys and values so each new token costs O(n), not O(n²).
- Step 4: Grouped Query Attention (GQA) → Share key/value heads to shrink the KV cache.
- Step 5: SwiGLU Feed-Forward Block (LLaMA MLP) → The SwiGLU feed-forward block used in LLaMA-style models.
- Step 6: Online Softmax (the FlashAttention Trick) → The one-pass softmax that makes FlashAttention possible.
- Step 7: Mixture-of-Experts Routing → Top-k expert routing and load balancing in mixture-of-experts.
- Step 8: Speculative Decoding → Draft with a small model, verify with the large one.
- Step 9: Token Cross-Entropy and Perplexity → Token cross-entropy and perplexity, the standard LM metric.
- Step 10: Direct Preference Optimization (DPO) → Direct preference optimisation: align without a reward model.
- Step 11: GRPO: Group-Relative Advantages & Objective → GRPO's group-relative advantages, used for reasoning models.
- Step 12: Int8 Weight Quantization (absmax) → Absmax int8 quantisation and the error it introduces.