Basis Lab

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 nn vectors into another

sequence of nn vectors of the same width dd. 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

d=512d = 512, which section 3.1 gives as the output width of every sub-layer and

embedding in the base model. The length is n=1000n = 1000 positions, which is longer

than any sentence the paper trained on and is chosen so that the term in n2n^2

and the term in d2d^2 can be compared as real numbers rather than as symbols.

Where a kernel width is wanted it is k=3k = 3, and where a restricted

neighbourhood is wanted it is r=64r = 64. 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.

FIG 1The opening table at a thousand positions and a width of 512
cost per layer, hundreds of millions of multiply-addsoperations that must run in orderlongest path between two positions
self-attention5.121.001.00
recurrent2.621000.001000.00
convolutional, k = 37.861.006.30
self-attention restricted to r = 640.331.0015.60
Read the first column and attention is the second dearest of the four rows. The shading puts the point where it belongs: the two dark cells are the recurrent row's two thousands, and the self-attention row holds a 1 in each of those two columns, neither of which is about arithmetic. The first column is , , and in that order, divided by a hundred million; the two marked cells are where the case for throwing recurrence away actually sits.

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

n×n=1,000,000n \times n = 1{,}000{,}000 ordered pairs, and each pair costs one dot product

across the width, 512512 multiply-adds, which in the base model is eight separate

dot products of 6464 because the width is split across eight heads. So the score

matrix costs 1,000,000×512=512,000,0001{,}000{,}000 \times 512 = 512{,}000{,}000 multiply-adds, which

is n2dn^2 d written out.

A recurrent layer does something different at every position. The state of width

512512 is multiplied by a 512×512512 \times 512 matrix, which is

512×512=262,144512 \times 512 = 262{,}144 multiply-adds, and that happens once per position,

giving 1000×262,144=262,144,0001000 \times 262{,}144 = 262{,}144{,}000, which is nd2n d^2 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

1,024,000,0001{,}024{,}000{,}000. A recurrent layer multiplies the input by a second

512×512512 \times 512 matrix as well as the state, so its real figure is nearer

524,288,000524{,}288{,}000. 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:

n2dnd2=nd=1000512=1.95\frac{n^2 d}{n d^2} = \frac{n}{d} = \frac{1000}{512} = 1.95

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 n=dn = d, which at this width is 512512 positions. Shorter than that and

attention is cheaper; longer and it is dearer, without limit.

FIG 2Cost per layer against sequence length, at a width of 512
0.00625.001250.001875.002500.001.0500.81000.51500.32000.0positions in the sequence
self-attentionrecurrentconvolutional, k = 3
The two straight lines are linear in length and the parabola is not, so it crosses each of them exactly once: the recurrent line where , which is , and the convolutional line where , which is . Left of 512 positions attention is the cheapest layer on the page. Between 512 and 1,536 it is dearer than recurrence and still cheaper than convolution. Past 1,536 it is the dearest of the three and the gap widens for ever. The first of those crossings is the whole content of the paper's sentence that self-attention is faster than recurrence when the sequence is shorter than the representation is wide, which is a conditional claim and is true of translated sentences.

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, nn was comfortably below dd 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 kk does the recurrent

layer's arithmetic kk times over, 3×262,144,000=786,432,0003 \times 262{,}144{,}000 = 786{,}432{,}000

at a width of three, and the paper says as much. Restricting attention to a

neighbourhood of r=64r = 64 positions costs

64×1000×512=32,768,00064 \times 1000 \times 512 = 32{,}768{,}000, 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 tt is a function of the state at position

t−1t-1. 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: 262,144262{,}144 weights read from memory to perform

262,144262{,}144 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.

FIG 3What has to wait for what
Take from this that the two columns are measuring the same arithmetic cut up two different ways. The totals are within a factor of two of each other; the number of times the machine has to stop and wait differs by a factor of a thousand. That is what a constant number of sequential operations buys, and in 2017 it was the difference between filling eight processors and starving them.

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.

FIG 4Multiply-adds performed per weight read from 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
A recurrent step reads every weight to perform one multiply-add with it, so the layer spends its time waiting for memory with its arithmetic units idle. The same weights multiplied into all thousand positions at once pay for that read a thousand times over. This is why a constant sequential count is worth paying nearly twice the arithmetic for, and it is also why the ratio got better rather than worse as processors grew faster than the memory feeding them.

The paper reports what that bought. Eight P100 processors, about 0.40.4 seconds

per training step and 100,000 steps for the base model, which is

100,000×0.4=40,000100{,}000 \times 0.4 = 40{,}000 seconds, or 11.111.1 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 1.01.0 second a step, so

300,000×1.0=300,000300{,}000 \times 1.0 = 300{,}000 seconds, which is 83.383.3 hours, or 3.473.47

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 LL such layers gives a view of

L(k−1)+1L(k-1) + 1 positions, because every extra layer widens the view by k−1k-1. 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.

FIG 5How deep a convolutional stack has to be before the ends meet
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 left count is contiguous kernels, where each layer adds two positions to the view, and the right is dilated kernels, where each layer multiplies the view by the kernel width instead of adding to it. Take from this that the path length column is pricing depth. Seven dilated layers at 786 million multiply-adds each is 5,505 million before the ends of the window have met once, against 512 million for a single attention layer that has already connected every pair.

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 1,000,0001{,}000{,}000 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

NextThe One Equation in It →

The rest of this course

  1. 01Why Attention Replaced Recurrence When It Costs Moreyou are here
  2. 02What the Attention Equation Actually Computesopening only
  3. 03Why Eight Attention Heads and Not One Wide Oneopening only
  4. 04Why the Position Signal Is a Ladder of Sine Wavesopening only
  5. 05Most of the Block Is Not Attentionopening only
  6. 06The Training Recipe, Read as a List of Repairsopening only
  7. 07Reading the Results Tables With the Cost Column Inopening only
  8. 08Sorting the Paper's Choices Into Three Pilesopening only

Read alongside