Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

State Space Models (Mamba, S4): A Frontier-Lab Interview Deep Dive

Why this exists. SSMs are the most credible challenger to transformers. They give sequence complexity with constant memory at inference — properties transformers don’t have. Frontier-lab interviews increasingly ask about Mamba, the selectivity mechanism, and hybrid models. This document covers the math without the dense academic notation.


1. The big picture

A state space model maintains a hidden state and applies a recurrence:

This is exactly an RNN with linear (no nonlinearity in the recurrence) dynamics. The trick is what you do with this.

Linear recurrences have two equivalent computational forms:

  1. Recurrent: compute from step by step. time, memory.
  2. Convolutional: unroll into a convolution , where is a learned kernel. with FFT.

The duality is the key idea: SSMs train via convolution (parallel) but generate via recurrence (constant memory per step). Best of both worlds.

Saying it out loud. If someone asks what a state space model is, I’d say it’s a machine that keeps one running summary of everything it has seen. Each step it fades the old summary a bit, mixes in the new token, and reads an answer off of it. The clever part isn’t the equation, it’s that because the update is linear you can compute it two different ways — step by step when generating, or all at once as a convolution when training. So you get an RNN’s constant-memory decoding without an RNN’s sequential training bottleneck. That duality is the entire reason anyone builds these.


2. The classical state space ODE

In plain language. This section is borrowed straight from control theory, where the same equations describe things like a cruise-control system or a robot arm. The idea is that there’s a hidden internal state you never see directly, an input pushing on it, and an output you read off it. The math below just writes down how the state changes over time and how the output is read out.

The continuous version:

For input signal , the state evolves linearly via ; output is a linear readout. Same equations as in control theory and signal processing.

Saying it out loud. The continuous form is the original: it says the rate of change of the internal state is some mix of the current state plus whatever the input is pushing in, and the output is a linear readout of that state. Control engineers have used exactly this to model physical systems for decades — think of the state as position and velocity and the input as the throttle. Machine learning borrows the form and just learns the matrices instead of deriving them from physics. The useful mental hook is that controls how memory decays on its own, controls what gets written in, and controls what gets read out.

Discretization

For machine learning, we work with discrete sequences. Use zero-order hold to discretize:

is a step size (often learned). The discretized recurrence:

Same form as before but with , as discrete-time matrices.

Saying it out loud. Discretization is how you get from smooth continuous time to one-token-at-a-time. You pick a step size and ask what the continuous system would have done over that interval, assuming the input stays constant across the step — that assumption is what ‘zero-order hold’ means. Out of that falls a matrix exponential, , which is the discrete-step version of the decay. The thing to actually remember is what means intuitively: large means the state moves a lot per token, small means the token barely registers, and Mamba’s whole contribution is making that dial input-dependent.


3. The convolutional view

In plain language. This section shows that if you write out the recurrence for a few steps, a pattern appears: the output at any time is just a weighted sum of all past inputs, with weights that follow a fixed geometric-ish pattern. That means the whole recurrence can be rewritten as a convolution with a single filter. The algebra below is just that unrolling done carefully.

Unrolling the recurrence:

This is a convolution with kernel (and if is included). For a length- sequence: .

The kernel has length (or up to ). Computing this convolution:

  • Direct: — same as attention.
  • FFT: — better but requires structured .

The breakthrough: choose such that can be efficiently computed.

Saying it out loud. If you unroll the recurrence by hand for a few steps, you notice the output is just a weighted sum of every past input, and the weight on something steps back is always . That’s exactly what a convolution is — one fixed filter slid over the sequence. And convolutions are perfect for GPUs, because every output position can be computed in parallel instead of waiting for the previous one. The catch is the filter is as long as your sequence, so you need an FFT to do it in , and you need a special structure on to build those powers cheaply in the first place. That last requirement is what the next two sections are about.


4. HiPPO: the theoretical foundation

In plain language. HiPPO is about how you pick the matrix at initialization. The problem it solves is that a random either forgets everything within a few steps or blows up. HiPPO derives a specific whose state is a compressed sketch of the entire history so far.

