4.1.13 · D3Transformer Architecture

Worked examples — Computational complexity of attention

2,709 words12 min readBack to topic

Before anything, let us make sure every symbol is earned.


The scenario matrix

Every question this topic can ask lands in one of these cells. The worked examples below are labelled with the cell they cover, and together they hit all of them.

Cell Regime / input What decides the answer Worked example
C1 Short sequence, Projections dominate Ex 1
C2 Long sequence, Attention dominates Ex 2
C3 Crossover, find Set the two terms equal Ex 3
C4 Scaling law ("double ") Ratio of new/old, quadratic behaviour Ex 4
C5 Head count varies Show cancels asymptotically Ex 5
C6 Degenerate inputs ; Limiting / boundary behaviour Ex 6
C7 Real-world memory word problem Bytes = floats × 4, sum over heads·layers Ex 7
C8 Exam twist (spot the wrong factor) The "" trap on projections Ex 8

Ex 1 — Cell C1: short sequence, projections rule

Forecast: is tiny compared to . Guess before reading: attention or projections? Write down your guess.

  1. Attention FLOPs . Why this step? This is the score-computation-plus-aggregation term; it grows with , but is small here. M.
  2. Projection FLOPs . Why this step? Q, K, V, and each cost ; they scale only linearly in , so a short sequence keeps them from shrinking. M.
  3. Ratio attention / projection . Why this step? The ratio is the single number that tells you who wins; means projections dominate.

Verify: Projections are larger. Sanity: , so the term () is squashed while () stays huge. Matches the parent's claim that short sequences are projection-bound. ✔


Ex 2 — Cell C2: long sequence, attention rules

Forecast: We just quadrupled-and-more the length. Guess the winner.

  1. Attention FLOPs . Why this step? — the quadratic term has finally exploded. B.
  2. Projection FLOPs B. Why this step? Projections grew only from Ex 1 (linear in ), while attention grew (quadratic).
  3. Ratio . Why this step? We compute the ratio because a single number tells us who dominates: now means attention is the bigger cost, the exact reverse of Ex 1 where the ratio was . Watching this one number flip from to is the whole story of the quadratic takeover.

Verify: Attention is bigger. Cross-check with Ex 1: going from to is longer. Attention so grows ; projections grow . Ratio should shift by : indeed . ✔ (This is the promised quadratic takeover.)


Ex 3 — Cell C3: find the crossover length

Forecast: From Ex 1 (: projections win) and Ex 2 (: attention wins), the crossover is somewhere between. Guess an order of magnitude.

  1. Set the terms equal: . Why this step? "Equal cost" is exactly the definition of the crossover; solving it locates .
  2. Cancel one (valid since ): . Why this step? Since , this simplifies beautifully to .
  3. Plug numbers: . Why this step? Astonishingly clean: crossover is at , independent of how you slice heads.

The figure below plots both costs against for this exact model. Follow the teal curve (projections, ) — it rises as a gentle straight line because it is linear in . The burnt-orange curve (attention, ) starts far lower on the left but bends upward as a parabola. The plum dot marks where they cross at ; the shaded teal region to its left is the "projections win" zone (Cell C1), and the shaded orange region to its right is the "attention wins" zone (Cell C2). The picture makes concrete why Ex 1 and Ex 2 came out the way they did: they live on opposite sides of that dot.

Figure — Computational complexity of attention

Verify: At : attention ; projection . Identical. ✔ And sits between and as predicted. This rule is why 512-dim models feel the quadratic wall around 2K tokens — see 4.3.2-Sparse-attention-patterns.


Ex 4 — Cell C4: the "double the length" scaling law

Forecast: Everyone's first guess is "2×." Is it?

  1. Attention factor: attention , so doubling gives . Why this step? Squaring the length is the whole reason the parent note warns about quadratic blow-up.
  2. Compute both totals. At : attention ; projection . Total . At : attention ; projection . Total . Why this step? The total mixes a piece with a piece, so the real factor lands between 2 and 4.
  3. Total factor . Why this step? We divide new-total by old-total because "growth factor" is by definition the ratio of the two bills; this single number is what you multiply your GPU budget by. It also confirms the intuition from step 2 that the real answer sits strictly between the pure-attention and the pure-projection .

