Stanford CME295, Lecture 5. Where FlashAttention and Speculative Decoding Get Their Speed (Series #AI)
In the fifth lecture of Stanford CME295, Afshine and Shervine Amidi explain two curious ways to speed up an LLM. In one, the GPU repeats some calculations. In the other, a small model joins a large one, so both work on the answer. Where do the time savings come from?
Take the NVIDIA H200 SXM. It has 141 GB of memory and can perform up to 990 trillion operations per second on dense BF16 matrices. The BF16 weights alone of a 70-billion-parameter model take up 140 GB. Even at the peak memory bandwidth of 4.8 TB/s, reading all the weights once takes about 29 ms (140 GB ÷ 4,800 GB/s). Training also needs memory for activations, gradients, and optimizer state. H200 specifications
FlashAttention. Why do the same calculation twice? (More in the FlashAttention paper.)
In Lecture 1, we looked at how a token gathers information from its context. In a straightforward implementation, the GPU writes a table of attention scores to main memory, one row per token. It then reads the table, converts the scores into weights with softmax, and writes a second table holding those weights. It has to read that table too, to add up the weighted value vectors. For a sequence of length N, each table has N × N entries.
In the attention formula, all the weights in a row share a denominator, so the division can wait until the end. FlashAttention takes a small block, computes its scores in fast on-chip memory, and immediately adds its contribution to two running totals, one for the numerator and one for the denominator. Then it can move on to the next block. The GPU gets the same result and avoids writing two full tables to main memory and reading them back.
Training needs the attention weights again to compute gradients. The GPU recomputes them for a small block and uses them on the spot. If recomputing those numbers on the chip takes less time than moving the whole table to main memory and back, the extra arithmetic saves time. In the FlashAttention authors’ GPT-2 experiment with a context length of 1,024 tokens, training took about a third of the time.
Speculative decoding. How many tokens can you check in one pass? (More in the speculative decoding paper.)
When the model reads a long prompt (prefill), it applies the same weights to many tokens at once. Compute often limits speed here. When it writes the answer (decode), each new token needs the weights and the cached keys and values from earlier positions. With few requests, the GPU often waits for data from memory. More on the two phases
On the H200, four ordinary decoding steps mean four reads of 140 GB each. The large model can check a four-token draft in one pass, as we saw in Lecture 4. The GPU reads each layer’s weights once and applies them to all four positions. At peak bandwidth, that’s about 116 ms for four weight reads and 29 ms for one. If the first three tokens are accepted and the fourth is replaced, the answer grows by four tokens.
Will the answer arrive four times as fast? Both estimates cover only the weight reads. The small model needs time to write its draft, and the large model still has to run the computations and read the cache. If the very first token is rejected, the other three proposals are discarded even though the GPU has already spent time checking them. In experiments with T5-XXL, the authors reported a 2–3× speedup.
Why check the draft? Left unchecked, the small model determines how often each token appears. We want the large model’s probabilities.
Suppose the next token can be either “yes” or “no.” The small model proposes “yes” 40% of the time, while the large model would choose it 20% of the time. So we only need to keep half the proposed yeses. We flip a fair coin. Heads, we keep “yes.” Tails, we switch to “no.” If the draft already says “no,” we keep it. That leaves half of 40%, or 20%, for “yes” and the remaining 80% for “no.” Exactly the large model’s probabilities.
FlashAttention saves time by avoiding writes and reads of intermediate tables, while speculative decoding checks several tokens each time it reads the model weights. If you’re short on time, you can jump straight to the parts on these two techniques: FlashAttention around minute 9 and speculative decoding around minute 92.
P.S. The Amidis put together a terrific course. I’d already read a lot on these subjects, but I hadn’t seen them explained this clearly :)
#AI #LLM #Engineering #Learning