Log inStart free
LESSON 9.C2 · 6 STAGES · 3 CONTROLS · 3 CHECKSPRO · PREVIEW

Attention patterns and their cost

Watch five attention methods run on one sentence and pay different costs.


Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 3 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 9.C2 · PATTERN BAYROW 16 · 136 SCORES · 2,176 FLOPS
01
Stage 1 of 6 · Full attention
In full attention, each token scores every earlier token. The 16 tokens need 136 scores.
Explain it simply

Each row of the grid is one token looking back at the tokens before it. A bigger square means more weight. Row 16, the full stop, spreads its weight over all 16 tokens. Every square costs work, so the grid grows with the square of the length.

A 16 by 16 grid holds 136 squares, one per pair of a token and an earlier token. Row 16 is shown as bars on the right.

tokensattention weights, outputslost weight, errorstateskipped pair
1122334455667788991010111112121313141415151616key j →query i ↓row 16 · “.”: weight on each key0.412345678910111213141516key j →full causal attentionfrom lesson 9.06136 pairs × 16 FLOPs = 2,176 FLOPs1122334455667788991010111112121313141415151616key j →query i ↓row 16 · “.”: weight on each key0.412345678910111213141516key j →full causal attentionfrom lesson 9.06136 pairs × 16 FLOPs = 2,176 FLOPs
Toy: 16 tokens, head width 4, untrained weights
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
aij=softmaxj ⁣(qi⋅kjd),j≤i\sy{a}{out}{a_{ij}} = \mathrm{softmax}_j\!\left(\frac{\sy{q}{data}{q_i} \cdot \sy{k}{data}{k_j}}{\sqrt{d}}\right),\quad j \le i
scores=16⋅172=136,136×4d=2176 FLOPs\text{scores} = \tfrac{16 \cdot 17}{2} = 136,\quad 136 \times 4d = 2176 \text{ FLOPs}

Each row shares its weight over every earlier key.

Common mix-ups

MYTHEvery cheaper method also shrinks the KV cache.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01Which method computes the fewest scores for the 16 tokens?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 3 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.