What sublayers make up a decoder block in the original encoder-decoder Transformer?

Last updated 22 November 2026

Each decoder block has three sublayers: masked multi-head self-attention, encoder-decoder (cross) attention, and a position-wise feed-forward network. Each is wrapped in a residual connection followed by LayerNorm.

  1. Masked (causal) self-attention:

    • Each target position attends only to earlier positions. Future positions are masked to −∞ before the softmax.

    • This preserves autoregressive generation while still allowing parallel teacher-forced training.

  2. Cross-attention:

    • Queries come from the decoder. Keys and values come from the final encoder output.

    • This is how the decoder conditions on the source sequence, for example to align with source words in translation. No causal mask is needed here.

  3. Position-wise FFN: It is the same per-token two-layer MLP as in the encoder.

  • Wrapping and stack:

    • Each sublayer uses LayerNorm(x + Dropout(Sublayer(x))).

    • The original model stacks 6 such blocks, followed by a linear projection and softmax over the vocabulary.

    • Decoder-only models such as GPT drop the cross-attention, leaving the same two sublayers as an encoder block plus a causal mask.

  • https://grasp.study/courses/fac94f80-b35b-4af6-840b-95c83799f1c5/modules/5e09b304-2509-4e33-8114-966831a60b8d/lessons/c1c32ab4-93c1-485a-9d3e-2f804ffdb0e3
  • https://varlam.net/2025-01-01-Transformers/
  • https://www.krishnaik.in/ai-tutorials/nlp/cross-attention
All Transformers interview questions