Part XII — Modern Sequence Architectures: RoPE, FlashAttention & State-Space Models · Chapter 4

The quadratic attention bottleneck

Hook

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?

Intuition

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.

Formalize

Self-attention builds an n×nn \times n score matrix — every one of nn queries compared against every one of nn keys, each comparison a dd-dimensional dot product:

attention ops=n2d,linear-layer ops=nd,attention opslinear-layer ops=n\text{attention ops} = n^2 d, \qquad \text{linear-layer ops} = nd, \qquad \frac{\text{attention ops}}{\text{linear-layer ops}} = n
  • nn — the sequence length: how many tokens are being processed.
  • dd — the embedding dimension: the size of each token's vector representation.
  1. The ratio is the whole chapter

    Attention costs exactly nn times more than a single per-token operation, where nn is the sequence length itself.

  2. d cancels out — only n matters

    The embedding dimension dd 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.

Play
at n = 128: attention = 131,072 ops, linear = 1,024 ops

The dashed line is the linear layer's cost; the bold curve is attention's. They start close together at small nn and pull apart fast — by n=128n=128, 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.

Worked example

Fixing d=8d=8 and doubling the sequence length twice, starting from n=8n=8:

  1. n = 8

    Attention: 82×8=5128^2 \times 8 = 512 ops. Linear layer: 8×8=648 \times 8 = 64 ops. Attention costs 8×8\times as much.

  2. n = 16 (doubled once)

    Attention: 162×8=2,04816^2 \times 8 = 2{,}048 ops — exactly 4×4\times the n=8n=8 value. Linear layer: 16×8=12816 \times 8 = 128 ops — exactly 2×2\times. Attention now costs 16×16\times as much, not 8×8\times.

  3. n = 32 (doubled again)

    Attention: 322×8=8,19232^2 \times 8 = 8{,}192 ops — 4×4\times the previous step, again. Linear layer: 32×8=25632 \times 8 = 256 ops — 2×2\times again. The ratio has doubled to 32×32\times. Every doubling of nn doubles the ratio too, because the ratio is nn.

Checkpoint

Find the smallest sequence length, among the candidates, where attention costs at least 20x a linear layer.

Pick a sequence length to try it
Summary
attention opslinear-layer ops=n\frac{\text{attention ops}}{\text{linear-layer ops}} = n

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 nn itself growing. That single number — not a bug, not a bad implementation, just what n2n^2 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?