- Year of appearance
- 2017
- Researchers
- Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, Illia Polosukhin
- Organisation
- Google Brain, Google Research
- Architecture type
- Encoder–decoder Transformer
- Learning mechanism
- supervised
- Tasks
- Machine translation, Sequence transduction
The first encoder-decoder model that relies entirely on attention, with no recurrence or convolution, so it processes all positions of the sequence in parallel.
Architecture diagram
Displayed configuration: Transformer (base): N = 6, d_model = 512, h = 8, d_ff = 2048 — The base configuration from Table 3 with a shared BPE vocabulary of 37 thousand (WMT’14 English-German).
Layer table (21 nodes)
| Layer | Type | Input | Output | Repetition | Parameters | Formula | MACs |
|---|---|---|---|---|---|---|---|
| Source tokens | input | 128 | 1 | 0 | — | ||
| Token embedding (shared) × √d | embed | 128 | 128 × 512 | 1 | 18,944,000 | 37,000 · 512 | — |
| Sinusoidal positional encoding | posenc | 128 × 512 | 128 × 512 | 1 | 0 | — | |
| Multi-head self-attention (8 heads) | mha | 128 × 512 | 128 × 512 | 6 | 1,050,624 | From the reference PyTorch implementation | 150,994,944 |
| Add & Norm | addnorm | 128 × 512 | 128 × 512 | 6 | 1,024 | From the reference PyTorch implementation | 0 |
| FFN 512←2048←512 | ffn | 128 × 512 | 128 × 512 | 6 | 2,099,712 | From the reference PyTorch implementation | 268,435,456 |
| Add & Norm | addnorm | 128 × 512 | 128 × 512 | 6 | 1,024 | From the reference PyTorch implementation | 0 |
| Final encoder LayerNorm | ln | 128 × 512 | 128 × 512 | 1 | 1,024 | From the reference PyTorch implementation | 0 |
| Target tokens (shifted right) | input | 128 | 1 | 0 | — | ||
| Target embedding (same table) | embed | 128 | 128 × 512 | 1 | 0 | Shared with the source embedding | — |
| Sinusoidal positional encoding | posenc | 128 × 512 | 128 × 512 | 1 | 0 | — | |
| Masked self-attention | masked_mha | 128 × 512 | 128 × 512 | 6 | 1,050,624 | From the reference PyTorch implementation | 150,994,944 |
| Add & Norm | addnorm | 128 × 512 | 128 × 512 | 6 | 1,024 | From the reference PyTorch implementation | 0 |
| Cross-attention (encoder → decoder) | cross_attention | 128 × 512 | 128 × 512 | 6 | 1,050,624 | From the reference PyTorch implementation | 150,994,944 |
| Add & Norm | addnorm | 128 × 512 | 128 × 512 | 6 | 1,024 | From the reference PyTorch implementation | 0 |
| FFN 512←2048←512 | ffn | 128 × 512 | 128 × 512 | 6 | 2,099,712 | From the reference PyTorch implementation | 268,435,456 |
| Add & Norm | addnorm | 128 × 512 | 128 × 512 | 6 | 1,024 | From the reference PyTorch implementation | 0 |
| Final decoder LayerNorm | ln | 128 × 512 | 128 × 512 | 1 | 1,024 | From the reference PyTorch implementation | 0 |
| Linear projection to the vocabulary (embedding weights) | fc | 128 × 512 | 128 × 37000 | 1 | 0 | Tied to the embedding matrix (Weight Tying) | 2,424,832,000 |
| Softmax | softmax | 128 × 37000 | 128 × 37000 | 1 | 0 | — | |
| Next-token probabilities | output | 128 × 37000 | 128 × 37000 | 1 | 0 | — |
Configuration figures: Transformer (base): N = 6, d_model = 512, h = 8, d_ff = 2048
- Input shape
- n source tokens and n target tokens
- Output shape
- n × 37000
- Depth
- 6 encoder layers + 6 decoder layers
- Complexity
- Attention O(n²·d) per layer, and the linear layers O(n·d²).
- Parameters note
- Table 3 in the paper: 65 million parameters for the base model.
- MACs note
- For a source and target of length n = 128: from torch.utils.flop_counter (including the tied projection to the vocabulary).
MAC = a multiplication followed by an addition. The figures are for one forward pass of one sample with the stated dimensions; they are an analytical calculation, not a measurement of run time.
Scientific explanation
Historical background
Published at NeurIPS 2017 under the title “Attention Is All You Need”; it achieved 28.4 BLEU on WMT’14 English-German and 41.8 on English-French at a much lower training cost than its predecessors, and became the basis of BERT, GPT, T5 and ViT.
The problem it addresses
Recurrent networks are slow because they are sequential, and they struggle to learn distant dependencies.
Core idea
Self-attention links any two positions directly with a path of length 1, and multi-head attention lets different relationships be represented in parallel, with positional encoding for order.
Architecture description
An encoder of 6 layers (self-attention + FFN) and a decoder of 6 layers (masked self-attention + cross-attention + FFN), with residual connections and layer normalisation after every sub-layer.
Data flow
Tokens → embedding + positional encoding → 6 encoder layers → source representations → (with the shifted target tokens) 6 decoder layers → linear projection → Softmax.
Inputs
The sequence of source tokens and the sequence of target tokens shifted right during training.
Outputs
A probability distribution over the vocabulary for each target position.
Training procedure
Adam (β₁ = 0.9, β₂ = 0.98, ε = 10⁻⁹) with a 4000-step warm-up schedule, Dropout 0.1 and label smoothing 0.1, on 8 P100 GPUs (12 hours for the base model).
Loss functions
Cross-entropy with label smoothing.
Computational complexity
O(n²·d) for attention and O(n·d·d_ff) for the feed-forward network per layer.
Computational requirements
Base model: 12 hours on 8 P100 GPUs.
Advantages
- Fully parallel training
- A short path between any two positions
- Great scalability that led to large language models
Limitations
- Attention cost is quadratic in the sequence length
- Needs a lot of data
- Does not know order without positional encoding
Equations
Scaled dot-product attention Scaled dot-product attention
\operatorname{Attention}(Q, K, V) = \operatorname{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right) VEach query takes a weighted average of the values according to its similarity with the keys.
Symbols: Q, K, V: queries, keys and values, d_k: key dimension
Multi-head attention Multi-head attention
\operatorname{MultiHead}(Q,K,V) = [\text{head}_1; \ldots; \text{head}_h]\, W^O,\quad \text{head}_i = \operatorname{Attention}(QW_i^Q, KW_i^K, VW_i^V)h parallel heads of dimension d_model/h, then a linear projection.
Symbols: h: number of heads, W^O: output projection
Position-wise feed-forward network Position-wise feed-forward network
\operatorname{FFN}(\mathbf{x}) = \max(0, \mathbf{x}W_1 + \mathbf{b}_1)W_2 + \mathbf{b}_2Two linear layers applied to each position independently (GELU instead of ReLU in BERT and GPT).
Symbols: W₁: d_model×d_ff, W₂: d_ff×d_model
Residual addition and layer normalisation Add & LayerNorm
\mathbf{y} = \operatorname{LayerNorm}\big(\mathbf{x} + \operatorname{Sublayer}(\mathbf{x})\big),\quad \operatorname{LN}(\mathbf{x}) = \gamma \odot \frac{\mathbf{x} - \mu}{\sqrt{\sigma^2 + \epsilon}} + \betaPost-LN ordering as in the original Transformer.
Symbols: μ, σ²: mean and variance of the vector
Sinusoidal positional encoding Sinusoidal positional encoding
PE_{(pos,2i)} = \sin\!\left(\frac{pos}{10000^{2i/d_{model}}}\right),\quad PE_{(pos,2i+1)} = \cos\!\left(\frac{pos}{10000^{2i/d_{model}}}\right)Adds order information because attention does not distinguish order by itself.
Symbols: pos: position, i: dimension
Causal mask Causal mask
\operatorname{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}} + M\right),\quad M_{ij} = \begin{cases} 0 & j \le i \\ -\infty & j \gt i \end{cases}Prevents position i from seeing later positions during generation.
Learning-rate schedule Learning-rate schedule
lr = d_{model}^{-0.5} \cdot \min\!\left(step^{-0.5},\; step \cdot warmup^{-1.5}\right)A linear increase over 4000 warm-up steps, then an inverse square-root decrease.
Symbols: warmup = 4000
Applications
Related models
- Predecessor of Sequence to Sequence (Seq2Seq) — Keeps the encoder-decoder idea and replaces LSTM with attention. Compare
- Successor of BERT (Bidirectional Encoder Representations from Transformers) — BERT is encoder-only. Compare
- Successor of GPT (Generative Pre-trained Transformer) — GPT is decoder-only. Compare
- Variant of BERT (Bidirectional Encoder Representations from Transformers) — Uses only the Transformer encoder. Compare
- Variant of GPT (Generative Pre-trained Transformer) — Uses only the Transformer decoder. Compare
- Variant of Vision Transformer (ViT) — Uses the Transformer encoder as is. Compare
- Variant of T5 (Text-to-Text Transfer Transformer) — T5 is an encoder-decoder in text-to-text form. Compare
- Successor of Vision Transformer (ViT) — ViT applied the encoder to image patches. Compare
- Variant of DETR (DEtection TRansformer) — A Transformer encoder-decoder. Compare
- Predecessor of Long Short-Term Memory — The Transformer replaced recurrence with attention. Compare
- Variant of XLNet — Built on Transformer-XL. Compare
- Alternative Long Short-Term Memory — Replaces recurrent networks. Compare
- Graph Attention Network — Applies the idea of attention to the neighbours. Compare
- CLIP (Contrastive Language–Image Pre-training) — The text encoder is a Transformer. Compare
- Residual Network (ResNet) — The Transformer uses a residual connection in every layer. Compare
- Variant of LLaVA (Large Language and Vision Assistant) — Both components are Transformers. Compare
References
- Original paper Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, Illia Polosukhin. (2017). Attention is all you need. NeurIPS 2017. arXiv:1706.03762 Verified
Version 1 — last updated 2026-11-15.
