3400: Model Architectures β± 2 min readΒ· π
Last updated: 2026-10-16
Overview
This module covers the major Transformer architecture families: encoder-decoder (T5, BART), decoder-only (GPT, LLaMA), and encoder-only (BERT). You'll learn the trade-offs and when to use each architecture for different tasks.
Module Documents
Learning Objectives
After completing this module, you will:
β
Understand encoder-decoder, decoder-only, and encoder-only architectures
β
Compare T5, BART, GPT, BERT, and LLaMA
β
Choose the right architecture for your task
β
Understand bidirectional vs causal attention
β
Implement different Transformer variants
β
Understand state-space models (Mamba) and multi-head latent attention (MLA)
Prerequisites
See PREREQUISITES.md for detailed requirements.
Key Concepts
The Three Architecture Families
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Transformer Architectures β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Encoder-Decoder Decoder-Only Encoder-Only β
β (T5, BART) (GPT, LLaMA) (BERT, RoBERTa)β
β β β β β
β βΌ βΌ βΌ β
β βββββββββββ βββββββββββ βββββββββββ β
β β Encoder β β Decoder β β Encoder β β
β βBidirect β β Causal β βBidirect β β
β ββββββ¬βββββ ββββββ¬βββββ ββββββ¬βββββ β
β β β β β
β βΌ βΌ β β
β βββββββββββ βββββββββββ β β
β β Decoder β β Decoder β β β
β β Causal β β Causal β β β
β ββββββ¬βββββ ββββββ¬βββββ β β
β β β β β
β βΌ βΌ βΌ β
β Seq2Seq Generation Understandingβ
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Architecture Comparison Table
Aspect
Encoder-Decoder
Decoder-Only
Encoder-Only
Attention Type
Bidirectional + Causal
Causal only
Bidirectional only
Output
Generates sequence
Generates sequence
Classifies/encodes
Use Case
Translation, summarization
Text generation
Classification, search
Examples
T5, BART
GPT, LLaMA, Mistral
BERT, RoBERTa
Training
Seq2Seq
Next token prediction
Masked token prediction
Inference Speed
Slower (2x passes)
Fast
Fast (single pass)
Encoder-Decoder Architecture
Structure
Input: "Translate English to German: The house is wonderful"
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β ENCODER (Bidirectional Attention) β
β β
β [The] [house] [is] [wonderful] β
β β β β β β
β Each token attends to ALL tokens β
β β
β Output: Context vector for each token β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Cross-Attention
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β DECODER (Causal Attention + Cross-Attention) β
β β
β [<SOS>] β [Das] β [Haus] β [ist] β [wunderbar] β
β β β β β β β
β Causal: Attend to previous decoder tokens β
β Cross: Attend to ALL encoder outputs β
β β
β Output: "Das Haus ist wunderbar" β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Key Models
Model
Training
Strengths
Weaknesses
T5
Text-to-text
Flexible, can do any task
Slow (2x computation)
BART
Denoising autoencoder
Good for summarization
Slower than decoder-only
mT5
Multilingual T5
101 languages
Larger model size
Decoder-Only Architecture
Structure
Input: "Once upon a time"
[Once] [upon] [a] [time] [<EOS>]
β β β β β
Each token attends to PREVIOUS tokens only
(causal / autoregressive)
[Once] β attends to []
[upon] β attends to [Once]
[a] β attends to [Once, upon]
[time] β attends to [Once, upon, a]
[<EOS>] β attends to [Once, upon, a, time]
Output: "Once upon a time, there was a..."
Key Models
Model
Params
Context
Strengths
Use Case
GPT-3
175B
2k
Zero-shot learning
General tasks
Llama 2
7B-70B
4k
Open weights, efficient
Research, production
Mistral
7B
8k
Fast, efficient
Edge deployment
Claude
?
100k+
Large context
Long documents
Encoder-Only Architecture
Structure
Input: "The [MASK] is on the table"
[The] [MASK] [is] [on] [the] [table]
β β β β β β
All tokens attend to ALL tokens
(bidirectional masking)
Output: "cat" (predict masked token)
Key Models
Model
Training
Strengths
Use Case
BERT
Masked LM
Deep understanding
Classification, search
RoBERTa
Improved BERT training
Better performance
NLU tasks
ALBERT
Parameter sharing
Efficient
Resource-constrained
When to Use Each Architecture
Decision Tree
Task: ?
β
ββ Text Generation β Decoder-Only (GPT, LLaMA)
β
ββ Sequence-to-Sequence β Encoder-Decoder (T5, BART)
β β
β ββ Translation β T5, mT5
β ββ Summarization β BART, T5
β ββ Question Answering β T5
β
ββ Understanding/Classification β Encoder-Only (BERT)
β
ββ Sentiment Analysis β BERT
ββ Named Entity Recognition β BERT
ββ Semantic Search β BERT, RoBERTa
Implementation Example
Decoder-Only Block (LLaMA-style)
import torch.nn as nn
class DecoderBlock ( nn . Module ):
"""
LLaMA-style decoder block with:
- RMSNorm pre-normalization
- SwiGLU activation
- Rotary positional embeddings
- Grouped-query attention (optional)
"""
def __init__ ( self , d_model = 4096 , n_heads = 32 , d_ff = 11008 ):
super () . __init__ ()
# Pre-normalization
self . norm1 = RMSNorm ( d_model )
self . norm2 = RMSNorm ( d_model )
# Causal self-attention
self . attn = CausalSelfAttention (
d_model = d_model ,
n_heads = n_heads ,
)
# Feed-forward with SwiGLU
self . ffn = SwiGLU ( d_model , d_ff )
def forward ( self , x , freqs_cis ):
"""
Args:
x: (batch, seq_len, d_model)
freqs_cis: Precomputed RoPE frequencies
Returns:
output: (batch, seq_len, d_model)
"""
# Pre-norm architecture
# Residual connection around attention
x = x + self . attn ( self . norm1 ( x ), freqs_cis )
# Residual connection around FFN
x = x + self . ffn ( self . norm2 ( x ))
return x
Encoder-Decoder Block (T5-style)
import torch.nn as nn
class EncoderDecoderBlock ( nn . Module ):
"""
T5-style encoder-decoder block.
"""
def __init__ ( self , d_model = 512 , n_heads = 8 , d_ff = 2048 ):
super () . __init__ ()
# Encoder
self . encoder_norm1 = LayerNorm ( d_model )
self . encoder_norm2 = LayerNorm ( d_model )
self . encoder_attn = SelfAttention ( d_model , n_heads )
self . encoder_ffn = FeedForward ( d_model , d_ff )
# Decoder
self . decoder_norm1 = LayerNorm ( d_model )
self . decoder_norm2 = LayerNorm ( d_model )
self . decoder_norm3 = LayerNorm ( d_model )
self . decoder_attn = CausalSelfAttention ( d_model , n_heads )
self . cross_attn = CrossAttention ( d_model , n_heads )
self . decoder_ffn = FeedForward ( d_model , d_ff )
def forward ( self , encoder_input , decoder_input ):
# Encoder
encoder_output = encoder_input
encoder_output = encoder_output + self . encoder_attn (
self . encoder_norm1 ( encoder_output )
)
encoder_output = encoder_output + self . encoder_ffn (
self . encoder_norm2 ( encoder_output )
)
# Decoder
decoder_output = decoder_input
decoder_output = decoder_output + self . decoder_attn (
self . decoder_norm1 ( decoder_output )
)
decoder_output = decoder_output + self . cross_attn (
self . decoder_norm2 ( decoder_output ),
encoder_output
)
decoder_output = decoder_output + self . decoder_ffn (
self . decoder_norm3 ( decoder_output )
)
return encoder_output , decoder_output
Assessment
See Also
Common Issues
Issue
Solution
Encoder-decoder too slow
Use decoder-only for generation
Poor translation quality
Use larger encoder-decoder model
Catastrophic forgetting
Use encoder-only for fine-tuning
Quick Reference
Model Selection by Task
Task
Best Architecture
Specific Model
Chatbots
Decoder-only
LLaMA, Mistral
Translation
Encoder-decoder
T5, mT5
Summarization
Both
BART (enc-dec), GPT (dec-only)
Classification
Encoder-only
BERT, RoBERTa
Code generation
Decoder-only
CodeLlama, StarCoder
RAG
Decoder-only
LLaMA with retrieval
Module Difficulty: βββ Advanced
Estimated Time: 12 hours total