Verify: Between 2 and 4, closer to 4 because attention already dominates. ✔ Attention alone: exactly. ✔ Memorable takeaway: near-quadratic regime, doubling length ≈ 3.3× the bill.


Ex 5 — Cell C5: does more heads cost more?

Forecast: "12 heads = 12× work" — true or trap? Commit to an answer.

  1. Attention, : . Why this step? One head sees the full dimension.
  2. Attention, : . Why this step? , so the product is unchanged — the heads share the budget, not multiply it.
  3. Projections (both): , identical in both cases because Q, K, V, act on the full once. Why this step? This is the exact point the parent's "misconception" callout hammers.

Verify: Attention identical () and projections identical. Head count did not multiply either term. ✔ Splitting heads only reshapes the same arithmetic — the core lesson of 4.1.7-Multi-head-attention.


Ex 6 — Cell C6: degenerate & limiting inputs

Forecast: Do the formulas quietly do the sensible thing, or blow up?

  1. (a) : attention ; projections ; memory . Why this step? No tokens ⇒ no work ⇒ zero. The formula degrades gracefully; no division-by-zero anywhere (softmax normalizes over the row, and there are no rows).
  2. (b) : attention ; projections ; score matrix is . Why this step? A single token attends only to itself — one score, softmax of one number . Projections still cost a full per matrix because you still transform that one vector. Projections dominate absolutely here (), matching Cell C1's logic taken to the limit.
  3. (c) : attention and ⇒ projections . Why this step? A zero-width head carries no information and costs nothing — the degenerate "no model" case. Useful as an algebraic sanity anchor, not a real config.

Verify: All three give finite, non-negative, sensible values; the only "special" one, , yields projections attention . ✔ No formula divides by or , so no singularities.


Ex 7 — Cell C7: real-world GPU memory word problem

Forecast: The parent quoted "~2.3 GB." Guess whether you'll reproduce it.

  1. Floats in one head's score matrix . Why this step? Every query–key pair produces one score; that's an grid — the memory analogue of the compute term. (This is exactly what 4.5.1-Memory-optimization-techniques tries to shrink.)
  2. Total floats . Why this step? Each of the 12 heads in each of the layers keeps its own score grid during the forward pass.
  3. Bytes (FP32) bytes GB. Why this step? Convert floats → bytes (×4), then bytes → GiB (÷ ). Units: floats × (bytes/float) = bytes. ✔
  4. FP16: halve the bytes/float → GB. Why this step? Precision only changes the bytes-per-float constant, not the count.

Verify: GB matches the parent's "~2.3 GB." ✔ FP16 gives exactly half: . ✔ This memory (not the parameters!) is why long-context training needs checkpointing — the second [!mistake] in the parent.


Ex 8 — Cell C8: the exam trap

Forecast: Does the head count belong in the projection term? (You solved this idea in Ex 5.)

  1. Spot the error: the extra factor is wrong. Projections Q, K, V, operate once over the full -dimensional space; heads are a reshape of that same output, not separate full-width projections. Why this step? This is the exact " trap" the parent's misconception callout warns about.
  2. Wrong value (what the claim gives): B. Why this step? We compute the wrong number explicitly so we can measure exactly how far off the trap lands — it arises from wrongly multiplying the correct projection cost by the head count . Naming the actual figure (29.0B) turns a vague "that's too big" into a precise "that's too big," which is what a grader must justify.
  3. Correct value: B. Why this step? Drop the spurious ; the true projection cost is smaller.

Verify: The claim over-counts by exactly : . ✔ Correct answer B (agrees with Ex 5). The tempting-but-wrong version is B.


Pulling the cases together

yes

no

equal at n = 4d

Given n and d

Is n less than 4d

Projections dominate 4nd2

Attention dominates n2 d

Short seq regime C1

Long seq regime C2

Crossover C3

Recall Self-test (reveal after answering)

The crossover length in terms of ::: (independent of head count). Doubling in the attention-dominated regime multiplies attention FLOPs by ::: (since attention , and ). Does using 12 heads instead of 1 multiply the projection cost by 12? ::: No — projections act once over the full ; heads only reshape it. Memory for score matrices scales as ::: floats per layer (quadratic in ). FP32 → FP16 changes the memory bill by a factor of ::: (bytes-per-float only).