Types of Transformer Architectures
Encoder-only, decoder-only, and encoder-decoder: mathematical formulation of each variant, their training objectives, causal vs bidirectional attention, and when to use which architecture.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- Encoder-Only (BERT-style)
- Decoder-Only (GPT-style)
- Encoder-Decoder (T5-style)
- Prefix LM (UniLM-style)
- Mixture of Experts (MoE)
- State Space Models (Mamba)
- Architecture Comparison
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Formalize the attention mask patterns for each architecture type.
- Derive the training objectives (MLM, CLM, span corruption) for each.
- Explain why decoder-only models dominate scaling.
- Describe MoE as a sparse conditional computation strategy.
- Explain the recurrence in state-space models and their linear complexity.
Notation
- — causal mask (lower triangular)
- — bidirectional mask (all zeros, no masking)
- — causal language model
- — masked language model
Core Intuition
The three main transformer variants differ in what each position can attend to:
- Encoder-only (bidirectional): every position sees everything — best for understanding/classification.
- Decoder-only (causal): each position sees only the past — best for generation.
- Encoder-decoder: encoder is bidirectional; decoder is causal but cross-attends to encoder — best for sequence-to-sequence tasks.
Transformer Architectures
Encoder-Only (BERT-style)
Architecture: transformer layers with bidirectional self-attention (no masking).
Attention pattern: Full attention matrix — position attends to all positions :
Training objective: Masked Language Modeling (MLM). Randomly mask 15% of tokens; predict the masked tokens from context:
Strengths: Rich bidirectional representations; excellent for classification, NER, sentence similarity.
Limitations: Cannot generate text autoregressively (no natural left-to-right ordering).
Models: BERT, RoBERTa, DeBERTa, ELECTRA.
Decoder-Only (GPT-style)
Architecture: transformer layers with causal self-attention.
Attention pattern: Lower-triangular mask — position attends only to :
Training objective: Causal Language Modeling (CLM). Predict the next token:
Strengths: Natural for generation; simple training (next-token prediction); emergent in-context learning at scale; most scalable architecture.
Why it dominates: One unified objective for all tasks (framed as text completion). KV-cache enables efficient autoregressive generation. Scales predictably with compute.
Models: GPT-2/3/4, LLaMA, Mistral, Claude, Gemini.
Encoder-Decoder (T5-style)
Architecture:
- Encoder: layers with bidirectional attention.
- Decoder: layers with causal self-attention + cross-attention to encoder.
Training objective: Span Corruption. Replace random spans with sentinel tokens; predict the original spans:
Strengths: Natural for sequence-to-sequence tasks (translation, summarization); encoder provides rich context for generation.
Limitations: More complex; harder to scale; encoder and decoder must both fit in memory.
Models: T5, BART, mBART, Flan-T5, UL2.
Prefix LM (UniLM-style)
Hybrid: The first tokens are bidirectional (prefix), remaining tokens are causal:
Advantage: Single model handles both understanding (prefix) and generation (causal suffix).
Models: UniLM, PaLM (partially), U-PaLM.
Mixture of Experts (MoE)
Replace the dense FFN with a sparse set of expert FFNs. A router selects top- experts per token:
where are the router weights and are the selected expert indices.
Properties:
- Total parameters: larger than dense model.
- Active parameters per token: same as dense (only experts fire).
- Training compute scales sublinearly with parameter count.
Router: → select top- entries.
Load balancing loss: Encourages uniform expert utilization to prevent "expert collapse":
where = fraction of tokens routed to expert , = average router probability for expert .
Models: Mixtral (8x7B), Switch Transformer, GShard, DeepSeek-MoE.
State Space Models (Mamba)
Replace attention entirely with a recurrent state-space computation:
where are learned (and input-dependent in Mamba's selective SSM).
Complexity: for sequential processing; via parallel scan during training.
No KV-cache needed: The fixed-size state replaces the growing KV-cache.
Tradeoff: Competitive with transformers for language modeling; less proven for tasks requiring precise retrieval of past tokens (in-context learning).
Models: Mamba, Mamba-2, Jamba (hybrid Mamba + attention).
Architecture Comparison
- Encoder-only: Bidirectional, MLM training, best for understanding tasks, cannot generate
- Decoder-only: Causal, CLM training, best for generation, scales best, dominant for LLMs
- Encoder-decoder: Bidirectional encoder + causal decoder, span corruption, best for seq2seq
- MoE: Sparse computation, scales parameters cheaply, same active compute
- SSM: Linear complexity, no attention matrix, fixed-size state, emerging alternative
Common Pitfalls
Pitfall 1. Using encoder-only models for generation. BERT cannot generate text naturally; you'd need iterative masking/infilling which is slow and awkward.
Pitfall 2. Assuming MoE = free parameters. While active compute is constant, memory for all expert weights must fit on GPUs. Communication costs for distributed experts are significant.
Pitfall 3. Declaring SSMs will replace transformers. Current evidence shows hybrid architectures (Jamba) work best; pure SSMs struggle with precise recall tasks.
Summary
- Decoder-only (causal) dominates LLMs due to simplicity, scalability, and emergent abilities.
- Encoder-only excels at understanding tasks with bidirectional context.
- Encoder-decoder is natural for sequence-to-sequence but harder to scale.
- MoE provides more parameters at constant active compute — key scaling strategy.
- SSMs offer linear complexity but trade off precise retrieval ability.
Exercises
Exercise 1. Write the attention mask matrix for a prefix LM with prefix length and total length .
Exercise 2. For a MoE with experts and top- routing, compute the ratio of total to active parameters.
Exercise 3. Derive the parallel scan algorithm for computing the SSM recurrence (equation 8) in .
Exercise 4. Prove that a decoder-only model with a bidirectional prefix is strictly more expressive than a pure causal model (for the prefix portion).
Exercise 5. Compute the FLOPs per token for a dense model with vs a MoE with per expert.