AlgoViz

Attention Is All You Need

medium 40 min

The 2017 paper that introduced the Transformer.

Remember it

The few things worth knowing cold, and how to make them stick.

Memory hook

Queries ask, keys answer, values come back. Everyone asks everyone, all at once.

A whisper line is slow and forgets the first word. In a ring, Max holds up a question card, every badge answers, and each bag pours into Max's cup as much as its badge matched.

Cheat sheet

Paper
Vaswani et al., NIPS 2017, arXiv 1706.03762
Equation
softmax(QKᵀ/√d_k) V
Base model
6 + 6 layers, 512, 8 heads of 64, 2048
Parameters
65M base, 213M big
Training
8 P100: 12 h base, 3.5 days big
BLEU
28.4 En-De, 41.8 En-Fr
Self-attention
O(n²·d), O(1) steps, O(1) path
Recurrent
O(n·d²), O(n) steps, O(n) path
KV cache
2 × L × kv heads × head dim × bytes × tokens
Since then
decoder-only, RoPE, pre-norm, GQA, FlashAttention

Spot it when you see

  • "How does an LLM work?"
  • Long context is slow or expensive.
  • GPU memory per user.
  • "Why not an RNN?"

Tricks that stick

  • Badge, question, bag: key, query, value.
  • Square root for the spread: √d_k.
  • Minus infinity, not zero, for the mask.
  • Prefill multiplies, decode reads.

Do not forget

  • Saying the Transformer invented attention.
  • Forgetting the factor 2 (K and V) in the cache formula.
  • Masking with 0 instead of minus infinity.
  • Quoting the 2017 design (post-norm, sinusoids) as current practice.
  • Calling FlashAttention an approximation: it is exact.