Transformers Needs review

Transformer

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)
LayerTypeInputOutputRepetitionParametersFormulaMACs
Source tokensinput12810—
Token embedding (shared) × √dembed128128 × 512118,944,00037,000 · 512—
Sinusoidal positional encodingposenc128 × 512128 × 51210—
Multi-head self-attention (8 heads)mha128 × 512128 × 51261,050,624From the reference PyTorch implementation150,994,944
Add & Normaddnorm128 × 512128 × 51261,024From the reference PyTorch implementation0
FFN ‏512←2048←512ffn128 × 512128 × 51262,099,712From the reference PyTorch implementation268,435,456
Add & Normaddnorm128 × 512128 × 51261,024From the reference PyTorch implementation0
Final encoder LayerNormln128 × 512128 × 51211,024From the reference PyTorch implementation0
Target tokens (shifted right)input12810—
Target embedding (same table)embed128128 × 51210Shared with the source embedding—
Sinusoidal positional encodingposenc128 × 512128 × 51210—
Masked self-attentionmasked_mha128 × 512128 × 51261,050,624From the reference PyTorch implementation150,994,944
Add & Normaddnorm128 × 512128 × 51261,024From the reference PyTorch implementation0
Cross-attention (encoder → decoder)cross_attention128 × 512128 × 51261,050,624From the reference PyTorch implementation150,994,944
Add & Normaddnorm128 × 512128 × 51261,024From the reference PyTorch implementation0
FFN ‏512←2048←512ffn128 × 512128 × 51262,099,712From the reference PyTorch implementation268,435,456
Add & Normaddnorm128 × 512128 × 51261,024From the reference PyTorch implementation0
Final decoder LayerNormln128 × 512128 × 51211,024From the reference PyTorch implementation0
Linear projection to the vocabulary (embedding weights)fc128 × 512128 × 3700010Tied to the embedding matrix (Weight Tying)2,424,832,000
Softmaxsoftmax128 × 37000128 × 3700010—
Next-token probabilitiesoutput128 × 37000128 × 3700010—

Configuration figures: Transformer (base): N = 6, d_model = 512, h = 8, d_ff = 2048

Parameters (computed from the layers)63,084,544 (≈63.08M)
Parameters (published in the source)65,000,000 (≈65M)
MACs8,363,966,464 (≈8.36B)
FLOPs ≈ 2 × MACs16,727,932,928 (≈16.73B)
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).
Calculation assumptions: nn.Transformer in PyTorch with a shared 37,000 × 512 embedding and a tied output projection. PyTorch adds two final normalisations (2,048 parameters), and the actual vocabulary size may differ slightly, so the total differs from the stated 65M.

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) V

Each 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}_2

Two 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}} + \beta

Post-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

References

  1. 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
Scientific review note: A first scientific draft based on the original paper and reference implementations; it needs review by a specialist before final approval.

Version 1 — last updated 2026-11-15.