
TL;DR
A transformer is a neural network architecture that relies on attention instead of recurrence or convolutions. Eight researchers introduced it in the 2017 paper “Attention Is All You Need”, for machine translation. Its main limit is cost: the work of a self-attention layer grows with the square of the sequence length.
- Purpose: A transformer maps an input sequence to an output sequence using attention alone.
- Parts: The 2017 model stacks six identical encoder layers and six identical decoder layers.
- Steps: Attention compares a query with all keys and uses the result to weight the values.
- Origin: Ashish Vaswani and seven co-authors submitted the paper on June 12, 2017, and it appeared at NIPS 2017.
- Limit: The paper lists self-attention cost per layer as O(n² · d), where n is the sequence length.
What a transformer is
The paper calls it “a new simple network architecture, the Transformer”. The abstract says it is “based solely on attention mechanisms, dispensing with recurrence and convolutions entirely”.
The authors built it for sequence transduction. That means turning one sequence of symbols into another, as in machine translation.
Large language models build on this design. Our explainer on LLMs covers what they do with it: What Is an LLM? Large Language Models Explained
What came before
The paper starts from recurrent networks. It says they “typically factor computation along the symbol positions of the input and output sequences”.
That causes a problem. In the authors' words: “This inherently sequential nature precludes parallelization within training examples”.
Attention already existed. Bahdanau, Cho and Bengio submitted a paper on September 1, 2014. They conjectured that “the use of a fixed-length vector is a bottleneck” and let a model “automatically (soft-)search for parts of a source sentence that are relevant to predicting a target word”.
The Transformer paper notes that such attention was used “in conjunction with a recurrent network” in all but a few cases. Its own model is “eschewing recurrence and instead relying entirely on an attention mechanism”.
The parts: encoder and decoder
Most competitive sequence transduction models have an encoder and a decoder, the paper says. The Transformer keeps that structure.
The encoder maps the input to a sequence of continuous representations. The decoder then generates the output “one element at a time”.
The encoder is “a stack of N = 6 identical layers”. Each layer has two sub-layers: a multi-head self-attention mechanism and a position-wise feed-forward network. Around each sub-layer sit a residual connection and layer normalization.
The decoder also has six identical layers. It adds a third sub-layer that attends to the output of the encoder. A mask stops each position from looking at later positions, so a prediction can depend only on outputs that are already known.
All sub-layers produce outputs of dimension 512 in the base model.
How attention works
The paper describes attention as mapping a query and a set of key-value pairs to an output. The output is a weighted sum of the values. A compatibility function of the query with each key sets the weights.
The authors call their version “Scaled Dot-Product Attention”. The model takes the dot products of the query with all keys and divides each by the square root of the key dimension. A softmax function then turns the results into weights.
Self-attention is the case where queries, keys and values come from the same sequence. The paper defines it as “an attention mechanism relating different positions of a single sequence in order to compute a representation of the sequence”.
One attention function is not enough. The authors use h = 8 parallel attention layers, called heads. Multi-head attention “allows the model to jointly attend to information from different representation subspaces at different positions”.
Tokens, vectors and order
The model first turns tokens into vectors. The paper uses “learned embeddings to convert the input tokens and output tokens to vectors of dimension dmodel”. Our explainer on tokens covers the first step: Tokens and Context Windows: What the Numbers Mean
Attention alone has no sense of order. The paper says that, with no recurrence and no convolution, the model needs information “about the relative or absolute position of the tokens in the sequence”.
The authors add positional encodings to the input embeddings. They use sine and cosine functions of different frequencies. They also tried learned positional embeddings and “found that the two versions produced nearly identical results”.
Why the authors chose self-attention
The paper compares three layer types on three points: computation per layer, how much work can run in parallel, and the path length between distant positions.
Table 1 of the paper lists the figures. A self-attention layer needs O(n² · d) operations and O(1) sequential operations. A recurrent layer needs O(n · d²) operations and O(n) sequential operations. Here n is the sequence length and d the representation dimension.
The paper concludes that self-attention layers are faster than recurrent layers when the sequence length n is “smaller than the representation dimensionality d”.
It names the cost for long inputs. To handle very long sequences, self-attention “could be restricted to considering only a neighborhood of size r”. The authors list this as future work.
What the paper reports
All numbers in this section are the authors' own results from 2017.
The big model reached 28.4 BLEU on the WMT 2014 English-to-German task. The abstract says this improves on the best earlier results “by over 2 BLEU”. On English-to-French it reached 41.8 BLEU, a single-model result, after training for 3.5 days on eight GPUs.
The base model trained for 100,000 steps, which took 12 hours on eight NVIDIA P100 GPUs. The authors also applied the model to English constituency parsing with large and limited training data.
The conclusion looks ahead. The authors plan “to extend the Transformer to problems involving input and output modalities other than text”.
From the paper to language models
Later work kept the architecture and changed the training. BERT is the first example we read. Its name stands for “Bidirectional Encoder Representations from Transformers”. Its abstract says the pre-trained model “can be fine-tuned with just one additional output layer”. The paper was submitted on October 11, 2018.
GPT-3 is the second. Its paper describes “an autoregressive language model with 175 billion parameters”. It states: “We use the same model and architecture as GPT-2”, with alternating dense and locally banded sparse attention patterns “in the layers of the transformer”.
The GPT-3 paper lists 96 layers for the 175 billion parameter model. The 2017 base model had six encoder layers and six decoder layers. How the field got from one to the other is in our history article: A Short History of Large Language Models, 2003 to 2023
What this article does not cover
We read the paper in its seventh arXiv version, dated August 2, 2023. We read the abstracts of the Bahdanau and BERT papers, not the full texts. We did not read the GPT-2 paper.
We do not explain the mathematics in full, and we do not cover later variants of the architecture. Every figure comes from the papers named below.
Sources
- Vaswani et al., Attention Is All You Need (arXiv:1706.03762, submitted June 12, 2017, version 7): https://arxiv.org/abs/1706.03762
- Vaswani et al., Attention Is All You Need (full text, PDF): https://arxiv.org/pdf/1706.03762
- Bahdanau, Cho and Bengio, Neural Machine Translation by Jointly Learning to Align and Translate (arXiv:1409.0473): https://arxiv.org/abs/1409.0473
- Devlin et al., BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding (arXiv:1810.04805): https://arxiv.org/abs/1810.04805
- Brown et al., Language Models are Few-Shot Learners (arXiv:2005.14165): https://arxiv.org/pdf/2005.14165







