Neural Machine Translation › The Transformer for Translation
Language AI HandbookAll chapters The encoder-decoder Transformer uses attention for communication within the encoder and the decoder, and to pass source information to target predictions.
In a unidirectional RNN, information from source position 1 reaches position 30 through 29 recurrent transitions. In global self-attention, position 30 can read position 1 within one layer. Compare those paths separately from arithmetic cost. The standard dense attention score matrix has entries for a source of length .
Self-attention can process the known positions of a training sequence in parallel. Autoregressive inference still generates positions successively. Vaswani et al. (2017), §5.2 report training on eight P100 GPUs for 12 hours for the base models and 3.5 days for the big models. Their Table 2 reports 28.4 BLEU on WMT 2014 English–German and 41.8 on English–French for the big models.
Residual connections provide an additive route around each attention or feed-forward sublayer. A short token-to-token path and a residual path describe the graph. To compare gradient behavior across architectures, measure it under controlled training configurations.
The three attention blocks
An encoder-decoder transformer for translation has three distinct attention operations, and it is worth keeping them straight:
- Encoder self-attention. Unmasked source positions can attend in both directions, with padded positions excluded as keys.
- Decoder masked self-attention. Causal, so position sees only positions in the target prefix. This preserves the autoregressive factorization.
- Decoder cross-attention. Queries come from the decoder, keys and values from the encoder output. In this translation model, cross-attention lets target generation read the source representation.
Cross-attention is the source-conditioning route in this encoder-decoder. Its weights are what we compare with reference correspondences in the worked example below.
In cross-attention, the decoder's hidden state at step forms a query , and the encoder output forms keys and values , . The output is . The softmax supplies the source weights for the sum.
Configuration choices that matter for MT
Vaswani et al.'s base Transformer used six encoder and six decoder layers, model width 512, eight heads, and feed-forward width 2048. Record the actual memory, latency, and data constraints before choosing a starting configuration.
- Pre-norm vs. post-norm. The original Transformer placed normalization after each residual addition (§3.1) and used learning-rate warmup (§5.3). Xiong et al. (2020) studied moving normalization inside the residual block and tested removing warmup in their pre-norm configurations. See Pre-Norm vs Post-Norm.
- Depth vs. width. Vary encoder and decoder depth separately when you want to know which side improves quality for your language pair under a fixed compute budget.
- Weight tying. If the target embedding and output projection use compatible shapes, sharing a weight matrix removes one separate set of weights. See Weight Tying.
- Position encoding. Compare absolute and relative position schemes at the input lengths you expect to use. Include document-length examples when the intended application needs document context.
Where decoder-only LLMs fit
A decoder-only Transformer can receive source text and a target-language instruction as a prefix, then generate the translation using causal self-attention. GPT-3 covers a prompted example of this approach. The rest of this chapter uses an encoder-decoder with separate source states.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!