Before you start: words you need#
| Word | Plain meaning |
|---|---|
| Sequence-to-sequence | A model that reads one sequence (a German sentence) and writes another (its English translation). |
| RNN | Recurrent neural network: reads tokens one after another, carrying a running summary. Step t cannot start until step t−1 is done. |
| Attention | Each position looks at every other position and takes a weighted average of their values, the weights set by how well a query matches each key (10.h). |
| Self-attention | Attention where the queries, keys and values all come from the same sequence. |
| Multi-head attention | Running several smaller attentions side by side, each with its own projections, then joining the results. |
| Encoder / decoder | The encoder reads the input; the decoder writes the output one token at a time, looking at what it has written and at the encoder’s output. |
| BLEU | A translation score from 0 to 100 that counts word overlaps with human translations. A 1–2 point gain was a big deal in 2017. |
| Autoregressive | Each new output token is produced from all the tokens before it (12.d). |
1. The paper#
- Title: “Attention Is All You Need”
- Authors: Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, Illia Polosukhin (Google Brain, Google Research, University of Toronto)
- Venue: NIPS 2017 (now called NeurIPS), Long Beach.
- Link: arXiv:1706.03762
2. The problem#
In 2017 the best translation systems were RNNs with attention bolted on (Google’s GNMT) or convolutional models (Facebook’s ConvS2S). RNNs have one deep flaw for hardware: they are sequential along the sequence. Token 50 waits for token 49. On a GPU, which wants thousands of independent operations at once, that wastes most of the chip during training, and long sentences make it worse.
Convolutions are parallel, but a word only “sees” a distant word after many stacked layers, so long-range relationships are hard to learn.
3. The idea#
Drop recurrence and convolution entirely. Build the whole model out of attention plus small per-position feed-forward networks. Self-attention connects every position to every other in one step, and every position can be computed at the same time during training.
The paper’s own comparison (Table 1) says it directly:
| Layer type | Work per layer | Sequential steps | Longest path between two tokens |
|---|---|---|---|
| Self-attention | O(n² · d) | O(1) | O(1) |
| Recurrent | O(n · d²) | O(n) | O(n) |
| Convolutional | O(k · n · d²) | O(1) | O(log_k n) |
The cost is the n² term: work and memory grow with the square of sequence length. That one term is behind half the papers in this module (FlashAttention, PagedAttention, StreamingLLM, sparse attention).
4. How it works#
The pieces are taught properly in 10.h (attention) and 10.i (the Transformer). The short version, in the paper’s terms:
- Scaled dot-product attention:
softmax(Q·Kᵀ / √d_k) · V. Dividing by √d_k keeps the dot products from growing so large that softmax saturates. - Multi-head: the base model splits d_model = 512 into h = 8 heads of d_k = d_v = 64. Each head has its own Q, K, V projections. Concatenate and project back.
- Encoder: 6 identical layers of (self-attention → feed-forward), each sub-layer wrapped in a
residual connection and followed by layer normalization:
LayerNorm(x + Sublayer(x)). - Decoder: 6 layers of (masked self-attention → attention over the encoder output → feed-forward). The mask stops position i from seeing positions after i, so the decoder stays autoregressive.
- Feed-forward: two linear layers with a ReLU between, inner size d_ff = 2048.
- Positional encoding: attention has no sense of order, so sine and cosine waves of different wavelengths are added to the input embeddings. Learned position embeddings worked “nearly identically” (Table 3, row E).
flowchart LR IN["Source tokens + positions"] --> ENC["Encoder x6: self-attention, feed-forward"] OUT["Tokens written so far + positions"] --> DEC["Decoder x6: masked self-attention, cross-attention, feed-forward"] ENC --> DEC DEC --> SM["Linear + softmax: next token"] SM -. "append, repeat" .-> OUT class IN,OUT io class ENC,DEC compute class SM neutral
The inference consequence is in the dotted arrow. Training is parallel across the whole target sentence because the true sentence is known. Generation is not: the decoder still produces one token per step. The paper says so in its last paragraph: “Making generation less sequential is another research goals [sic] of ours.” Everything in Module 12 — the KV cache (12.e), prefill versus decode (12.c), continuous batching (12.j) — exists to make that sequential loop cheap.
5. Results#
From the paper’s Table 2 and Table 3 (WMT 2014 newstest2014):
| Model | Params | EN→DE BLEU | EN→FR BLEU | Training cost (FLOPs) |
|---|---|---|---|---|
| GNMT + RL (previous Google system) | – | 24.6 | 39.92 | 2.3·10¹⁹ (DE) |
| ConvS2S ensemble (previous best) | – | 26.36 | 41.29 | 7.7·10¹⁹ (DE) |
| Transformer base | 65 M | 27.3 | 38.1 | 3.3·10¹⁸ |
| Transformer big | 213 M | 28.4 | 41.8 | 2.3·10¹⁹ |
- Base model: 100,000 steps, about 12 hours on one machine with 8 NVIDIA P100 GPUs.
- Big model: 300,000 steps, 3.5 days on the same machine.
- One discrepancy to know about: the text of section 6.1 says the big model reached 41.0 on EN→FR, while the abstract and Table 2 say 41.8. The abstract and table are the figures usually quoted.
- Ablations (Table 3): one head was 0.9 BLEU worse than the best setting; too many heads also hurt. Smaller d_k hurt quality, which the authors read as “compatibility is not easy to compute”.
The headline was not just higher BLEU but higher BLEU for a fraction of the training FLOPs.
6. What held up and what did not#
Held up
- Attention plus feed-forward blocks, residuals and normalization: every frontier language model in 2026 is still recognisably this.
- Multi-head attention with Q, K, V projections.
- The parallel-training argument. It is why Transformers scaled to trillions of training tokens.
Changed since
- Decoder-only won for generation. GPT-style models drop the encoder and cross-attention.
- Pre-norm, not post-norm. Modern models normalise before each sub-layer, usually with RMSNorm (11.e), because post-norm is unstable at depth.
- Rotary position embeddings (RoPE) replaced the added sinusoids in most open models.
- Gated feed-forward (SwiGLU) replaced ReLU (10.e).
- Fewer K/V heads. Multi-head attention stores one K and V per head, which makes the KV cache large. MQA, GQA (30.d) and MLA (30.v) all shrink it.
- Many FFNs, few used. Mixture-of-experts layers (30.e) replace the single FFN in most large 2026 models.
- The n² cost stayed. It is attacked by kernels (30.f, 30.n) and sparsity (30.y), not removed. Linear-attention and state-space hybrids (24.b) exist but have not replaced full attention at the frontier.
7. Where it lives in 2026#
- Taught in depth: attention in 10.h, the full block in 10.i, attention as kernels in 11.f, the inference view in 12.a.
- Every engine in this material (vLLM, SGLang, TensorRT-LLM; Module 15) is, at its core, a machine for running this decoder loop efficiently.
- The original Tensor2Tensor code is archived; reference implementations people read today are Hugging Face Transformers and the model files inside vLLM (19.g).
Checked on 13 November 2026.
8. Questions#
- Why is an RNN hard to train fast on a GPU, and how does self-attention fix that?
- What is the √d_k in scaled dot-product attention for?
- Training is parallel across the target sentence; generation is not. Explain why.
- Write the per-layer cost of self-attention and of a recurrent layer. When is self-attention cheaper?
- Name four changes between the 2017 Transformer and a 2026 open model such as Llama or Qwen.
- Which part of this paper makes the KV cache necessary, and which later papers shrink it?
- The paper reports 41.8 in one place and 41.0 in another. How would you check which is right?
9. Further reading#
- arXiv:1706.03762, especially sections 3.2 (attention) and 4 (why self-attention).
- The Annotated Transformer — the paper re-implemented line by line in PyTorch.
- 10.h, 10.i and 12.a in this material.
- Next: Megatron-LM (30.c).