How do we make LLMs cheaper to train and faster to serve — without losing quality? Our last regular lecture follows one question, where do the time and memory go?, from efficient attention and Mixture of Experts through quantization, LoRA and ZeRO to the KV cache, speculative decoding, FlashAttention and vLLM. Watch it in short chapters with English and Chinese subtitles, follow the lecture map below, then try the ideas in Colab.
Four acts, eleven chapters. First why efficiency decides who can build and use LLMs; then two ways to change the model (efficient architectures and compression); then two ways to change how we run it: cheaper training and faster serving. Keep the four costs from slide 2 in mind — FLOPs, bytes moved, bytes stored and communication — because every trick attacks one of them. Click a chapter to watch it.
Scaling laws reward ever more compute, model size grows far faster than GPU memory, and frontier models are expensive to use. The methods split into changes within the Transformer and changes that work on any trained network — and for LLMs, memory and communication join FLOPs as the costs that matter.
Trained weights are low-rank and attention heads and FFN neurons are decomposable, so there is redundancy to remove. Sparse patterns (Longformer, Big Bird) let each token attend to fewer tokens; MQA, GQA and MLA store fewer keys and values per token.
A router sends each token to only a few expert FFNs: the capacity of a huge model at the compute of a small one. Top-k and hash routing, fine-grained and shared experts, load-balancing losses, DeepSeekMoE and DeepSeek-V2, and upcycling a dense model into an MoE.
Snap every weight to a small integer grid with a scale and a zero point. Outliers stretch the grid, so LLM.int8() keeps them in FP16, and GPTQ and AWQ make 4-bit work. INT8 is almost free; INT4 is the sweet spot for local models.
Pruning removes weights, heads or whole layers; only structured pruning makes dense hardware faster, and the model needs to recover afterwards, as in LLM-Shearing. Distillation trains a small student on a teacher's soft labels — and for LLMs, mostly on the teacher's generated data.
Freeze the pre-trained model and learn a thin update: LoRA's B·A trains about 0.4% of a 4096×4096 matrix and merges back with zero latency. Adapters and prefix-tuning insert parameters elsewhere, and small modules might be composed like Lego bricks.
Data parallelism copies the model and splits the data; model parallelism splits the model. Mixed-precision Adam needs 16 bytes per parameter — 112 GB for a 7B model — and ZeRO shards optimizer states, gradients and weights so memory per GPU falls with the number of GPUs.
Prefill is compute-bound, decoding is memory-bound. The KV cache grows with every token and, not the weights, limits the batch size; PagedAttention stores it in small blocks like virtual memory, cutting the waste from 60–80% to under 4%.
A small draft model proposes several tokens and the large model checks them all in one pass. Accept with probability min(1, q/p), otherwise resample from the normalized max(0, q−p): the output distribution is exactly the big model's, typically 2–3× faster.
Standard attention writes the N×N score matrix to slow HBM and reads it back. FlashAttention computes attention tile by tile in on-chip SRAM, using a running max and sum to merge softmax blocks exactly: more FLOPs, 9× fewer bytes moved, 5.7× faster.
vLLM looks like an operating system: a scheduler with continuous batching, a block manager with page tables, copy-on-write sharing for parallel sampling and beam search, and preemption. The outlook: sparse and hybrid attention, low-bit formats, disaggregated serving and efficient reasoning.
Three short notebooks turn the lecture's arithmetic into experiments. Click Open in Colab, then Runtime → Run all. Each ends with exercises. All three run on the free CPU runtime with small models (SmolLM2-135M, GPT-2); no GPU needed.
Key terms of the lecture. Each link opens the video at the slide where the idea is explained.
The lecture's outlook (slide 165) asks where the next ten-times saving will come from. Its answer: the frontier is co-design — model architecture, numerics, kernels and serving systems built together. Six trends to watch:
DeepSeek-V3.2 adds a lightweight “lightning indexer” that scores past tokens and lets each query attend only to the top-k of them (k = 2,048) — Longformer's idea (Chapter 2), but learned.
Most layers become linear-attention layers with a fixed-size state; only every fourth layer keeps a growing KV cache. Kimi Linear reports up to 75% less KV cache and up to 6× decoding throughput at 1M tokens.
FP8 training is now standard — DeepSeek-V3 was trained largely in FP8 (slide 55). NVFP4 on NVIDIA Blackwell stores 4-bit floats with one scale per block of 16 values: the group-wise quantization of Notebook 2, in hardware.
Compute-bound prefill and memory-bound decode (slide 114) now run on separate GPU pools, with the KV cache moved between them over fast networks. Mooncake serves Kimi this way; llm-d brings it to Kubernetes on top of vLLM.
For reasoning models, thinking tokens dominate the bill. Thinking budgets, adaptive effort, multi-token prediction and speculative decoding (Chapter 9) all aim at cheaper long chains of thought.
"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%."
The lecture's method: first ask where the time and memory go, then optimize the critical part.
"The biggest lesson that can be read from 70 years of AI research is that general methods that leverage computation are ultimately the most effective, and by a large margin."
If quality is bought with compute (slide 6), every saving buys a better model.
"We argue that a missing principle is making attention algorithms IO-aware — accounting for reads and writes between levels of GPU memory."
Bytes moved, not FLOPs — the lesson of Chapter 10.
"Many insects have a larval form that is optimized for extracting energy and nutrients from the environment and a completely different adult form that is optimized for the very different requirements of traveling and reproduction."
Train big, deploy small: the idea behind distillation and compression (Chapters 4–5).