DL0036 Transformer Architecture II

What are the main differences between the encoder and decoder in a Transformer?

Answer

The encoder builds rich bidirectional representations of the source sequence: every token attends to every other token. The decoder is built for generation: its self-attention is causally masked so each position only sees the past, and it inserts an extra cross-attention sub-layer that reads the encoder’s output. Same building blocks, different wiring for two different jobs: understanding vs generating.

(1) Self-Attention Masking: Encoder self-attention is unmasked (full bidirectional context); decoder self-attention is masked so position t attends only to \leq t.
(2) Cross-Attention: Absent in the encoder; present in every decoder layer: queries from the decoder state, keys/values from the encoder output.
(3) Inputs & Role: Encoder consumes the source sequence once; decoder consumes the shifted-right target (teacher forcing) and produces next-token distributions.

Side-by-side encoder and decoder stacks separated by a dashed line, with the encoder's two sub-layers versus the decoder's three sub-layers and a cross-attention arrow bridging encoder output into the decoder.

Figure 1: Two stacks, three differences: the decoder adds a causal mask on self-attention and a cross-attention bridge to the encoder output.

Mathematical Formulation (decoder self-attention mask):
\mathrm{Attn}(Q, K, V, M) = \mathrm{softmax}\left(\frac{QK^\top}{\sqrt{d_k}} + M\right)V
M_{ij} = 0 \;\text{if}\; j \leq i,\quad -\infty \;\text{if}\; j > i

Where:

  • M is the causal mask: -\infty above the diagonal zeroes out future positions after softmax; the encoder simply omits M.
  • i, j index query and key positions; j \leq i means “past or present only”.
AspectEncoderDecoder
Self-attentionUnmasked: all positionsMasked: past positions only (causal)
Cross-attentionNot presentPresent: attends to encoder outputs
Positional encodingAdded to source embeddingsAdded to target embeddings (shifted right)
InputSource sequenceShifted target + encoder outputs
FunctionEncode source into contextual representationsGenerate target autoregressively with source context

Table 1: Encoder vs decoder at a glance. The decoder is a superset: same sub-layers plus masking and the cross-attention bridge.

Three mini diagrams showing query key value sources for encoder self-attention, masked decoder self-attention, and cross-attention with queries from the decoder and keys and values from the encoder output.

Figure 2: Q/K/V sources for the three attention types: only cross-attention mixes sequences: Q from the decoder, K/V from the encoder.

Why “Shifted Right”: During training the decoder receives the target sequence shifted one position right (prefixed with a start token), so the prediction at position t is supervised against token t while the input only ever reveals tokens before it. This is teacher forcing, and it keeps training fully parallel despite autoregressive inference.


Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *