Chapter 19: The Three Transformer Architectures (Decoder, Encoder, Encoder-Decoder)
Chapter 19: The Three Transformer Architectures (Decoder, Encoder, Encoder-Decoder)
The Critical Question: What Exactly Did We Build?
Here's something important we haven't told you yet: The transformer architecture actually comes in THREE different flavors, and we've been teaching you ONE specific type!
You might be wondering: "Wait, I learned about THE transformer. Isn't there just one?"
Answer: No! The original paper "Attention Is All You Need" introduced an encoder-decoder architecture, but modern transformers come in three main types:
- Encoder-only (like BERT)
- Decoder-only (like GPT) ← This is what we taught you!
- Encoder-Decoder (like T5, the original)
This is like learning to drive: We taught you to drive an automatic car (decoder-only). But there are also manual transmission cars (encoder-only) and hybrid cars (encoder-decoder). The basic principles are the same (steering, gas, brake), but they work differently for different purposes!
Let's understand all three so you're not confused when you hear about BERT or T5!
Part 1: Understanding the Three Architectures
The Restaurant Analogy (Three Different Restaurant Types)
1. The Buffet Restaurant (Encoder-Only): BERT
How it works:
- You see ALL the food at once (entire sentence visible)
- You can look at any dish to understand the full menu (bidirectional attention)
- Goal: Understand what's available, classify it, answer questions about it
- But you CAN'T create new dishes (no generation)
Example tasks:
- "Is this sentence positive or negative?" (sentiment analysis)
- "What entities are in this text?" (named entity recognition)
- "What's the answer to this question based on this paragraph?" (question answering)
In BERT: The model sees "The cat sat on the ___" and can look at BOTH "The cat sat on the" (before) AND "mat" (after) to understand the whole sentence!
2. The Chef Creating a Recipe (Decoder-Only): GPT ← YOU LEARNED THIS!
How it works:
- Start with ingredients you have (input prompt)
- Add one ingredient at a time (generate one token at a time)
- Each new ingredient can only use what you've ALREADY added (causal masking)
- Can't see future ingredients (no peeking ahead!)
- Goal: Create something new step-by-step
Example tasks:
- "Complete this story: 'Once upon a time...'" (text generation)
- "Continue this code: 'def calculate...'" (code completion)
- "Chat with me!" (conversational AI)
In GPT: When predicting after "I love", the model can ONLY see "I love" (not future words). It generates "pizza", then generates the next word seeing "I love pizza", and so on!
This is what we taught you! Everything in Chapters 1-17 is the decoder-only architecture!
3. The Translator Restaurant (Encoder-Decoder): T5, Original Transformer
How it works:
- Encoder side: Read the entire input (like reading a menu in French)
- Understand it fully (bidirectional attention, can see all words)
- Create a rich representation
- Decoder side: Generate output one word at a time (translate to English)
- Decoder can attend to the full encoded input
- But generates autoregressively (one word at a time)
Example tasks:
- "Translate this English sentence to French" (translation)
- "Summarize this long article" (summarization)
- "Convert this text to a question" (text-to-text transformations)
In T5:
- Encoder reads: "I love pizza" (full sentence, bidirectional)
- Encoder creates rich representation: [encoded understanding]
- Decoder generates: "J'" → "aime" → "la" → "pizza" (French translation, one word at a time)
Visual Comparison
ENCODER-ONLY (BERT-style)
Reads the full input at once
Attention direction: both left and right
Best mental model: a reader
Typical output: classification, extraction, search/retrieval features
DECODER-ONLY (GPT-style)
Reads only what has already been generated
Attention direction: left-to-right with causal masking
Best mental model: a writer
Typical output: the next token, then the next token, then the next...
ENCODER-DECODER (T5-style)
Encoder reads the full source input; decoder writes the target output step by step
Attention direction: encoder is bidirectional, decoder is causal, decoder also cross-attends to encoder
Best mental model: a translator
Typical output: translated, summarized, or transformed text
Part 2: Key Differences Explained
Difference 1: Attention Direction
Encoder (bidirectional): Like reading a completed sentence
"The cat sat on the mat"
Each word can attend to ALL other words (before and after)
"sat" can look at "The" (before) AND "mat" (after)
Decoder (causal/autoregressive): Like writing a sentence
"The cat sat on the ___"
Each word can ONLY attend to previous words
"sat" can look at "The" and "cat" (before) but NOT "mat" (future)
The mask difference:
Encoder (BERT):
Attention matrix (all words can see all words):
The cat sat on the mat
The [ ✓ ✓ ✓ ✓ ✓ ✓ ]
cat [ ✓ ✓ ✓ ✓ ✓ ✓ ]
sat [ ✓ ✓ ✓ ✓ ✓ ✓ ]
on [ ✓ ✓ ✓ ✓ ✓ ✓ ]
the [ ✓ ✓ ✓ ✓ ✓ ✓ ]
mat [ ✓ ✓ ✓ ✓ ✓ ✓ ]
Decoder (GPT): ← What you learned!
Attention matrix (causal masking):
The cat sat on the mat
The [ ✓ ✗ ✗ ✗ ✗ ✗ ]
cat [ ✓ ✓ ✗ ✗ ✗ ✗ ]
sat [ ✓ ✓ ✓ ✗ ✗ ✗ ]
on [ ✓ ✓ ✓ ✓ ✗ ✗ ]
the [ ✓ ✓ ✓ ✓ ✓ ✗ ]
mat [ ✓ ✓ ✓ ✓ ✓ ✓ ]
Difference 2: Use Cases
Encoder-only (BERT): Understanding and classification
- ✓ Sentiment analysis: "Is this review positive or negative?"
- ✓ Question answering: "Based on this paragraph, what is the answer?"
- ✓ Named entity recognition: "Find all person names in this text"
- ✓ Text classification: "Is this email spam?"
- ✗ Cannot generate new text (no causal masking, would see its own output!)
Decoder-only (GPT): Generation
- ✓ Text completion: "Once upon a time..." → generates story
- ✓ Chatbots: "Hello!" → generates response
- ✓ Code generation: "def sort_array" → generates function
- ✓ Creative writing: "Write a poem about..." → generates poem
- ⚠ Can also do understanding tasks, but less efficiently than encoders
Encoder-Decoder (T5): Transformation tasks
- ✓ Translation: English → French
- ✓ Summarization: Long article → short summary
- ✓ Question generation: Paragraph → questions about it
- ✓ Text-to-text anything: Flexibly handles many tasks
Difference 3: Architecture Components
What you learned (Decoder-only):
Input Embedding
↓
+ Positional Encoding
↓
┌─────────────────────┐
│ Decoder Block 1 │
│ - Masked Attention │ ← Causal mask!
│ - FFN │
└─────────────────────┘
↓
┌─────────────────────┐
│ Decoder Block 2 │
│ - Masked Attention │ ← Causal mask!
│ - FFN │
└─────────────────────┘
↓
... (more decoder blocks)
↓
Output Layer (Vocabulary)
Encoder-only (BERT):
Input Embedding
↓
+ Positional Encoding
↓
┌─────────────────────┐
│ Encoder Block 1 │
│ - Attention (full) │ ← No mask! Bidirectional!
│ - FFN │
└─────────────────────┘
↓
┌─────────────────────┐
│ Encoder Block 2 │
│ - Attention (full) │ ← No mask! Bidirectional!
│ - FFN │
└─────────────────────┘
↓
... (more encoder blocks)
↓
Classification Layer (or pooling)
Encoder-Decoder (T5, Original):
INPUT ENCODER SIDE
↓
Input Embedding
↓
+ Positional Encoding
↓
┌─────────────────────┐
│ Encoder Block 1 │
│ - Attention (full) │ ← Bidirectional!
│ - FFN │
└─────────────────────┘
↓
┌─────────────────────┐
│ Encoder Block 2 │
│ - Attention (full) │
│ - FFN │
└─────────────────────┘
↓
[Encoded Representation]
↓ ─────────────────────────┐
↓
DECODER SIDE
↓
Output Embedding
↓
+ Positional Encoding
↓
┌─────────────────────────┐
│ Decoder Block 1 │
│ - Masked Attention │ ← Causal!
│ - Cross-Attention │ ← NEW! Attends to encoder!
│ - FFN │
└─────────────────────────┘
↓
┌─────────────────────────┐
│ Decoder Block 2 │
│ - Masked Attention │
│ - Cross-Attention │
│ - FFN │
└─────────────────────────┘
↓
Output Layer
Part 3: Which One Should You Use?
The Tool Selection Guide
It's like choosing between a hammer, screwdriver, and wrench:
- All are tools
- All have their place
- Using the wrong one makes the job harder!
Choose Decoder-Only (GPT) when:
- ✓ You want to generate text
- ✓ You want open-ended responses
- ✓ You want a chatbot or assistant
- ✓ You want code completion
- ✓ You want creative writing
Examples:
- "Write a story about a dragon"
- "Continue this email: 'Dear sir...'"
- "Generate Python code to sort a list"
Choose Encoder-Only (BERT) when:
- ✓ You want to understand or classify text
- ✓ You have complete sentences to analyze
- ✓ You want bidirectional context (can see everything)
- ✓ You want to extract information
Examples:
- "Is this product review positive or negative?"
- "Find all company names in this document"
- "Which category does this news article belong to?"
Choose Encoder-Decoder (T5) when:
- ✓ You want to transform text from one form to another
- ✓ You have fixed input and want generated output
- ✓ Input and output have different lengths
- ✓ You want conditional generation
Examples:
- "Translate this to French"
- "Summarize this 1000-word article in 100 words"
- "Convert this paragraph to bullet points"
Part 4: What Makes Decoder-Only Special (What You Learned)
Why GPT Architecture Became Dominant
In 2017-2019: Everyone used Encoder-Decoder (like the original paper)
- Google's T5
- Facebook's BART
- Most translation systems
Then GPT-2 and GPT-3 happened (decoder-only):
- Simpler architecture (only one type of block!)
- Scales better (can train HUGE models)
- Surprisingly good at BOTH generation AND understanding
- Easier to train
The revelation: You don't need two separate components! A sufficiently large decoder can:
- Generate text (its obvious strength)
- Understand text (by treating understanding as generation: "Sentiment: positive")
- Translate (prompt it: "Translate to French: I love pizza → ")
- Summarize (prompt it: "Summary: [long text] → TL;DR:")
This is why GPT-3, GPT-4, Claude, and most modern LLMs use decoder-only architecture!
The Simplicity Advantage
Encoder-Decoder: Two separate components to tune
Encoder: 6 layers with bidirectional attention
+ Decoder: 6 layers with masked attention + cross-attention
= Complex! More hyperparameters, harder to optimize
Decoder-Only: One component repeated
Decoder: 12 layers with masked attention
= Simple! Same block repeated, easier to scale to 96+ layers
The trade-off:
- Encoder-Decoder: More specialized, slightly better at specific tasks
- Decoder-Only: More general, easier to scale, one model for everything
Modern trend: Decoder-only is winning because:
- Simpler = easier to train massive models
- One architecture = one codebase
- Prompting is flexible enough for most tasks
- Scales well to very large models (modern frontier LLMs are enormous, though companies often don't disclose the exact parameter counts)
Part 5: The Missing Piece (Cross-Attention in Encoder-Decoder)
"What's cross-attention?"
In encoder-decoder models, the decoder has three types of attention:
1. Self-Attention (Masked): Look at previously generated tokens
- This is what you learned in Chapter 5!
- Causal masking applied
2. Cross-Attention (NEW): Look at the encoder's output
- Decoder's Query
- Encoder's Keys and Values
- No causal mask (can attend to full encoded input)
3. Feed-Forward: Individual processing
- This is what you learned in Chapter 7!
Cross-attention is like: You're writing an essay (decoder) while constantly referring back to your research notes (encoder).
Example: Translation
Input (English): "I love pizza"
Encoder processes "I love pizza" bidirectionally → creates rich representation
Decoder generates French:
Generating "J'":
- Self-attention: Look at what I've generated so far (nothing yet)
- Cross-attention: Look at entire English input "I love pizza" → understands this is about "I"
- Generates: "J'"
Generating "aime":
- Self-attention: Look at "J'"
- Cross-attention: Look at entire English input → understands "love" is the verb
- Generates: "aime"
Generating "la":
- Self-attention: Look at "J' aime"
- Cross-attention: Look at entire English input → understands "pizza" needs an article
- Generates: "la"
The cross-attention lets the decoder constantly "peek" at the source text while generating!
Part 6: What You Learned vs What Exists
Clarifying Your Knowledge
You learned:
- ✅ Embeddings (Chapter 3) - Used by all three architectures
- ✅ Positional encoding (Chapter 4) - Used by all three
- ✅ Self-attention with causal masking (Chapter 5, 12) - Decoder-only specific
- ✅ Multi-head attention (Chapter 5) - Used by all three
- ✅ Feed-forward network (Chapter 7) - Used by all three
- ✅ Residual connections (Chapter 8) - Used by all three
- ✅ Layer normalization (Chapter 8) - Used by all three
- ✅ Stacking blocks (Chapter 9) - Used by all three
- ✅ Output head (Chapter 10) - Decoder-only (vocab projection)
- ✅ Causal masking (Chapter 12) - Decoder-only specific
- ✅ Autoregressive generation (Chapter 13) - Decoder-only specific
You didn't learn:
- ✗ Bidirectional attention (used in encoders)
- ✗ Cross-attention (used in encoder-decoder)
- ✗ Encoder-specific output layers (pooling, classification heads)
But here's the great news: About 80% of what you learned applies to ALL transformers! The core concepts (embeddings, positional encoding, attention mechanism, FFN, residuals) are universal!
To understand encoder-only (BERT):
- Remove causal masking (Chapter 12) → allow full bidirectional attention
- Change output head (Chapter 10) → add classification layer instead of vocabulary
- That's it! Everything else is the same!
To understand encoder-decoder (T5):
- Add an encoder stack (like decoder but bidirectional)
- Add cross-attention in decoder blocks (decoder attends to encoder output)
- Keep causal masking in decoder self-attention
- Rest is the same!
Part 7: Modern Landscape (What's Actually Used)
The Popularity Contest
Decoder-Only (GPT-style) is the dominant pattern in today's frontier LLM landscape:
OpenAI:
- GPT-3, GPT-3.5, and GPT-4-class systems: Decoder-only
- Used by ChatGPT, GitHub Copilot
Meta (Facebook):
- LLaMA, LLaMA 2, LLaMA 3: Decoder-only
Anthropic:
- Claude-family systems: Decoder-only
Google:
- PaLM-family and Gemini-family systems rely heavily on decoder-style language model foundations in many major LLM deployments
Why the shift?
- Simpler to scale: One architecture scales cleanly
- In-context learning: Prompting is incredibly powerful
- General-purpose: One model, many tasks
- Transfer learning: Pre-train once, use everywhere
Encoder-Only (BERT-style) still used for:
- Search engines (understanding queries)
- Document classification
- Specialized understanding tasks
- When you DON'T need generation
Encoder-Decoder less common but still used for:
- Translation services
- Summarization systems
- Text-to-text transformation tools
The trend: Decoder-only is becoming the default because it's simpler and more general-purpose!
Part 8: Summary - What You Actually Know
The Congratulations Moment
You learned the DECODER-ONLY architecture (GPT-style), which is:
✅ The most popular architecture today (GPT-class, Claude-class, and LLaMA-class systems) ✅ The most general-purpose (can do generation + understanding) ✅ The simplest to understand (one component type, repeated) ✅ The most scalable (easiest to train at massive scale) ✅ The foundation for modern AI (ChatGPT, GitHub Copilot, etc.)
This was the RIGHT architecture to learn first!
You could have learned:
- Encoder-only (BERT) - more specialized, less popular now
- Encoder-decoder (T5) - more complex, two components to understand
But you learned decoder-only, which gives you:
- Understanding of the most modern systems
- Knowledge applicable to GPT-4, Claude, LLaMA
- Foundation to understand the others (just minor modifications)
- Career-relevant skills (most jobs use decoder-only models)
How to Think About the Three Types
Simple mental model:
Encoder: A reader who understands but can't write
- Reads everything first
- Understands deeply
- Classifies, extracts, analyzes
- But doesn't create new content
Decoder: A writer who creates step-by-step
- Writes one word at a time
- Can only see what's written so far
- Generates, creates, continues
- This is what you mastered!
Encoder-Decoder: A translator who reads then writes
- Reads source fully (encoder)
- Writes target step-by-step (decoder)
- Best for transforming content
You learned the writer (decoder) - the most versatile and widely used pattern in modern LLMs.
Course navigation: Previous: Chapter 18 - From Language Model to ChatGPT | Next: Chapter 20 - Quick Quizzes