Skip to content
AI360Xpert
Core ML

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.

A state space model compresses all past inputs into a fixed-size hidden state h that is updated each step using learned matrices A and B; the output is read from the state via C, giving linear-time inference with no attention score matrix
A state space model compresses all past inputs into a fixed-size hidden state h that is updated each step using learned matrices A and B; the output is read from the state via C, giving linear-time inference with no attention score matrix

Why Does This Exist?

The attention complexity problem is real: every token attending to every other token gives O(N2)O(N^2) compute. Linear attention approximates softmax with a kernel to get O(N)O(N), but pays in quality. A completely different family of models — state space models — achieves O(N)O(N) at training and O(1)O(1) 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 hh that is updated according to a transition matrix AA each time a new input xx arrives, plus an input matrix BB that injects the new input into the state, and an output matrix CC that reads the output yy from the state:

ht=Aht1+Bxt,yt=Chth_t = A h_{t-1} + B x_t, \quad y_t = C h_t

The state hh is the model's compressed memory. At inference, generating the next token costs O(d2)O(d^2) — 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 AA, 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 AA determines how the summary decays and evolves; BB determines how new inputs modify it; CC 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 AA and BB are discretised to obtain a discrete-time recurrence using a zero-order hold or bilinear method. The result is a set of discrete matrices Aˉ\bar{A} and Bˉ\bar{B} that depend on a learned step size Δ\Delta:

Aˉ=exp(ΔA),Bˉ=(AˉI)A1BΔB\bar{A} = \exp(\Delta A), \quad \bar{B} = (\bar{A} - I) A^{-1} B \approx \Delta B

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 y=xKy = x * K where KK is the SSM's impulse response (a vector of length NN). Computing a convolution of two NN-vectors costs O(NlogN)O(N \log N) via FFT — competitive with attention for many sequence lengths.

The two views — recurrent for inference, convolutional for training — are mathematically equivalent given the same AA, BB, CC. The model trains efficiently and infers efficiently.

The HiPPO initialisation

The choice of AA matters a lot. Random AA typically leads to vanishing/exploding gradients, just as in RNNs. HiPPO (High-Order Polynomial Projection Operators) initialises AA 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 order

Both 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 AA, BB, CC 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 hh updated per token via h=Ah+Bxh = Ah + Bx; output is y=Chy = Ch.
  • Training uses the convolutional view (O(NlogN)O(N \log N) via FFT); inference uses the recurrent view (O(1)O(1) per step, O(d2)O(d^2) compute).
  • Memory at inference is O(d)O(d) — the state — regardless of how many tokens have been seen.
  • HiPPO initialisation gives AA 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.
  • 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 O(N)O(N), also using a running state, but derived from kernel approximations rather than control theory.
  • Attention Complexity establishes the O(N2)O(N^2) 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.

Related concepts