Concept · Chapter 11: Inside Modern LLMs
The Quadratic Cost of Attention
Self-attention compares every token with every other token, so its score matrix, and a naive implementation's time and memory, grow with the square of the sequence length.
The problem
Long contexts are useful (whole documents, codebases, conversations), but doubling the context quadruples the work of computing attention over it.
The solution
Know which part grows how: the score matrix is n × n per head, the rest of the layer grows linearly. Then avoid storing the n × n matrix (FlashAttention), shrink what is stored per token (KV cache, GQA), or change attention itself.
The consequence
Context length became an engineering budget. Exact attention got much faster without changing its quadratic arithmetic; many approximate alternatives were proposed, but cutting arithmetic often failed to cut wall-clock time.
You should understand first
- Vectors
- Dot Product
- Embeddings
- Attention
- Probability and Distributions
- Softmax
- Self-Attention
- Matrix Multiplication
- The Quadratic Cost of Attention
Where the square comes from
For a sequence of tokens, each attention head computes the scores : every query against every key, an matrix (self-attention). The softmax runs over each row and the result multiplies . The projections and the feed-forward layer handle each token separately, so they grow only linearly with .
Dao and colleagues open the FlashAttention paper with this: Transformers are slow and memory-hungry on long sequences because the time and memory of self-attention are quadratic in sequence length Established.
Tiny example. At 8,192 tokens, one head's score matrix has million entries, 134 MB in 16 bits, for one head in one layer. At 32,768 tokens it is 16 times that. Storing all of them for the backward pass is how attention used to run out of memory.
Training versus decoding
During training and prefill, all positions are processed together and the full matrix is computed. During decoding with a KV cache, the new token's single query meets cached keys: one row of the matrix, linear in per token. Summed over a long generation the quadratic total comes back, and the cache itself grows with .
Three ways out
- Don't store the matrix. Compute exact attention in tiles that never write the scores to slow memory: FlashAttention.
- Store less per token. Shrink the cache that long contexts fill: grouped-query attention, quantization.
- Change attention. Attend to a local window, a sparse pattern or a compressed summary. The FlashAttention authors observe that approximate attention methods reduce arithmetic but often don't achieve a wall-clock speed-up Established.
What to remember
- Scores QKᵀ: an n × n matrix per head per layer, for n tokens.
- Double the context: 4× the score entries, 2× everything else.
- 8,192 tokens: about 67 million scores per head per layer (134 MB in 16 bits).
- During decoding with a KV cache, each new token's attention costs grow linearly with the context.
- FlashAttention makes memory linear by not storing the matrix; the arithmetic stays quadratic.
Key papers
Attention Is All You Need
Ashish Vaswani, Noam Shazeer et al. · 2017 · NeurIPS 2017
Introduced the Transformer — the architecture behind BERT, GPT and nearly every modern large language model, and later adapted to vision, audio and more.
How to read it: Section 3 is the architecture — read it with Figure 1 open. Sections 3.2.1–3.2.2 contain the attention equation. You can skim the training details on a first pass.
FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness
Tri Dao, Daniel Y. Fu et al. · 2022
Showed that attention was slow because of memory traffic, not arithmetic, and fixed it exactly: same outputs, far fewer reads and writes, memory linear in sequence length.
How to read it: Figure 1 (the memory hierarchy and the tiling loop) and Algorithm 1 carry the idea; the IO-complexity proofs can wait.
Watch
Stanford Online
Stanford CS336 I Language Modeling from Scratch | Spring 2025 | Lecture 5: GPUs
The hardware background for this chapter: why memory movement, not arithmetic, so often sets the speed.