8.7 — State Space Models (Mamba) — brief
Goal: Derive the continuous-time linear state space model y(t) = C h(t), h'(t) = A h(t) + B x(t), discretise it to a recurrence, and motivate Mamba's selective SSM where A, B, C are input-dependent. Understand why SSMs achieve O(n) inference (no KV cache) and where they sit relative to attention in 2026.
1. Why this session exists
Every architecture in Chapter 8 so far is a Transformer with one sub-module modified. State space models (SSMs), and their leading instantiation Mamba (Gu & Dao, 2023[3], arXiv:2312.00752), are different: they replace attention entirely with a recurrent sequence operator. Per token they need no KV cache, and per generated token they run in time instead of attention's (with the cached-K, V matmul). For long-context inference this is a substantial wall-clock advantage.
The question is whether SSMs can match attention on quality at scale. As of 2026 the answer is: pure SSMs are competitive but not strictly better than Transformers at the frontier. Hybrid architectures (§8.8) that combine SSM and attention layers are an active research direction and have produced models that match dense Transformers at iso-compute. So SSMs matter both as a standalone variant and, more importantly, as the recurrent half of every hybrid model.
This session is brief on purpose. We define the continuous SSM, discretise it to a recurrence, motivate the selective (input-dependent) version that became Mamba, count its compute and memory, and place it in the 2026 architectural landscape. The Mamba paper is dense; we will skim its hardware-aware "parallel scan" details and refer the reader to the paper for the kernel-level implementation.
2. The continuous-time SSM
Fix state dimension and signal dimension (here we treat single-channel input; multi-channel SSMs are a per-channel collection of single-channel models). A linear time-invariant state space model is the continuous-time system
with state , input , output , and matrices . This is the canonical control-theory SSM (Kalman 1960; not specific to deep learning)[1].
The matrix defines the intrinsic dynamics of the hidden state — how it evolves in the absence of input. defines how new input enters the state. defines what part of the state is observed as output.
2.1 The convolutional view
Solving the ODE (assuming ):
So is the convolution of with the kernel . The kernel is determined entirely by . This is the operative observation that links SSMs to convolutions and, by Fourier, to fast algorithms.
3. Discretisation
To use SSMs on discrete token sequences, we discretise the ODE with step size . The Zero-Order Hold (ZOH) discretisation, which Mamba uses:
so the recurrence becomes
This is a linear recurrent network. Given the state and input , the new state requires one matrix-vector multiply (cost ) plus one matrix-vector multiply (cost ); the output requires one multiply (cost ). Per-step cost: .
The crucial structural difference from a standard RNN (5.5) is that the recurrence is linear — no per-step non-linearity inside the state update. Non-linearity is added externally (a SwiGLU-like block applied to ). Linearity is what enables the parallel-scan formulation of §5.
3.1 Structured : from S4 to Mamba
The naive matrix has free parameters per channel, which is impractical at the channel counts (, etc.) of LLM-scale models. The S4 paper (Gu et al., 2022, arXiv:2111.00396)[2] introduced structured matrices — diagonal-plus-low-rank — that have parameters and admit fast convolutional evaluation via FFTs.
Mamba uses a simpler structured form: is diagonal with negative real entries, so the recurrence is a per-state-coordinate exponential decay:
This has only parameters per channel, and each coordinate of the state evolves independently: . Different coordinates capture different timescales of state retention — small means slow decay (long memory), large means fast decay (short memory). Empirically, training discovers a spread of timescales without needing explicit constraints.
4. Selective SSM: Mamba
The S4 line gave good signal-processing models but lost to Transformers on language tasks. The Mamba paper's diagnosis: a time-invariant SSM (with fixed ) cannot do content-dependent state updates. For language, the model often needs to decide "keep this token's information in state" vs "forget it" based on the token itself — what attention's content-based addressing does, and what a content-blind SSM cannot.
Selective SSM (Mamba's contribution): make functions of the input.
The recurrence becomes
with the input-dependent discretised . The matrix stays time-invariant (so the eigenstructure is preserved); only the coupling into and out of state, and the time step, vary per token.
The interpretation: controls "how much time has passed" between tokens — large means strong decay of past state (fast forgetting), small means weak decay (long memory). controls how the current token's content enters the state — the model can choose to gate the token in (large ) or skip it (small ). controls what part of the state is read out at this step.
This is the SSM analogue of attention's content-addressed gathering: per token, the model dynamically chooses what to remember, how strongly, and what to output.
4.1 Why this destroys the convolutional structure
Time-invariant SSMs are convolutions (§2.1), so they can be computed in via FFT. Selective SSMs are time-varying convolutions — the kernel depends on the input — and admit no direct FFT trick.
Mamba's solution is the parallel scan: the linear recurrence is an "associative scan" over the sequence (the operator is associative when you collect the correctly into pairs). Associative scans can be computed in depth with work using the standard parallel-prefix-sum algorithm. The Mamba paper implements this with a hardware-aware CUDA kernel that minimises HBM↔SRAM transfers, similar in spirit to FlashAttention but for the scan operation.
Per-token training compute: for state update, for output projection — same per-token cost as a Transformer FFN of width (no quadratic-in- term, because there is no attention matrix to compute).
5. Compute and memory: the headline comparison
Per-layer cost comparison for a single token at position :
| Operator | Train: FLOPs per token | Inference: FLOPs per generated token | KV cache | |---|---|---|---| | MHA (with FlashAttention) | | | bytes | | GQA- | | | bytes | | MLA | | | bytes | | Selective SSM | | | (state is scalars, fixed not growing) |
The headline: SSM compute per generated token is constant in . There is no cache that grows with sequence length. The hidden state has fixed dimension , independent of .
This is the structural reason for SSMs' interest in long-context generation:
- At , a Transformer reads MB of KV cache per generated token (LLaMA-7B-class). An SSM reads KB of state. The SSM is faster at the memory-bandwidth level.
- At , the Transformer reads GB per generated token. The SSM still reads KB. The SSM is faster.
- The crossover point at which an SSM is "free" is essentially at ; the constant cost is small enough that SSMs are competitive with attention even at short sequences.
5.1 But quality?
The Mamba paper's main empirical claim: at iso-parameters, Mamba matches Transformer perplexity on Pile-style pretraining and outperforms on long-context benchmarks (where the Transformer's effective context degrades). Subsequent work (Mamba-2, Dao & Gu 2024[4]; Hymba; etc.) has confirmed and extended this at the 1B–3B scale.
But at the frontier (B+ activated parameters), pure SSMs have not yet matched dense Transformer LLMs on standard benchmarks. The leading hypotheses for the residual gap:
- In-context retrieval. Attention's content-addressed lookup is exact (or nearly so, modulo softmax); an SSM's state is a lossy compression of the past. Tasks that require pulling a specific past token verbatim (e.g., needle-in-haystack) are easier for attention.
- Heuristic inductive biases of attention. Decades of attention-related architectural tuning have given the field a host of small improvements (RoPE, GQA, sliding-window) that SSMs have not yet seen full attention to.
- Training stability. SSMs at very large scale have known numerical-stability issues that the field has not fully worked through.
The current state of the art is to combine attention and SSM, which is the subject of §8.8 (hybrids).
6. The block
A Mamba block, in shape, is
x ── RMSNorm ── linear up-proj (to 2*d) ── split ──┐
│
┌── Selective SSM (with selective Δ, B, C) ──┤
│ │
└── SiLU ──────────── elementwise * ────────┘──── linear down-proj (back to d) ── + (residual) ── x_out
In words: pre-norm, up-project to , split into two halves of width . Apply the selective SSM to one half. Apply SiLU to the other half. Elementwise-multiply (a gating like SwiGLU). Down-project back to . Residual add. There is no separate FFN block: the SSM block already includes its own non-linearity and gating, so a Mamba layer is one block (not the two of attention + FFN in a Transformer).
This means a Mamba "layer" at fixed parameter count corresponds to about 2× the number of layers in a Transformer with the same parameters. Mamba-3B has 64 layers; a Transformer-3B has . The depth doubles but each layer is cheaper.
7. What we keep going forward
-
An SSM is a linear (or near-linear) recurrent operator , . Discretised from a continuous-time linear ODE. With diagonal , the state coordinates evolve at independent timescales.
-
Selective SSM (Mamba) makes input-dependent. This is the SSM analogue of attention's content-addressed lookup. Time-invariant SSMs (S4) lost to Transformers on language; selective SSMs are competitive.
-
SSMs have KV-like state, not . Decode per token is constant in , vs Transformer's . The wall-clock advantage at long is enormous.
-
At iso-parameters, Mamba matches Transformer at B and underperforms slightly at frontier scale. The leading hypothesis is that exact in-context retrieval needs attention; a lossy state compression does not suffice.
-
The 2026 frontier uses hybrids. Interleave Mamba and attention layers (Jamba, Samba) to combine the per-token-throughput advantage of SSMs with the recall accuracy of attention. This is the subject of §8.8.
(a) Write the continuous-time SSM of §2. Derive the convolutional form with . Why is this only valid if is time-invariant?
(b) State the ZOH discretisation of §3. Derive the per-step cost for naive and for diagonal . What's lost by restricting to diagonal?
(c) Explain in your own words why a time-invariant SSM cannot do "selective forgetting" of input tokens. Why is making input-dependent (the Mamba contribution) the natural fix?
(d) Compute the per-generated-token bandwidth cost for (i) MHA on LLaMA-7B at , (ii) Mamba-7B-class with state dim , both fp16. Which is more memory-traffic-efficient?
(e) A Mamba block has one sub-module that combines SSM and gating; a Transformer block has two sub-modules (attention + FFN). Argue why this means a Mamba "layer" at fixed parameters corresponds to roughly 2× the layer count of a Transformer.
(f) State the leading hypothesis (§5.1, item 1) for why pure SSMs underperform Transformers at frontier scale. Why do hybrid architectures (§8.8) plausibly close the gap?
References
- [1]
Kalman, R. E. (1960). "A new approach to linear filtering and prediction problems". Trans. ASME—J. Basic Eng., 82, 35–45. — Canonical state-space formulation of linear filtering, ancestor of all SSM architectures (8.7 §2).
↩ - [2]
Gu, A., Goel, K., & Ré, C. (2022). "Efficiently modeling long sequences with structured state spaces" (S4). arXiv:2111.00396 — Structured (diagonal-plus-low-rank) for efficient long-sequence SSM (8.7 §3.1).
↩ - [3]
Gu, A., & Dao, T. (2023). "Mamba: Linear-time sequence modeling with selective state spaces". arXiv:2312.00752 — Selective SSM with input-dependent ; hardware-aware parallel scan (8.7 §4).
↩ - [4]
Dao, T., & Gu, A. (2024). "Transformers are SSMs: Generalized models and efficient algorithms through structured state space duality" (Mamba-2). arXiv:2405.21060 — SSM-attention duality; Mamba-2 architecture (8.7 §5.1).
↩