Sequence Modeling in ML
“Predicting the next word, the next stock price, the next frame.”
TL;DR
Sequence modeling is the foundation of modern AI, from chatbots to protein folding. This article covers the full evolution from RNNs through Transformers to state space models like Mamba, including the math of self-attention, KV caching for efficient inference, Flash Attention for memory efficiency, positional encodings, and production serving with continuous batching and speculative decoding. These concepts underpin LLM serving infrastructure and RAG systems.

1. The Problem: Sequential Data
Many real-world problems involve sequences:
- Text: “The cat sat on the ___” → “mat”
- Time Series: Stock prices, weather, sensor data.
- Video: Predict next frame.
- Audio: Speech recognition, music generation.
Challenge: The output depends on context (previous elements in the sequence).
2. Evolution of Sequence Models
1. Recurrent Neural Networks (RNN)
- Idea: Hidden state
h_tcarries information from previous steps. - Equation: \(h_t = \tanh(W_h h_{t-1} + W_x x_t + b)\)
- Problem: Vanishing gradients. Can’t remember long-term dependencies.
2. LSTM (Long Short-Term Memory)
- Gates: Forget, Input, Output gates control information flow.
- Advantage: Can remember 100+ steps.
- Disadvantage: Sequential processing (can’t parallelize).
3. Transformer (Attention is All You Need)
- Self-Attention: Every token attends to every other token.
- Parallelization: Process entire sequence at once.
- Scalability: Powers GPT, BERT, LLaMA.
3. Transformer Architecture
Key Components:
- Self-Attention:
\(\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V\)
- Query (Q): “What am I looking for?”
- Key (K): “What do I have?”
- Value (V): “What information do I provide?”
- Multi-Head Attention:
- Run attention multiple times with different learned projections.
- Allows model to attend to different aspects (syntax, semantics, etc.).
- Positional Encoding:
- Transformers have no notion of order.
- Add sinusoidal encodings: \(PE_{(pos, 2i)} = \sin(pos / 10000^{2i/d})\)
- Feed-Forward Network:
- Two linear layers with ReLU: \(\text{FFN}(x) = \max(0, xW_1 + b_1)W_2 + b_2\)
4. Training Strategies
1. Teacher Forcing
- During training, feed ground truth as input (even if model predicted wrong).
- Pro: Faster convergence.
- Con: Exposure bias (model never sees its own mistakes during training).
2. Scheduled Sampling
- Gradually mix ground truth with model predictions during training.
- Start with 100% teacher forcing, decay to 0%.
3. Curriculum Learning
- Start with short sequences, gradually increase length.
5. Inference Strategies
1. Greedy Decoding
- At each step, pick the token with highest probability.
- Fast but suboptimal.
2. Beam Search
- Keep top-
kcandidates at each step. - Explore multiple paths, pick the best overall sequence.
- Better quality but slower.
3. Sampling
- Sample from the probability distribution.
- Temperature: Control randomness.
- T → 0: Greedy (deterministic).
- T → ∞: Uniform (random).
6. System Design: Real-Time Translation
Scenario: Google Translate (Text-to-Text).
Architecture:
- Encoder: Processes source sentence (English).
- Decoder: Generates target sentence (French).
- Attention: Decoder attends to relevant encoder states.
Optimizations:
- Caching: Cache encoder output (source doesn’t change).
- Batching: Process multiple requests together.
- Quantization: INT8 weights are 4x smaller than FP32 and usually faster to run.
7. Design Sketch: Serving a GPT-3-Scale Model
Challenge: Serve a 175B parameter model with low latency.
Solutions:
- Model Parallelism: Split model across 8 GPUs (175B parameters in FP16 is about 350 GB of weights).
- KV Cache: Cache key/value tensors from previous tokens.
- Speculative Decoding: Use a small model to draft, large model to verify.
8. Deep Dive: Attention Mechanism Math
Let’s break down the attention formula step by step.
Input: Sequence of vectors \(X = [x_1, x_2, \ldots, x_n]\), each \(x_i \in \mathbb{R}^d\).
Step 1: Linear Projections
\[Q = XW_Q, \quad K = XW_K, \quad V = XW_V\]- \(W_Q, W_K, W_V \in \mathbb{R}^{d \times d_k}\) are learned weight matrices.
Step 2: Compute Attention Scores
\[S = \frac{QK^T}{\sqrt{d_k}}\]- \(S \in \mathbb{R}^{n \times n}\). Entry \(S_{ij}\) is the “compatibility” between query i and key j.
- Divide by \(\sqrt{d_k}\) to prevent dot products from becoming too large (which would make softmax saturate).
Step 3: Softmax
\[A = \text{softmax}(S)\]- Each row sums to 1. \(A_{ij}\) is the “attention weight” from position i to position j.
Step 4: Weighted Sum
\[\text{Output} = AV\]- Each output vector is a weighted combination of all value vectors.
9. Deep Dive: KV Caching for Autoregressive Decoding
Problem: When generating token t, we recompute keys and values for tokens 1, 2, …, t-1 (wasteful!).
Solution: Cache the Key and Value matrices.
Without Caching:
for t in range(max_len):
# Recompute K, V for all previous tokens
K = compute_keys(tokens[:t+1]) # O(t * d^2)
V = compute_values(tokens[:t+1])
output = attention(Q, K, V)
With Caching:
K_cache, V_cache = [], []
for t in range(max_len):
# Only compute K, V for new token
k_t = compute_key(tokens[t]) # O(d^2)
v_t = compute_value(tokens[t])
K_cache.append(k_t)
V_cache.append(v_t)
K = concat(K_cache)
V = concat(V_cache)
output = attention(Q, K, V)
Speedup: Per-step projection work drops from O(t·d²) to O(d²). Attention over the cache is still O(t·d) per step, which is why long contexts stay memory-bound.
10. Deep Dive: Flash Attention
Problem: Standard attention requires materializing the N × N attention matrix in memory.
- For N = 4096, that’s 16.8M floats = 64MB per head in FP32.
- A model with 96 heads (GPT-3 has 96) would need about 6GB per layer, per sequence, just for attention scores.
Flash Attention (Dao et al., 2022):
- Idea: Compute attention in blocks, never materialize the full matrix.
- Algorithm:
- Divide
Q, K, Vinto blocks. - Load one block of
Qand one block ofKinto SRAM. - Compute partial attention scores.
- Accumulate results.
- Divide
- Speedup: The paper reports up to 3x end-to-end training speedups on GPT-2, and attention memory becomes linear in sequence length.
11. Deep Dive: Positional Encoding
Transformers have no notion of order. We add positional information.
Sinusoidal Encoding (Original Transformer):
\[PE_{(pos, 2i)} = \sin\left(\frac{pos}{10000^{2i/d}}\right), \quad PE_{(pos, 2i+1)} = \cos\left(\frac{pos}{10000^{2i/d}}\right)\]Why this works:
- Different frequencies for different dimensions.
- Model can learn to attend to relative positions.
Learned Positional Embeddings (BERT, GPT):
- Treat position as a lookup table.
- More flexible, but limited to max sequence length seen during training.
Rotary Position Embedding (RoPE):
- Used in LLaMA, PaLM.
- Rotates query and key vectors based on position, so attention scores depend on relative distance.
- With rescaling tricks (position interpolation, YaRN) and brief fine-tuning, it extends to longer contexts than seen in training.
12. System Design: Chatbot with Context
Scenario: Build a chatbot that remembers conversation history.
Challenge: Context grows with each turn. How to handle long conversations?
Approach 1: Sliding Window
- Keep only last
Ntokens (e.g., 2048). - Pro: Fixed memory.
- Con: Forgets old context.
Approach 2: Summarization
- Periodically summarize old context.
- Pro: Retains important info.
- Con: Lossy compression.
Approach 3: Retrieval-Augmented
- Store conversation in a vector DB.
- Retrieve relevant past messages based on current query.
- Pro: Scalable to infinite history.
- Con: Requires embedding model + DB.
13. Deep Dive: Beam Search Implementation
def beam_search(model, start_token, beam_width=5, max_len=50):
# Initialize beam with start token
beams = [([start_token], 0.0)] # (sequence as a list of ids, log_prob)
for _ in range(max_len):
candidates = []
for seq, score in beams:
if seq[-1] == EOS_TOKEN:
candidates.append((seq, score))
continue
# Get next token probabilities
logits = model(seq)
log_probs = F.log_softmax(logits, dim=-1)
# Top-k tokens
top_k_probs, top_k_tokens = torch.topk(log_probs, beam_width)
for prob, token in zip(top_k_probs, top_k_tokens):
new_seq = seq + [token.item()]
new_score = score + prob.item()
candidates.append((new_seq, new_score))
# Keep top beam_width candidates
beams = sorted(candidates, key=lambda x: x[1], reverse=True)[:beam_width]
return beams[0][0] # Best sequence
14. Production Optimizations
1. Model Quantization
- Convert FP32 → INT8.
- Size: 4x smaller; speedup depends on whether the hardware has fast INT8 kernels.
- Accuracy Loss: Often small with careful calibration; always measure on your task.
2. Distillation
- Train a small “student” model to mimic a large “teacher”.
- DistilBERT: 40% smaller, 60% faster, 97% of BERT’s performance.
3. Pruning
- Remove unimportant weights (e.g., magnitude pruning).
- Caveat: Magnitude pruning can remove a large share of BERT-size weights with modest accuracy loss, but unstructured sparsity rarely speeds up GPUs; 2:4 structured sparsity is the hardware-supported case.
15. Deep Dive: Sequence-to-Sequence Models (Seq2Seq)
Architecture: Encoder-Decoder with Attention.
Use Cases:
- Machine Translation (English → French)
- Summarization (Long article → Short summary)
- Question Answering (Context + Question → Answer)
Key Innovation: Attention Mechanism (Bahdanau et al., 2015)
- Problem: Fixed-length context vector bottleneck.
- Solution: Decoder attends to all encoder states.
Implementation:
class Seq2SeqWithAttention(nn.Module):
def __init__(self, vocab_size, hidden_size):
super().__init__()
# Inputs are one-hot tensors [seq_len, batch, vocab_size] to keep the sketch short
self.encoder = nn.LSTM(vocab_size, hidden_size, bidirectional=True)
self.decoder = nn.LSTM(vocab_size + 2 * hidden_size, hidden_size) # token + context
self.attention = nn.Linear(hidden_size * 3, 1)
self.output = nn.Linear(hidden_size, vocab_size)
def forward(self, src, tgt):
# Encode: encoder_outputs is [src_len, batch, 2 * hidden]
encoder_outputs, (h, c) = self.encoder(src)
# Start the decoder from the sum of both directions' final states
decoder_hidden = h.sum(dim=0, keepdim=True) # [1, batch, hidden]
c = c.sum(dim=0, keepdim=True)
outputs = []
for t in range(tgt.size(0)):
# Compute attention scores
scores = self.attention(torch.cat([
decoder_hidden.expand(encoder_outputs.size(0), -1, -1),
encoder_outputs
], dim=2))
# Attention weights
attn_weights = F.softmax(scores, dim=0)
# Context vector
context = (attn_weights * encoder_outputs).sum(dim=0)
# Decoder step
decoder_input = torch.cat([tgt[t], context], dim=1)
output, (decoder_hidden, c) = self.decoder(decoder_input.unsqueeze(0), (decoder_hidden, c))
outputs.append(self.output(output))
return torch.cat(outputs, dim=0)
16. Advanced: Sparse Attention Mechanisms
Problem: Full attention is O(N²). For N = 100k (long documents), this is prohibitive.
Solutions:
1. Longformer (Beltagy et al., 2020):
- Local Attention: Each token attends to
wneighbors (sliding window). - Global Attention: A few tokens (e.g.,
[CLS]) attend to everything. - Complexity: O(N·w) instead of O(N²).
2. BigBird (Zaheer et al., 2020):
- Random Attention: Each token attends to
rrandom tokens. - Block Attention: Divide sequence into blocks, attend within blocks.
- Global Attention: Special tokens attend globally.
3. Linformer (Wang et al., 2020):
- Low-Rank Projection: Project
K, Vto lower dimension. - Complexity: O(N·k) where k ≪ N.
Code (Longformer-style Local Attention): this version builds the full N × N mask to show the pattern; real implementations compute only the band, which is where the savings come from.
import math
import torch
import torch.nn.functional as F
def local_attention(Q, K, V, window_size=256):
N, d = Q.shape
# Create attention mask (band matrix)
mask = torch.zeros(N, N)
for i in range(N):
start = max(0, i - window_size // 2)
end = min(N, i + window_size // 2)
mask[i, start:end] = 1
# Compute attention
scores = (Q @ K.T) / math.sqrt(d)
scores = scores.masked_fill(mask == 0, float('-inf'))
attn = F.softmax(scores, dim=-1)
return attn @ V
17. Case Study: AlphaFold (Protein Folding)
Problem: Predict 3D protein structure from amino acid sequence.
Why Sequence Modeling?
- Protein is a sequence of amino acids (A, C, D, E, …).
- Structure depends on long-range interactions (residue 10 affects residue 500).
AlphaFold 2 Architecture:
- Evoformer: Transformer variant with:
- MSA (Multiple Sequence Alignment) Attention: Attend across evolutionary related sequences.
- Pair Representation: Attend to pairwise residue relationships.
- Structure Module: Predicts 3D coordinates.
Key Innovation: Attention over both sequence and structure space.
Result: At CASP14 (2020), its predictions were competitive with experimental structures for many targets, a major step on a 50-year-old problem.
18. Deep Dive: Mixture of Experts (MoE)
Idea: Instead of one giant model, use many small “expert” models. Route each input to the most relevant experts.
Architecture:
Input → Router (Gating Network) → Top-K Experts → Combine Outputs
Example: Switch Transformer (Google, 2021)
- 1.6 Trillion parameters (the largest Switch-C model).
- But each token passes through only one expert per MoE layer, so per-token compute is a small fraction of the parameter count (sparse activation).
- Routing: Each token is sent to 1 expert (out of 2048).
Benefits:
- Scalability: Add more experts without increasing per-token compute.
- Specialization: Experts can pick up different patterns, though routing analyses of models like Mixtral found little clean topic-level specialization.
Challenges:
- Load Balancing: Ensure all experts are used equally.
- Training Instability: Router can collapse (send everything to one expert).
Code:
class MoELayer(nn.Module):
def __init__(self, num_experts, hidden_size):
super().__init__()
self.router = nn.Linear(hidden_size, num_experts)
self.experts = nn.ModuleList([
nn.Sequential(
nn.Linear(hidden_size, hidden_size * 4),
nn.ReLU(),
nn.Linear(hidden_size * 4, hidden_size)
) for _ in range(num_experts)
])
def forward(self, x):
# Route
router_logits = self.router(x)
router_probs = F.softmax(router_logits, dim=-1)
# Top-1 expert per token
expert_idx = torch.argmax(router_probs, dim=-1)
# Apply experts, scaled by the router probability (as in Switch);
# without this scaling the router receives no gradient
output = torch.zeros_like(x)
for i, expert in enumerate(self.experts):
mask = (expert_idx == i)
if mask.any():
output[mask] = router_probs[mask, i].unsqueeze(-1) * expert(x[mask])
return output
19. Production Serving Architecture
Scenario: Serve GPT-3-scale model (175B params) with <1s latency.
Architecture:
┌─────────────┐
│ Client │
└──────┬──────┘
│
▼
┌─────────────────┐
│ Load Balancer │
└──────┬──────────┘
│
┌───┴───┐
▼ ▼
┌─────┐ ┌─────┐
│ GPU │ │ GPU │ (Model Parallelism: 8 GPUs per replica)
│ 1 │ │ 2 │
└─────┘ └─────┘
Optimizations:
1. Continuous Batching (Orca, 2022):
- Don’t wait for all requests to finish.
- As soon as one finishes, add a new request to the batch.
- Benefit: Much higher throughput, since batch slots never sit idle waiting for the longest request.
2. PagedAttention (vLLM, 2023):
- Store KV cache in paged memory (like OS virtual memory).
- Benefit: Reduce memory waste from fragmentation.
3. Speculative Decoding:
- Use a small model (1B) to draft 5 tokens.
- Use large model (175B) to verify in parallel.
- Speedup: Leviathan et al. (2023) report 2-3x on T5-XXL with identical output distributions.
20. Evaluation Metrics for Sequence Models
1. Perplexity (Language Modeling): \(PPL = \exp\left(-\frac{1}{N}\sum_{i=1}^N \log P(w_i \mid w_{<i})\right)\)
- Lower is better.
- Interpretation: “How surprised is the model by the test data?”
2. BLEU (Machine Translation):
- Measures n-gram overlap between prediction and reference.
- Range: 0-100. Scores only compare systems on the same test set; use sacreBLEU for reproducible numbers.
3. ROUGE (Summarization):
- Similar to BLEU, but focuses on recall.
4. Exact Match (QA):
- Percentage of predictions that exactly match ground truth.
21. Common Pitfalls and How to Avoid Them
Pitfall 1: Exposure Bias (Teacher Forcing)
- Model trained on ground truth, tested on its own predictions.
- Fix: Scheduled sampling or reinforcement learning.
Pitfall 2: Length Bias in Beam Search
- Longer sequences have lower cumulative probability.
- Fix: Length normalization: \(\text{score} = \frac{\log P(y)}{\lvert y \rvert^\alpha}\)
Pitfall 3: Catastrophic Forgetting (Fine-Tuning)
- Fine-tuning on Task B makes model forget Task A.
- Fix: Elastic Weight Consolidation (EWC) or multi-task learning.
Pitfall 4: OOM (Out of Memory) During Training
- Gradient accumulation: Simulate large batch with small batches.
- Fix: Call
(loss / 4).backward()every step, and every 4th step calloptimizer.step()thenoptimizer.zero_grad().
Pitfall 5: Ignoring Positional Encoding Limits
- BERT trained on 512 tokens can’t handle 1024.
- Fix: Use RoPE or ALiBi (Attention with Linear Biases).
22. Deep Dive: State Space Models (Mamba / S4)
Problem: Transformers are O(N^2). RNNs are O(N) but hard to train. Can we get the best of both?
Solution: Structured State Space Models (SSMs).
Key Idea:
- Model the sequence as a continuous-time system: \(h'(t) = Ah(t) + Bx(t), \quad y(t) = Ch(t)\)
- Discretize it for digital computers.
- Training (S4): With fixed A, B, C the model can be computed as a Convolution (parallelizable like Transformers).
- Inference: Can be computed as a Recurrence (Constant memory like RNNs).
Mamba (Gu & Dao, 2023):
- Introduces Selection Mechanism: The step size and the matrices B and C depend on the input \(x_t\).
- Allows the model to “selectively” remember or ignore information. Input-dependent parameters rule out the convolution trick, so Mamba trains with a hardware-aware parallel scan.
- Performance: The paper reports matching or beating Transformers of similar size on language modeling at the scales tested (up to a few billion parameters), with linear scaling O(N).
Code (Simplified Mamba Block):
class MambaBlock(nn.Module):
def __init__(self, d_model, d_state=16, d_conv=4):
super().__init__()
self.dt_rank = math.ceil(d_model / 16)
self.in_proj = nn.Linear(d_model, d_model * 2)
# Depthwise causal conv: pad both sides, keep the first seq_len outputs
self.conv1d = nn.Conv1d(d_model, d_model, kernel_size=d_conv,
groups=d_model, padding=d_conv - 1)
# Input-dependent SSM parameters: low-rank step size, B and C
self.x_proj = nn.Linear(d_model, self.dt_rank + 2 * d_state)
self.out_proj = nn.Linear(d_model, d_model)
def forward(self, x): # x: [batch, seq_len, d_model]
seq_len = x.size(1)
# 1. Project
x_and_res = self.in_proj(x)
x, res = x_and_res.chunk(2, dim=-1)
# 2. Conv
x = self.conv1d(x.transpose(1, 2))[..., :seq_len].transpose(1, 2)
x = F.silu(x)
# 3. SSM (Selective Scan)
# This is the core Mamba magic (usually implemented in CUDA)
y = selective_scan(x, self.x_proj(x))
# 4. Output
return self.out_proj(y * F.silu(res))
23. Deep Dive: Reinforcement Learning from Human Feedback (RLHF)
Problem: LLMs are trained to predict the next token, not to be helpful or safe.
Pipeline (InstructGPT / ChatGPT):
- Supervised Fine-Tuning (SFT):
- Collect human demonstrations (Question + Answer).
- Fine-tune base model.
- Reward Modeling (RM):
- Collect comparison data (Model generates A and B; Human says A > B).
- Train a Reward Model to predict human preference.
- Loss (A preferred over B): \(-\log \sigma(r(A) - r(B))\).
- PPO (Proximal Policy Optimization):
- Optimize the SFT model to maximize the reward from RM.
- Constraint: Don’t drift too far from SFT model (KL Divergence penalty).
Impact:
- Aligns model with human intent.
- Reduces toxicity and hallucinations (mostly).
24. Advanced: Long-Context Transformers (Ring Attention)
Problem: How to train on 1 Million tokens?
- Memory is the bottleneck. Even with Flash Attention, activations don’t fit on one GPU.
Ring Attention (Liu et al., 2023):
- Idea: Distribute the sequence across multiple GPUs in a ring.
- Each GPU holds a chunk of Query, Key, Value.
- Step 1: Compute local attention.
- Step 2: Pass Key/Value blocks to the next GPU in the ring.
- Step 3: Compute attention with new KV blocks.
- Repeat: Until all KV blocks have visited all GPUs.
Result:
- Maximum sequence length grows linearly with the number of GPUs in the ring.
- Enables “Needle in a Haystack” retrieval over entire books.
25. Interview Questions for Sequence Modeling
Q1: Why do we divide by \(\sqrt{d_k}\) in Attention? Answer: To scale the dot products. If \(d_k\) is large, dot products become huge, pushing Softmax into regions with extremely small gradients (vanishing gradients).
Q2: What is the difference between Post-Norm and Pre-Norm? Answer:
- Post-Norm:
LayerNorm(x + Sublayer(x)). Original Transformer. Harder to train (warmup needed). - Pre-Norm:
x + Sublayer(LayerNorm(x)). Used in GPT-3/LLaMA. More stable gradients, easier to train.
Q3: How does RoPE handle sequence length extrapolation? Answer: By rotating the vectors, the attention score depends only on relative distance (m − n). On its own RoPE still degrades beyond the training length, but rescaling methods (position interpolation, NTK-aware scaling, YaRN) extend it with brief fine-tuning, which absolute learned embeddings cannot do.
Q4: Explain the “KV Cache” memory usage. Answer: Memory = 2 (K and V) × batch × seq_len × layers × kv_heads × head_dim × bytes per value. For Llama 2 7B (32 layers, 32 KV heads of dimension 128, FP16), that is 2 × 32 × 32 × 128 × 2 bytes = 512 KB per token, or 2 GB for one 4,096-token sequence. For long sequences this dominates memory; grouped-query attention cuts kv_heads, and PagedAttention reduces fragmentation.
Q5: Why use MoE? Answer: To decouple model size (parameters) from compute cost (FLOPs). A model can hold a trillion parameters but activate only a small fraction of them per token, enabling massive capacity with reasonable inference latency.
26. Ethical Considerations
1. Hallucinations:
- Sequence models are “stochastic parrots”. They generate plausible-sounding but potentially false text.
- Risk: Misinformation in medical/legal contexts.
- Mitigation: RAG (Retrieval Augmented Generation) to ground answers in facts.
2. Bias and Toxicity:
- Models learn biases present in training data (internet).
- Risk: Hate speech, stereotypes.
- Mitigation: RLHF, careful data curation, red-teaming.
3. Dual Use:
- Code generation models can write malware.
- Mitigation: Safety guardrails, refusal to answer harmful queries.
27. Common Mistakes in Sequence Modeling
1. Training on Test Data (Data Leakage):
- Accidentally including the test set in the pre-training corpus.
- Consequence: Overestimated performance.
- Fix: De-duplication (MinHash) against evaluation benchmarks.
2. Ignoring Tokenizer Issues:
- Different tokenizers (BPE, WordPiece) handle whitespace and special characters differently.
- Consequence: Poor performance on code or multilingual text.
- Fix: Use a robust tokenizer like Tiktoken or SentencePiece.
3. Incorrect Masking in Causal Attention:
- Allowing tokens to attend to future tokens during training.
- Consequence: Model learns to cheat, fails at inference.
- Fix: Verify the causal mask (entries above the diagonal are −∞).
4. Underestimating Inference Cost:
- Focusing only on training loss.
- Consequence: Model is too slow/expensive to deploy.
- Fix: Monitor FLOPs per token and KV cache size during design.
28. Glossary of Terms
- Token: The atomic unit of text processing (word, subword, or character).
- Embedding: A dense vector representation of a token.
- Attention: Mechanism to weigh the importance of different input tokens.
- Self-Attention: Attention applied to the sequence itself.
- Cross-Attention: Attention applied between two sequences (e.g., Encoder-Decoder).
- Logits: The raw, unnormalized scores output by the last layer.
- Softmax: Function to convert logits into probabilities.
- Temperature: A hyperparameter that controls the randomness of sampling.
- Beam Search: A heuristic search algorithm that explores a graph by expanding the most promising node in a limited set.
- Perplexity: A measurement of how well a probability model predicts a sample.
29. Further Reading
- “Attention Is All You Need” (Vaswani et al., 2017): The Transformer paper.
- “BERT: Pre-training of Deep Bidirectional Transformers” (Devlin et al., 2019): Masked language modeling.
- “Language Models are Few-Shot Learners” (Brown et al., 2020): GPT-3 and in-context learning at 175B parameters.
- “Flash Attention” (Dao et al., 2022): Memory-efficient attention.
- “Switch Transformers” (Fedus et al., 2021): Mixture of Experts at scale.
30. Conclusion
Sequence modeling has transformed from simple statistical methods (n-grams) to the behemoth Large Language Models that define the current AI era. The journey from RNNs to LSTMs and finally to Transformers represents a shift from sequential processing to parallel, attention-based architectures. This evolution has enabled us to model not just language, but code, biology, and even robot actions as sequences. As we look forward, the challenges of infinite context length, efficient inference, and alignment with human values remain the frontier of research. Whether you are building a chatbot, a translation system, or a protein folder, understanding the underlying mechanics of attention and state management is the key to unlocking the potential of sequence models.
31. Summary
| Model | Pros | Cons |
|---|---|---|
| RNN | Simple | Vanishing gradients |
| LSTM | Long memory | Sequential (slow) |
| Transformer | Parallel, Scalable | O(N^2) memory |
| Flash Attention | Memory efficient | Complex implementation |
| MoE | Trillion-scale models | Load balancing challenges |
FAQ
Why do Transformers divide attention scores by the square root of the key dimension?
Without scaling, dot products between query and key vectors grow proportionally with the dimension, pushing softmax into saturation regions with extremely small gradients. Dividing by sqrt(d_k) normalizes the variance of dot products to approximately 1 regardless of dimension, ensuring softmax produces meaningful probability distributions and gradient signals for stable training.
How does KV caching speed up autoregressive decoding?
Without caching, step t recomputes the key and value projections for all t previous tokens, so projection work grows quadratically over a generation. KV caching stores previously computed keys and values, so each step computes K and V only for the new token, appends them to the cache, and attends over the cache. Per-step work becomes linear in context length, a major speedup for long sequences. Memory management for the KV cache is a primary concern in LLM serving.
What is Flash Attention and why does it matter?
Standard attention materializes the full N x N attention matrix in GPU high-bandwidth memory (HBM), which is prohibitive for long sequences. Flash Attention computes attention in tiles using fast on-chip SRAM, never storing the full matrix in HBM. It uses online softmax to accumulate results incrementally. The paper reports up to 3x end-to-end training speedups (GPT-2), and memory for attention drops from quadratic to linear in sequence length, enabling much longer contexts. It is critical infrastructure for modern LLMs.
How do state space models like Mamba compare to Transformers?
Mamba models sequences as discretized continuous-time systems with a selection mechanism that allows input-dependent state transitions. During training, a hardware-aware parallel scan processes the whole sequence at once (earlier SSMs like S4 used a convolution, which input-dependent parameters rule out). During inference, it runs as a constant-memory recurrence, similar to RNNs. This gives O(N) scaling instead of the O(N²) of full attention; the Mamba paper reports quality matching Transformers of similar size at the scales it tested, with better efficiency on long sequences.