Before you start, here is the vocabulary reminder that makes every answer readable.
Read the two figures first — every answer below points back at them.
Figure 1 is the growth-curve picture. The horizontal axis is the sequence length n; the vertical axis is the number of operations (in millions) for a fixed d=512. The black line is the projection cost nd2 (a straight line — linear in n). The red line is the attention cost n2d (a parabola — quadratic in n). Follow the red parabola upward and notice it stays below the black line for small n, then crosses it at the dashed vertical mark where n=d, and shoots above it afterwards. That single crossing point is the whole story of "which term dominates": left of it, projections rule; right of it, attention rules.
Figure 2 is the score-matrix picture. Each little square is one entry Sij = the score between query token i (row) and key token j (column). A filled square is a score we actually compute and store. On the left ("full attention") the entire n×n grid is filled black — every token pair exists, which is why the cost is quadratic. On the right ("causal attention") only the red lower triangle is filled — token i may only look at itself and earlier tokens, so roughly half the grid (n2/2) is computed. Compare the two grids side by side: masking removes a constant fraction, but the grid is still n×n in shape, so the asymptoticn2 cost is unchanged.
Multi-head attention is asymptotically h times more expensive than single-head attention.
False. The h heads split the dimension (dk=d/h), so h heads each costing n2dk sum to h⋅n2(d/h)=n2d — identical to one full-dimension head (the red parabola in Figure 1 does not move when you change h).
The linear projection cost gets multiplied by the number of heads h.
False. Q, K, V and WO each act once on the full d-dimensional space; splitting into heads is just a reshape, so the projection term stays O(nd2) (the black line in Figure 1), never O(hnd2).
Doubling the sequence length doubles the total compute.
False in general. The dominant n2d term quadruples when you double n (walk twice as far right on the red parabola in Figure 1); only the nd2 projection term doubles. For long sequences the quadratic term wins, so cost roughly quadruples.
For very short sequences (n≪d), the linear-projection term dominates, not attention.
True. When n is small you are left of the crossover in Figure 1, where the black nd2 line sits above the red n2d curve, so most work is in the Q/K/V/output projections, not the score matrix.
Doubling the batch size b leaves the total cost unchanged.
False. Every sequence in the batch runs the full pipeline independently, so both time and memory scale linearly with b: the true cost is O(bn2d+bnd2). Batching improves hardware utilization, not the per-sequence cost.
The O(n2) memory cost disappears at inference time.
Only under specific conditions. With batch size 1 and streaming/flash-style kernels that never materialize the full matrix, peak working memory can fall toward O(nd); but any framework that caches the whole score matrix, or a batch of size b, still holds O(bn2), and training always must store it for backprop.
Self-attention connects any two tokens in a single step, unlike an RNN.
True. Attention has a depth-1 path between every pair of tokens — that is why the full n×n grid in Figure 2 exists — whereas an RNN needs up to n sequential steps to relay information across the sequence.
Sparse attention reduces the asymptotic time below O(n2d).
True. Patterns like O(nnd) fill only a structured subset of the Figure 2 grid, trading global exactness for sub-quadratic cost — see 4.3.2-Sparse-attention-patterns.
A learned relative-position bias table is "free" because it is just an addition to the scores.
False. A relative/pairwise positional bias adds one bias term to every one of the n×n scores, an extra O(n2) compute and, for learned bias tables, extra memory of the same shape — it rides on top of the same quadratic bottleneck rather than removing it. See 5.2.3-Positional-encoding-scaling.
Linear attention removes the quadratic term entirely and is exact.
Half-true — sub-quadratic, but not exact. Full attention computes (QKT)V: forming QKT first costs n2. Linear attention replaces softmax with a kernel feature map so the products can be reassociated as Q(KTV); now KTV is a small dk×dk matrix built in O(nd2), dodging the n×n step. But softmax is non-linear and cannot be factored this way, so the kernel is an approximation of it — same shape of output, different function.
"Attention FLOPs are h⋅n2⋅d, because we do n2d work per head across h heads."
Error: double counting d. Each head does n2dk work, and dk=d/h. The h heads together give n2d, nothn2d. The head factor cancels the split dimension.
"The output projection WO is free, so total projection cost is 3nd2."
Error: WO is not free. Q, K, V and the output projection each cost nd2, giving ≈4nd2. Forgetting WO undercounts the linear term by a quarter.
"Memory is O(n2d) because we store the score matrix and it depends on d."
Error: the stored matrix is n×n, not n×n×d. Scores are a single number per token pair (one square per cell in Figure 2), so memory is O(n2) per sequence — total O(bn2) with a batch — plus O(bnd) for the Q/K/V/output activations.
"Since dk≈d, we can just write total time as O(n2d) and drop nd2."
Error: dropping nd2 is wrong when n<d. Left of the crossover in Figure 1 the nd2 term dominates, so both terms must be kept: O(n2d+nd2).
"An RNN uses less memory (O(d)) and less time, so it is strictly better for long sequences."
Error: better on memory, not on usable long-range connection. RNNs need n sequential steps and suffer vanishing gradients over long distances — attention's cost buys instant global reach, which the RNN cannot match.
"The softmax step is the quadratic bottleneck."
Error: softmax is O(n2) but cheap per element. The quadratic shape comes from building the n×n score matrix QKT (the O(n2d) step); softmax merely normalizes those already-computed scores.
"Adding a relative-position bias makes attention asymptotically slower."
Error: it is the same order. The bias adds O(n2) additions on top of the O(n2d) score computation, so it inflates the constant factor and memory but does not change the O(n2d) growth shape.
Why is the score matrix S=QKT inherently quadratic in n?
Because every token's query must be compared against every token's key to decide who to attend to, producing all n×n pairwise scores (the full grid in Figure 2) — there is no way to get a full attention weight without the pair existing.
Why does using more heads not change asymptotic complexity?
More heads reshape the same d-dimensional space into h blocks of size d/h; the total arithmetic is conserved because the per-head savings (dk<d) exactly offset the head count. See 4.1.7-Multi-head-attention.
Why can Q, K, V be split across heads but WO is still applied once over the full d?
Because Q/K/V are inputs to h independent attention computations, so they are literally sliced into h blocks of width d/h that each head consumes separately. WO acts after the heads are concatenated back into one d-vector — there is only one full-width vector left to mix, so there is nothing to slice and it costs nd2 exactly once.
Why does attention overtake linear projections only at large n?
Projections grow like nd2 (linear in n, the black line in Figure 1) while attention grows like n2d (quadratic, the red curve); the ratio is nd2n2d=n/d, so attention wins once n>d — exactly the crossover marked in the figure.
Why does the batch factor b multiply cost but never appear in O(n2d) shorthand?
Each of the b sequences runs the identical pipeline, so b is a clean linear multiplier on both compute and memory (O(bn2d+bnd2)); we drop it when comparing the shape of two architectures because it multiplies them equally, but you must restore it when sizing a real GPU.
Why does reassociating (QKT)V into Q(KTV) make linear attention cheap?
Matrix multiplication is associative, so the result is unchanged, but KTV collapses the n axis first into a tiny dk×dk summary — you never build the n×n grid of Figure 2, turning n2d into nd2. This only works once softmax is dropped, because softmax sits betweenQKT and V and blocks the reordering.
Why can streaming (row-by-row) attention cut peak memory from O(n2) to O(nd) at inference?
Process the score matrix of Figure 2 one query row at a time: for query i compute its n scores against all keys, softmax that single row, immediately multiply by V to get that token's output, then discard the row before starting row i+1. You only ever hold one length-n row plus the length-d running output, so working memory is O(n+d)=O(nd) overall, never the full n×n grid — this is the core idea behind flash/streaming attention.
Why is gradient checkpointing important for long-context training?
The n×n activation matrices across all layers dominate memory; checkpointing recomputes them in the backward pass instead of storing them, trading extra compute for much lower peak memory. See 4.5.1-Memory-optimization-techniques.
Why does the dk scaling inside softmax not affect the complexity?
It is a single scalar division applied per score — an O(n2) operation with tiny constant — so it changes numerical stability, not the asymptotic cost.
Why can't we naively feed an entire novel into a standard transformer?
A novel is tens of thousands of tokens; the Figure 2 grid becomes enormous (e.g. 50k2=2.5 billion entries per head per layer), exploding n2 memory and compute past hardware limits — hence efficient-attention research exists.
What happens to the cost ratio n/d when n=d exactly?
Attention and projection terms are asymptotically balanced (n2d=nd2 when n=d) — this is precisely the crossover mark in Figure 1; neither dominates, and constant factors (the 4 in 4nd2) decide which is larger in practice.
What is the complexity when n=1 (a single token)?
Attention degenerates: the score grid of Figure 2 is a single 1×1 square, softmax is trivial (weight =1), and cost collapses to the projections O(d2) — the quadratic term vanishes.
How does causal (masked) attention change the count?
In autoregressive models each token attends only to itself and earlier tokens — the red lower triangle in Figure 2, about n2/2 scores. The constant halves but the asymptotic cost stays O(n2d); masking saves a factor of 2, not an order.
What if all keys are identical, so every score is equal?
The softmax outputs a uniform distribution and the output becomes the plain average of value vectors; complexity is unchanged — cost depends on shapes, not on the values inside the matrices.
How does adding a relative-position bias change the picture?
It adds an O(n2) bias term (and, for a learned table, O(n2) extra memory) on top of the score matrix — the same quadratic shape, a larger constant. See 5.2.3-Positional-encoding-scaling.
In the limit n→∞ with d fixed, which term governs everything?
The n2d attention term (the red parabola in Figure 1), since it grows without bound faster than the linear nd2 term; asymptotically the projection cost becomes a rounding error.
What happens to memory if you compute attention but never store scores for backprop (inference only)?
You can process row-by-row and discard each row's scores after use (see the streaming Why-question), so peak working memory can drop toward O(nd) per sequence.
If h=d (one dimension per head, dk=1), does the model get cheaper?
No — attention is still hn2dk=d⋅n2⋅1=n2d, unchanged; you have simply fragmented the representation as far as it can go without altering asymptotic cost.
Recall One-line summary to lock in
The true total is O(bn2d+bnd2): the first term is the unavoidable all-pairs score grid, the second is the four full-width projections — b multiplies everything, and neither term ever carries a factor of h.