Part IV's self-attention let every token look at every other token, all at once — no fixed window, no recency bias. That power has a price nobody mentioned yet: what happens to that cost as the sequence gets long?
Attention's cost isn't just "bigger" than a linear layer's — it's a completely different shape of bigger. Double the sequence length and the linear layer's cost doubles too. Attention's quadruples.
Self-attention builds an score matrix — every one of queries compared against every one of keys, each comparison a -dimensional dot product:
- — the sequence length: how many tokens are being processed.
- — the embedding dimension: the size of each token's vector representation.
- The ratio is the whole chapter
Attention costs exactly times more than a single per-token operation, where is the sequence length itself.
- d cancels out — only n matters
The embedding dimension changes the raw op counts, but it cancels out of the ratio completely — it's the sequence length alone that decides how much worse attention gets.
The dashed line is the linear layer's cost; the bold curve is attention's. They start close together at small and pull apart fast — by , attention has left the line so far behind that the line is barely visible near the bottom of the chart. That gap is the entire reason the rest of this part exists.
Fixing and doubling the sequence length twice, starting from :
- n = 8
Attention: ops. Linear layer: ops. Attention costs as much.
- n = 16 (doubled once)
Attention: ops — exactly the value. Linear layer: ops — exactly . Attention now costs as much, not .
- n = 32 (doubled again)
Attention: ops — the previous step, again. Linear layer: ops — again. The ratio has doubled to . Every doubling of doubles the ratio too, because the ratio is .
Find the smallest sequence length, among the candidates, where attention costs at least 20x a linear layer.
A Transformer processing a 512-token paragraph pays a manageable quadratic cost. The same architecture on a 100,000-token codebase or a book-length context pays a cost that's grown by a factor of nearly 200 relative to the paragraph case, purely from the ratio itself growing. That single number — not a bug, not a bad implementation, just what does — is the reason an entire research area exists around finding attention alternatives that don't pay it. The next chapter looks at the first one: what if the softmax inside attention were swapped for something that let the whole computation be rearranged into a running sum instead of a full matrix?