Skip to content

Phase 3: Transformer Physics & LLM Internals [3000]

⏱ 7 min read·📅 Last updated: 2026-10-24

Table of Contents


Overview

Dismantling the Generative Pre-trained Transformer architecture.

This phase covers the internal mechanics of transformer models, from attention mechanisms to decoding strategies, enabling you to:

  • Understand the self-attention mechanism that powers modern LLMs
  • Implement attention from scratch for deeper understanding
  • Learn Flash Attention optimization for memory efficiency
  • Master positional embeddings (RoPE) for sequence understanding
  • Compare tokenization strategies (BPE, SentencePiece, TikToken)
  • Analyze activation functions and normalization layers
  • Choose between decoder-only and encoder-decoder architectures

Why Transformer Internals Matter

The Knowledge Gap

Without Understanding Internals:
┌─────────────────────────────────────────────────────────┐
│ - Black-box approach to LLMs                            │
│ - Cannot debug model behavior                           │
│ - Limited optimization capabilities                     │
│ - Cannot choose right architecture for task             │
│ - Struggle with long context windows                    │
│ - Cannot implement custom modifications                 │
└─────────────────────────────────────────────────────────┘

With Deep Understanding:
┌─────────────────────────────────────────────────────────┐
│ + Understand every component of transformer             │
│ + Implement attention mechanisms from scratch           │
│ + Optimize for memory and speed                         │
│ + Choose right model for your use case                  │
│ + Extend context windows efficiently                    │
│ + Build custom model architectures                      │
└─────────────────────────────────────────────────────────┘

Performance vs Understanding

graph LR
    A[Surface Level] -->|Basic Usage| B[API Calls]
    B -->|Limited Control| C[Pre-built Models]

    D[Deep Understanding] -->|Implementation| E[Custom Attention]
    E -->|Full Control| F[Optimized Architectures]

    A -->|Upgrade Path| D

    style A fill:#f88
    style D fill:#8f8
    style F fill:#8f8

Decoder-Only Transformer Architecture

graph TB
    subgraph "Input Processing"
        A[Input Text] --> B[Tokenizer]
        B --> C[Token IDs]
        C --> D[Embedding Layer]
    end

    subgraph "Transformer Block × N"
        D --> E[Layer Norm 1]
        E --> F[Multi-Head Self-Attention]
        F --> G[Residual +]
        G --> H[Layer Norm 2]
        H --> I[Feed Forward Network]
        I --> J[Residual +]
    end

    subgraph "Output Processing"
        J --> K[Final Layer Norm]
        K --> L[Output Projection]
        L --> M[Softmax]
        M --> N[Next Token Probabilities]
    end

    N -->|Autoregressive| A

    style A fill:#e1f5fe
    style N fill:#c8e6c9
    style F fill:#fff9c4
    style I fill:#f8bbd0

Self-Attention Mechanism

graph LR
    subgraph "Input"
        A1[Query] --> Q[Linear Q]
        A2[Key] --> K[Linear K]
        A3[Value] --> V[Linear V]
    end

    subgraph "Attention Computation"
        Q --> ATN[Attention Scores<br/>Q × K^T / √d]
        K --> ATN
        ATN --> SM[Softmax]
        SM --> WV[Weighted Values<br/>Softmax × V]
        V --> WV
    end

    subgraph "Output"
        WV --> O[Linear Output]
        O --> OUT[Attention Output]
    end

    style Q fill:#bbdefb
    style K fill:#bbdefb
    style V fill:#bbdefb
    style ATN fill:#fff9c4
    style OUT fill:#c8e6c9

Tokenization Pipeline

graph TD
    A[Raw Text] --> B[Preprocessing<br/>Lowercase, Normalize]
    B --> C[Subword Splitting<br/>BPE/SentencePiece]
    C --> D[Vocabulary Lookup]
    D --> E[Token IDs]
    E --> F[Add Special Tokens<br/>BOS, EOS, PAD]
    F --> G[Attention Mask]
    G --> H[Position Encoding<br/>RoPE/Absolute]
    H --> I[Input Embeddings]

    style A fill:#e1f5fe
    style I fill:#c8e6c9
    style C fill:#fff9c4
    style H fill:#f8bbd0

Module Structure

[3100] Attention Architectures

Document Description Time Difficulty
3101: Self-Attention Multi-head, Masked, Scaled Dot-Product 4h Advanced
3102: Flash Attention Memory-efficient attention (IO-aware) 4h Advanced
3103: SDPA and torch.compile Fused kernels and compiled execution 4h Advanced

What You'll Learn:

  • Scaled dot-product attention mechanism
  • Multi-head attention and its benefits
  • Causal masking for autoregressive generation
  • KV cache optimization for faster inference
  • Flash Attention tiling strategy
  • IO-aware algorithm design
  • Memory optimization techniques
  • SDPA backend dispatch and torch.compile guard behavior

Hands-On Practice:

  • Implement self-attention from scratch
  • Visualize attention patterns
  • Compare standard vs Flash Attention
  • Profile memory usage
  • Benchmark inference speed
  • Test on long sequences

[3200] Embedding Latent Spaces

Document Description Time Difficulty
3201: RoPE Absolute vs Relative positions 3h Intermediate
3202: Tokenizer Sciences BPE, SentencePiece, Tiktoken 3h Intermediate
3203: Word-Embedding Tradition Skip-gram, negative sampling, CBOW, GloVe 3h Intermediate

What You'll Learn:

  • Rotary Position Embeddings (RoPE)
  • Absolute vs relative position encoding
  • Sinusoidal embeddings
  • Learned positional embeddings
  • ALiBi (Attention with Linear Biases)
  • Byte Pair Encoding (BPE)
  • SentencePiece (Unigram LM)
  • TikToken algorithm
  • Tokenizer comparison and trade-offs
  • The word-embedding tradition: skip-gram, negative sampling, CBOW, and GloVe's co-occurrence ratio

Hands-On Practice:

  • Implement RoPE from scratch
  • Compare absolute vs relative encoding
  • Visualize rotation matrices
  • Compare BPE vs SentencePiece
  • Analyze token distributions
  • Test on different languages
  • Measure compression ratio
  • Train skip-gram from scratch on a toy corpus

[3300] The Decoding Block

Document Description Time Difficulty
3301: Activation Functions GELU, SwiGLU vs RELU 2h Intermediate
3302: Normalization Layers BatchNorm vs LayerNorm vs RMSNorm 2h Intermediate
3303: Activation Comparison Comparative analysis 2h Advanced
3304: Decoding Sampling Strategies Temperature, top-k, nucleus, min-p, typical-p 2h Intermediate

What You'll Learn:

  • ReLU and its limitations
  • GELU (Gaussian Error Linear Unit)
  • SwiGLU (Swish-Gated Linear Unit)
  • GeLU vs SwiGLU comparison
  • BatchNorm vs LayerNorm vs RMSNorm
  • Pre-Norm vs Post-Norm placement
  • Stability in deep networks
  • Temperature, top-k, nucleus, min-p, and typical-p sampling

Hands-On Practice:

  • Compare activation performance
  • Analyze gradient flow
  • Benchmark training speed
  • Implement custom activations
  • Test normalization strategies
  • Walk the sampler stack and compare truncation strategies

[3400] Model Architectures

Document Description Time Difficulty
3401: Encoder-Decoder T5, BART architectures 4h Intermediate
3402: Decoder-Only GPT, LLaMA architectures 4h Intermediate
3404: Beyond Attention — SSMs and MLA SSMs, Mamba, and DeepSeek MLA 4h Advanced
3403: Architecture Comparison Comparative guide 3h Advanced

What You'll Learn:

  • Decoder-only (GPT, LLaMA, Mistral)
  • Encoder-decoder (T5, BART)
  • Encoder-only (BERT)
  • Mixture of Experts (MoE)
  • Cross-attention mechanisms
  • Bidirectional vs unirectional attention
  • When to use each architecture
  • State-space models (Mamba) and multi-head latent attention (MLA)

Hands-On Practice:

  • Load and analyze different architectures
  • Compare parameter counts
  • Benchmark inference speed
  • Analyze attention patterns
  • Choose architecture for specific tasks

[3500] Multimodal Models

Document Description Time Difficulty
3501: Vision-Language Models CLIP, BLIP, LLaVA, multimodal RAG 5h Advanced
3502: Audio Models Whisper, AudioLM, voice assistants 5h Advanced

What You'll Learn:

  • Vision-language architectures (CLIP, BLIP, LLaVA)
  • Contrastive image-text learning
  • Multimodal RAG systems
  • Audio transformers (Whisper, AudioLM)
  • Applying multimodal models to real-world tasks

Hands-On Practice:

  • Implement CLIP-style contrastive encoding
  • Build multimodal RAG pipelines
  • Transcribe and analyze audio with Whisper
  • Apply VLMs to real-world tasks

Learning Path

graph TD
    A[Start: Phase 2 Complete] --> B{Your Goal?}

    B -->|Implement Attention| C[3101: Self-Attention]
    B -->|Optimize Memory| D[3102: Flash Attention]
    B -->|Understand Positions| E[3201: RoPE]
    B -->|Build Tokenizer| F[3202: Tokenizer Sciences]
    B -->|Compare Activations| G[3301: Activation Functions]
    B -->|Choose Architecture| H[3401: Model Architectures]
    B -->|Understand Multimodal| M[3501: Vision-Language Models]
    B -->|Build Audio Apps| N[3502: Audio Models]

    C --> I[EXP_3101: Attention Experiment]
    D --> J[EXP_3102: Flash Attention Experiment]

    E --> K[EXP_3201: RoPE Experiment]
    F --> L[EXP_3202: Tokenizer Experiment]
    M --> O[EXP_3501: Multimodal RAG Experiment]

    style C fill:#e1f5fe
    style D fill:#fff3e0
    style E fill:#f3e5f5
    style H fill:#fce4ec
    style M fill:#e8f5e9
    style N fill:#fffde7

Time Estimates

Module Reading Practice Total
3100: Attention Architectures 12h 4-6h 16-18h
3200: Embedding Latent Spaces 9h 4h 13h
3300: The Decoding Block 8h 3h 11h
3400: Model Architectures 15h 4h 19h
3500: Multimodal Models 10h — 10h
Total 54h 15-17h 69-71h

3500 practice time is not yet estimated; phase totals cover the estimated modules.


Cheat Sheets

Single-page references for the surfaces this phase teaches:

Key Takeaways

You Will Learn

After completing this phase, you will be able to:

  1. Implement Self-Attention from Scratch

    • Understand scaled dot-product attention
    • Build multi-head attention mechanisms
    • Apply causal masking for generation
    • Optimize with KV cache
  2. Optimize Memory Usage

    • Implement Flash Attention
    • Use tiling strategies
    • Apply IO-aware algorithms
    • Reduce memory from O(N²) to O(N)
  3. Master Positional Embeddings

    • Implement RoPE (state-of-the-art)
    • Compare absolute vs relative encoding
    • Apply ALiBi for extrapolation
    • Extend context windows
  4. Choose Right Tokenization

    • Compare BPE, SentencePiece, TikToken
    • Analyze vocabulary efficiency
    • Optimize for specific domains
    • Handle multilingual text
  5. Select Architecture for Task

    • Decoder-only for generation
    • Encoder-decoder for translation
    • Encoder-only for understanding
    • Mixture of Experts for scale

Common Pitfalls

Pitfall 1: Attention Implementation

Pitfall: Incorrect attention mask leading to information leakage

import torch
# Wrong: No causal mask
def attention(Q, K, V):
    scores = Q @ K.T / sqrt(d)
    weights = softmax(scores, dim=-1)
    return weights @ V
# Problem: Model sees future tokens!

# Right: Apply causal mask
def attention(Q, K, V, mask=None):
    scores = Q @ K.T / sqrt(d)
    if mask is not None:
        scores = scores.masked_fill(mask == 0, -1e9)
    weights = softmax(scores, dim=-1)
    return weights @ V

# Create causal mask
causal_mask = torch.tril(torch.ones(seq_len, seq_len))

Pitfall 2: Memory Management

Pitfall: Computing full attention matrix causing OOM

import torch
# Wrong: Full attention computation
attention_weights = torch.softmax(
    Q @ K.T / sqrt(d),  # O(N²) memory!
    dim=-1
)

# Right: Use Flash Attention or chunking
from flash_attn import flash_attn_func
output = flash_attn_func(Q, K, V)  # O(N) memory

# Or chunk manually
def chunked_attention(Q, K, V, chunk_size=512):
    outputs = []
    for i in range(0, Q.size(1), chunk_size):
        Q_chunk = Q[:, i:i+chunk_size]
        scores = Q_chunk @ K.T / sqrt(d)
        weights = torch.softmax(scores, dim=-1)
        outputs.append(weights @ V)
    return torch.cat(outputs, dim=1)

Pitfall 3: Position Encoding

Pitfall: Using absolute positional encoding that doesn't extrapolate

import torch.nn as nn
import torch
# Wrong: Learned absolute positions (poor extrapolation)
class LearnedPositionalEmbedding(nn.Module):
    def __init__(self, max_seq_len, d_model):
        self.embeddings = nn.Parameter(torch.randn(max_seq_len, d_model))

