The Annotated Transformer

Alexander M. Rush2018

Abstract

A major aim of open-source NLP is to quickly and accurately reproduce the results of new work, in a manner that the community can easily use and modify. While most papers publish enough detail for replication, it still may be difficult to achieve good results in practice. This paper is an experiment. In it, I consider a worked exercise with the goal of implementing the results of the recent paper. The replication exercise aims at simple code structure that follows closely with the original work, while achieving an efficient usable system. An implicit premise of this exercise is to encourage researchers to consider this method for new results.

Introduction

In the fast-moving world of deep learning, there is often a wide chasm between a published paper and a working implementation. Even when papers include significant detail, small nuances in hyperparameter tuning, initialization, or data processing can prevent researchers from replicating results. This gap contributes to a "replication crisis" in open-source NLP, where the theoretical understanding of a model does not always translate into a functional system.

"The Annotated Transformer" addresses this issue by providing a pedagogical landmark: a line-by-line PyTorch implementation of the seminal "Attention is All You Need" paper. By interleaving the original research text with executable code, this work creates a "literate programming" blueprint that makes the complex Transformer architecture accessible and reproducible for the broader community.

The Transformer Architecture

The core of the Transformer is a departure from traditional recurrent and convolutional layers, relying instead entirely on attention mechanisms to draw global dependencies between input and output. The model follows an encoder-decoder structure, where both the encoder and decoder are composed of stacks of identical layers.

The Transformer model architecture, consisting of stacked encoder and decoder layers using multi-head attention and point-wise, fully connected layers.

Multi-Head Attention

One of the most critical components implemented is Multi-Head Attention. Rather than performing a single attention function with $d_{model}$-dimensional keys, values, and queries, the implementation linearly projects the queries, keys, and values $h$ times with different, learned linear projections to $d_k$, $d_k$, and $d_v$ dimensions, respectively.

Multi-Head Attention consists of several attention layers running in parallel.

This allows the model to jointly attend to information from different representation subspaces at different positions. The implementation highlights the necessity of scaling the dot-product attention by $1/\sqrt{d_k}$ to prevent gradients from vanishing during training when dot products grow large in magnitude.

Technical Components

Beyond attention, the implementation details several vital sub-modules:

  • Encoder-Decoder Stacks: A modular approach using nn.ModuleList to clone identical layers, ensuring the architecture remains clean and extensible.
  • Position-wise Feed-Forward Networks: Each layer in the encoder and decoder contains a fully connected feed-forward network, which is applied to each position separately and identically.
  • Positional Encoding: Since the model contains no recurrence or convolution, sinusoidal functions are injected to provide the model with information regarding the relative or absolute position of the tokens in the sequence.
  • Regularization: Techniques such as Label Smoothing are implemented. By training the model to be slightly "unsure" of its predictions, the implementation demonstrates that while mathematical perplexity may increase, actual translation accuracy (BLEU score) improves.

Experimental Results

The implementation was validated using standard benchmarks from the original work, specifically the WMT 2014 English-German and English-French datasets. The English-German dataset consists of roughly 4.5 million sentence pairs, while the English-French dataset is significantly larger at 36 million sentences.

Training the base model took approximately 12 hours on 8 NVIDIA P100 GPUs. The replication successfully achieved high-performance benchmarks reported in the original literature:

  • WMT 2014 English-to-German: 28.4 BLEU
  • WMT 2014 English-to-French: 41.8 BLEU

These results confirm that the "literate programming" approach does not sacrifice performance for readability. Furthermore, by using sinusoidal positional encodings, the model demonstrated an ability to extrapolate to sequence lengths longer than those encountered during training.

Conclusion

"The Annotated Transformer" demonstrates that code is the ultimate documentation. By providing a concrete, efficient, and usable system alongside the original mathematical descriptions, the chasm between theory and practice is bridged. While production-grade systems may require additional complexities like beam search or multi-GPU optimizations, this annotated format provides a clear foundation. This exercise suggests that such documentation should become a standard for new research to ensure that the community can build upon new ideas immediately and accurately.

Summary updated

Abstract

A major aim of open-source NLP is to quickly and accurately reproduce the results of new work, in a manner that the community can easily use and modify. While most papers publish enough detail for replication, it still may be difficult to achieve good results in practice. This paper is an experiment. In it, I consider a worked exercise with the goal of implementing the results of the recent paper. The replication exercise aims at simple code structure that follows closely with the original work, while achieving an efficient usable system. An implicit premise of this exercise is to encourage researchers to consider this method for new results.
BibTeX
@misc{rush2018annotated,
  author = {Alexander M. Rush},
  title = {{The Annotated Transformer}},
  year = {2018},
  doi = {10.18653/v1/w18-2509},
}
Figure