Before you move a control in the demo, say what you expect to change.
Foundation reference
Efficient Attention at Scale: KV Cache, GQA & FlashAttention
A key–value (KV) cache stores the keys and values of earlier tokens so attention can reuse them during generation. Llama 2 70B and the Llama 3 models use grouped-query attention with 8 key-value heads to shrink the KV cache (Touvron et al., 2023; Grattafiori et al., 2024).
Study guide
Where to start
Attention needs O(T²) arithmetic, so why is memory traffic, rather than arithmetic, the main cost that FlashAttention removes?
How to study this pagePredict · Check · Generalize · Continue
Compare what the demo shows with the key equation and with the papers cited below.
Identify the part of the idea that survives a change of notation or model, so you can recognize it in another paper or system.
The newer notebook covers the same concept and has an interactive demo.
Why It Matters for Modern Models
- A key–value (KV) cache stores the keys and values of earlier tokens so attention can reuse them during generation. Llama 2 70B and the Llama 3 models use grouped-query attention with 8 key-value heads to shrink the KV cache (Touvron et al., 2023; Grattafiori et al., 2024).
- KV-cache memory grows linearly with context length and batch size. For a model with about 7 billion parameters, 32 layers, 32 heads of width 128 and no head sharing, 16-bit keys and values take 524,288 bytes (0.5 MiB) per token, so about 27,000 cached tokens (summed over the batch) take as much memory as the 14 GB of 16-bit weights; beyond that, reading the cache is most of the memory traffic in each decode step.
- FlashAttention made training faster and longer contexts practical by reducing memory traffic without approximating attention (Dao et al., 2022).
- KV-cache compression is an active research area: SnapKV keeps, for each head, only the prompt positions that receive the most attention from the end of the prompt (Li et al., 2024), and RazorAttention keeps the full cache only for a few "retrieval" heads (Tang et al., 2025).
Questions to Think About
- When does the KV cache, rather than the weights, become the largest use of inference memory?
- Why does GQA save memory and bandwidth without reducing the attention arithmetic each query head performs?
- Why can some KV-compression methods drop most cached positions for many heads, and which heads need the full cache?
Interactive Demos
Core Math
If you prefer intuition first, try the demos above, then come back for the math.
At long context, the cost of attention is set less by its formula than by how keys and values are stored and moved.
Autoregressive attention with a KV cache:
During decoding, the model forms the new query and attends to the cached keys and values of all earlier tokens:
where are stored, not recomputed. Without the cache, every new token would recompute keys and values for the whole prefix, so generating tokens would repeat projection work.
Grouped-Query Attention (GQA):
Multiple query heads share fewer KV heads. Let query head map to KV-head group :
This interpolates between multi-head attention (, no sharing) and multi-query attention (, all query heads share one key-value head).
KV cache memory:
For one sequence, each layer stores keys and values for every past token:
where is the context length and the factor 2 counts keys and values. A GQA cache is the fraction of the full multi-head cache, a reduction by the factor .
FlashAttention: standard attention writes the full score matrix to slow GPU memory (HBM) and reads it back. FlashAttention splits the computation into tiles that fit in fast on-chip memory (SRAM) and combines the partial softmax results exactly, so it returns the same output with memory linear in and far fewer HBM reads and writes (Dao et al., 2022).