Self-Attention Complexity

What You’ll Learn

You’ll learn why self-attention costs O(n2d)O(n^2 d) time and O(n2)O(n^2) 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×nn \times n result in step 1.

Self-Attention: Three Operations

Step 1: Q × Kᵀ
Q4×3×Kᵀ3×4=Scores4×4
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)O(n^2 d) work (the two large matrix multiplications). Step 2 (softmax) is O(n2)O(n^2), which is dd times cheaper. The n×nn \times n attention matrix is the bottleneck in both time and space.

(Forgot how Q, K, V work? Review Self-Attention)

Counting Operations

Why O(n2d)O(n^2 d)? Think about what happens when you multiply Q (n×dn \times d) by K⊤K^\top (d×nd \times n).

The result is an n×nn \times n matrix. Each entry is a dot product of two dd-dimensional vectors: dd multiplications, d−1d - 1 additions. So each entry costs O(d)O(d) work, and there are n×nn \times n entries:

n×n×d=n2dn \times n \times d = n^2 d

Softmax operates on each of the nn rows independently: exponentiate nn values, sum, divide. That’s O(n)O(n) per row, nn rows: O(n2)O(n^2). Compared to n2dn^2 d, this is negligible (since dd is typically 64 to 128).

The final multiplication (weights ×\times VV) is an n×nn \times n matrix times an n×dn \times d matrix, costing n×d×n=n2dn \times d \times n = n^2 d again.

Quick Check
Which operation in self-attention is the cheapest?
Your turn
Choose an answer to check your understanding.

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
QKT n²d
33.6M
softmax n²
262.1K
× V n²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⊤QK^\top 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×nn \times n score matrix dominates once n>dn > d.

Why This Is a Problem

A 1,000-token input produces a 1,000 ×\times 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)O(n^2) to O(n)O(n) by never materializing the full matrix)
  • Architectures like Mamba use state-space models for O(n)O(n) time and space
  • Linear attention replaces softmax with kernels to avoid the n×nn \times 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.

Quick Summary

StepTimeSpaceIntuition
QK⊤QK^\topO(n2d)O(n^2d)O(n2)O(n^2)Every query scores against every key
softmaxO(n2)O(n^2)O(n2)O(n^2)Normalize each row (cheap)
×V\times VO(n2d)O(n^2d)O(nd)O(nd)Blend values by attention weights
TotalO(n2d)O(n^2d)O(n2+nd)O(n^2 + nd)Quadratic in sequence length
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×nn \times 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×nn \times 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.