Attention Is All You Need (Transformer)

Attention Is All You Need introduced the Transformer, a sequence transduction architecture built entirely out of attention mechanisms, with no recurrence and no convolution.

  • Paper: Vaswani, Shazeer, Parmar, Uszkoreit, Jones, Gomez, Kaiser, Polosukhin (Google Brain/Research and University of Toronto), NeurIPS 2017, arXiv:1706.03762.
  • Evaluation: WMT 2014 English-to-German and English-to-French machine translation.
  • Why it matters: the architecture nearly all subsequent large language models and vision transformers (including DETR) are built on.

Core idea

Prior state-of-the-art sequence models (RNNs, LSTMs, GRUs) factor computation along the position of the sequence: hidden state depends on , which is inherently sequential and precludes parallelization within a training example. Convolutional alternatives (ByteNet, ConvS2S) parallelize but require or layers stacked to connect distant positions.

The Transformer instead relies entirely on self-attention (also called intra-attention) to compute representations of a sequence, relating all positions to each other with a constant number of sequential operations. This buys:

  • Full parallelization within a training example — no step depends on the previous step’s output.
  • Constant path length between any two positions, versus for recurrent layers, making it easier to learn long-range dependencies.
  • Faster training in wall-clock time despite comparable or lower total complexity, because there is nothing sequential to bottleneck it.

The tradeoff is that per-layer complexity is (quadratic in sequence length , linear in representation dimension ) versus for a recurrent layer — cheaper only when , which is typically true for sentence-level translation.

Architecture

The Transformer follows the standard encoder-decoder structure, but both stacks are built purely from attention and position-wise feed-forward layers (see Figure 1).

The Transformer model architecture
Figure 1 from the paper: encoder (left) and decoder (right) stacks.

Encoder: a stack of identical layers. Each layer has two sub-layers: (1) multi-head self-attention, (2) a position-wise fully connected feed-forward network. A residual connection wraps each sub-layer, followed by layer normalization: the output of each sub-layer is . To make the residual connections work, every sub-layer and the embedding layers produce outputs of dimension .

Decoder: also a stack of identical layers, with a third sub-layer inserted per layer that performs multi-head attention over the encoder’s output (encoder-decoder attention). The decoder’s self-attention sub-layer is masked — positions are prevented from attending to subsequent positions, by setting the corresponding softmax inputs to — so that, combined with the output embeddings being offset by one position, predictions for position can only depend on known outputs at positions . This preserves the auto-regressive property at inference.

Embeddings and softmax: input and output tokens are converted to -dimensional vectors via learned embeddings. The same weight matrix is shared between the input embedding, the output embedding, and the pre-softmax linear transformation (weight tying). Embedding weights are multiplied by .

Scaled dot-product attention

An attention function maps a query and a set of key-value pairs to an output, computed as a weighted sum of the values, where each value’s weight comes from a compatibility function between the query and the corresponding key. The Transformer uses:

where , , are matrices packing together queries, keys (-dimensional), and values (-dimensional). This is dot-product attention identical to the standard formulation except for the scaling factor. Dot-product attention is much faster and more space-efficient than additive attention (which uses a feed-forward network with one hidden layer to compute compatibility) since it reduces to highly optimized matrix multiplication, and the two perform similarly for small . But for large , unscaled dot products grow large in magnitude — assuming components of and are independent random variables with mean 0 and variance 1, their dot product has variance — pushing the softmax into regions with extremely small gradients. Scaling by counteracts this.

Multi-head attention

Rather than performing a single attention function with -dimensional keys, values, and queries, the model linearly projects , , times with different learned projections to , , dimensions, performs attention in parallel on each projected version, concatenates the results, and projects once more:

with projection matrices , , and . A single attention head averages over all positions, which inhibits the ability to focus on multiple different things at once; multi-head attention lets the model jointly attend to information from different representation subspaces at different positions. With heads and , the total computational cost is similar to single-head attention at full dimensionality, since each head operates on a reduced dimension.

The model uses multi-head attention in three places:

  • Encoder-decoder attention: queries come from the previous decoder layer, keys/values come from the encoder output — every decoder position can attend over the whole input sequence, mimicking typical seq2seq attention.
  • Encoder self-attention: queries, keys, and values all come from the output of the previous encoder layer; every position attends to all positions in that layer.
  • Decoder self-attention: each position attends to all positions up to and including itself (masked, as described above).

Position-wise feed-forward networks

Each encoder and decoder layer also contains a fully connected feed-forward network, applied identically and separately to each position:

Two linear transformations with a ReLU in between — equivalently, two convolutions with kernel size 1. Parameters are shared across positions within a layer but differ from layer to layer. Input/output dimensionality is ; the inner layer has dimensionality .

Positional encoding

Since the model contains no recurrence or convolution, it has no inherent notion of token order, so positional information is injected by adding a positional encoding to the input embeddings at the bottom of both stacks (same dimension so the two can be summed). The paper uses fixed sinusoids of different frequencies:

