II · THE IDEA · ARTIFICIAL INTELLIGENCE
Transformer Neural Machine Translation
▶ Listen · narrated
Before the Transformer, machine translation leaned on recurrent networks that processed tokens one by one. A single architecture change removed that bottleneck and redefined how models handle sequences.
At a glance
- Architecture
- Encoder–decoder built only from attention and feed-forward layers
- Core claim
- Neither recurrence nor convolution is required for transduction
- WMT 2014
- 28.4 BLEU English–German; 41.8 BLEU English–French
- Base training
- About 12 hours on eight NVIDIA P100 GPUs
- Stack depth
- Six identical layers in the encoder and in the decoder
Think of translating a sentence as organising a table of people who must all pass notes. In an older recurrent model the notes travel along a line: person one writes to person two, who writes to person three, and so on. Distant people wait on everyone in between.
The Transformer gives everyone a broadcast channel at once. In each layer, every position publishes a query (what am I looking for?), keys (what do I contain?) and values (what do I pass along?). Matching queries to keys decides how much of each value to mix in. Doing this several times in parallel—multi-head attention—lets the model track different kinds of link at once, such as syntax in one head and nearby wording in another.
Because that broadcast does not know left from right by itself, the model adds a positional pattern to each token’s embedding so order is still visible. Stack six such layers for reading the source sentence and six for writing the translation, let the writer’s layers also consult the reader’s final notes, and you have the architecture from the 2017 paper. On standard translation benchmarks it beat heavier recurrent systems while training in a long working day on eight GPUs.
The Transformer is an encoder–decoder transducer whose layers contain no recurrence and no convolution. Let d_model be the residual stream width (512 base, 1024 big). Token embeddings are scaled by sqrt(d_model) and added to positional encodings PE(pos, 2i) = sin(pos / 10000^{2i/d_model}), PE(pos, 2i+1) = cos(pos / 10000^{2i/d_model}), then fed to a stack of N = 6 identical encoder layers.
Each encoder layer is: x ← LayerNorm(x + MultiHead(x, x, x)) x ← LayerNorm(x + FFN(x)) with residual connections around both sub-layers. Multi-head attention uses h = 8 heads (base). For each head, projections W^Q_i, W^K_i, W^V_i map to d_k = d_v = d_model/h = 64. Scaled dot-product attention is softmax(QK^T / sqrt(d_k)) V. Heads are concatenated and projected by W^O. The position-wise FFN is max(0, xW_1 + b_1)W_2 + b_2 with inner width 2048 (base).
The decoder mirrors this stack but inserts a third sub-layer: cross-attention over encoder outputs (queries from the decoder, keys and values from the encoder). Decoder self-attention adds a causal mask, setting future logits to −∞ before the softmax, so position i cannot attend to j > i. Training minimises token-level cross-entropy with label smoothing ε_ls = 0.1. Optimisation uses Adam with β1 = 0.9, β2 = 0.98, ε = 10^{-9} and the paper’s learning-rate schedule: linear warmup for 4000 steps then inverse-square-root decay in step number. Regularisation includes residual dropout P_drop = 0.1 (base) and attention dropout.
On WMT 2014 En–De (shared ~37k BPE vocab) the big model reports 28.4 BLEU; on En–Fr (~32k word-piece vocab) 41.8 BLEU. Base En–De training is given as approximately 12 hours on 8× P100. Ablations in the paper vary head count, depth, and positional encoding type; a learned positional embedding performs nearly identically to sinusoids on their translation tasks. Complexity tables contrast per-layer path lengths: self-attention O(1) versus recurrent O(n) and convolutional O(log_k n) for kernel width k.
Limitations explicit in the design include O(n² d) attention cost in sequence length, the need for an external positional signal, and auto-regressive decoding that remains sequential at inference even though training parallelises across positions.
Look closer
Scaled dot-product attention
Queries, keys and values are packed into matrices. Compatibility is the scaled product QK transpose, divided by the square root of the key dimension so that large inner products do not push the softmax into vanishing-gradient regions. The result weights the values. The paper presents this as simpler and faster in practice than additive attention when dimensions are large.
Multi-head rather than single
Instead of one attention pass in d_model dimensions, the model projects into several lower-dimensional subspaces—eight heads in the base configuration—runs attention in parallel, concatenates the outputs and projects again. The authors argue that separate heads can attend to different kinds of relation at different positions, which a single averaged head would blur.
Order without recurrence
Because self-attention has no inherent sense of sequence order, the model adds positional encodings to the input embeddings. The published design uses fixed sine and cosine functions of different frequencies. The paper also reports a learned-embedding variant that performed nearly identically on the translation tasks they measured.
The story
Attention Is All You Need, posted to arXiv in June 2017 by Vaswani and colleagues at Google Brain and Google Research, proposed a sequence transduction architecture that dropped recurrence and convolution entirely. The authors called it the Transformer. Its job, in the experiments that made the claim concrete, was neural machine translation: map a sentence in one language to a sentence in another.
Earlier strong systems mixed recurrent networks—often bidirectional encoders and gated decoders—with attention over encoder states. Recurrence gave the model a way to carry information along a sequence, but it also forced step-by-step computation along that length. The Transformer replaced that path with stacked self-attention. Every position could, in a single layer, draw on every other position in the same sequence, subject only to masking in the decoder so that generation remained auto-regressive.
The architecture is an encoder–decoder. The encoder is a stack of six identical layers. Each layer has two sub-layers: multi-head self-attention, then a position-wise feed-forward network applied independently at each position. Residual connections wrap both sub-layers, and layer normalisation follows. The decoder is also six layers, with a third sub-layer that performs multi-head attention over the encoder’s output. Decoder self-attention is masked so that position i cannot attend to positions greater than i.
Representations are d_model-dimensional vectors—512 in the base model, 1024 in the larger configuration. Attention is multi-headed: the paper’s base setup uses eight heads with reduced key, query and value dimensions so that total compute stays comparable to a single full-dimensional head. The attention itself is scaled dot-product: the product of queries and keys is divided by the square root of the key dimension before the softmax, a detail the authors introduce to keep gradients well behaved when dimensions grow.
Because pure attention is permutation-invariant without help, the model injects positional encodings into the input embeddings. The default scheme is sinusoidal—fixed sine and cosine functions of different frequencies—so that relative positions remain linearly recoverable. A learned positional embedding variant performed nearly as well on their translation benchmarks.
Training used the standard WMT 2014 English–German and English–French data. Byte-pair encoding built the vocabularies. The base model trained for roughly twelve hours on eight NVIDIA P100 GPUs; the larger model took longer and scored higher. On English–German the big model reached 28.4 BLEU, improving over previous best published results including ensembles. On English–French it reached 41.8 BLEU at a fraction of the training cost of earlier competitive systems.
The paper also reports English constituency parsing experiments, showing that the same architecture transferred beyond translation when trained on modest supervised data. The broader claim, stated in the title and defended in the ablations, was that attention is a sufficient primitive for sequence transduction once depth, multi-head structure and positional signals are in place.
Why it mattered then
In 2017 the dominant path to strong neural machine translation still ran through recurrence. Attention had already become a standard add-on to encoder–decoder RNNs, but the sequential dependency along time steps limited parallelisation and made long sequences expensive to train. Convolutional alternatives existed and could parallelise better within a layer, yet they still needed stacked depth or wide kernels to connect distant positions. The Transformer offered a different bargain: constant path length between any pair of positions in a layer, full parallelisation across the sequence during training, and a simpler layer design built from matrix multiplies and feed-forwards. The WMT results gave that bargain empirical weight. A model that looked almost austere on paper—no recurrence, no convolution—matched or beat specialised systems while training in hours on a modest multi-GPU setup. That combination of clarity and scoreboard performance is why the paper travelled so quickly through the research community.
Why it matters now
The encoder–decoder Transformer described in the paper is the direct ancestor of the architectures behind modern large language models, vision transformers and multimodal systems. Many later models keep only the decoder stack, or only the encoder, but the block design—multi-head attention, residual paths, layer norm, position-wise feed-forwards—remains recognisable. The paper’s emphasis on parallel training and short path lengths also set expectations for scale. Once sequence computation no longer walked token by token, hardware utilisation and dataset size became the binding constraints, and those constraints have shaped the decade since. Reading the original still clarifies which pieces were present from the start (scaled dot-product attention, multi-head structure, sinusoidal positions, masked decoder self-attention) and which arrived later.
The surprising detail
The title is not a metaphor. The authors literally remove recurrence and convolution and show competitive translation with attention and feed-forwards alone. Equally striking is how small the base training run was by later standards: about twelve hours on eight P100 GPUs for a model that set a new single-model state of the art on WMT 2014 English–German. The sinusoidal positional encoding is another understated choice—fixed, not learned—yet a learned alternative performed almost the same in their tests, leaving open a design question that later work continued to revisit.
What is disputed
The second listed source addresses learning in Krein spaces and is not part of the Transformer paper’s evidence. Claims here follow only Attention Is All You Need. Later variants change normalisation placement, positional schemes and attention recipes; those are outside this paper’s results.
Remember this
The Transformer showed that stacked multi-head attention, with positional encodings and feed-forward layers, can replace recurrence for sequence transduction.
Test yourself
Why does the decoder mask future positions in its self-attention, and what would break at training time if that mask were removed while still training with teacher forcing?
The mask keeps auto-regressive generation consistent: when predicting position i the model may only attend to positions up to i. Without it, training with full target context would let each position see later tokens it would not have at inference, so the network could learn to copy or rely on future information that disappears at test time. The mask aligns the training-time information flow with left-to-right decoding.
Go deeper
- [1706.03762] Attention Is All You Need · arxiv.org
- [1809.02157] Scalable Learning in Reproducing Kernel Krein Spaces · arxiv.org
Image: Original diagram, The Daily Triptych. Licence: Original work. Source.