HiPPO (High-order Polynomial Projection Operators) — Gu et al. 2020. Provides a principled choice for :

The HiPPO matrix is constructed so that the hidden state is a compressed representation of the history of up to time . Specifically, the columns of represent coefficients of a polynomial approximation of in some basis (Legendre, Fourier, etc.).

This gives the SSM a principled inductive bias: the model can in principle “remember” all of history, weighted by an interpretable polynomial basis.

Saying it out loud. HiPPO answers a very concrete question: what should be so the state actually remembers? The trick is to stop thinking of the state as raw memory and start thinking of it as the coefficients of a curve fit — the state stores the best polynomial approximation of everything the model has seen. Once you frame it that way you can solve for the matrix that keeps that approximation optimal as new data arrives, and that’s the HiPPO matrix. So instead of hoping the optimizer discovers long-range memory, you build it in at step zero — and empirically that’s worth tens of points on long-range benchmarks, not a few.

Why this matters for ML

A randomly-initialized SSM doesn’t have any reason to remember long-range patterns. HiPPO initialization guarantees that, at init, the model can capture history with bounded error. Empirically: HiPPO-initialized SSMs train much better than randomly initialized ones.

Saying it out loud. The blunt version is that random initialization basically never gives you long-range memory. The eigenvalues of a random are either slightly inside the unit circle, in which case memory decays exponentially and you’ve forgotten anything past a few dozen steps, or slightly outside, in which case the state explodes. HiPPO lands you in the narrow useful regime by construction, with a proven bound on how much history the state preserves. It’s a great example of an initialization choice mattering more than an architecture choice — same model, same parameter count, and the ablation swings Long Range Arena results dramatically.


5. S4: Structured State Spaces

S4 (Gu, Goel, Re, 2022). Practical SSM that combines HiPPO with computational efficiency.

Key contributions

1. Diagonal Plus Low-Rank (DPLR) parameterization of .

HiPPO matrices are dense. S4 reparameterizes as (diagonal + rank-1 update). This makes computing tractable.

2. Efficient kernel computation.

The convolution kernel can be computed in time using a Cauchy-style structured matrix multiplication (instead of for general ).

3. Stable parameterization.

Use HiPPO-LegS initialization for theoretical guarantees, plus tricks to ensure ’s eigenvalues stay stable.

Saying it out loud. S4’s problem was purely computational: to build the convolution kernel you need every power of up to sequence length, and for a dense matrix that’s hopeless inside a training loop. So S4 constrains to a diagonal matrix plus a rank-one correction, which is expressive enough to represent HiPPO but structured enough that the powers collapse into fast Cauchy-kernel math. Add a careful parameterization that keeps eigenvalues stable and you get an kernel. The tradeoff is implementation pain — DPLR is genuinely nasty to write, which is why later work found that plain diagonal with good init gets most of the benefit.

Result

S4 was the first SSM to match transformers on long-range tasks (Long Range Arena benchmark) while having complexity. It established SSMs as a credible architecture.

Saying it out loud. The headline is that S4 was the first state space model that actually beat transformers at something people cared about. On Long Range Arena, which has sequences in the thousands of steps, S4 cleared the whole benchmark including Path-X, which every transformer variant had been failing at chance level. That result is what turned SSMs from a control-theory curiosity into a serious architecture line. The honest caveat is that Long Range Arena is not language — S4’s language modeling was still noticeably behind, and closing that gap took Mamba.


6. Mamba: Selective State Spaces

Mamba (Gu & Dao, 2023). The breakthrough that made SSMs competitive at LLM scale.

The selectivity insight

In plain language. This is the core of Mamba. The equations below say that instead of using the same fixed matrices at every position, the model computes fresh ones from the current token. That single change lets each token control how strongly it writes into memory and how fast old memory fades.

In S4, the matrices , , are shared across all positions — a single linear time-invariant (LTI) system. This is fast (the convolution view works) but inflexible: the model cannot decide that some inputs are more “important” or change its dynamics based on input.

Mamba makes , , and input-dependent:

The state update becomes:

In Mamba 1, is initialized as a diagonal real-valued matrix (S4D-Real / HiPPO-LegS-diagonal), a simplification from the full HiPPO-DPLR structure of S4. Its discretization now depends on input via . Each token can choose how much to remember (large ) vs forget (small ). This is the “selective” mechanism.

Saying it out loud. The insight is that S4 treats every token identically, which is a strange thing for a language model to do. A comma and a person’s name got the exact same dynamics, because the matrices were fixed across the sequence — that’s what ‘time-invariant’ means, and it’s what made the convolution trick legal. Mamba makes , and into cheap linear functions of the current token, so each position sets its own remember-versus-forget dial. Concretely, a big means this token writes hard into the state and the state persists; a small one means it slides by without disturbing anything. That’s what fixed the selective-copying tasks that S4 simply could not do.

Cost of selectivity

The convolutional view no longer works: depends on input, so it’s no longer a single shared kernel. Mamba reverts to the recurrent view but uses a parallel scan algorithm (Blelloch scan) to compute the recurrence in parallel.

Parallel scan: compute in parallel ops with work.

Hardware-aware implementation in CUDA. Throughput comparable to (or better than) attention on modern GPUs.

Saying it out loud. There’s no free lunch here — the moment the matrices depend on the input, the convolution view dies. The kernel was one fixed filter for the whole sequence, and now every position has a different one, so there’s nothing to precompute and FFT. Mamba goes back to the recurrence but computes it with a parallel scan, the same associativity trick behind fast prefix sums, which turns sequential steps into parallel ones. The other half is engineering: the CUDA kernel keeps state in SRAM instead of round-tripping through HBM, exactly like FlashAttention, and without that memory-traffic work the whole thing would be slower than the attention it replaces.

Result

Mamba matches transformer quality at the same parameter count and compute on language modeling, with sequence complexity and constant-memory inference. The first credible drop-in replacement for transformer attention.

Saying it out loud. The bottom line is that Mamba was the first SSM to actually match transformers on language modeling at matched parameters and compute, not just on synthetic long-range benchmarks. On top of that it kept the two structural advantages: linear scaling in sequence length and a decode state that doesn’t grow with context. That combination is what made people take it seriously as a drop-in replacement rather than a niche architecture. The asterisk is scale — the results were convincing up to a few billion parameters, and the recall gap reappears as you go bigger.


7. Why SSMs are interesting for LLMs

Linear sequence complexity

Attention is in compute and memory. SSMs are (with factors for parallel scan). At long context (32K+), this is a huge advantage.

Saying it out loud. Attention compares every token to every other token, so cost scales with the square of context length — double the context and you quadruple the work. An SSM just walks the sequence once updating a fixed-size state, so it’s linear. At 32K tokens that difference is enormous on paper. The honest caveat is that attention kernels are extremely well optimized, so the crossover point where SSMs actually win in wall-clock is further out than the asymptotics suggest — at 2K context the transformer is usually still faster.

Constant-memory inference

For autoregressive decoding: each step is work and memory. KV cache doesn’t grow. Massive memory savings for long-context inference.

Saying it out loud. This is the advantage that’s hardest to argue with. When a transformer generates, it keeps a key and value vector for every token it has ever seen, so the cache grows linearly with context and every new token has to read all of it. An SSM keeps one fixed-size state — same size at token ten as at token a hundred thousand. In serving terms that’s the difference between a KV cache measured in tens of gigabytes at long context and a state measured in megabytes, which directly translates to how many concurrent users fit on one GPU.

Empirical quality

Mamba matches transformer quality on many language tasks at small-to-medium scale (≤7B). At larger scale, an “in-context recall” gap re-emerges (transformers’ attention is naturally good at copying). Mamba-2 (Dao & Gu 2024) reformulates the selective SSM as structured state space duality (SSD) — using semiseparable matrices, the SSM operation becomes a structured matmul that maps onto tensor cores efficiently. This is a substantial speedup, not a minor improvement, and enables much larger state dimensions. Hybrid models (Jamba, Falcon Mamba 7B, Codestral Mamba) interleave SSM and attention layers to recover transformer-level recall while keeping Mamba’s long-context efficiency.

