Attention Is All You Need · Section 4

4 Why self-attention

The paper's argument for its design, in one table: what a self-attention layer costs against a recurrent or a convolutional one, on three counts.

Table 1, live

Section 4 of the paper compares self-attention with the layers it replaced, on three counts: how much work a layer does, how many steps must happen one after another, and how many steps a signal needs to travel between two far-apart words. Set the sizes and watch the counts.

The paper's Table 1, with your numbers
Layer type Complexity per layer Sequential operations Maximum path length
Self-Attention O(n²·d)
= 51,200
O(1)
= 1
O(1)
= 1
Recurrent O(n·d²)
= 2,621,440
O(n)
= 10
O(n)
= 10
Convolutional O(k·n·d²)
= 7,864,320
O(1)
= 1
O(log_k(n))
= 3
Self-Attention (restricted) O(r·n·d)
= 81,920
O(1)
= 1
O(n/r)
= 1

The formulas are those of the paper's Table 1 (section 4), as written there; the values are the formulas without O(...), so they show how the costs grow, not times.

With n = 10 and d = 512, self-attention does less work per layer than a recurrent layer: n is smaller than d, so n²·d is 51.2 times smaller than n·d².
Complexity per layer as the sentence grows, with d = 512 On a logarithmic scale; self-attention's line crosses the recurrent layer's where n = d = 512. 1000 1e4 1e5 1e6 1e7 1e8 1e9 1e10 0 500 1000 1500 2000 n, tokens in the sentence Complexity per layer n = d n
  • Self-Attention
  • Recurrent
  • Convolutional
  • Self-Attention (restricted)

How to read the table

n is the number of tokens in the sentence, d the length of each token's vector, k the width of a convolution's window and r how many neighbours a restricted self-attention looks at. O(...) means “grows like”: the table counts how the cost grows, not seconds.

Self-attention compares every token with every other one: n × n pairs, each a dot product of d numbers, so n²·d. A recurrent layer walks the sentence one token at a time, n steps, each multiplying a vector by a d × d matrix, so n·d². When n is smaller than d, which is usual for sentences (our model: about 10 tokens and d = 64; the paper: d = 512), self-attention is cheaper. For very long texts it is not, which is why long contexts cost so much in today's models.

The other two columns are where self-attention wins outright. A recurrent layer must take its n steps one after another, so it cannot use a GPU's parallel power within a sentence; self-attention does all pairs at once. And two words n places apart are one step apart in self-attention, but n steps apart in a recurrent layer, so a signal between them has far to travel and fades.

In the paper (4)

“In terms of computational complexity, self-attention layers are faster than recurrent layers when the sequence length n is smaller than the representation dimensionality d”

On its own page: Table 1 explorer

The paper's Table 1 with your own numbers: for self-attention, recurrent, convolutional and restricted self-attention layers, the work per layer, the sequential operations and the maximum path length, as formulas and as values for the n, d, k and r you set. A chart shows the work per layer as the sentence grows and where self-attention overtakes recurrence.

What this section says

Section 4 compares self-attention with the layers it replaces on three counts, which the paper calls desiderata. Work per layer: self-attention compares every pair of the n positions, about n² · d operations; a recurrent layer multiplies a state of d numbers by a d × d matrix at each position, about n · d². Sequential operations: steps that must wait for each other, n for recurrence, a constant for the others. Maximum path length: how many steps a signal needs between the two furthest positions, the count of section 2.

Self-attention wins the last two outright. On the first it wins while n is smaller than d, which the paper notes is usually so for sentences in translation, with d = 512 and word pieces. For very long inputs it suggests restricting attention to a neighbourhood of r positions, which makes the work r · n · d and the path n / r (the last row of Table 1).

Convolutions, the third row, cost k times more than recurrence per layer and need a stack of layers to connect distant positions. The section closes with a side benefit: attention weights can be looked at, and many heads seem to follow the structure of the sentence (the appendix).

In the paper's words

“As noted in Table 1, a self-attention layer connects all positions with a constant number of sequentially executed operations, whereas a recurrent layer requires \(O(n)\) sequential operations.”

Section 4 In plain words: Self-attention links every position in a fixed number of steps; a recurrent layer needs one step per position.

“As side benefit, self-attention could yield more interpretable models.”

Section 4 In plain words: A bonus: the attention weights may show what the model is doing.

Quoted from Vaswani et al., Attention Is All You Need, arXiv:1706.03762v7(opens in a new tab). The figures on this page are redrawn, not copied.

The notation

The symbols of this section, in the paper and in our model
Symbol Meaning In the paper Our miniature
\(n\) the sequence length a sentence of word pieces up to 10 tokens in training
\(d\) the representation size, d_model 512 64
\(k\) a convolution's kernel size: how many neighbours it reads any not used
\(r\) the neighbourhood of restricted self-attention a suggestion for future work not used
\(O(\cdot)\) how a cost grows with the sizes, constants left out Table 1 the same

Try it

Three layers, one sentence
Sentence
Dimension d
Playback speed

The chosen sentence through three kinds of layer. A recurrent layer reads one word per step, passing a state along, and the counter adds a step for each word; the first word reaches the last only through every state in between. Convolutions with a window of three words connect neighbours, and each further layer widens what a word can see by two, until the first and the last word meet. Self-attention connects every word to every other in one step. Last, the paper's Table 1 filled in for this sentence's n and the d chosen above, and whether self-attention costs more than recurrence.

How our miniature differs

With our d = 64, self-attention costs less work per layer than recurrence up to sentences of 64 tokens, and more from 65 on; the paper's d = 512 moves that point to 513. Our sentences have at most 10 tokens, far below either.

Prepared in the Course by

Check yourself

Three questions a reader of this section should now be able to answer. The lab checks your answer against the paper and our models.

The three counts

Which three counts does section 4 compare the layer types on?

Where self-attention costs more

With d = 512, from which sequence length n does a self-attention layer do more work than a recurrent one?

Your answer
A whole number

Restricted self-attention

With n = 1000 and a neighbourhood of r = 100, what is the maximum path length n / r of restricted self-attention?

Your answer
A whole number
An unhandled error has occurred. Reload 🗙

Reconnecting…

Your connection to the Transformers and Attention Lab dropped, probably a Wi-Fi blip. Hold on while it reconnects.

The connection could not be restored. The page will reload in a few seconds.