Neural Machine Translation › The Transformer for Translation

Michael BrenndoerferNovember 26, 20263 min read
Language AI HandbookAll chapters
Part 27 of 67Machine Translation and Speech Translation
Chapter 191Neural Machine Translation › The Transformer for Translation
Reading section 5 of 12 · The Transformer for Translation · 3 minSection 5 of 12 · 3 min

Section 5 of 12 · 3 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

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 m2m^2 entries for a source of length mm.

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 tt sees only positions ≤t\le t 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.

Cross-Attention as the Translation Interface

In cross-attention, the decoder's hidden state at step tt forms a query qt=WQht(dec)q_t = W_Q h_t^{(dec)}, and the encoder output forms keys and values kj=WKhj(enc)k_j = W_K h_j^{(enc)}, vj=WVhj(enc)v_j = W_V h_j^{(enc)}. The output is ∑jsoftmax⁡j(qt⊤kj/dk) vj\sum_j \operatorname{softmax}_j(q_t^\top k_j / \sqrt{d_k})\, v_j. 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!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy