Skip to content

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

Document Description Difficulty Time
3401: Encoder-Decoder Architectures T5, BART, and sequence-to-sequence models ⭐⭐ 4 hrs
3402: Decoder-Only Models (GPT, LLaMA, Mistral) GPT, LLaMA, Mistral architectures ⭐⭐ 4 hrs
3404: Beyond Attention β€” SSMs and MLA State-space models, Mamba, DeepSeek MLA ⭐⭐⭐ 4 hrs
3403: Model Architecture Comparison Comparative guide with benchmarks ⭐⭐⭐ 3 hrs

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

Experiment Description
EXP_3401: Encoder-Decoder Build T5 from scratch

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