Skip to the content

fatehi.devx2Learning

Papers, Attention Is All You Need

Attention Is All You Need

The 2017 paper that replaced recurrence with attention in a translation model, the Transformer, and so gave almost every later large model its parts.

Before thisnothing: it lies on the shore

A translation model without recurrence

In June 2017 eight researchers at Google Brain, Google Research and the University of Toronto, Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan Gomez, Ɓukasz Kaiser and Illia Polosukhin, published a model for translating text that did without the main building block of its time. They called it the Transformer. Almost every large language model since is built from its parts.

The task: an English sentence goes in, its German translation comes out, and the model is a closed box for now. Inside, the words can be read in two ways. One after another, as a recurrent network reads them: a signal passes from word to word, six beats for six words. Or every word with every word at once, as attention reads them: the fifteen pairs of words are linked directly and all carry their signal in one beat.

In 2017 the best translation systems were encoder-decoder networks: one network, the encoder, reads the sentence to translate, and a second, the decoder, writes the translation. Both were usually recurrent networks. A recurrent network reads a sentence one word at a time and keeps a running summary, its state: the state after the third word is made from the third word and the state after the second. That chain has two costs. A computer cannot make the states of one sentence in parallel, since each waits for the one before, so training is slow. And what the first word says has to pass through every state in between before it can affect the last, so links between distant words are hard to learn.

The paper drops the chain. Its model uses attention, a way for each position to read every other position directly and decide for itself which ones matter. Attention had already been added to recurrent translators, to let the decoder look back at the input sentence. The Transformer keeps attention and nothing recurrent: hence the title.

The same six words, as the same token vectors the next figure starts from, read in two ways. A recurrent encoder, the same cell as on the recurrent-networks page, reads one word a step, its state going round its loop to meet the next word: six steps, and what “The” says passes five more steps before it reaches “mat”. The Transformer’s encoder layer, the self-attention part of the next figure, takes all six vectors together: its array of weights, one cell for every pair of words, lights at once, and six new vectors leave in one step.

The price is the number of connections: with n words, attention reads n × n pairs, 36 for six words, where the recurrent network makes n steps. For sentences, which are shorter than the width of the vectors the model works with (512 numbers per word in the paper), the pairs cost less than the recurrent network’s steps, and all of them can be computed at once.

The model

The Transformer keeps the encoder-decoder shape. Each word first becomes a vector, a list of 512 numbers (tokens and embeddings), and a second vector that encodes the word’s place in the sentence is added to it, because attention by itself does not see word order (position encoding).

The encoder is a stack of six identical layers. In each, every word first reads all the words of the sentence through self-attention, then passes alone through a small feed-forward network. The decoder is a stack of six layers too, with three parts each: masked self-attention, in which each word it has written reads only the words before it, since the rest are not written yet; cross-attention, in which it reads the encoder’s output, the English sentence; and a feed-forward network. Every attention layer runs eight heads side by side, each comparing the words in its own way (multi-head attention). Around every part the input is added back to the output (residual connections) and the sum is rescaled (layer normalisation). At the top a linear layer and a softmax turn the decoder’s vector into a probability for every word of the vocabulary.

The Transformer translating a sentence. The encoder reads all six English words at once. The decoder then writes the German sentence one word at a time: each step reads the words written so far and the encoder’s output, and its chosen word is fed back in as the next step’s input. After every sublayer the residual bus beside it adds the sublayer’s input back to its output (+), and a layer normalisation rescales the sum. Each attention layer has eight heads, each with its own Q, K and V; their outputs are joined (concat) and mixed by one weight matrix, WO. (The paper’s model has six layers in each stack; one of each is drawn.)

The decoder still writes one word after another, since each word depends on the ones before it. What changed is training: there the whole correct translation is known in advance, so the mask lets the decoder learn all its positions at once, and the encoder never waits at all.

What it achieved

The paper trained two sizes of the model. The base model has 65 million parameters; the big one, with vectors of 1,024 numbers and sixteen heads, has 213 million. They learned to translate from the WMT 2014 data: about 4.5 million English-German sentence pairs, and 36 million English-French sentences, cut into tokens by byte-pair encoding and similar methods. On one machine with eight NVIDIA P100 graphics processors the base model trained in 12 hours, the big model in three and a half days, by lowering the cross-entropy of the correct next word with the Adam optimiser, dropout and label smoothing.

Translation quality is measured with BLEU, a score from 0 to 100 of how many runs of one to four words a translation shares with human translations of the same sentences. On English to German the big model scored 28.4, more than 2 points above the best earlier result, an ensemble of several models; even the base model beat every earlier model, at a fraction of their training cost. On English to French the big model scored 41.8 in the paper’s table (its text says 41.0), above every earlier single model, at about a fifth of the training cost of the best of them.

English to German: translation quality (BLEU on the newstest2014 sentences) against the computation spent on training, from the paper’s Table 2. The two Transformers are gold. The others are Google’s recurrent GNMT with reinforcement learning (24.6), Facebook’s convolutional ConvS2S (25.16), a mixture-of-experts model (26.03), and ensembles of several ConvS2S models (26.36) and of several GNMT models (26.30).

The paper also took the model apart: with one head instead of eight, quality dropped by 0.9 BLEU points, and with too many heads it dropped as well; smaller keys in attention hurt; learned position vectors worked as well as the fixed waves. And the same model, with almost no changes, parsed English sentences into their grammatical structure better than every earlier method but one.

What came after

Within two years the Transformer’s two halves had become models of their own: encoders like BERT, trained to fill in hidden words, and decoders like GPT, trained to write the next word, which grew into today’s large language models. The same blocks now read images (vision transformers), sound, DNA and proteins. What carried over is the paper’s central bet: a stack of attention and feed-forward layers, trained in parallel on a great deal of data, learns the structure of a sequence better than a network that reads it one step at a time.

Test yourself

  1. Why could a recurrent translator not make the states of one sentence in parallel?

    Each state is made from its word and the state before it, so the state of the fifth word needs the fourth, which needs the third, and so on: the steps form a chain that has to be walked in order.

  2. With 40 words in a sentence, how many pairs of words does one attention layer compare, and how many steps does a recurrent layer take?

    Attention compares 40 × 40 = 1,600 pairs, all at once; a recurrent layer takes 40 steps, one after another.

  3. The decoder writes one word at a time even in a Transformer. Why does training not have to wait for it?

    During training the whole correct translation is known, so every position of the decoder can be computed at once; the mask keeps each position from reading the words after it, so it learns exactly what it will face when it writes.

  4. What does a BLEU score of 28.4 measure?

    How closely the model’s translations match human translations of the same test sentences, counted as shared runs of one to four words, on a scale from 0 to 100.

Where it leads

Kept in this browser only, never sent.

Paths through it