Saying it out loud. The picture is: competitive at small and medium scale, with a specific weakness that grows with scale. Up to around 7B, Mamba matches transformers on most language benchmarks. What re-emerges as you scale is in-context recall — the ability to copy something verbatim from earlier in the prompt, which attention does natively via induction heads and a compressed state has to approximate. Mamba-2 helped on the efficiency side by reformulating the operation as structured matmuls that hit tensor cores, but the fix for recall is architectural: interleave a few attention layers, which is what Jamba and the other hybrids do.

Why they haven’t replaced transformers (yet)

  • In-context learning weaker. Transformers’ attention is naturally good at copying from earlier in context (induction heads). SSMs have weaker copy-and-recall behavior empirically.
  • Calibration / uncertainty. Transformers’ attention provides interpretable patterns; SSMs less so.
  • Ecosystem. Transformers have years of optimization (FlashAttention, vLLM, paged attention). SSM tooling is younger.
  • Scaling laws. Whether SSMs match transformers at frontier scale (100B+) is still being established.

Saying it out loud. Three things, and only one is really about quality. The genuine quality issue is recall — a fixed-size state has to compress everything, so exact lookup from far back in the context is fundamentally harder than for attention, which can just point at the token. The other two are ecosystem and evidence: every serving stack in the world is built around attention, and nobody has published a clean frontier-scale head-to-head. That’s why labs are hedging with hybrids instead of betting a full pretraining run on a pure SSM.


8. Hybrid architectures

Recent research suggests mixing attention and SSM layers gives the best of both:

  • Attention layers for in-context learning, copy, and exact recall.
  • SSM layers for long-range mixing with cost.

Examples

Jamba (AI21, 2024). 7-to-1 SSM-to-attention ratio. 256K+ context. Combines Mamba blocks with transformer attention blocks and MoE.

Zamba (Zyphra). Hybrid SSM-attention with MoE.

Bamba, Samba, Hymba — various hybrid designs. Active research area.

The frontier-lab interview question: “Are pure SSMs going to replace transformers?” Most likely answer: hybrids are the practical compromise; pure SSMs may not catch up at frontier scale, but mixed-block architectures will be increasingly common.

Saying it out loud. The pattern across all of these is the same: mostly SSM layers with a few attention layers sprinkled in, usually around one attention layer per six or seven SSM layers. Jamba was the first production one, from AI21 in 2024, and it added mixture-of-experts on top to get more capacity without more compute per token. The reason it works is that you don’t need attention everywhere — you need it in a handful of places where exact retrieval matters, and the SSM majority keeps the cache small. The number that made people pay attention was Jamba fitting 140K tokens of context on a single 80GB GPU, which a dense transformer that size simply cannot do.


9. Mamba vs LSTM vs RNN

People sometimes ask “isn’t this just an RNN?”

Yes, mathematically

Mamba is a linear RNN (no nonlinearity in the recurrence — selectivity is in the input-dependent matrices, not in a nonlinear gate).

But practically very different

  • LSTM: nonlinear gates, hard to parallelize, vanishing gradients with depth. Mamba: linear recurrence, parallel scan, stable gradients via structured .
  • Vanilla RNN: unstable, can’t be trained at scale. Mamba: HiPPO-initialized, stable, scales.
  • Linear attention: also , but Mamba’s selectivity gives more expressiveness.

So “linear RNN” is technically right but misleading. Mamba is what RNNs always wanted to be.

Saying it out loud. Saying Mamba is just an RNN is technically true and completely unhelpful. Every property that made RNNs unusable comes from details Mamba changed: the nonlinearity in the recurrence, which forces sequential training and vanishing gradients, and the dense random recurrent matrix, which makes it unstable. Mamba keeps the recurrence linear so it parallelizes with a scan, uses a structured diagonal with principled init so gradients stay well-behaved over thousands of steps, and puts its gating in the input-dependent instead. The lesson is that in sequence modeling the equation tells you almost nothing — initialization and hardware mapping decide whether something scales.


10. The mathematical machinery (briefly)

