Quadratic Attention Bottleneck › The All-Pairs Problem

Michael BrenndoerferUpdated June 21, 20256 min read
Language AI HandbookAll chapters
Part 17 of 67Efficient Attention
Chapter 117Quadratic Attention Bottleneck › The All-Pairs Problem
Reading section 2 of 13 · The All-Pairs Problem · 6 minSection 2 of 13 · 6 min

Book contents

Section 2 of 13 · 6 min read

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 nn tokens, this means n2n^2 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 nn tokens with representations of dimension dkd_k, we first project each token into QKV vectors, then compute:

Attention(Q,K,V)=softmax(QKTdk)V\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V

where:

  • Q∈Rn×dkQ \in \mathbb{R}^{n \times d_k}: the query matrix, with one row per token representing "what this token is looking for"
  • K∈Rn×dkK \in \mathbb{R}^{n \times d_k}: the key matrix, with one row per token representing "what this token contains"
  • V∈Rn×dvV \in \mathbb{R}^{n \times d_v}: the value matrix, with one row per token representing the information to aggregate
  • dkd_k: the dimension of queries and keys, used as a scaling factor
  • nn: the sequence length (number of tokens)

The critical operation is QKTQK^T, which produces an n×nn \times n attention score matrix. Every element (i,j)(i, j) in this matrix represents how much token ii should attend to token jj. Computing all n2n^2 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 (i,j)(i, j) requires knowledge of both token ii's query and token jj's key. Until you compute all n2n^2 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.

In[3]:
Code
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]
Out[4]:
Console
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.0x

The "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 O(n2)O(n^2) 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.

Out[5]:
Visualization
Line plot with parabolic growth of pairwise interactions on linear scale.
Pairwise interaction count on a linear scale, showing the parabolic explosion from near zero at short sequences to over 16 million at 4096 tokens. The area under the curve grows far faster than a linear relationship would suggest.
Log-log plot showing linear relationship with slope 2 and 4x growth annotations.
The same data on a log-log scale, revealing the underlying structure as a straight line with slope 2. Each doubling of sequence length produces exactly 4x more pairs, confirming the n-squared relationship with the annotated 4x markers.

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 n2n^2 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.