Start studying
Module 5 · Transformers Module contents
Module 5  ·  Card 6 / 19intermediate

Why Attention Costs O(n²)

Self-attention compares every token with every other token, so n tokens make n squared comparisons.

Double the context and the work quadruples, which is why long inputs get expensive so fast.

The attention matrix itself is n by n per head, and holding it is often the real memory limit.

This quadratic term is the single reason context windows were small for so long.

Flash attention removes the memory cost by never storing the matrix, though the compute stays quadratic.

Learn in depth ↓
Module 05 · Transformers · 19 concepts
In depth  /  15 lines

Why Attention Costs O(n²), in more detail

Attention scores a query against every key, and in self-attention every token supplies both.

With n tokens that is n times n scores, computed for each head of each layer.

The compute therefore grows with the square of the sequence length, written O(n squared).

Feed-forward layers, by contrast, grow linearly, so attention comes to dominate as context lengthens.

At five hundred tokens the quadratic term is negligible; at a hundred thousand it is everything.

There are two separate costs and they are worth separating, because their fixes differ.

The first is arithmetic: the multiplications needed to build and apply the score matrix.

The second is memory: that matrix is n by n per head and must exist somewhere while softmax runs.

For long sequences the memory cost bites first, and it is what produces out-of-memory errors.

Flash attention attacks exactly that by computing attention in tiles and never materialising the full matrix.

It is mathematically identical, faster in practice, and leaves the quadratic arithmetic untouched.

Sparse and windowed attention change the maths instead, letting each token attend to a subset.

Linear attention approximates the softmax so the operation can be reordered into linear cost.

These approximations trade quality for reach and have never fully displaced exact attention.

Meanwhile the practical answer at inference is the KV cache, which avoids recomputing past keys and values.

Deep dive

What is underneath Why Attention Costs O(n²)

The two terms that matter

time O(n2d)memory O(n2)\text{time } O(n^2 d) \qquad \text{memory } O(n^2)
  • nn: sequence length.
  • dd: model width.

The time comes from QK⊤QK^{\top}: an n×dn \times d matrix times a d×nd \times n matrix is n2dn^2 d multiply-adds, and the multiplication by VV costs the same again.

The memory comes from the score matrix itself, which is n×nn \times n and has to exist before the softmax can normalise it.

What quadratic actually costs

Doubling the sequence length quadruples both.

n=1,000  ⟹  106 scoresn=100,000  ⟹  1010 scoresn = 1{,}000 \implies 10^6 \text{ scores} \qquad n = 100{,}000 \implies 10^{10} \text{ scores}

At 100,000 tokens the score matrix in fp16 is 20 GB, per head, per layer. That is not a slow model, it is a model that does not run.

This single term is why context windows grew slowly for years while parameter counts grew fast.

Against the alternative

self-attention O(n2d)recurrent O(nd2)\text{self-attention } O(n^2 d) \qquad \text{recurrent } O(n d^2)

Attention is cheaper than recurrence whenever n<dn < d, which covers most sentences: 512 tokens against a width of 4096.

It is more expensive when the sequence is long, which is exactly the regime everyone now wants.

So the comparison that made attention look cheap in 2017 has inverted, and the field has been chasing the difference ever since.

The three ways out

  1. Make it exact but cheaper in memory, by never storing the full matrix. FlashAttention does this and is not an approximation.
  2. Make it sparse, so each position attends to a subset rather than everything. Sliding windows and dilated patterns take the term to O(nn)O(n \sqrt{n}) or O(n)O(n).
  3. Change the operation, replacing softmax attention with something linear in nn. Linear attention and state-space models take this route.
  4. Reduce what is stored rather than what is computed, which is grouped-query attention attacking the cache instead.
  5. Accept the cost and pay it, which is what most production models still do below about 32,000 tokens.

The distinction that gets muddled

Compute and memory are separate problems with separate fixes.

FlashAttention removes the O(n2)O(n^2) memory but keeps the O(n2d)O(n^2 d) arithmetic, and it is still a large speedup because attention is bandwidth-bound rather than compute-bound.

Sparse attention removes arithmetic and changes what the model can represent. The first is free, the second is a trade.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
What happens to the cost of self-attention when the context length doubles?
Transformers

19 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close