Transformers have one fatal flaw.
That self-attention mechanism from Episode 2? It scales quadratically with sequence length. Double the context, quadruple the compute. This is fine at 4K tokens. Painful at 128K. And essentially impossible at 1 million tokens without tricks.
What if there was an architecture with linear scaling? Double the context, double the compute. No attention mechanism at all. No KV cache. No quadratic bottleneck.
That's Mamba โ the state space model that's challenging the transformer's throne. It processes 1 million tokens with the same per-token cost as processing 1,000. And it's not a toy โ it matches transformer quality on language tasks while being dramatically faster at inference.
The Quadratic Problem
Let's make the transformer's scaling problem concrete:
Sequence Length Attention Compute Relative Cost
| 1K tokens | 1M operations | 1x |
|---|---|---|
| 4K tokens | 16M operations | 16x |
| 32K tokens | 1B operations | 1,024x |
| 128K tokens | 16B operations | 16,384x |
| 1M tokens | 1T operations | 1,000,000x |
Self-attention computes a score between every pair of tokens. With N tokens, that's Nยฒ pairs. This is why long-context models are so expensive โ and why companies charge a premium for 128K+ context.
State space models eliminate this entirely.
What Is a State Space Model?
A state space model (SSM) comes from control theory โ the math behind autopilots, robot controllers, and signal processing systems. The idea: maintain a hidden state that gets updated as each new input arrives.
The Continuous Form
In continuous time (control theory), a state space model is:
dx/dt = Ax + Bu (state equation)
y = Cx + Du (output equation)where:
x = hidden state vector
u = input (one element at a time)
y = output
A = state transition matrix (how state evolves)
B = input projection matrix (how input affects state)
C = output projection matrix (how state becomes output)
D = skip connection (direct input-to-output)The Discrete Form
For processing sequences (text, audio), we discretize to get:
x_k = ฤยทx_{k-1} + Bฬยทu_k (update state with new input)
y_k = Cยทx_k (read output from state)where:
k = timestep (each token)ฤ, Bฬ = discretized versions of A, B
Think of it like this: the model maintains a summary vector (the state) that gets updated with each new token. To produce an output, it reads from this summary. It never looks back at previous tokens โ it only looks at its current state.
Token 1: "The" โ state_1 = update(state_0, "The")
Token 2: "cat" โ state_2 = update(state_1, "cat")
Token 3: "sat" โ state_3 = update(state_2, "sat")
Token 4: "on" โ state_4 = update(state_3, "on")To predict token 5: read(state_4) โ "the"
Why Linear Scaling?
Each token update is a constant-time operation โ one matrix multiply regardless of how many tokens came before. Process 1,000 tokens? 1,000 matrix multiplies. Process 1,000,000 tokens? 1,000,000 matrix multiplies. Linear, not quadratic.
Architecture Per-Token Compute Memory for Context
| Transformer | O(N) per token (attention over all N) | O(N) KV cache |
|---|---|---|
| SSM | O(1) per token | O(d) fixed state |
| Transformer 128K | Huge KV cache, slow | ~32GB for 70B |
| SSM 128K | Same speed as 1K | ~megabytes |
๐ง
S4: The Breakthrough
S4 (Structured State Spaces for Sequence Modeling, 2021) was the paper that proved SSMs could compete with transformers on real tasks.The key innovation: how to parameterize the A matrix. A naive state matrix loses long-range information โ early tokens get "forgotten" as the state evolves. S4 uses the HiPPO (High-Order Polynomial Projection Operator) initialization:
A = HiPPO matrix (specially structured)
This matrix is designed to continuously approximate the history
of inputs as polynomial coefficients. It "remembers" optimally.
S4 also showed that SSMs can be computed as a convolution during training (parallel, fast on GPUs) and as a recurrence during inference (sequential, memory-efficient).
Training: Convolution mode โ process all tokens in parallel
Inference: Recurrence mode โ process one token at a time with fixed state
This dual computation mode is a huge advantage:
Training benefits from GPU parallelism (like transformers)
Inference benefits from constant memory (unlike transformers)
๐
Mamba: The Selective SSM
Mamba (December 2023, Albert Gu and Tri Dao) is the SSM that actually threatens transformers. Its key innovation: selective state spaces โ making the SSM parameters input-dependent.The Problem with Basic SSMs
In a standard SSM, A, B, and C are fixed matrices โ the same for every input token. This means the model processes "the" and "photosynthesis" with the exact same dynamics. It can't selectively focus on important tokens or ignore filler.
Mamba's Fix: Selection Mechanism
Mamba makes B, C, and ฮ (the discretization step) functions of the input:
python
Standard SSM (fixed parameters)
A = fixed_matrix
B = fixed_matrix
C = fixed_matrix
Mamba (input-dependent parameters)B = linear(input) # Changes based on what the token is
C = linear(input) # Changes based on what the token is
ฮ = softplus(linear(input)) # Controls how much to update stateThe ฮ (delta) parameter is especially important โ it controls the "gate" for each token:
Large ฮ โ focus on the current input, update state significantly
Small ฮ โ ignore the current input, keep existing state
This is conceptually similar to the gating mechanism in LSTMs, but implemented through the state space formalism.
The Selection Effect
Input: "The weather in Mumbai is usually hot and humid during summer months"
ฮ values (learned):
"The" โ small ฮ (filler word, don't update much)
"weather" โ medium ฮ (somewhat important)
"Mumbai" โ LARGE ฮ (critical โ location matters!)
"is" โ small ฮ (filler)
"usually" โ small ฮ (filler)
"hot" โ LARGE ฮ (key fact!)
"and" โ small ฮ (filler)
"humid" โ LARGE ฮ (key fact!)
...
The model learns to selectively compress the input sequence โ keeping important information and discarding filler. This is why Mamba matches transformer quality despite never doing explicit attention.
Mamba Architecture
A full Mamba model stacks Mamba blocks (replacing transformer blocks):
Input tokens
โToken Embedding
โโโ Mamba Block 1 โโโโโโโโโโโโโโโโโโโโโโโ
โ Linear โ Conv1D โ SSM โ Linear โ
โ + Skip connection + LayerNorm โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโ Mamba Block 2 โโโโโโโโโโโโโโโโโโโโโโโ
โ (same structure) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ... (N blocks)
โOutput head โ next token predictionInside a Mamba Block
python
def mamba_block(x):
# Two parallel branches
# Branch 1: Selective SSM path
z = linear_proj(x) # Expand dimension
z = silu(conv1d(z)) # Local context (kernel=4)
z = selective_ssm(z) # The magic โ selective state space# Branch 2: Gate
gate = silu(linear_proj(x))# Combine
output = z * gate # Element-wise gating
output = linear_proj(output) # Project back to model dim
return output + x # Residual connectionThe Conv1D with kernel size 4 gives local context (similar to looking at neighboring tokens), while the SSM provides global context through its recurrent state.
Mamba vs Transformers: Head to Head
Feature Transformer Mamba
Attention O(Nยฒ) quadratic None
| Per-token compute | O(N) during generation | O(1) constant |
|---|---|---|
| Memory during inference | KV cache grows with context | Fixed state size |
| Training parallelism | Full (attention is parallel) | Full (convolution mode) |
| Long-range dependencies | Explicit (attend to any token) | Implicit (through state) |
| Hardware optimization | Heavily optimized (FlashAttention) | Selective scan kernel |
| Copy/retrieval tasks | Excellent | Weaker |
| Language modeling quality | Gold standard | Competitive (close gap) |
Where Mamba Wins
Long sequences โ 1M+ tokens without the KV cache problem
Inference speed โ constant per-token cost regardless of context
Memory efficiency โ fixed state instead of growing KV cache
Audio/signal processing โ naturally good at continuous signals
Where Transformers Win
In-context retrieval โ "What was the 3rd word in paragraph 7?" requires looking back
Copying โ reproducing exact sequences from context
Established ecosystem โ years of optimization, tooling, and scaling research
๐ฌ
Mamba-2 and Hybrid Models
Mamba-2 (2024) bridged the gap further by showing that selective SSMs can be expressed as a form of structured attention โ unifying the two paradigms mathematically.
The most promising direction: hybrid models that use both:
Layer 1: Mamba block (efficient long-range)
Layer 2: Mamba block
Layer 3: Attention block (precise retrieval)
Layer 4: Mamba block
Layer 5: Mamba block
Layer 6: Attention block
...
Jamba (AI21 Labs) was the first production hybrid โ 52B params with a mix of Mamba and attention layers. It gets the best of both worlds: Mamba's efficiency for most processing, attention's precision for retrieval tasks.Model Architecture Context Notes
Mamba-1 (2.8B) Pure Mamba Unlimited Research model
| Jamba (52B) | Mamba + Attention hybrid | 256K | First production hybrid |
|---|---|---|---|
| Zamba (7B) | Mamba + shared attention | 128K | Efficient hybrid |
| NVIDIA Hymba | Mamba-2 + Attention hybrid | 128K | State-of-the-art hybrid |
๐ก๏ธ
The Hardware Angle
Mamba requires a custom CUDA kernel called selective scan โ and this is both its strength and weakness:
Strength: The selective scan kernel (written by Tri Dao, the FlashAttention author) is highly optimized and memory-efficient.
Weakness: Transformers have years of hardware optimization. Every GPU vendor optimizes for matrix multiplications and attention. Mamba's selective scan is newer and less optimized.
Transformer training: cuBLAS (GEMM) + FlashAttention โ highly optimized
Mamba training: cuBLAS (GEMM) + Selective Scan โ newer, less optimized
The gap is closing, but transformers have a hardware ecosystem advantage.
๐ฆ
Will SSMs Replace Transformers?
Honest answer: probably not entirely, but they'll coexist.
The most likely future:
Pure Mamba for streaming/continuous data (audio, sensor data, very long documents)
Hybrid Mamba+Attention for general language models (best of both worlds)
Pure Transformers for tasks requiring precise retrieval and in-context learning
The transformer isn't going away โ it's too good at too many things. But SSMs have shown that the quadratic attention bottleneck isn't the only path to intelligence. And for the long-context future (million-token windows, streaming audio, always-on agents), SSMs are essential.
๐
Practical Takeaways
SSMs process sequences with constant per-token cost โ linear scaling instead of quadratic
Mamba's key innovation is selection โ input-dependent parameters let it focus on important tokens
Dual computation modes โ convolution for parallel training, recurrence for efficient inference
No KV cache needed โ fixed-size state replaces the ever-growing cache of transformers
Hybrid models are the future โ combining Mamba efficiency with attention precision
Transformers aren't dead โ they're better at retrieval and in-context learning, so the future is likely a mix
๐
What's Next?
Episode 62: Test-time Compute โ Why do "thinking" models like o1 and DeepSeek-R1 take longer to answer? What are thinking tokens? How does spending more compute during inference improve reasoning? The answer changes how you think about AI costs.
โ Previous
Ep 60: Diffusion Models
Next โ
Ep 62: Test-Time Compute
Next: Episode 62 โ Test-Time Compute
Ask GPT-4 a tricky math problem. It answers in 2 seconds. Wrong. Ask o1 the same thing. It thinks for 30 seconds. Right.
This is part of a 98-episode series covering AI engineering from tokens to production deployment.