Decoder-Only vs. Encoder-Decoder: GPT vs. BERT vs. T5

LLMs

Chapter 6 · Decoder-Only vs. Encoder-Decoder: GPT vs. BERT vs. T5

llm1-5 built one Transformer block. The 2017 "Attention Is All You Need" paper nlp1-8 already cited actually proposed stacking these blocks into an encoder-decoder pair, for machine translation specifically — nlp1-8's own real motivating task. This chapter surveys the three architecture families that emerged from arranging Transformer blocks differently, and explains why one of them became dominant.

Three Families, Three Attention Patterns

FamilyAttention directionTraining objectiveTypical use
Encoder-only (BERT)Bidirectional — every token attends to every other token, both directionsMasked language modeling — predict randomly hidden tokens from surrounding contextClassification, understanding tasks
Encoder-decoder (T5)Encoder bidirectional; decoder causal, cross-attending to the encoderText-to-text — map an input sequence to an output sequenceTranslation, summarization
Decoder-only (GPT)Causal — each token attends only to itself and earlier tokensNext-token prediction over one continuous sequenceGeneral-purpose generation, the dominant modern shape

Encoder-Only: BERT's Bidirectionality

BERT (2018) stacks only encoder-style Transformer blocks, where every token can attend freely to every other token in the sequence, in both directions. This is the exact Transformer-era counterpart of nlp1-7's own bidirectional LSTM — both exist to let a token's own representation be informed by context on both sides, not just what came before. BERT trains via masked language modeling: randomly hide some tokens in the input and predict them using the full surrounding context, generalizing nlp1-5's own context-prediction training signal to a full Transformer stack.

Encoder-Decoder: T5 and the Original Transformer

This is the architecture nlp1-8 already introduced conceptually — an encoder reads the entire input sequence (bidirectionally), and a decoder generates the output sequence one token at a time, at each step cross-attending back to the encoder's own output. T5 generalizes this into a "text-to-text" framing, treating every task — translation, summarization, classification — as converting one string of text into another.

Decoder-Only: Causal Masking

GPT's own family uses no separate encoder at all — one single stack of Transformer blocks, with a critical modification to llm1-4's own attention formula: a causal mask that forces each token to attend only to itself and tokens before it, never tokens that come later.

scores = (Q @ K.T) / sqrt(d_k)
scores = scores.masked_fill(future_positions, -inf)   # block attention to later tokens
weights = softmax(scores, axis=-1)                      # -inf becomes exactly 0 after softmax
output = weights @ V

Setting future-position scores to negative infinity before softmax guarantees their resulting attention weight is exactly zero — a token generating its own prediction can never "peek" at tokens that haven't been generated yet. This single change is what makes autoregressive generation coherent: predicting each next token uses only what has genuinely already been produced.

Why Decoder-Only Became the Dominant General-Purpose Shape

Delivering nlp1-10's own third claim, mechanically
nlp1-10's own capstone named this directly: nlp1-6's sentiment classifier and nlp1-7's NER tagger were genuinely separate pipelines, each with its own architecture and training objective. A decoder-only model needs neither. Nearly any task can be reframed as "generate the correct continuation of this text" — sentiment becomes "Review: ... Sentiment:", NER becomes "Sentence: ... Named entities:". The same trained weights, the same architecture, perform both — steered entirely by how the input text is framed, not by swapping out the model.

This also explains the simplicity advantage: one stack, one training objective (predict the next token), rather than BERT's separate masked-prediction objective or T5's split encoder/decoder training. Because generating a coherent next token requires the model to have effectively "understood" everything so far, useful general-purpose representations emerge as a side effect of the generative objective alone — no separate understanding-specific objective needs to be designed in.

The GPT Lineage

This decoder-only, causally-masked architecture is the actual mechanism behind historyai3-7's own GPT-1 through GPT-3 lineage namecheck — the architecture story, not yet the training story. How pretraining at real scale actually works is llm1-7's own job; how GPT-3 became ChatGPT through fine-tuning and RLHF is llm1-9's.

Hands-On Exercises

Exercise 1

Explain the specific attention-direction difference between BERT, T5, and GPT, and explain why BERT's own bidirectionality is described as the Transformer-era counterpart of nlp1-7's bidirectional LSTM.

📄 View solution
Exercise 2

Explain mechanically how causal masking works, using this chapter's own code, and explain why setting future-position scores to negative infinity (rather than, say, zero) is the correct way to block attention to future tokens.

📄 View solution
Exercise 3

Using this chapter's own sentiment/NER reframing example, explain precisely why a decoder-only model can perform both nlp1-6's and nlp1-7's own tasks without needing two separately built pipelines, and explain what specifically changes between the two cases (and what doesn't).

📄 View solution

Chapter 6 Quick Reference

  • Encoder-only (BERT) — bidirectional attention, masked language modeling, understanding tasks; the Transformer-era counterpart of nlp1-7's BiLSTM
  • Encoder-decoder (T5) — nlp1-8's own original architecture, generalized to a text-to-text framing
  • Decoder-only (GPT) — causal masking via `-inf` on future positions, next-token prediction, the dominant general-purpose shape
  • Causal masking is what makes autoregressive generation coherent — no peeking at ungenerated tokens
  • Decoder-only wins on flexibility: one architecture, reframed via text, replaces nlp1-6's and nlp1-7's own separately-built pipelines — nlp1-10's own third claim, delivered mechanically
  • The mechanism behind historyai3-7's own GPT lineage — training story deferred to llm1-7 (pretraining) and llm1-9 (fine-tuning/RLHF)
  • Next chapter: Pretraining at Scale: Self-Supervised Learning