Attention Is All You Need
medium 40 minThe 2017 paper that introduced the Transformer.
Check yourself
One question at a time. Each answer explains why.
Question 1 of 12
What did the Transformer remove from translation models?
Why can a Transformer train faster than an RNN on the same GPUs?
q·k for d_k = 64 has variance about 64. What do we divide the scores by?
A chat model generates text. Which mask does its self-attention use?
In encoder-decoder attention, where do the keys and values come from?
KV cache for 32 layers, 8 KV heads of 128, fp16, 8,192 tokens?
FlashAttention or PagedAttention: which one changes the result of attention?
Sequence length 8,192, d = 512. Which layer type does more work per layer?
What did learned positional embeddings do in Table 3, row E?
You need embeddings for a search index. Which shape do you pick?
Bahdanau attention versus Transformer self-attention: what is the main difference?
Base model: 0.4 s per step for 100,000 steps. About how long?
Pick an answer to see why.
Not quite. The abstract: based solely on attention, dispensing with recurrence and convolutions. Attention itself already existed.
Not quite. An RNN's step t waits for step t - 1. Self-attention has O(1) sequential steps (Table 1).
Not quite. √64 = 8 brings the variance back to 1, so softmax does not saturate.
Not quite. Generation must not see future tokens. The paper does this in decoder self-attention (§3.2.3).
Not quite. Queries come from the decoder, keys and values from the encoder output (§3.2.3).
Not quite. 2 × 32 × 8 × 128 × 2 × 8,192 = 1,073,741,824 bytes.
Not quite. FlashAttention changes the order of memory reads and writes; PagedAttention changes how the KV cache is laid out in memory. Both compute the same exact attention.
Not quite. n²·d = 34.4 billion against n·d² = 2.1 billion. Attention is cheaper only when n is smaller than d.
Not quite. 25.7 against 25.8 BLEU. The authors kept sinusoids hoping they extend to longer inputs.
Not quite. Every token sees both sides of the text, which suits representing a whole passage.
Not quite. Bahdanau (2014) added attention to RNNs. The Transformer removed the RNNs and attends within each sequence too.
Not quite. 40,000 s is about 11.1 hours. The paper rounds to 12 hours (§5.2).
Flashcards
Say your answer, then reveal it.
Card 1 of 12
Query, key, value: what does each do?
Query: what this token looks for. Key: what it offers for matching. Value: what it hands over when chosen.
Say your answer, then reveal it.
The attention equation?
softmax(QKᵀ / √d_k) V (equation 1, §3.2.1).
Say your answer, then reveal it.
Why divide by √d_k?
q·k of random unit-variance parts has variance d_k. Large scores saturate softmax and the gradients vanish (footnote 4).
Say your answer, then reveal it.
Why positional encoding?
Attention ignores order. Sine and cosine waves of growing wavelength are added to each embedding (§3.5).
Say your answer, then reveal it.
What does the decoder mask do?
Sets scores for later positions to minus infinity, so their weight is exactly 0 and a word cannot see the words it must predict.
Say your answer, then reveal it.
What does multi-head add?
Several relations at once. 8 heads of 64 cost about the same as one of 512; one head was 0.9 BLEU worse (Table 3, A).
Say your answer, then reveal it.
Table 1: self-attention against recurrent?
Cost O(n²·d) vs O(n·d²); sequential steps O(1) vs O(n); longest path O(1) vs O(n).
Say your answer, then reveal it.
KV cache size formula?
2 × layers × KV heads × head dim × bytes per number × tokens.
Say your answer, then reveal it.
Encoder, decoder, encoder-decoder: uses?
Encoder: embeddings and classification (BERT). Decoder: generation and chat (GPT). Both: translation, summarisation.
Say your answer, then reveal it.
Base model shape and training?
6 + 6 layers, d_model 512, 8 heads, d_ff 2048, 65M parameters, 12 hours on 8 P100 GPUs.
Say your answer, then reveal it.
What replaced sinusoidal positions in most models?
Rotary position embedding (RoPE), applied inside attention (Su and others, 2021).
Say your answer, then reveal it.
Headline results?
28.4 BLEU English to German, 41.8 English to French (Table 2), big model, 3.5 days.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.