Why is standard attention O(n²) in sequence length, and what specifically is the n²?
The n² is the size of the attention score matrix QKᵀ: every query key pair gets one entry. Both compute and memory scale quadratically with sequence length.
Imagine a roomful of people where everyone has to shake hands with everyone else and remember each handshake. With 10 people you get 100 handshakes; with 100 people you get 10,000. The work grows with the square of the headcount, not the headcount itself. Attention does the same thing, every token has to look at every other token, so the bookkeeping for a length-n sequence is shaped like an n by n grid. That n by n grid is the n² you keep hearing about.
Detailed answer & concept explanation~5 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
5 min: where the n² lives inside QKᵀ, the difference between compute and memory complexity, the production escape-hatch hierarchy (FlashAttention, PagedAttention, ring attention, sparse patterns), and the parameter count confound to avoid.
| Approach | Compute | Memory | Exact? |
|---|---|---|---|
| Standard attention | O(n²·d) | O(n²) | Yes |
| FlashAttention | O(n²·d) | O(n) | Yes |
| Sliding window (w) | O(n·w·d) | O(n·w) | Within window only |
| Linear attention | O(n·d²) | O(n·d) | No (kernel approx) |
| Mamba / SSM | O(n·d²) | O(n·d) | No (recurrence) |
Real products, models, and research that use this idea.
- FlashAttention v2 and v3 are the default attention kernels in PyTorch SDPA and JAX as of 2026 and underlie every major training run.
- vLLM's PagedAttention is the standard KV-cache manager for high-throughput serving of Llama 4 Maverick, DeepSeek V4, and Qwen 3.5.
- Mistral and Gemma 4 use sliding window attention to bound per-layer cost on long context workloads.
- Ring attention is the technique behind Gemini 3.1 Pro's multi-million token context window on TPU pods.
- DeepSeek V4's Native Sparse Attention interleaves local and global attention layers to keep the global-attention bill small.
What an interviewer would ask next. Try answering before peeking at the approach.
QIf FlashAttention has the same O(n²) compute but is 2-4× faster wall clock, what does that tell you about where attention's bottleneck actually was?
QWhy don't linear attention variants replace softmax attention in production despite being asymptotically faster?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Confusing the n² compute cost with the parameter count. Attention has O(d²) parameters per layer (the projection matrices), independent of sequence length. The n² is purely about the sequence-axis interaction.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.