In plain language. This section collects the three formulas you’d actually be asked to reproduce on a whiteboard: how continuous time becomes discrete steps, what the convolution filter looks like written out, and the associativity trick that makes the recurrence parallelizable. None of it is deep — it’s the notation you need to have at your fingertips.

For interview-grade understanding:

Discretization:

or the zero-order hold formula above.

Convolution kernel:

Parallel scan for selective SSM:

The associativity trick: define . Then can be computed via prefix scan over pairs. Parallelizable; runs in parallel time.

For diagonal in Mamba (each state dim is independent), this scan is straightforward. The hardware-aware Mamba kernel does this efficiently.

Saying it out loud. If I had to whiteboard this, three things carry the whole story. First, discretization is a matrix exponential — you can quote the Taylor expansion if pressed, and the first-order term is usually enough to make the point. Second, the convolution kernel is literally the sequence and so on, which is why structure on matters so much. Third, the parallel scan works because composing two linear updates gives you another linear update, and that composition is associative — associativity is exactly the property that lets you use a tree instead of a chain, so steps become depth. In Mamba, is diagonal, so each state dimension scans independently and the whole thing is embarrassingly parallel.


11. Common interview gotchas

GotchaStrong answer
“Aren’t SSMs just RNNs?”Yes, mathematically — linear RNNs. But the structured , parallel scan, and selectivity make them practical at scale, unlike vanilla RNNs.
“Why does Mamba’s selectivity help?”Each token can decide how much to remember vs forget, making the dynamics input-dependent. Closes the expressiveness gap with attention.
“What’s the convolutional view?”Linear recurrence unrolls into a convolution , where . Allows parallel training via FFT (in S4) or scan (in Mamba).
“Why doesn’t Mamba use the convolutional view?”Selectivity makes input-dependent, breaking the single-kernel property. Must use parallel scan instead.
“Memory advantage of SSMs?”Constant memory at decode (state size ), vs attention’s growing KV cache. Big win for long-context generation.
“Why hasn’t Mamba replaced transformers?”Weaker copy/recall, less mature ecosystem, scaling laws unclear at frontier scale. Hybrid models are the practical compromise.
“What’s HiPPO?”Principled initialization of such that is a polynomial approximation of . Enables long-range memory at init.

12. The 8 most-asked SSM interview questions

  1. What’s an SSM? Linear recurrence . Trains via convolution; generates via recurrence.
  2. What’s Mamba? SSM with input-dependent (selectivity). Matches transformer quality at complexity.
  3. What’s HiPPO? Theoretical initialization for that makes the state a polynomial approximation of past inputs.
  4. Why is the convolutional view useful? Parallelizes training (FFT, ). The recurrence runs sequentially.
  5. Why does Mamba use parallel scan, not convolution? Selectivity makes input-dependent; can’t use a fixed kernel.
  6. Memory advantage of SSMs? Constant memory at decode (), vs growing KV cache for transformers.
  7. What’s the in-context learning gap? Transformers excel at copy-and-recall via induction heads. SSMs are weaker; hybrid models compensate.
  8. What’s a hybrid SSM-attention model? Mix attention and SSM layers. Jamba, Zamba. Combines both architectures’ strengths.

13. Drill plan

  1. Memorize the recurrence and its convolutional unrolling.
  2. Know HiPPO’s role in initializing .
  3. Explain Mamba’s selectivity: input-dependent .
  4. Know parallel scan = how Mamba parallelizes training.
  5. Cite hybrid models (Jamba) as the practical compromise.
  6. Drill INTERVIEW_GRILL.md.

14. Further reading

  • Gu et al., “HiPPO: Recurrent Memory with Optimal Polynomial Projections” (2020).
  • Gu, Goel, Re, “Efficiently Modeling Long Sequences with Structured State Spaces” (S4, 2022).
  • Gu & Dao, “Mamba: Linear-Time Sequence Modeling with Selective State Spaces” (2023).
  • Dao & Gu, “Transformers are SSMs: Generalized Models and Efficient Algorithms Through Structured State Space Duality” (Mamba-2, 2024).
  • AI21, “Jamba” (2024) — hybrid SSM-Transformer.