The Quadratic Cost of Attention
Self-attention compares every token in a sequence to every other token, so both its compute and memory grow with the square of sequence length — the single biggest structural limit on how much context a transformer can afford to read at once.
Prerequisites: Scaled Dot-Product Attention and the √d Factor, The Self-Attention Mechanism
Doubling the length of the text you feed a transformer does not double the work it has to do — it roughly quadruples it. That single fact explains why early transformers were capped at a few hundred or a couple thousand tokens of context, why "long-context" models were a genuine research problem rather than a solved detail, and why an entire family of alternative attention mechanisms exists purely to work around it. The cause is structural, not a bug: self-attention is defined as every token comparing itself against every other token, and the number of pairs in a sequence of length grows as .
The analogy: a round-robin conversation versus a broadcast
If 10 people are in a room and everyone wants to have a private one-on-one conversation with everyone else at some point, that's conversations. With 20 people it's — not double, more than quadruple. That is exactly the shape of attention: every token "talks to" every other token by computing a similarity score, so the number of scores to compute doesn't scale with how many tokens there are, it scales with how many pairs of tokens there are.
Where the actually comes from
In words: is an matrix of query vectors (one row per token), is an matrix of key vectors. Multiplying by produces an matrix — one entry for every possible (query token, key token) pair. That matrix has entries no matter how you compute it, and it must be fully formed (or tiled through, as in FlashAttention and IO-Aware Attention) because attention needs every token's relevance to every other token before it can decide how much to weight each one.
The compute cost of forming is multiply-adds, and the memory cost of storing it — even briefly — is numbers. Everything downstream (softmax, multiplying by ) inherits that same shape.
Worked example 1: counting entries directly
At tokens, the attention matrix has entries per head. Double the sequence to : entries — exactly 4×, not 2×, confirming the quadratic relationship directly by counting. At (a modest "long context" length): million entries, per head, per layer — for a 32-layer, 32-head model, that's over 68 billion entries if every one were materialised simultaneously.
Worked example 2: compute cost scaling in a table
Fixing and counting multiply-adds for forming alone ():
| Sequence length | Multiply-adds () | |
|---|---|---|
| 512 | 262,144 | 16.8 million |
| 2,048 (4×) | 4,194,304 | 268 million (16×) |
| 8,192 (4× again) | 67,108,864 | 4.3 billion (16×) |
Each time sequence length quadruples, the compute for this one step increases by 16×, not 4× — confirming that cost scales as the square of length, so a model handling documents instead of paragraphs pays a punishing, accelerating tax on every doubling of context.
Drag the coefficient above and watch how steeply the curve bends upward — that shape, compute or memory plotted against sequence length, is literally what the term in attention looks like; a linear-cost method would instead trace a straight line on the same axes.
Self-attention's compute and memory scale as in sequence length because it computes a score for every pair of tokens — doubling the context roughly quadruples the cost, which is the single structural reason long-context transformers needed dedicated engineering, not just bigger hardware.
What this means in practice
The cost is why "just feed the whole document in" was infeasible for years, and why a whole ecosystem of fixes exists at different layers: FlashAttention and IO-Aware Attention makes the existing computation run fast without changing its asymptotic shape, while Sparse Attention: Longformer and BigBird and Linear Attention and Kernel Feature Maps change the algorithm itself to avoid computing all pairs in the first place. Choosing between them is an active engineering decision every team serving long-context models has to make, trading off exactness, memory, and latency.
It is easy to conflate "quadratic in sequence length" with "quadratic in model size" — they are unrelated. A model can have billions of parameters (quadratic cost has nothing to do with parameter count) and still process short sequences cheaply, or have relatively few parameters and still choke on a very long sequence, because the term depends only on how many tokens are in a single forward pass, not on how large the network is.
Practice
- If forming the attention matrix at takes 1 second, roughly how long would you expect it to take at , assuming pure scaling with no other bottlenecks?
- Why does restricting attention to a fixed-width local window (as in worked example's right-hand diagram) change the cost from to ?
Related concepts
Practice in interviews
Further reading
- Vaswani et al., Attention Is All You Need (2017)
- Tay et al., Efficient Transformers: A Survey (2020)