Attention Is All You Need: the big picture
Why a 2017 translation paper became the blueprint for every frontier model
Everything after this lesson — GPT, BERT, Whisper, ViT, Claude — is a variation on the architecture in one 2017 paper. Understanding why attention replaced recurrence (parallelism and short paths), how the encoder and decoder stacks fit together, and how the model is trained and run gives you the map you need for the detailed derivations in 7.2 Scaled dot-product and multi-head attention, fully derived–7.4 Inside the block: residuals, LayerNorm, FFN and embeddings, the architecture families in 7.5 Encoder-only: BERT and friends (and sentence embeddings)–7.7 Encoder–decoder: T5, BART and choosing an architecture, and the inference engineering in 7.10 Efficient inference: KV cache, GQA, FlashAttention, speculative decoding, quantization.
You'll be able to
- Explain the two costs of recurrence that self-attention removes and quantify them with the per-layer complexity table
- Trace a token through the original encoder–decoder: embeddings, positional encodings, self-attention, cross-attention, feed-forward, output softmax
- Describe how the transformer is trained with teacher forcing and run autoregressively at inference
- List what the 2017 design got right and which components were later replaced
Key ideas
- Recurrence costs O(n) sequential steps and O(n) path length; self-attention costs O(1) of both at the price of O(n²d) compute per layer.
- The original transformer is an encoder–decoder: 6 encoder blocks (self-attention + FFN) and 6 decoder blocks (masked self-attention + cross-attention + FFN), d_model = 512, 8 heads.
- Cross-attention takes queries from the decoder and keys/values from the encoder; masked self-attention keeps the decoder autoregressive.
Show 3 more key ideas
- Training uses teacher forcing with a causal mask, so all next-token predictions are computed in one parallel forward pass; inference generates one token at a time.
- Warmup-then-decay learning rates, label smoothing, dropout 0.1 and BPE vocabularies were set here and became defaults.
- What survived: self-attention, multi-head attention, explicit positions. What changed: pre-norm, RoPE, SwiGLU, RMSNorm, decoder-only dominance, MoE, scale.
Suggested path through this lesson
Why recurrence had to go
By 2016 the best translation systems were encoder–decoder RNNs with attention (6.6 Seq2seq, encoder–decoder and beam search, 6.7 Attention is born). They worked, but two costs were baked into the recurrence . First, sequential computation: position cannot be processed before position , so a sentence of tokens needs dependent steps while GPUs, which love parallel work, sit idle. Second, path length: information from token 1 reaches token only after passing through transformations, each an opportunity to forget or to shrink a gradient. LSTM gates (6.5 LSTM and GRU) soften the second problem; nothing fixes the first.
Attention had already shown that letting the decoder look directly at every encoder state removes the bottleneck of a single summary vector. The 2017 paper by Vaswani et al. asked the radical question: what if attention is the only mechanism that mixes information across positions?
Intuition. An RNN passes a note down a line of people, one at a time. Self-attention puts everyone in a room: each person looks at everyone else and decides whom to listen to, all at once.
The cost table that justified the bet
Table 1 of the paper compares one layer of each design for a sequence of tokens with representation size :
| layer type | compute per layer | sequential operations | maximum path length |
|---|---|---|---|
| self-attention | |||
| recurrent | |||
| convolutional (kernel ) |
Self-attention pays in compute but wins everywhere else: no sequential dependency, and every pair of positions is one step apart. When (typical sentences, ) it is even cheaper than recurrence. The term only became painful years later with long contexts — the whole story of 7.10 Efficient inference: KV cache, GQA, FlashAttention, speculative decoding, quantization and 7.12 Beyond vanilla attention: state space models, long context and hybrids.
A tour of the architecture
The original model is an encoder–decoder for translation. Read the diagram bottom to top.
- Embeddings + positions. Each token id becomes a learned vector of size , scaled by , and a positional encoding is added (7.3 Positional information: sinusoidal, learned, RoPE, ALiBi) because attention alone cannot tell order.
- Encoder stack. identical blocks. Each block: multi-head self-attention (7.2 Scaled dot-product and multi-head attention, fully derived) → add the residual and apply LayerNorm → position-wise feed-forward network (two linear layers with a ReLU, inner size ) → add & norm (7.4 Inside the block: residuals, LayerNorm, FFN and embeddings). The encoder reads the whole source sentence bidirectionally.
- Decoder stack. Also 6 blocks, each with three sub-layers: masked self-attention over the target tokens generated so far (a position may not look at later positions), cross-attention whose queries come from the decoder and whose keys and values come from the encoder output, and the feed-forward network.
- Output. A linear layer maps each decoder position to vocabulary logits, and a softmax gives . The output projection shares its weights with the input embeddings.
The base model has about 65M parameters with heads of size ; the "big" model doubles to 1024 with 16 heads (213M parameters).
How it was trained and run
Training is teacher forcing (6.6 Seq2seq, encoder–decoder and beam search): feed the whole target sentence shifted right by one token, apply the causal mask, and compute cross-entropy on all positions in parallel — one forward pass yields predictions. Details that became standard: Adam with , a learning rate that warms up linearly for 4,000 steps and then decays like (the formula is ), dropout 0.1, label smoothing 0.1, and byte-pair-encoded vocabularies of roughly 32–37k tokens (6.1 Text to numbers: tokenization). The base model trained for 12 hours on 8 P100 GPUs and reached 27.3 BLEU on English→German; the big model took 3.5 days and set new records (28.4 BLEU En→De, 41.8 En→Fr) at a fraction of the compute of previous systems.
At inference the decoder is autoregressive: start from a begin token, run the whole stack, pick a next token (beam size 4 with a length penalty in the paper), append it, and repeat. The encoder runs once; the decoder runs once per output token — the asymmetry that later motivates the KV cache (7.10 Efficient inference: KV cache, GQA, FlashAttention, speculative decoding, quantization).
What changed between 2017 and 2026
The paper made three contributions that survived: self-attention as the mixing operation, multi-head attention, and the idea that positions must be injected explicitly. Almost everything else was revised:
- Decoder-only won for general-purpose models. GPT dropped the encoder entirely (7.6 Decoder-only: GPT and autoregressive generation); BERT kept only the encoder (7.5 Encoder-only: BERT and friends (and sentence embeddings)); T5-style encoder–decoders remain for translation, speech and some summarization (7.7 Encoder–decoder: T5, BART and choosing an architecture).
- Normalization moved before each sub-layer (pre-norm), making deep stacks train without fragile warmup (7.4 Inside the block: residuals, LayerNorm, FFN and embeddings).
- Components were swapped: sinusoidal positions → rotary embeddings, ReLU → GELU/SwiGLU, LayerNorm → RMSNorm, dense FFNs → mixture-of-experts (7.11 Mixture of Experts), and dropout mostly disappeared at scale.
- Scale became the method. The same block, stacked 96 times with , is GPT-3 (7.9 Scaling laws and compute-optimal training).
The rest of this phase follows that arc: the attention math (7.2), positions (7.3), the block (7.4), the three architectural families (7.5–7.7), building one yourself (7.8), scaling (7.9), inference efficiency (7.10), MoE (7.11) and what lies beyond attention (7.12).
Where this shows up later
- The attention equation is derived and computed by hand in 7.2 Scaled dot-product and multi-head attention, fully derived; positional encodings in 7.3 Positional information: sinusoidal, learned, RoPE, ALiBi; the block and its parameter count in 7.4 Inside the block: residuals, LayerNorm, FFN and embeddings.
- The encoder/decoder split becomes the taxonomy of modern models: 7.5 Encoder-only: BERT and friends (and sentence embeddings), 7.6 Decoder-only: GPT and autoregressive generation, 7.7 Encoder–decoder: T5, BART and choosing an architecture.
- Teacher forcing and autoregressive decoding reappear in sampling (8.5 Decoding and sampling) and inference optimization (7.10 Efficient inference: KV cache, GQA, FlashAttention, speculative decoding, quantization).
- Vision Transformers apply exactly this encoder to image patches (5.7 Vision Transformers); Whisper is this encoder–decoder applied to audio (8.10 Multimodal models).
Common pitfalls (4)
- Thinking the transformer removed the need for the encoder–decoder idea — it kept it; dropping the encoder came later with GPT.
- Assuming attention is always cheaper than recurrence: per layer it is O(n²d) vs O(nd²), so for sequences longer than d it costs more compute, just less wall-clock time.
- Confusing training (parallel, teacher-forced) with inference (sequential, autoregressive) — they have completely different cost profiles.
- Reading the 2017 paper as the current state of the art: post-norm, sinusoidal positions and ReLU FFNs are rarely used in new models.
Recap: all 6 key points
- Recurrence costs O(n) sequential steps and O(n) path length; self-attention costs O(1) of both at the price of O(n²d) compute per layer.
- The original transformer is an encoder–decoder: 6 encoder blocks (self-attention + FFN) and 6 decoder blocks (masked self-attention + cross-attention + FFN), d_model = 512, 8 heads.
- Cross-attention takes queries from the decoder and keys/values from the encoder; masked self-attention keeps the decoder autoregressive.
- Training uses teacher forcing with a causal mask, so all next-token predictions are computed in one parallel forward pass; inference generates one token at a time.
- Warmup-then-decay learning rates, label smoothing, dropout 0.1 and BPE vocabularies were set here and became defaults.
- What survived: self-attention, multi-head attention, explicit positions. What changed: pre-norm, RoPE, SwiGLU, RMSNorm, decoder-only dominance, MoE, scale.
Watch
The best first picture of what a transformer does to a sequence of tokens; watch before reading.
Read
Exercises
- Three-pass read of the paper
Apply the three-pass method (11.1 How to read (and reproduce) papers) to Attention Is All You Need. After pass two, write one paragraph answering: what problem, what mechanism, what evidence? Expected: you can reproduce Figure 1 from memory and explain each arrow.
- Trace the shapes
For a batch of 32 source sentences of 40 tokens and target sentences of 50 tokens with and , write down the tensor shape after every sub-layer of one encoder block and one decoder block, including the cross-attention score matrix. Expected: encoder self-attention scores are ; cross-attention scores are .
- Compute the complexity table
Using the table in the notes, compute compute-per-layer for self-attention and for a recurrent layer at for , and tokens. Expected: attention is cheaper at , equal at , and 256× more expensive at 128k tokens — the reason long context needs the techniques of 7.10 Efficient inference: KV cache, GQA, FlashAttention, speculative decoding, quantization and 7.12 Beyond vanilla attention: state space models, long context and hybrids.
Quiz
Flashcards
These cards are now in your spaced-repetition queue. Review all due cards →