MODULE 9  ยท  The Cutting Edge

State Space Models: Mamba and the Transformer Alternative

Self-attention scales quadratically with sequence length. Double the context, quadruple the compute. Mamba says there's a better way.

๐Ÿ“… Mar 2026
โฑ 10 min read
๐ŸŽฏ Episode 61 of 98
State Space ModelsMambaArchitectureAlternatives
In this episode

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 tokens1M operations1x
4K tokens16M operations16x
32K tokens1B operations1,024x
128K tokens16B operations16,384x
1M tokens1T operations1,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)

snippet
code
y = Cx + Du        (output equation)

where:

snippet
code
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:

snippet
code
x_k = ฤ€ยทx_{k-1} + Bฬ„ยทu_k    (update state with new input)
y_k = Cยทx_k                   (read output from state)

where:

snippet
code
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.

snippet
code
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

TransformerO(N) per token (attention over all N)O(N) KV cache
SSMO(1) per tokenO(d) fixed state
Transformer 128KHuge KV cache, slow~32GB for 70B
SSM 128KSame speed as 1K~megabytes

๐Ÿ”ง

S4: The Breakthrough

snippet
code
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

snippet
code
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)

snippet
code
A = fixed_matrix
B = fixed_matrix
C = fixed_matrix
Mamba (input-dependent parameters)
snippet
code
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 state

The ฮ” (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

example
code
โ†“

Token Embedding

example
code
โ†“

โ”Œโ”€ Mamba Block 1 โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ Linear โ†’ Conv1D โ†’ SSM โ†’ Linear โ”‚

โ”‚ + Skip connection + LayerNorm โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

example
code
โ†“

โ”Œโ”€ Mamba Block 2 โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ (same structure) โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

example
code
โ†“

... (N blocks)

example
code
โ†“
snippet
code
Output head โ†’ next token prediction

Inside a Mamba Block

python

def mamba_block(x):

example
code
# 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
example
code
# Branch 2: Gate
    gate = silu(linear_proj(x))
example
code
# Combine
    output = z * gate               # Element-wise gating
    output = linear_proj(output)    # Project back to model dim
    return output + x               # Residual connection

The 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 computeO(N) during generationO(1) constant
Memory during inferenceKV cache grows with contextFixed state size
Training parallelismFull (attention is parallel)Full (convolution mode)
Long-range dependenciesExplicit (attend to any token)Implicit (through state)
Hardware optimizationHeavily optimized (FlashAttention)Selective scan kernel
Copy/retrieval tasksExcellentWeaker
Language modeling qualityGold standardCompetitive (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

...

snippet
code
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 hybrid256KFirst production hybrid
Zamba (7B)Mamba + shared attention128KEfficient hybrid
NVIDIA HymbaMamba-2 + Attention hybrid128KState-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.

โ† Previous Ep 60: Diffusion Models: How AI Turns Noise Into Art