State Space Models
A state space model compresses the entire past into a fixed-size hidden state that is updated one input at a time, giving linear-time inference without the quadratic score matrix that attention requires.
Why Does This Exist?
The attention complexity problem is real: every token attending to every other token gives compute. Linear attention approximates softmax with a kernel to get , but pays in quality. A completely different family of models — state space models — achieves at training and per step at inference by never computing pairwise scores at all.
SSMs come from control theory, not deep learning. The core idea is a linear dynamical system: a hidden state that is updated according to a transition matrix each time a new input arrives, plus an input matrix that injects the new input into the state, and an output matrix that reads the output from the state:
The state is the model's compressed memory. At inference, generating the next token costs — one matrix multiply to update the state, one to read the output — regardless of how many tokens came before. That's the hardware win: O(1) memory and O(1) compute per step.
Gu et al. formalised this as the Structured State Space Sequence Model (S4) in 2021, showing that with a carefully chosen HiPPO initialisation for , the model could capture long-range dependencies that RNNs and truncated attention both miss.
Think of It Like This
A rolling stock ticker versus a full trade history
A financial dashboard showing a rolling average doesn't store every trade ever made. It maintains a compact running summary — the current mean, variance, trend — and updates that summary as each new trade arrives. The summary has fixed size no matter how many trades have passed. Looking up "the trend" costs one read of the summary, not a scan of the history.
An SSM is that rolling summary, but learned. The state matrix determines how the summary decays and evolves; determines how new inputs modify it; determines what to extract. At inference, generating a token costs one update and one read — no scan.
How It Actually Works
The discretisation step
Continuous-time SSMs are defined with differential equations. To use them on discrete sequences (tokens, not audio samples in continuous time), the continuous matrices and are discretised to obtain a discrete-time recurrence using a zero-order hold or bilinear method. The result is a set of discrete matrices and that depend on a learned step size :
Training efficiency: the convolutional view
During training, you have all tokens at once, so you can compute the SSM as a convolution rather than a recurrence. The state unrolled over time gives a response where is the SSM's impulse response (a vector of length ). Computing a convolution of two -vectors costs via FFT — competitive with attention for many sequence lengths.
The two views — recurrent for inference, convolutional for training — are mathematically equivalent given the same , , . The model trains efficiently and infers efficiently.
The HiPPO initialisation
The choice of matters a lot. Random typically leads to vanishing/exploding gradients, just as in RNNs. HiPPO (High-Order Polynomial Projection Operators) initialises to be a matrix that theoretically compresses the past optimally — projecting the input history onto a family of orthogonal polynomials. In practice this means the model starts with a good inductive bias for long-range dependencies, rather than having to learn that from scratch.
Show Me the Code
Running a tiny SSM recurrence and verifying the output matches the equivalent convolution.
import numpy as np
rng = np.random.default_rng(9)n, d = 16, 4 # sequence length, state dimension
A = 0.9 * np.eye(d) # simple stable diagonal AB = rng.standard_normal((d, 1)) * 0.1C = rng.standard_normal((1, d)) * 0.1x = rng.standard_normal((n, 1)) # input sequence
# Recurrent form (inference mode)h = np.zeros(d)y_rec = np.zeros(n)for t in range(n): h = A @ h + (B @ x[t]).squeeze() y_rec[t] = (C @ h).squeeze()
# Convolutional form: build impulse response K, then convolveK = np.zeros(n)h_imp = np.zeros(d)B_flat = B.squeeze()for t in range(n): h_imp = A @ h_imp + B_flat K[t] = (C @ h_imp).squeeze()
# Linear convolution (truncated to n outputs)y_conv = np.convolve(x.squeeze(), K)[:n]
print("Max diff recurrent vs conv:", np.abs(y_rec - y_conv).max())# ~1e-14 — same computation, different execution orderBoth views produce the same output to floating-point precision.
Watch Out For
Assuming SSMs are strictly better than attention
SSMs trade the quadratic cost of attention for a fixed-size state, which is a lossy compression. When a task requires precise recall of a specific token from far back in the sequence — something attention can do exactly via its score matrix — an SSM's fixed state may not have stored the right information. On tasks with dense, diffuse long-range dependencies, SSMs often match or beat attention. On tasks requiring precise long-range recall, attention tends to win.
Confusing the recurrent and convolutional forms with different models
The recurrent form (used at inference) and the convolutional form (used at training) are two execution strategies for the same mathematical model. The weights , , are the same. Switching between modes doesn't change the model — it changes how you compute its output. Both paths must agree; if they don't, there's a numerical or implementation bug.
The Quick Version
- An SSM maintains a fixed-size hidden state updated per token via ; output is .
- Training uses the convolutional view ( via FFT); inference uses the recurrent view ( per step, compute).
- Memory at inference is — the state — regardless of how many tokens have been seen.
- HiPPO initialisation gives an inductive bias toward remembering long-range structure from the start.
- SSMs trade exact recall (attention's strength) for constant inference cost; Mamba adds input-dependent selection to improve recall quality.
What to Read Next
- Mamba extends SSMs with input-dependent selection, closing much of the quality gap with attention on language tasks.
- Hybrid Attention–SSM Architectures interleave SSM and attention layers to get both properties.
- Linear Attention is the attention-family analogue — also , also using a running state, but derived from kernel approximations rather than control theory.
- Attention Complexity establishes the cost that SSMs sidestep entirely.
- Vanilla RNN is the simpler recurrent predecessor — SSMs share the fixed-state structure but solve the vanishing gradient problem via structured initialisation.