What are the structural differences between encoder-only, decoder-only, and encoder-decoder Transformer architectures?

Last updated 22 November 2026

The three differ in which attention masks they use and whether there is cross-attention. Encoder-only uses bidirectional self-attention. Decoder-only uses causal (masked) self-attention. Encoder-decoder pairs a bidirectional encoder with a causal decoder that also cross-attends to the encoder output.

  • Encoder-only (BERT, RoBERTa):

    • Layer = full self-attention + FFN, with no mask, so each token sees the whole sequence.

    • Outputs one contextual vector per token, typically topped with a classification or token-tagging head.

    • Pretrained with masked language modeling. It cannot generate text autoregressively in a natural way.

  • Decoder-only (GPT, Llama):

    • Layer = causal self-attention + FFN. A lower-triangular mask means token i only sees tokens up to i.

    • Prompt and output share one stream. An LM head predicts the next token.

    • Supports a KV cache for incremental decoding.

  • Encoder-decoder (original Transformer, T5, BART):

    • Encoder stack: bidirectional self-attention over the input.

    • Decoder stack, in three sublayers per layer:

    • 1) causal self-attention over generated tokens; 2) cross-attention, with queries from the decoder and keys/values from the final encoder output; 3) FFN.

    • Input and output are separate sequences with separate positional handling.

  • Shared pieces across all three:

    • Token embeddings, positional information, residual connections, LayerNorm, and multi-head attention.

    • The architectural difference really comes down to the mask and the presence of cross-attention.

  • https://huggingface.co/learn/llm-course/en/chapter1/6
  • https://www.youtube.com/shorts/YlZtPdrtJL4
  • https://magazine.sebastianraschka.com/p/understanding-encoder-and-decoder
All Transformers interview questions