State space models and Mamba
Replace attention with structured state space models (S4, Mamba) for linear-time sequence mixing.
Self-attention is in sequence length and quickly becomes a bottleneck. State space models (SSMs) offer a different sequence-mixing operation that runs in time and memory while matching or beating Transformer quality on long-context tasks. The line runs from continuous-time linear systems through S4 to Mamba, which is the modern hardware-aware variant powering long-context language models.
The starting point: linear state space systems
A continuous-time linear SSM maps input to output via a hidden state :
Discretize with step size :
This is a linear recurrent network: hidden state evolves linearly, output is a linear projection. For input sequence , the output can be computed:
- Recurrently: time, memory per step.
- As a convolution: for an appropriately defined convolution kernel . Used for parallel training.
So SSMs are both linear RNNs (for sequential inference) and convolutions (for parallel training).
S4 (Structured State Space)
Gu et al. (2022)[1]. The state matrix is parameterized as a structured matrix (HiPPO matrices) that gives the SSM a useful prior over long-range dependencies.
Key contributions:
- HiPPO initialization: is initialized to project the input history onto Legendre polynomials, giving the model a built-in inductive bias for compressing the past.
- Diagonal parameterization: with the right basis, is diagonal, and the convolution can be computed efficiently in the frequency domain.
- Long Range Arena benchmark wins: S4 dominated on synthetic long-context tasks where Transformers struggled.
S4 was the proof of concept that an alternative to attention could compete with Transformers on long-context modeling.
Mamba (2024)
Gu and Dao[2]. The breakthrough that made SSMs competitive with Transformers for language modeling.
Two innovations:
- Selective SSM: the state matrices become functions of the input, not fixed parameters. The model selects what to remember based on the current token. This is the SSM analog of attention's content-based addressing.
- Hardware-aware kernel: a custom CUDA kernel that fuses the SSM computation, exploits the GPU memory hierarchy, and avoids materializing the hidden state for the whole sequence.
The resulting Mamba block is a drop-in replacement for the attention sublayer of a Transformer:
x = x + Mamba(LayerNorm(x))
x = x + FFN(LayerNorm(x))Mamba achieves Transformer-quality language modeling at sequence lengths where attention becomes infeasible (100K+ tokens), with constant-time per-token inference (no growing KV cache).
Mamba-2 (2024)
Refinement of Mamba with:
- State space duality (SSD): a unified theoretical framework showing SSMs and attention are special cases of a more general family.
- Larger state dimensions: efficient via the SSD reformulation.
- Improvements in modeling quality at scale.
The Mamba-2 paper showed that careful SSM design can match the parameter count vs perplexity curve of well-tuned Transformers.
Why SSMs might (or might not) replace attention
Strengths:
- Linear scaling in sequence length (training and inference).
- Constant inference memory: no growing KV cache.
- Strong long-range modeling: HiPPO and selective mechanisms.
- Hardware-friendly: dense matrix-vector products, cache-friendly.
Weaknesses or open questions:
- Local copy/lookup tasks that attention nails: SSMs sometimes underperform. Hybrid architectures (next-to-last D3 lesson) address this.
- In-context learning scaling laws: less well-studied than Transformers.
- Less mature tooling: the Transformer ecosystem (HuggingFace, PyTorch primitives, training recipes) is decades ahead.
Mamba and its descendants are viable; whether they replace attention or coexist alongside it is an open question as of 2024.
The relationship to RNNs
SSMs are linear RNNs with a specific parameterization. Compared to LSTM/GRU:
- Linear vs gated nonlinear updates.
- Structured matrices with mathematical properties (HiPPO) vs general gates.
- Parallel training via convolution view vs sequential.
The selective version (Mamba) adds back input-dependence, recovering some of the gating flexibility of LSTM while preserving the parallel training story.
Exercises
Exercise 1. Unroll the discrete SSM recurrence , (with ) and show that the output is a convolution . Give the convolution kernel in terms of .
Solution
Step 1. Apply the recurrence repeatedly from :
Each input enters at step and is propagated forward by applications of .
Step 2. Project to the output with :
Step 3. Re-index with (so , runs from to ):
This is a convolution with kernel . The kernel is fixed (input-independent) for a non-selective SSM, which is exactly why training can run as a single convolution in parallel rather than stepping the recurrence.
Exercise 2. Take the SSM diagonal so with each . Show that the kernel is a sum of decaying geometric modes , and explain what an eigenvalue near 1 versus near 0 does to the model's memory. (This is the behavior the lesson's figure plots.)
Solution
Step 1. With diagonal, . Writing as a row and as a column,
The kernel at lag is a weighted sum of the powers , one geometric sequence per state coordinate.
Step 2. Each mode decays geometrically in the lag because . The decay rate is set by : the half-life of mode is the lag at which , namely .
Step 3. An eigenvalue with close to 1 decays slowly, so its mode keeps a large weight at large lag : that coordinate carries long-range memory, mixing inputs from far in the past into the current output. An eigenvalue close to 0 decays almost immediately (), so its mode forgets after one or two steps and contributes only local context. The spectrum of is the model's memory spectrum, which is what HiPPO initialization is chosen to shape.
Exercise 3. Mamba makes and (and the step ) functions of the input token, calling the result a selective SSM. Explain why this input-dependence breaks the convolution view of Exercise 1, and what the model gives up and gains by accepting that. Contrast the asymptotic inference cost of a selective SSM with softmax attention for generating a sequence of length .
Solution
Step 1. The convolution view of Exercise 1 relied on a fixed kernel , the same at every position. When depend on the input token , the effective kernel changes from step to step, so there is no single to convolve with: the output is no longer a convolution of the input with a position-independent filter.
Step 2. What it gives up: the simple parallel-convolution training path. Mamba recovers parallelism with a hardware-aware parallel-scan kernel instead of an FFT convolution, which is more engineering but still work. What it gains: content-based selection. Like attention's content addressing, the model can choose what to write into and read from its state based on the current token, which fixes copy/lookup tasks that a fixed-kernel SSM handles poorly.
Step 3. Inference cost for length . A selective SSM keeps a fixed-size hidden state and updates it in per token, so generating tokens is time with memory: constant per-token cost, no growing cache. Softmax attention stores a KV cache that grows to length and scores each new query against the whole cache, an per-token cost and total, with cache memory. The SSM's constant per-token cost is the long-context advantage.
The big picture
The next lesson covers another path to sub-quadratic sequence mixing: linear attention, which keeps the QKV machinery but replaces the softmax with a kernel feature map that lets the computation be reordered to linear cost.
References
- [1]Gu, A., Goel, K., & Ré, C. (2022). "Efficiently modeling long sequences with structured state spaces (S4)". ICLR. arXiv:2111.00396. HiPPO-structured SSMs with the convolution/recurrence duality.↩
- [2]Gu, A., & Dao, T. (2023). "Mamba: linear-time sequence modeling with selective state spaces". arXiv:2312.00752. Input-dependent (selective) SSM with a hardware-aware kernel.↩