Chapter 5

Transformer Architecture Families

Compare encoder-only, decoder-only, and encoder-decoder Transformers through their information flow, masks, objectives, and uses. The chapter uses BERT, GPT, and T5 as bounded examples rather than as separate API surveys.

The Transformer began as an encoder-decoder model for sequence-to-sequence tasks. Later systems retained only the encoder, retained only the decoder, or adapted both sides under new training objectives. These families reuse many operations from the previous chapters, but they do not expose the same information to each position.

This chapter compares three common families:

  • an encoder-only model lets each non-padding input position read the whole input sequence;
  • a decoder-only model uses a causal mask so each position reads only the allowed prefix;
  • an encoder-decoder model encodes a source sequence, then uses causal target self-attention and cross-attention to the encoded source.

The comparison holds the tokens small. We will use source tokens A B C and, when a separate target is needed, target tokens X Y. Exact visibility matrices will show permitted routes before any model learns attention weights.

Four Questions Define the Comparison

For every architecture, ask:

  1. Which sequence supplies the query?
  2. Which sequence supplies the keys and values?
  3. Which pairs of positions are permitted by the mask?
  4. Which targets and loss positions define learning?

These questions are more reliable than choosing an architecture from its name. Two systems may use similar blocks but different masks, objectives, readouts, or source-target interfaces.

After this chapter

  • Identify self-attention, causal attention, and cross-attention paths.
  • Match architecture masks and objectives to suitable tasks.
  • Distinguish the original Transformer from a modern decoder-only LLM.

Lessons

  1. 01
    One Block, Three Information-Flow Families

    Compare encoder-only, decoder-only, and encoder-decoder information flow with shared tokens, exact masks, and explicit Q/K/V sources.

    1 exercise
  2. 02
    Encoder-Only Models Build Contextual Input Records

    Trace encoder-only full self-attention, contextual record shapes, padding behavior, and task readouts.

    1 exercise
  3. 03
    Masked-Token Prediction Uses Corrupted Inputs

    Trace masked-token corruption, target selection, bidirectional evidence, leakage risks, and selected-position loss.

    1 exercise
  4. 04
    Decoder-Only Models Predict the Next Token

    Derive decoder-only causal visibility, shifted next-token targets, parallel training, and sequential generation.

    1 exercise
  5. 05
    Encoder-Decoder Models Separate Source and Target Streams

    Trace encoder and decoder states, teacher-forced target shifting, causal self-attention, cross-attention, and conditional generation.

    1 exercise
  6. 06
    Cross-Attention Reads Encoder Records

    Derive cross-attention Q/K/V sources, target-source tensor shapes, numerical scores, source masks, and interventions.

    1 exercise
  7. 07
    Match Architecture, Objective, and Interface to a Task

    Match encoder, decoder, or encoder-decoder starting points to task contracts, objectives, interfaces, and declared costs.

    1 exercise
  8. 08
    Audit an Architecture-Family Specification

    Audit a Transformer family specification using dependency-ordered evidence, failure signatures, and source or causality interventions.

    1 exercise

Review and practice

  1. Review

    Review Transformer family information flow, shapes, masks, objectives, distinctions, and audit order.

  2. Exercises

    Solve Transformer family visibility, target, QKV source, shape, selection, and debugging exercises.

Chapter progress