16. Stacking layers
Chapters 9 to 15 built one layer: attention, Add & Norm, feed-forward, Add & Norm. The transformer is that layer stacked N times in the encoder, and a slightly bigger one stacked N times in the decoder.
Encoder layer 1 of 2: “it” looks most at “it” (24.4 %, averaged over the four heads), and the layer changes its vector by 77.8 %.
| Token | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | … 56 more |
|---|---|---|---|---|---|---|---|---|---|
| it | 0.22 | −0.39 | 0.93 | −0.07 | −0.41 | −1.40 | 0.02 | 0.73 | … |
The paper's model drawn in Figure 1's style and built up in three stages: one encoder layer with its four parts; the encoder, N of those layers on top of the embeddings; then the decoder beside it, N layers of six parts, each reading the encoder's output, with Linear and Softmax on top. Our model has N = 2. Pick a layer, or send the sentence on to the next one: the panel says where the chosen word looked most in that layer, how much the layer changed its vector, and shows the vector.
The idea
Everything so far fits in one layer: multi-head attention lets the words look at each other, the feed-forward network works on each word, and each of the two is wrapped in a residual connection and a layer normalisation. A layer takes one vector per word and returns one vector per word, of the same size. So its output can be the next layer's input, and layers can be stacked as high as you like.
Each layer refines what the one below produced. In the first, a word mostly sees its neighbours and itself; higher up, it can combine what other words gathered in the layer before, so links across the sentence can form in two hops. The paper stacks N = 6 layers on each side; our model, N = 2.
The decoder's layer has one part more: between its masked self-attention and its feed-forward network sits an attention that looks at the encoder's output, the English (chapter 18). Every decoder layer reads the same final output of the encoder.
A worked example: “it” and “Hund”, layer by layer
In “Anna has a small dog . She likes it .”, follow “it” up the encoder. In layer 1 it looks most at “it” (24 %, averaged over the four heads), and the layer changes its vector by 78 %. In layer 2 it looks most at “dog” (32 %), and the layer changes it by 51 %: one layer up, the pronoun has found its noun.
On the decoder side, where it writes “Hund”: in layer 1 its encoder–decoder attention looks most at “</s>” (25 %), in layer 2 at “dog” (100 %). The second layer has found the English word to translate.
Each encoder layer of ours has 49,728 weights and each decoder layer 66,240; with N = 2 on each side, the layers hold 231,936 of the model's 244,800 weights. The rest is the shared embedding matrix.
The formula
- \(x^{(0)}\)
- the words' embeddings with their positions (chapters 5, 12 and 15)
- \(\operatorname{Layer}_l\)
- layer l: its own weights, the same structure as every other layer
- \(x^{(l)}\)
- the output of layer l: still one vector of d_model numbers per word
- \(N\)
- how many layers are stacked: 6 per side in the paper, 2 in ours
- \(x^{(N)}\)
- the encoder's output, which every decoder layer reads
Why it matters
Depth is one of the main dials of model size. Large published models stack dozens of layers, and every token you send or receive passes through all of them, one after the other: the layers cannot run in parallel for the same token, which is part of why a deep model answers more slowly than a shallow one of similar width.
Because every layer has the same shape, the architecture is simple to scale: more layers, wider vectors, more heads. Chapter 23 puts our two layers, the paper's six and published sizes side by side.
Where it fails
- Layers are not neat stages. It is tempting to say “layer 1 does grammar, layer 2 meaning”; what a layer does is learned and spread out, and our measurements show tendencies, not rules.
- Deeper is not automatically better. The paper's Table 3 (row C) varies the size of the model; deeper and wider models cost more to train and to run, and pay off only with enough data.
- The decoder reads only the encoder's last layer. Whatever the earlier encoder layers worked out reaches the decoder only through the final output.
Exercises
Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.
Layer 2, “it”
In the figure's first sentence, which word does “it” look at most in the encoder's second layer, averaged over the four heads?
One encoder layer
How many weights does one encoder layer of our model have? The panel shows it.
The paper's decoder stack
The paper's decoder has N = 6 layers of three sub-layers each. How many sub-layers is that, each followed by its own Add & Norm?
Who reads the encoder?
Which part of the decoder reads the encoder's output?
In the paper
Section 3.1: “The encoder is composed of a stack of N = 6 identical layers. Each layer has two sub-layers. The first is a multi-head self-attention mechanism, and the second is a simple, position-wise fully connected feed-forward network.” And: “The decoder is also composed of a stack of N = 6 identical layers. In addition to the two sub-layers in each encoder layer, the decoder inserts a third sub-layer, which performs multi-head attention over the output of the encoder stack.” Figure 1 draws one layer of each, marked N×.
In the paper: section 3.1