Log inStart free
LESSON 9.06 · 5 STAGES · 2 CONTROLS · 3 CHECKSPRO · PREVIEW

Attention complexity

Watch the score grid grow fourfold each time the token count doubles.


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

Go Pro · $8.25/mo
EXPLAINER · 9.06 · ATTENTION BAY16 TOKENS · 256 SCORES · 2,048 FLOPS
01
Stage 1 of 5 · Score grid
Each query scores every key. 16 tokens make 16 × 16 = 256 attention scores.
Explain it simply

The panel shows the last token's top score: the full stop meets "warm". Repeated words, like "the", share one vector and no position, so their rows and columns match. One score costs 4 multiply-adds: 8 FLOPs. This head reads every key, like an encoder. A decoder keeps the causal half: 136 scores.

A 16 by 16 grid of scores, one row per query and one column per key. The grid costs 2,048 FLOPs.

tokens, Q, K, Vweight matrices, tilesscores, outputnegative score, too big
thecatsatonthematbecauseitwaswarmandthesunwasout.thecatsatonthematbecauseitwaswarmandthesunwasout.one score: query 16 · key 10 "warm"q16k10q×k0.280.620.13−0.061.172.18−0.36−0.320.331.35−0.050.020.82= sum ÷ √41 score = 4 multiply-adds256 scores × 4 = 1,024 multiply-adds× 2 = 2,048 FLOPsevery key, as in an encoder · a decoder's causal half: 136scores S = QKᵀ ÷ √dkeys →queries →16 × 16 = 256 scoresthecatsatonthematbecauseitwaswarmandthesunwasout.thecatsatonthematbecauseitwaswarmandthesunwasout.one score: query 16 · key 10 "warm"q16k10q×k0.280.620.13−0.061.172.18−0.36−0.320.331.35−0.050.020.82= sum ÷ √41 score = 4 multiply-adds256 scores × 4 = 1,024 multiply-adds× 2 = 2,048 FLOPsevery key, as in an encoder · a decoder's causal half: 136scores S = QKᵀ ÷ √dkeys →queries →16 × 16 = 256 scores
Toy: 1 head · the first n tokens of the 9.C2 text · one vector per word · random, untrained weights
STAGE 1 / 5
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
S=Q K⊤d\sy{s}{out}{S} = \frac{\sy{q}{data}{Q}\,\sy{k}{data}{K}^{\top}}{\sqrt{\sy{d}{param}{d}}}
2 n2d=2⋅162⋅4=2,048 FLOPs2\,n^2 d = 2 \cdot 16^2 \cdot 4 = 2{,}048\ \text{FLOPs}

Each score is one query dot one key, scaled by one over root d.

Common mix-ups

MYTHTwice the tokens means twice the attention cost.

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

Predict, then reveal
3 CHECKS · PRO
Q01Double the tokens from 16 to 32. How many times more scores does the grid hold?

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

PRO LESSON · YOU WATCHED STAGE 1 OF 5
Unlock the full explainer
  • 4 more stages and 2 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.