Reading the Paper That Started It
Why Attention Replaced Recurrence When It Costs More
Rebuilding the paper's opening comparison in numbers, where attention turns out to be the dearer layer and the deciding column is the one nobody quotes.
Table 1 of the paper is three columns wide and it carries the whole argument for
throwing recurrence away. It is also the most misquoted table in the subject,
because the column that gets quoted is the one that decides nothing.
The table compares three ways of turning a sequence of vectors into another
sequence of vectors of the same width . A recurrent layer, a
convolutional layer, and a self-attention layer. Of each it asks three
questions. How much arithmetic does one layer do. How much of that arithmetic
has to happen in a fixed order. How far apart can two positions be and still
reach each other. The answers are published as orders of growth, which is the
honest way to write them down and a poor way to understand them, so the first
job is to put numbers in.
Two sizes are fixed for everything below, both from the paper. The width is
, which section 3.1 gives as the output width of every sub-layer and
embedding in the base model. The length is positions, which is longer
than any sentence the paper trained on and is chosen so that the term in
and the term in can be compared as real numbers rather than as symbols.
Where a kernel width is wanted it is , and where a restricted
neighbourhood is wanted it is . A multiply-add below means one
multiplication and one addition into a running total, which is what the inner
loop of a matrix product does.
| cost per layer, hundreds of millions of multiply-adds | operations that must run in order | longest path between two positions | |
|---|---|---|---|
| self-attention | 5.12 | 1.00 | 1.00 |
| recurrent | 2.62 | 1000.00 | 1000.00 |
| convolutional, k = 3 | 7.86 | 1.00 | 6.30 |
| self-attention restricted to r = 64 | 0.33 | 1.00 | 15.60 |
The three columns are three different questions
The first column is total arithmetic inside one layer. It is a question about
money and about memory bandwidth, and it is the only column with a cost in it.
The second column is the minimum number of operations that have to be performed
one after another, because each needs the result of the one before. It is a
question about whether a machine with thousands of arithmetic units can be kept
busy. Work that waits cannot be shared out.
The third column is the number of steps a signal takes to get from one position
to another, forwards or backwards. The paper attaches it to the long-term
dependency literature rather than to any timing result, and that is the correct
reading: a long path is where a gradient gets lost, so this column is about what
the network can be taught, not about how long teaching it takes.
A layer can win one column and lose another, and most of the rows do.
The arithmetic at a thousand positions
Self-attention compares every position with every position. That is
ordered pairs, and each pair costs one dot product
across the width, multiply-adds, which in the base model is eight separate
dot products of because the width is split across eight heads. So the score
matrix costs multiply-adds, which
is written out.
A recurrent layer does something different at every position. The state of width
is multiplied by a matrix, which is
multiply-adds, and that happens once per position,
giving , which is written out.
Here is the step that is usually skipped. Both of those counts are half of the
layer. Attention multiplies the weights into the values afterwards, a second
product of exactly the same shape, so the real figure is nearer
. A recurrent layer multiplies the input by a second
matrix as well as the state, so its real figure is nearer
. The factor of two appears on both sides and cancels in the
comparison, which is why the orders of growth in the table can drop it. The
ratio survives untouched:
At a thousand positions, attention does nearly twice the arithmetic of the layer
it replaced. The ratio is the length divided by the width, so the two are equal
when , which at this width is positions. Shorter than that and
attention is cheaper; longer and it is dearer, without limit.
The paper states that condition plainly and then says why it held for the work
in front of it: sentence representations in translation, cut into word pieces,
are tens of tokens long and not thousands. Section 5.1 batches about 25,000
source tokens at a time, which at those lengths is hundreds of sentences, not
one. For the problem on the desk in 2017, was comfortably below and the
first column favoured attention. That is a fact about machine translation, not
about architecture, and it is the first thing to stop believing when the same
layer is pointed at a document.
Convolution loses this column outright. A kernel of width does the recurrent
layer's arithmetic times over,
at a width of three, and the paper says as much. Restricting attention to a
neighbourhood of positions costs
, which is the cheapest row in the
table by a factor of eight, and the paper proposes it for long sequences as
future work rather than reporting it as a result.
The column that decided it
A recurrent state at position is a function of the state at position
. Nothing at position two can start until position one has finished. Over a
thousand positions that is a thousand steps in a fixed order, and each step is
the matrix-vector product above: weights read from memory to perform
multiply-adds, one arithmetic operation per weight fetched.
Attention has no such chain. The same projection matrix is multiplied into all
thousand positions in one matrix product, so each weight fetched is used a
thousand times before it is thrown away. The score matrix is one product, the
softmax is elementwise, the weighted sum of values is one more product. The
count of steps that must wait for each other does not grow with the sequence at
all.
The mechanism behind the middle column is worth one number of its own, because
it is the number the hardware of 2017 was built around. Ask how much arithmetic
each weight does once it has been read out of memory.
- the weights in one 512 by 512 projection, which is 512 squared
- the positions multiplied by that same projection in one product
- a matrix meeting a single vector, which is what one recurrent step is
The paper reports what that bought. Eight P100 processors, about seconds
per training step and 100,000 steps for the base model, which is
seconds, or hours, given in section 5.2
as twelve hours. Those twelve hours bought 27.3 on the English to German test
set, which is above every earlier system in the results table, the ensembles
included. The 28.4 that gets quoted as the headline figure is a different row:
the big model, trained for 300,000 steps at about second a step, so
seconds, which is hours, or
days, written in the same section as 3.5 days. The twelve hours and the best
score in the paper are not the same run.
The distance between two positions
Between position one and position one thousand, a recurrent network has a
thousand edges and an attention layer has one. That is the third column and for
those two rows there is nothing more to say.
Convolution is where the column earns its keep. A layer of width three gives
each output a view of three inputs, and stacking such layers gives a view of
positions, because every extra layer widens the view by . So
the two ends of a thousand-position window are not connected at all until the
stack is deep enough, and the paper's own entry for the contiguous case grows
with the length divided by the kernel width.
- positions in the window, 1000 here
- the kernel width, 3 here
- the number of stacked layers needed before the two ends of the window influence each other
- the number of times a view multiplying by three has to multiply before it covers a thousand positions
The six layers the paper stacks in each half of the model are not there to reach
across the sequence, because one layer already does. They are there for depth in
the ordinary sense, each one working on the representation the one below it
produced.
What the table does not say
It does not mention memory. The score matrix for one head at a thousand
positions is numbers, eight million across the heads of one
layer, and all of it has to be kept for the backward pass. That is the cost that
later work on long sequences actually fought, and it is nowhere in the table,
because the table counts arithmetic.
It does not mention parameters, or quality, or anything measured. Every number
in it is a count derived from the shape of the layer, which is why it belongs at
the front of the paper rather than in the results. And the last row is a
proposal the paper explicitly leaves for future work, so it reports nothing at
all.
What the table does say, read with the middle column in front, is this. Attention
is the dearer layer at any length past the width, and it was adopted anyway,
because it was the only one of the three whose work could all be done at once.
The next lesson takes the single equation that does that work and rebuilds it,
including the one constant in it that looks like a tuning parameter and is not.
Recap
- At a thousand positions and a width of 512 a self-attention layer performs 512 million multiply-adds where a recurrent layer performs 262 million, so attention is the dearer layer by exactly the ratio of length to width.
- The column that decides the comparison is the number of operations that have to run one after another, which is constant for attention and proportional to the sequence length for recurrence, and only the first of those can fill a parallel processor.
- The distance a signal travels between two positions is a claim about what a network can learn rather than about how fast it runs, and it is one step for attention against a thousand for recurrence over a thousand positions.
This is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents