18. The decoder and encoder–decoder attention
The decoder writes the translation one word at a time. Each new word looks back at the German already written, through a mask, and across at the English, through encoder–decoder attention, before the softmax picks it.
The paper's model translating the chosen sentence, one German word per step, choosing the most likely word each time. On the left, the German written so far and the masked self-attention of the newest position over it: it sees only what is already written. On the right, the encoder–decoder attention of that position over the English, one row of an alignment map that grows as the translation does, and how sure the softmax was of the word. Both are averaged over the four heads of the last decoder layer. Last, the whole map, and one head's weight on the English noun when the decoder writes the German for “it”.
The idea
The decoder is the right half of Figure 1. It writes: it starts from the start token <s>, predicts the first German word, appends it, and runs again for the next, until it writes the end token </s>. Each run is a stack of N layers, and each layer has three parts.
- Masked self-attention over the German written so far (chapter 10): a position may look only at itself and the words before it, so training on whole sentences cannot cheat.
- Encoder–decoder attention: the queries come from the decoder, the keys and values from the encoder's output. This is where the translation happens: each German position looks across at the English and takes what it needs.
- The feed-forward network, as in every layer, each sub-layer with its Add & Norm.
Drawn row by row, the encoder–decoder attention is an alignment map: for each German word, how much it looked at each English word. On our model it is sharp: when writing an adjective, the decoder looks almost only at the English adjective.
A worked example: “kleinen” and “ihn”
Translating “Anna has a small dog . She likes it .”, the decoder writes “Anna hat einen kleinen Hund . Sie mag ihn .”. When it writes “kleinen”, its last layer's encoder–decoder attention puts 99.9 % on “small”, averaged over the four heads: the German adjective looks at the English one.
“It” is harder: German needs the noun's gender, and the noun is in the first sentence. When the decoder writes “ihn”, encoder–decoder head 2.1 puts 100.0 % of its weight on “dog”. Over the held-out sentences, the head gallery finds this head on the English noun 99.9 % of the time when the decoder writes the German for “it” (29 cases).
Meanwhile the masked self-attention of the position writing “ihn” sees 9 tokens, <s> and the German before it, and nothing after. The softmax was 94.2 % sure of “ihn”.
The formula
- \(y\)
- the decoder's vectors after its masked self-attention: one per German position
- \(m\)
- the encoder's output, the memory: one vector per English token
- \(yW^Q\)
- the queries come from the German
- \(mW^K,\ mW^V\)
- the keys and values come from the English
- \(\operatorname{softmax}(\cdots)\)
- one row per German position: how much it looks at each English token, adding up to 1
Why it matters
Writing one token at a time, each depending on all before it, is how every generative model answers you, and it is why output is slower and usually priced higher than input. The decoder runs once per token it writes; the encoder ran once for the whole input.
Encoder–decoder attention is also the original version of what you do when you paste a document into a prompt and ask about it: the writing side consults a separate, fully read source. GPT-style models fold source and answer into one sequence instead (chapter 22).
Where it fails
- One word at a time, no going back. A word, once chosen, stays; a poor early choice shapes everything after it. Beam search (chapter 19) keeps a few options open to soften this.
- Attention is not alignment. Our map is sharp because our sentences are simple and nearly word for word; on real text, German word order and words with no direct English counterpart make the map spread out.
- The pronoun works only within reach. Our model saw the noun and the pronoun within two short sentences; a noun many sentences back is much harder for any model.
Exercises
Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.
Where does “kleinen” look?
In the clip's first sentence, which English word does the decoder look at most when it writes “kleinen”?
The pronoun head
When the decoder writes “ihn” in the clip's first sentence, what share of its weight does encoder–decoder head 2.1 put on “dog”? In percent, one decimal.
What the mask lets through
When the decoder computes the position that writes the fourth German word, how many tokens can its masked self-attention see?
Queries, keys and values
In encoder–decoder attention, where do the queries, keys and values come from?
In the paper
Section 3.1: the decoder is a stack of N = 6 identical layers that adds to the encoder's two sub-layers “a third sub-layer, which performs multi-head attention over the output of the encoder stack”, and masks its self-attention so that a position's prediction depends only on the words before it. Section 3.2.3: “In "encoder-decoder attention" layers, the queries come from the previous decoder layer, and the memory keys and values come from the output of the encoder. This allows every position in the decoder to attend over all positions in the input sequence.”
In the paper: section 3.1, 3.2.3