# Right: RoPE for better extrapolation
def rotate_position(x, seq_len, dim):
    theta = torch.arange(dim // 2) / (dim // 2)
    theta = 1.0 / (10000 ** theta)
    freqs = torch.outer(torch.arange(seq_len), theta)
    emb = torch.polar(torch.ones_like(freqs), freqs)
    return apply_rotary_emb(x, emb)

Pitfall 4: Tokenizer Choice

Pitfall: Using wrong tokenizer for your domain

# Wrong: Using GPT-2 tokenizer for code
from transformers import GPT2Tokenizer
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
# Problem: Poor tokenization for code syntax

# Right: Use domain-specific tokenizer
from transformers import CodeLlamaTokenizer
tokenizer = CodeLlamaTokenizer.from_pretrained('codellama/CodeLlama-7b')
# Better: Fine-tune tokenizer on your domain

Pitfall 5: Activation Function

Pitfall: Using ReLU in transformer (dead neurons)

import torch.nn.functional as F
import torch.nn as nn
# Wrong: ReLU causes dead neurons
activation = nn.ReLU()

# Right: Use GELU or SwiGLU
activation = nn.GELU()  # Better gradients

# Or SwiGLU (state-of-the-art for LLMs)
class SwiGLU(nn.Module):
    def __init__(self, dim, hidden_dim):
        self.w = nn.Linear(dim, hidden_dim)
        self.v = nn.Linear(dim, hidden_dim)
        self.w2 = nn.Linear(hidden_dim, dim)

    def forward(self, x):
        return self.w2(F.silu(self.w(x)) * self.v(x))

Pitfall 6: Normalization Placement

Pitfall: Post-Norm in deep networks (training instability)

import torch.nn as nn
# Wrong: Post-Norm (instability in deep networks)
class TransformerBlock(nn.Module):
    def forward(self, x):
        x = x + self.attention(self.norm1(x))  # Post-Norm
        x = x + self.ffn(self.norm2(x))
        return x

# Right: Pre-Norm (stable for deep networks)
class TransformerBlock(nn.Module):
    def forward(self, x):
        x = x + self.attention(self.norm1(x))  # Pre-Norm
        x = x + self.ffn(self.norm2(x))
        return x

Pitfall 7: Architecture Selection

Pitfall: Using wrong architecture for task

# Wrong: Using BERT for text generation
from transformers import BertForMaskedLM
model = BertForMaskedLM.from_pretrained('bert-base-uncased')
# Problem: BERT is encoder-only, not designed for generation

# Right: Use decoder-only for generation
from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained('gpt2')
# Or encoder-decoder for translation
from transformers import AutoModelForSeq2SeqLM
model = AutoModelForSeq2SeqLM.from_pretrained('t5-base')


Pro Tips

Attention Optimization

Tip: Use KV cache for faster autoregressive generation

import torch
# Standard generation: Recompute attention each token (slow)
def generate_slow(model, input_ids, max_length=100):
    for _ in range(max_length):
        outputs = model(input_ids)  # Recomputes all attention
        next_token = torch.argmax(outputs.logits[:, -1, :])
        input_ids = torch.cat([input_ids, next_token.unsqueeze(0)], dim=1)
    return input_ids

# Optimized: Use KV cache
def generate_fast(model, input_ids, max_length=100):
    past_key_values = None
    for _ in range(max_length):
        outputs = model(
            input_ids,
            past_key_values=past_key_values,  # Cache KV pairs
            use_cache=True
        )
        past_key_values = outputs.past_key_values
        next_token = torch.argmax(outputs.logits[:, -1, :])
        input_ids = next_token.unsqueeze(0)
    return input_ids

# Result: 2-3x speedup!

Memory Reduction

Tip: Combine gradient checkpointing with Flash Attention

from torch.utils.checkpoint import checkpoint

def forward_with_checkpointing(attention, ffn, x):
    # Enable larger batch sizes with limited VRAM
    x = checkpoint(attention, x)  # Checkpoint attention
    x = checkpoint(ffn, x)         # Checkpoint FFN
    return x

# Benefits:
# - Reduces memory by 2-3x
# - Enables training larger models
# - Trade-off: ~20% slower (recompute on backward)

Tokenizer Efficiency

Tip: Analyze token distribution before choosing tokenizer

import numpy as np
def analyze_tokenizer_efficiency(text, tokenizer):
    tokens = tokenizer.encode(text)
    stats = {
        'total_tokens': len(tokens),
        'unique_tokens': len(set(tokens)),
        'compression_ratio': len(text.split()) / len(tokens),
        'avg_token_length': np.mean([len(tokenizer.decode([t])) for t in tokens])
    }
    return stats

# Compare tokenizers:
text = "Your domain-specific text here..."
for name in ['gpt2', 'codellama', 'sentencepiece']:
    tokenizer = AutoTokenizer.from_pretrained(name)
    stats = analyze_tokenizer_efficiency(text, tokenizer)
    print(f"{name}: {stats}")

Architecture Debugging

Tip: Visualize attention patterns to understand model

import matplotlib.pyplot as plt

def visualize_attention(model, text, layer=0, head=0):
    inputs = model.tokenizer(text, return_tensors='pt')
    outputs = model(**inputs, output_attentions=True)
    attention = outputs.attentions[layer][0, head].detach().numpy()

    tokens = model.tokenizer.convert_ids_to_tokens(inputs['input_ids'][0])

    plt.figure(figsize=(10, 8))
    plt.imshow(attention, cmap='viridis')
    plt.xticks(range(len(tokens)), tokens, rotation=90)
    plt.yticks(range(len(tokens)), tokens)
    plt.colorbar()
    plt.title(f'Attention Patterns - Layer {layer}, Head {head}')
    plt.show()

# Reveals what tokens attend to what!

Performance Benchmarking

Tip: Profile different components to find bottlenecks

import torch.profiler as profiler

def profile_model(model, input_ids):
    with profiler.profile(
        activities=[profiler.ProfilerActivity.CPU, profiler.ProfilerActivity.CUDA],
        record_shapes=True
    ) as prof:
        _ = model(input_ids)

    print(prof.key_averages().table(sort_by="cuda_time_total"))
    # Shows which operations take the most time!

# Common findings:
# - Attention: Usually 40-60% of time
# - FFN: 30-40% of time
# - Embedding lookup: 5-10% of time
# - Use this to prioritize optimization efforts


Performance Benchmarks

Attention Mechanisms

Method Memory (seq=2048) Speed Accuracy Best For
Standard Attention O(N²) = 67 MB 1.0x 100% Short sequences
Flash Attention O(N) = 2 MB 2.5x 100% Long sequences
Memory Efficient O(N√N) = 8 MB 1.8x 99.9% Medium sequences
Sparse Attention O(N log N) 3.0x 98% Very long sequences

Tokenizer Comparison

Tokenizer Vocab Size Compression Multilingual Code Support
GPT-2 BPE 50k 1.2x No Poor
GPT-4 TikToken 100k 1.5x Yes Good
SentencePiece 32k-250k 1.4x Yes Medium
CodeLlama 32k 1.3x No Excellent

Activation Functions

Activation Speed Gradient Quality Dead Neurons Used In
ReLU 1.0x Medium Yes Old models
GELU 1.1x Good No BERT, GPT-2
SwiGLU 1.2x Excellent No LLaMA, Mistral

Hands-on Practice

  1. EXP_3101: Self-Attention

    • Implement attention from scratch
    • Visualize attention patterns
    • Benchmark multi-head configurations
  2. EXP_3102: Flash Attention

    • Compare standard vs flash attention
    • Profile memory usage
    • Benchmark inference speed
    • Test on long sequences
  3. EXP_3201: RoPE

    • Implement RoPE from scratch
    • Compare absolute vs relative
    • Visualize rotation matrices
    • Test position encoding
  4. EXP_3202: Tokenizer

    • Compare BPE vs SentencePiece
    • Analyze token distributions
    • Test on different languages
    • Measure compression ratio
  5. EXP_3401: Encoder-Decoder

    • Compare architecture types
    • Benchmark performance
    • Analyze use cases
  6. EXP_3501: Multimodal RAG

    • Build CLIP-style image-text search
    • Construct multimodal RAG pipelines
    • Apply VLMs to real-world tasks

Prerequisites

Before starting this phase, ensure you understand:

  • PyTorch Basics (from 2200: Frameworks)
  • Tensor Operations (from 2100: Calculus)
  • Backpropagation (from 2100: Calculus)
  • Neural Networks (from 2300: Deep Learning)

See PREREQUISITES for details.


Assessment

Validate your knowledge with:


  • 3100: Attention - Self-attention and Flash Attention
  • 3200: Embeddings - Position encoding and tokenization
  • 3300: Decoding - Activation functions, normalization, and sampling strategies
  • 3400: Architectures - Model types and comparison
  • 3500: Multimodal - Vision-language and audio models

Next Steps

After completing this phase:

  1. Apply to Your Projects

    • Implement custom attention mechanisms
    • Optimize memory usage with Flash Attention
    • Choose right tokenizer for your domain
    • Select appropriate architecture
  2. Continue Learning

    • Phase 4: Quantize models for deployment
    • Phase 5: Fine-tune models for your tasks
    • Phase 6: Build RAG systems
    • Phase 7: Create agentic AI systems

Module Duration: 69-71 hours (54 reading + 15-17 practice)

Difficulty: ⭐⭐⭐ Advanced

Ready to understand transformers? Start with 3101: Self-Attention Deep Dive or 3102: Flash Attention