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.