1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
51 small wins to finish your pathNext question →
Why is attention O(n²)? What are FlashAttention and other long-context techniques?
30-second answerSay your answer out loud first, then reveal.
Cost of standard attention
- Compute: O(n² · d) per layer.
- Memory for scores: O(n²) per head. At n = 128K, 128K² ≈ 16 billion entries per head per layer, which is impossible to store naively.
FlashAttention (Dao et al. 2022; v2, v3)
- Key insight: attention is memory-bandwidth bound. The bottleneck is moving data between HBM (large, slow) and SRAM (small, fast).
- Tiles Q, K and V into blocks, computes the softmax incrementally (online softmax), and never writes the full score matrix to HBM.
- Exact attention, not an approximation. Memory is linear in n, and it's several times faster.
- Standard in modern training and inference stacks.
Other long-context techniques
| Technique | Idea | Trade-off |
|---|---|---|
| Sliding-window attention | Each token attends to the last W tokens | Loses direct long-range links (stacking layers extends reach) |
| Sparse / block-sparse attention | Attend to selected positions | Pattern design; may miss information |
| Linear attention / SSMs (Mamba) / hybrids | O(n) recurrent-style mixing | Recall over long contexts can be weaker; hybrids help |
| Ring attention / context parallelism | Split long sequences across GPUs | Communication overhead |
| RoPE scaling (YaRN etc.) | Extend positional range (Q40) | Needs some fine-tuning |
Related
This is what real progress feels like.