Volume 3: LLM Internals & Architecture¶
"Understanding the Transformer" - Deep dive into how LLMs work internally.
Volume Overview¶
Difficulty: ⭐⭐⭐ Advanced
Time: 3-4 weeks (part-time)
Prerequisites: Volume 2 (or strong understanding of tensors and backpropagation)
What You'll Learn¶
After completing this volume, you will be able to:
- ✅ Explain the transformer architecture in detail
- ✅ Implement self-attention from scratch
- ✅ Understand Flash Attention optimization
- ✅ Work with positional embeddings (RoPE)
- ✅ Compare different tokenization strategies
- ✅ Analyze activation functions and normalization
- ✅ Compare decoder-only vs encoder-decoder models
Why This Volume Matters¶
Before fine-tuning, optimizing, or deploying LLMs, you need to understand how they work internally. This volume gives you:
- Architectural intuition - Understand every component of a transformer
- Implementation knowledge - Build attention mechanisms from scratch
- Optimization awareness - Learn Flash Attention and efficiency techniques
- Model comparison - Choose the right architecture for your use case
Learning Path¶
Week 1: Attention Mechanisms¶
Day 1-3: Self-Attention Deep Dive¶
The core innovation of transformers
- 3101: Self-Attention (3-4 hours)
- Scaled Dot-Product Attention
- Multi-Head Attention
- Masked Attention (causal)
- Attention patterns analysis
- KV cache optimization
Key Concepts:
# Self-attention formula:
Attention(Q, K, V) = softmax(QK^T / √d_k) V
# Multi-head:
Head_i = Attention(QW_Q^i, KW_K^i, VW_V^i)
MultiHead = Concat(Head_1, ..., Head_h)W^O
# Causal masking:
mask = torch.triu(torch.ones(seq_len, seq_len), diagonal=1).bool()
Practice:
import torch.nn.functional as F
import torch
# Implement from scratch:
import math
def scaled_dot_product_attention(Q, K, V, mask=None):
# 1. Compute scores
scores = torch.matmul(Q, K.transpose(-2, -1)) / math.sqrt(Q.size(-1))
# 2. Apply mask if provided
if mask is not None:
scores = scores.masked_fill(mask == 0, -1e9)
# 3. Softmax
attention_weights = F.softmax(scores, dim=-1)
# 4. Weight values
output = torch.matmul(attention_weights, V)
return output, attention_weights
- Experiment: Self-Attention (2 hours)
- Implement attention from scratch
- Visualize attention patterns
- Benchmark multi-head configurations
Checkpoint: You understand and can implement self-attention
Day 4-5: Flash Attention¶
Memory-efficient attention
- 3102: Flash Attention (3-4 hours)
- IO-aware algorithm
- Tiling strategy
- Memory optimization
- Implementation details
- Speed benchmarks
Key Innovation:
Standard Attention: O(N²) memory
Flash Attention: O(N) memory with tiling
Trade-off: Approximate attention for 2-4x speedup
- Experiment: Flash Attention (2-3 hours)
- Compare standard vs flash attention
- Profile memory usage
- Benchmark inference speed
- Test on long sequences
Checkpoint: You understand Flash Attention optimization
Week 2: Embeddings & Tokenization¶
Day 1-3: Positional Embeddings¶
How models understand token position
- 3201: RoPE (2-3 hours)
- Absolute vs Relative positions
- Rotary Position Embeddings
- Sinusoidal embeddings
- Learned positional embeddings
- ALiBi (Attention with Linear Biases)
RoPE Formula:
import torch
def rotate_position(x, seq_len, dim):
# Create rotation matrix
theta = torch.arange(dim // 2) / (dim // 2)
theta = 1.0 / (10000 ** theta)
# Create positions
pos = torch.arange(seq_len)
# Compute rotation
freqs = torch.outer(pos, theta)
emb = torch.polar(torch.ones_like(freqs), freqs)
# Apply rotation
x_rotated = apply_rotary_emb(x, emb)
return x_rotated
- Experiment: RoPE (2 hours)
- Implement RoPE from scratch
- Compare absolute vs relative
- Visualize rotation matrices
- Test position encoding
Checkpoint: You understand positional embeddings
Day 4-5: Tokenization¶
How text becomes tokens
- 3202: Tokenizer Sciences (2-3 hours)
- BPE (Byte Pair Encoding)
- SentencePiece (Unigram)
- TikToken (OpenAI)
- Tokenizer comparison
- Vocabulary size impact
- Special tokens handling
Tokenization Examples:
Input: "Hello, world!"
BPE: ["Hello", ",", " world", "!"]
SentencePiece: ["▁Hello", ",", "▁world", "!"]
TikToken: [15496, 11, 1917, 0]
Tokenization affects:
- Model vocabulary size
- Sequence length
- Out-of-vocabulary handling
- Multilingual support
- Experiment: Tokenizers (2 hours)
- Compare BPE vs SentencePiece
- Analyze token distributions
- Test on different languages
- Measure compression ratio
Checkpoint: You understand tokenization strategies
Week 3: Decoding & Architecture¶
Day 1-2: Activation Functions¶
Why GELU and SwiGLU over ReLU?
- 3301: Activation Functions (2 hours)
- ReLU and its problems
- GELU (Gaussian Error Linear Unit)
- SwiGLU (Swish-Gated Linear Unit)
- GeLU vs SwiGLU comparison
- Performance impact
Comparison:
import torch.nn as nn
import torch
# ReLU: Simple but dead neurons
relu = torch.nn.ReLU()
# GELU: Smoother, better gradients
gelu = torch.nn.GELU()
# SwiGLU: State-of-the-art for LLMs
# Used in LLaMA, Mistral, etc.
class SwiGLU(nn.Module):
def forward(self, x):
return x * torch.sigmoid(x) * (x @ gate)
Practice:
- Compare activation performance
- Analyze gradient flow
- Benchmark training speed
Checkpoint: You understand modern activation functions
Day 3: Normalization Layers¶
Stabilizing deep networks
- 3302: Normalization Layers (1-2 hours)
- BatchNorm vs LayerNorm
- RMSNorm (Root Mean Square)
- Pre-Norm vs Post-Norm
- Stability in deep networks
Key Differences:
# BatchNorm: Normalize across batch
BatchNorm: mean/var over batch dimension
# LayerNorm: Normalize across features
LayerNorm: mean/var over feature dimension
# RMSNorm: Simplified LayerNorm (no mean centering)
RMSNorm: x / sqrt(mean(x^2) + epsilon)
Checkpoint: You understand normalization choices
Day 4-5: Model Architectures¶
Decoder-only vs Encoder-Decoder
-
3402: Decoder-Only Models (GPT, LLaMA, Mistral) (4 hours)
- GPT family (GPT-2, GPT-3, GPT-4)
- LLaMA and derivatives
- Mistral and Mixtral
- Mixture of Experts (MoE)
-
3401: Encoder-Decoder Architectures (4 hours)
- T5 family
- BERT and variants
- Sequence-to-sequence models
- When to use each
Comparison:
Decoder-only (GPT, LLaMA, Mistral):
- Best for generation
- Causal masking
- Autoregressive
Encoder-decoder (T5, BART):
- Best for translation/summarization
- Cross-attention
- Bidirectional encoder
Encoder-only (BERT):
- Best for understanding/classification
- No generation
- Masked language modeling
- 3403: Model Architecture Comparison (2-3 hours)
- Sparse activation
- Load balancing
- Routing strategies
- Mixtral architecture
Checkpoint: You can choose the right architecture
Volume 3 Capstone Projects¶
Project A: Implement Transformer from Scratch¶
Time: 8-10 hours
Difficulty: ⭐⭐⭐⭐
Tasks:
- Implement multi-head self-attention
- Build transformer block (attention + FFN)
- Stack blocks to create full model
- Implement causal masking
- Test on text generation
Skills Demonstrated:
- Understanding of transformer architecture ✅
- PyTorch implementation ✅
- Model debugging ✅
Project B: Analyze Attention Patterns¶
Time: 4-6 hours
Difficulty: ⭐⭐⭐
Tasks:
- Extract attention weights from trained model
- Visualize attention patterns
- Analyze multi-head diversity
- Compare across layers
- Document findings
Skills Demonstrated:
- Attention analysis ✅
- Visualization ✅
- Pattern recognition ✅
Project C: Implement Custom Tokenizer¶
Time: 6-8 hours
Difficulty: ⭐⭐⭐
Tasks:
- Implement BPE tokenizer from scratch
- Train on custom corpus
- Compare with standard tokenizers
- Analyze vocabulary efficiency
- Optimize for domain
Skills Demonstrated:
- Tokenizer implementation ✅
- Algorithm understanding ✅
- Domain adaptation ✅
Volume 3 Checklist¶
Use this checklist to track your progress:
Core Content (Required)¶
- 3101: Self-Attention (3-4 hours)
- EXP_3101: Self-Attention (2 hours)
- 3102: Flash Attention (3-4 hours)
- EXP_3102: Flash Attention (2-3 hours)
- 3201: RoPE (2-3 hours)
- EXP_3201: RoPE (2 hours)
- 3202: Tokenizer Sciences (2-3 hours)
- EXP_3202: Tokenizer (2 hours)
- 3301: Activation Functions (2 hours)
- 3302: Normalization Layers (1-2 hours)
- 3401: Encoder-Decoder Architectures (4 hours)
- 3402: Decoder-Only Models (4 hours)
Total Core Time: ~30-35 hours
Capstone Projects (Choose 1)¶
- Project A: Transformer from Scratch (8-10 hours)
- Project B: Analyze Attention Patterns (4-6 hours)
- Project C: Custom Tokenizer (6-8 hours)
Cross-References¶
How Volume 3 Connects to Other Volumes:¶
Self-Attention (3101) →
- Volume 4: Flash Attention for long contexts
- Volume 6: Cross-attention in RAG
- Volume 7: Efficient inference
Flash Attention (3102) →
- Volume 4: Context window optimization
- Volume 6: Long-context RAG
- Volume 7: Production deployment
RoPE (3201) →
- Volume 4: Position encoding in quantization
- Volume 5: Fine-tuning with RoPE scaling
- Volume 6: Position-aware retrieval
Tokenizers (3202) →
- Volume 5: Domain-specific tokenization
- Volume 6: Token-efficient RAG
- Volume 7: Token optimization
Architecture (3401-3403) →
- Volume 4: Model-specific optimization
- Volume 5: Architecture-aware fine-tuning
- Volume 7: Production model selection
Volume 3 Statistics¶
| Metric | Value |
|---|---|
| Core Documents | 12 files |
| Experiments | 4 experiments |
| Capstone Projects | 3 projects |
| Estimated Time | 30-35 hours (core) + 4-10 hours (project) |
| Difficulty | ⭐⭐⭐ Advanced |
Key Takeaways¶
Attention Mechanism¶
Core Formula:
Attention(Q, K, V) = softmax(QK^T / √d_k) V
Where:
- Q (Query): What I'm looking for
- K (Key): What I can match against
- V (Value): What I get back
Multi-Head Attention:
- Parallel attention mechanisms
- Each head learns different patterns
- Concatenated and projected
Causal Masking:
- Prevents looking at future tokens
- Essential for autoregressive generation
- Upper triangular mask
Flash Attention¶
Key Innovation:
- Tiled attention computation
- Reduces memory from O(N²) to O(N)
- IO-aware algorithm design
- 2-4x speedup on long sequences
Positional Embeddings¶
Absolute vs Relative:
- Absolute: Learn position indices
- Relative: Learn position differences
- RoPE: Rotary position encoding (state-of-the-art)
RoPE Benefits:
- Relative position encoding
- Extrapolates to longer sequences
- No learned parameters
Tokenization¶
Trade-offs:
- Larger vocab → Fewer tokens, larger embedding matrix
- Smaller vocab → More tokens, smaller model
Popular Tokenizers:
- BPE: GPT-2, GPT-3, RoBERTa
- Unigram: T5, mT5
- TikToken: GPT-3.5, GPT-4
Troubleshooting¶
Common Issues in Volume 3¶
Problem: Attention pattern visualization confusing
- Solution: Start with single head, simple input
Problem: Flash Attention not available
- Solution: Check CUDA version, try xformers alternative
Problem: RoPE implementation complex
- Solution: Use Hugging Face implementation first, then customize
Problem: Tokenizer produces different results
- Solution: Check version, special tokens, preprocessing
For more help, see TROUBLESHOOTING-Common-Issues.md
After Volume 3¶
You're Ready For:¶
Volume 4: Quantization - Optimize models for your hardware
Volume 5: Fine-Tuning - Adapt models to your domain
Volume 6: RAG - Add external knowledge to models
Skills You've Gained:¶
# You can now:
✅ Explain transformer architecture
✅ Implement attention from scratch
✅ Understand Flash Attention
✅ Work with positional embeddings
✅ Analyze tokenization strategies
✅ Compare model architectures
✅ Choose the right model for your use case
Next Steps¶
- Track your progress in PROGRESS-TRACKER.md
- Continue to Volume 4 to learn quantization
- OR skip to Volume 6 for RAG systems
- Review the VOLUME-GUIDE.md for alternative learning paths
Recommended Resources:
- Attention Is All You Need - Original transformer paper
- Illustrated Transformer - Visual guide
- Flash Attention Paper - Flash Attention details
Volume 3 Status: 🟢 Complete
Maintainer: Minder Academy Team