Quadratic Attention Bottleneck › The All-Pairs Problem
Language AI HandbookAll chapters Quadratic Attention Bottleneck › The All-Pairs ProblemOne section · Sections in this chapter2 / 13 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Self-attention computes relationships between every pair of tokens in a sequence. For a sequence of tokens, this means pairwise interactions. This is the defining structural feature of attention, and it is the root cause of every scaling challenge discussed in this chapter.
To understand why all pairs are necessary, consider what attention is trying to accomplish. When reading the sentence "The trophy didn't fit in the suitcase because it was too big," a model needs to determine that "it" refers to "the trophy," not "the suitcase." To resolve this correctly, the word "it" must simultaneously consider "trophy," "suitcase," and "big" and compare their relevance. Attention provides exactly this capability: every token can look at every other token and decide how much weight to give each one. Restricting this to a subset of tokens would risk missing the very long-range dependencies that make attention powerful.
The complete attention formula captures this all-pairs comparison in matrix form. To compute attention for a sequence of tokens with representations of dimension , we first project each token into QKV vectors, then compute:
where:
- : the query matrix, with one row per token representing "what this token is looking for"
- : the key matrix, with one row per token representing "what this token contains"
- : the value matrix, with one row per token representing the information to aggregate
- : the dimension of queries and keys, used as a scaling factor
- : the sequence length (number of tokens)
The critical operation is , which produces an attention score matrix. Every element in this matrix represents how much token should attend to token . Computing all entries is what gives attention its power: any token can directly influence any other, regardless of distance. But this same property creates the quadratic scaling problem.
Consider the contrast with sequential processing. In a recurrent neural network, information from token 1 must propagate through tokens 2, 3, 4, and so on to reach token 100. In attention, token 1 directly connects to token 100 in a single step. This short path length is why transformers excel at capturing long-range dependencies. The price is computing all pairwise interactions up front, and that price grows quadratically.
Notice that there is no mathematical shortcut here. The score matrix entry at position requires knowledge of both token 's query and token 's key. Until you compute all scores, you cannot determine which tokens attend to which. Later in this part of the book, we will explore approximations that avoid computing the full matrix, but they do so at the cost of either accuracy or certain types of dependencies.
def count_attention_pairs(n):
"""Count the number of pairwise interactions in attention."""
return n * n
# Demonstrate quadratic growth
sequence_lengths = [128, 256, 512, 1024, 2048, 4096]
pairs = [(n, count_attention_pairs(n)) for n in sequence_lengths]Pairwise Interactions in Self-Attention:
Sequence Length Pairs Growth Factor
--------------------------------------------------
128 16,384 -
256 65,536 4.0x
512 262,144 4.0x
1,024 1,048,576 4.0x
2,048 4,194,304 4.0x
4,096 16,777,216 4.0xThe "Growth Factor" column reveals the quadratic pattern with stark clarity: doubling the sequence length quadruples the number of interactions. At 128 tokens, we compute roughly 16,000 pairs. At 4,096 tokens, this explodes to over 16 million pairs. The 4x growth for each 2x increase in sequence length is the signature of complexity, and it compounds relentlessly as sequence lengths scale toward the tens or hundreds of thousands of tokens that modern applications demand.
In practice, this means that a model processing a 32,768-token document performs roughly 65,000 times more attention interactions than the same model processing a 512-token paragraph. No other component of the transformer scales this aggressively with input length, which is why attention dominates the resource budget at long sequences.


The first panel shows the raw explosion in pair count, rising from near zero at short sequences to over 16 million at 4,096 tokens. The second panel, using logarithmic scales on both axes, reveals the underlying structure: a straight line with slope 2, confirming the relationship. Each doubling of sequence length produces exactly a 4x increase in pairs, marked along the curve.
The log-log view is particularly instructive because it turns the quadratic relationship into something visually linear, making the constant growth factor visible at every step. Any time you see a log-log plot produce a straight line with slope 2, you are looking at quadratic scaling.