You’ll learn why self-attention costs O(n2d) time and O(n2) space by following the matrix shapes. You’ll also see which operation creates the bottleneck and why quadratic cost is the central engineering constraint for long-context transformers.
Three Operations, One Bottleneck
Self-attention boils down to three matrix operations. The complexity question is really: what shapes are these matrices, and how expensive is each multiplication?
Step through the visualization below. Watch the matrix shapes at each step, especially the n×n result in step 1.
Self-Attention: Three Operations
Step 1: Q × Kᵀ
Time: O(n²d)Space: O(n²)
Each of the n×n entries requires a d-length dot product. 4×4 = 16 entries, each costing 3 multiplications. The n×n matrix is the quadratic bottleneck.
What to look for: Steps 1 and 3 both produce O(n2d) work (the two large matrix multiplications). Step 2 (softmax) is O(n2), which is d times cheaper. The n×n attention matrix is the bottleneck in both time and space.
Why O(n2d)? Think about what happens when you multiply Q (n×d) by K⊤ (d×n).
The result is an n×n matrix. Each entry is a dot product of two d-dimensional vectors: d multiplications, d−1 additions. So each entry costs O(d) work, and there are n×n entries:
n×n×d=n2d
Softmax operates on each of the n rows independently: exponentiate n values, sum, divide. That’s O(n) per row, n rows: O(n2). Compared to n2d, this is negligible (since d is typically 64 to 128).
The final multiplication (weights ×V) is an n×n matrix times an n×d matrix, costing n×d×n=n2d again.
With Q,K∈Rn×d and V∈Rn×d:
Step 1:QK⊤∈Rn×n
Time: O(n⋅n⋅d)=O(n2d)
Step 2:softmax(QK⊤) (row-wise, in-place)
Time: O(n⋅n)=O(n2)
Step 3:softmax(QK⊤)⋅V∈Rn×d
Time: O(n⋅d⋅n)=O(n2d)
Total:O(n2d)+O(n2)+O(n2d)=O(n2d)
For space: the attention score matrix QK⊤ is n×n, so O(n2). The inputs Q,K,V are each n×d, totaling O(nd). Total space: O(n2+nd).
🤔Quick Check
Which operation in self-attention is the cheapest?
Your turn
Choose an answer to check your understanding.
Try another option
Compare the formulas: which one doesn't have the d factor?
Correct!
Softmax costs O(n\u00b2), while the two matrix multiplications each cost O(n\u00b2d). Since d is typically 64-128, softmax is 64-128x cheaper. It's negligible in practice.
Answer: B
Softmax costs O(n\u00b2), while the two matrix multiplications each cost O(n\u00b2d). Since d is typically 64-128, softmax is 64-128x cheaper. It's negligible in practice.
Attempts: 0
Feeling the Quadratic Growth
Numbers make this concrete. Click through different sequence lengths below and watch how the operation counts grow. Toggle between time and space to see what dominates.
How Cost Scales with Sequence Length
d = 128
QKTn²d
33.6M
softmaxn²
262.1K
× Vn²d
33.6M
Total: 67.4M operations
Softmax (262.1K) is 128x smaller than the matrix multiplications. It's negligible.
What to look for: Double the sequence length, and the cost roughly quadruples. At 4K tokens, QK⊤ alone requires over 2 billion operations. The softmax bar is barely visible next to the matrix multiplications. Toggle to “Space” and notice how the n×n score matrix dominates once n>d.
Why This Is a Problem
A 1,000-token input produces a 1,000 × 1,000 attention matrix: 1 million entries. A 10,000-token input produces 100 million entries. A 100,000-token input produces 10 billion entries.
This quadratic scaling is why:
GPT-2 had a 1,024-token context window
Processing a full book (500K tokens) would require a 250-billion-entry attention matrix per layer
Researchers developed Flash Attention (reduces memory from O(n2) to O(n) by never materializing the full matrix)
Architectures like Mamba use state-space models for O(n) time and space
Linear attention replaces softmax with kernels to avoid the n×n matrix entirely
✍️Fill in the Blanks
Self-attention has time complexity O() and space complexity O(). The bottleneck is the attention matrix.
Fill in every blank, then check your answer.
A blank is still empty.
Fill in every blank before checking your answer.
Not quite.
Try another answer, or reveal the solution.
Correct.
One accepted answer is shown in each blank.
Answer revealed.
The blanks now show the answers.
Quick Summary
Step
Time
Space
Intuition
QK⊤
O(n2d)
O(n2)
Every query scores against every key
softmax
O(n2)
O(n2)
Normalize each row (cheap)
×V
O(n2d)
O(nd)
Blend values by attention weights
Total
O(n2d)
O(n2+nd)
Quadratic in sequence length
Multi-Head Attention Complexity
With h attention heads, each head uses dimension dk=d/h. The cost per head is O(n2⋅d/h), and with h heads the total stays O(n2d). Multi-head attention doesn’t change the asymptotic complexity. It just reorganizes how the d dimensions are used.
Including the Linear Projections
The full self-attention layer also includes the WQ, WK, WV, and WO projection matrices, each d×d. Projecting all n tokens through one of these costs O(nd2). With four projections:
Projection cost=O(nd2)
So the complete self-attention layer is O(n2d+nd2). For short sequences (n<d), the projection cost dominates. For long sequences (n>d), the attention matrix dominates.
Feed-Forward Network Complexity
For comparison, the FFN layer (with hidden size 4d) costs O(nd2) in time but only O(nd) in space (the largest intermediate is the n×4d hidden activation). The FFN is sequence-length-linear, so it’s never the bottleneck for long sequences.
Learning Checklist
I can break self-attention into three operations and state the cost of each
I know which operation is cheapest (softmax) and why
I can explain why doubling sequence length roughly quadruples the cost
I understand that the n×n matrix is both the time and space bottleneck
I’m aware of approaches that avoid this quadratic cost (Flash Attention, linear attention, SSMs)
Now that you see why the n×n matrix is the bottleneck, learn how Linear Attention avoids it entirely by reordering the computation, or how the KV Cache eliminates redundant work during generation.