where is the position and the dimension; wavelengths form a geometric progression from to . This choice was hypothesized to let the model easily learn to attend by relative position, since can be expressed as a linear function of for any fixed offset . Learned positional embeddings were also tried and produced nearly identical results (Table 3, row E); sinusoids were kept because they may let the model extrapolate to sequence lengths longer than those seen during training.

Why self-attention

The paper motivates self-attention over recurrent and convolutional layers along three axes — total computational complexity per layer, the amount of computation parallelizable (minimum sequential operations), and the path length between long-range dependencies:

Layer TypeComplexity per LayerSequential OperationsMaximum Path Length
Self-Attention
Recurrent
Convolutional
Self-Attention (restricted)

Self-attention connects all positions with a constant number of sequential operations, giving the shortest possible path length between any two positions — the key factor the authors identify for learning long-range dependencies. For very long sequences, self-attention could be restricted to a neighborhood of size around each position to keep computation tractable, at the cost of increasing maximum path length to (left as future work). As a side benefit, the authors note self-attention yields more interpretable models — individual heads appear to specialize, with some learning syntactic and semantic structure.

Hyperparameters

As reported for the base and big models on WMT 2014 En-De/En-Fr:

Architecture (base model)

  • Layers: encoder layers, 6 decoder layers
  • Model dimension : 512
  • Feed-forward inner dimension : 2048
  • Attention heads : 8
  • Key/value dimension per head : 64
  • Dropout : 0.1 (applied to each sub-layer output before the residual add+norm, and to the sum of embeddings and positional encodings in both stacks)
  • Label smoothing : 0.1
  • Parameters: ~65M

Architecture (big model)

  • , , , , (0.3 for En-De, 0.1 for En-Fr)
  • Parameters: ~213M

Data

  • En-De: WMT 2014, ~4.5M sentence pairs, shared source-target byte-pair-encoding vocabulary of ~37,000 tokens
  • En-Fr: WMT 2014, 36M sentences, 32,000 word-piece vocabulary
  • Batching: sentence pairs batched by approximate sequence length, ~25,000 source and ~25,000 target tokens per batch

Optimizer

  • Adam, , ,
  • Learning rate schedule: — linear warmup for the first , then inverse-square-root decay

Hardware and schedule

  • 8 NVIDIA P100 GPUs, one machine
  • Base model: 100,000 steps, ~0.4s/step, 12 hours total
  • Big model: 300,000 steps, ~1.0s/step, 3.5 days total

Inference

  • Beam search, beam size 4, length penalty
  • Max output length: input length + 50, with early termination
  • Checkpoint averaging: last 5 checkpoints (base, written at 10-minute intervals) or last 20 checkpoints (big)

Results

  • WMT 2014 En-De: the big Transformer achieves 28.4 BLEU, more than 2.0 BLEU above the best previously reported result (including ensembles), at a fraction of the training cost ( FLOPs vs. e.g. for the Deep-Att + PosUnk ensemble).
  • WMT 2014 En-Fr: the big model reaches 41.8 BLEU, a new single-model state of the art, at under the training cost of the previous best single model.
  • Even the base model surpasses all previously published models and ensembles on En-De at a small fraction of their training cost ( FLOPs).
  • Model variations (Table 3, ablations on En-De dev): reducing to a single attention head costs 0.9 BLEU versus the best head count, but quality also degrades with too many heads — 8 is a sweet spot at fixed compute. Reducing attention key size hurts quality, suggesting dot-product compatibility isn’t the only viable option. Bigger models perform better, and dropout is important to prevent overfitting. Learned positional embeddings vs. sinusoidal positional encoding gives nearly identical results.
  • Generalization: a 4-layer Transformer with was applied to English constituency parsing on the Penn Treebank WSJ, a task with strong structural output constraints. Despite minimal task-specific tuning, it reached 91.3 F1 (WSJ-only, 40K sentences) and 92.7 F1 (semi-supervised, ~17M sentences), outperforming nearly all previously reported models except the Recurrent Neural Network Grammar, and beating the BerkeleyParser even when trained on WSJ-only data — evidence the architecture is not specific to translation.

Limitations

  • Quadratic self-attention complexity in sequence length () makes very long sequences expensive; the paper’s own proposed fix (restricting attention to a local neighborhood of size ) was left as future work rather than implemented.
  • Fixed sinusoidal positional encoding injects only a weak, additive notion of order; it was empirically indistinguishable from learned positional embeddings at the sequence lengths tested.
  • Results were demonstrated on machine translation and (as a generalization check) constituency parsing; the paper does not evaluate other modalities, which it explicitly flags as future work (images, audio, video, and more local/restricted attention mechanisms for large inputs).

See also

  • DETR — applies the Transformer encoder-decoder directly to object detection, replacing the sequence-to-sequence framing with set prediction
  • Mask2Former — masked-attention Transformer decoder for segmentation, descended from the same decoder cross-attention idea
  • All Articles