# Attention Is All You Need

> Replaces recurrence with stacked self-attention. It trains in parallel, sets translation records of 28.4 BLEU on English-German and 41.8 on English-French at a fraction of prior training cost, and became the blueprint for modern LLMs.

- arXiv: https://arxiv.org/abs/1706.03762
- Authors: Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Lukasz Kaiser, Illia Polosukhin
- Published: 2017-06-12
- Topic: Architectures
- Visual carousel: https://instaxiv.com/p/1706.03762
- Explanation by InstaXiv, a secondary educational summary. Cite the original paper for exact results. InstaXiv is not affiliated with or endorsed by arXiv or Cornell University.

## Drop recurrence entirely. Attention alone translates better and trains faster.

**28.4** BLEU on WMT14 English to German

## Sequence models were stuck reading one token at a time

**RNNs and LSTMs:** Each hidden state waits on the previous one. Training cannot parallelize across a sentence, and signals between distant words must survive many sequential steps.

**The Transformer:** Every token attends to every other token in one matrix multiply. Any two positions are a single step apart, and the whole sequence trains in parallel.

Path length between any two words drops from $O(n)$ to $O(1)$.

## Encoder and decoder, built only from attention and MLPs

![Figure 1](https://cdn.instaxiv.com/figures/1706.03762/figure1.webp)

Left stack encodes the source; right stack generates the target. Each layer repeats attention, then a feed-forward block, each wrapped in a residual connection and layer norm. (Figure 1)

## Scaled dot-product attention in one line

$$
\mathrm{Attention}(Q,K,V) = \mathrm{softmax}\!\left(\frac{Q\,K^{\top}}{\rose{\sqrt{d_k}}}\right)V
$$

Each query scores every key, softmax turns scores into weights, and the output mixes the values. Dividing by $\sqrt{d_k}$ keeps softmax from saturating.

- $Q$: Queries: what each token is looking for
- $K$: Keys: what each token offers to be matched on
- $V$: Values: the content that actually gets mixed
- $\sqrt{d_k}$: Scale factor; keys are 64-dimensional per head

## Eight heads attend in parallel, each in its own subspace

![Figure 2](https://cdn.instaxiv.com/figures/1706.03762/figure2.webp)

Left: a single attention head. Right: Q, K and V are projected eight times, attended separately, then concatenated, so each head can learn a different kind of relationship. (Figure 2)

## Order comes from sine waves, not recurrence

1. **Embed tokens**: Each token becomes a 512-dimensional vector, with weights shared between input embeddings and the output projection.
2. **Add positional encodings**: Sines and cosines of different frequencies are added, so relative offsets become functions the model can learn to attend by.
3. **Stack six layers**: Encoder and decoder each stack six layers; the decoder masks future positions so generation stays autoregressive.

## New records at a fraction of the training cost

| Method | EN→DE BLEU | EN→FR BLEU |
| --- | --- | --- |
| GNMT + RL | 24.6 | 39.92 |
| ConvS2S | 25.16 | 40.46 |
| ConvS2S ensemble | 26.36 | 41.29 |
| Transformer base | 27.3 | 38.1 |
| **Transformer big (+2.0 BLEU)** | **28.4** | **41.8** |

_WMT 2014 newstest. The big model trained for 3.5 days on eight P100 GPUs._

## Quadratic attention became the next bottleneck

- **Cost grows with length squared**: Self-attention compares every pair of tokens, so memory and compute scale as $n^2$. Long documents get expensive fast.
- **Tested on translation and parsing**: Evidence covered WMT translation and English constituency parsing. Its reach into language modeling and vision came from later work.
- **Extrapolation was a hope**: Sinusoidal encodings were chosen because they might extrapolate to longer sequences; the paper did not test that.

Best used when: Sequences fit in memory and parallel hardware matters more than per-token cost.

## Key takeaways

- Attention alone, with no recurrence or convolution, is enough for state-of-the-art sequence modeling.
- Parallel training across positions is what made scaling to today's model sizes practical.
- BERT, GPT and nearly every modern LLM descend directly from this block.

## Links

- arXiv: https://arxiv.org/abs/1706.03762
- PDF: https://arxiv.org/pdf/1706.03762
- Code: https://github.com/tensorflow/tensor2tensor

## Citation

```bibtex
@article{vaswani2017attention,
  title   = {Attention Is All You Need},
  author  = {Vaswani, Ashish and Shazeer, Noam and Parmar, Niki and Uszkoreit, Jakob and Jones, Llion and Gomez, Aidan N. and Kaiser, Lukasz and Polosukhin, Illia},
  journal = {arXiv preprint arXiv:1706.03762},
  year    = {2017},
  url     = {https://arxiv.org/abs/1706.03762}
}
```
