21. Why self-attention
Why did the paper drop recurrence altogether? Section 4 compares three kinds of layer on three costs: the work per layer, how much must happen one step after another, and how far apart two words are inside the network.
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.
The idea
Before the transformer, translation models read sentences with recurrent networks (chapter 7): one word per step, each step waiting for the last. Section 4 of the paper argues for self-attention on three costs, and the simulation counts each.
- Work per layer. Self-attention compares every pair of words: about n² × d operations for n words of d numbers. A recurrent layer multiplies a d-number state by a d × d matrix once per word: about n × d².
- Sequential operations. A recurrent layer needs n steps one after the other; convolutions and self-attention do all positions at once, so their work can be spread over a GPU.
- Path length. How many steps information takes between two words. For the first and last word: n − 1 states for recurrence, several stacked layers for convolutions, a single step for self-attention. Short paths make long-range links, such as a pronoun and its noun, easier to learn.
The work per layer favours self-attention while n is smaller than d. The paper notes that this is most often the case for the sentences of machine translation, against its d = 512, and it holds for ours.
A worked example: ten words
“Anna has a small dog . She likes it .” has n = 10 words. The recurrent layer takes 10 steps one after another; its first word reaches its last through 9 states. Convolutions with a window of 3 need 5 layers before the first word and the last are connected. Self-attention: one step, path 1.
With our d = 64: self-attention's work is n² × d = 6,400, recurrence's n × d² = 40,960, a convolution's k × n × d² = 122,880. Self-attention is the cheapest of the three, and stays so for sentences up to 64 words.
The formula
- \(n\)
- the sequence length: words or tokens in the sentence
- \(d\)
- the representation size, d_model: 512 in the paper, 64 in ours
- \(k\)
- a convolution's window: how many neighbouring words it reads
- \(O(1),\ O(n),\ O(\log_k n)\)
- Table 1's sequential operations and path lengths: constant for self-attention, growing with n for recurrence, with the logarithm of n for dilated convolutions
Table 1 gives the convolutions' path as \(O(\log_k n)\), for dilated convolutions that skip further at each layer; the plain convolutions in the clip need about \(n/k\) layers, which the paper also mentions.
Why it matters
The n² is the reason context windows are limited and long prompts cost more than short ones: doubling the length of a prompt roughly quadruples the attention work of every layer. Today's models with very long contexts use variants of attention or much more hardware to push the crossover away; the trade-off in Table 1 is still there.
The other two columns are why transformers took over: no step waits for the one before during training, so the work spreads over thousands of GPU cores, and any word can reach any other in one step.
Where it fails
- Long inputs turn the table. Once n exceeds d, self-attention's work per layer is larger than recurrence's, and it keeps growing with n².
- Writing is still sequential. Table 1 counts one layer over a known sequence; a decoder writing text still produces one token at a time (chapter 18).
- The counts hide constants. O(n² · d) and O(n · d²) say how costs grow, not how many seconds a layer takes on real hardware.
Exercises
Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.
Sequential steps
How many sequential steps does a recurrent layer need for the clip's first sentence?
Convolutions to connect
With a window of k = 3, each convolution layer lets a word see two more words. How many layers connect the first and last of 10 words?
The crossover
With the paper's d = 512, from how many words n does a self-attention layer do more work than a recurrent one?
Which is cheaper?
For a sentence of n = 10 words with d = 64, which layer does less work per layer, self-attention or recurrence?
In the paper
Section 4 names the three desiderata: “One is the total computational complexity per layer. Another is the amount of computation that can be parallelized, as measured by the minimum number of sequential operations required. The third is the path length between long-range dependencies in the network.” Table 1 sets them out per layer type, and the text draws the conclusion: “self-attention layers are faster than recurrent layers when the sequence length n is smaller than the representation dimensionality d”.
In the paper: section 4