Search

Search papers and lessons

State Space Model

Level 4 · 15 min read

A state space model turns a signal into a hidden state that evolves by a fixed linear rule, and a sequence model built on it can process very long inputs in time that grows only linearly with length.

On this page

  • Colour = topic mix
  • Calculus
  • Linear algebra
  • Discrete math
  • Architectures
  • Sign in to see your progress

TL;DR

A state space model describes a system by a hidden state that changes over time by a fixed linear rule, plus a readout that turns that state into an output. The state is a short memory of everything the system has seen so far. Once the rule is written as a discrete recurrence, the model can process a sequence one step at a time or all at once as a convolution.

Modern sequence models, such as S4 and Mamba, use this structure as a long-range memory that is cheaper than attention when the sequence gets long. The price is that the basic version applies the same rule at every step, so it cannot decide what to remember based on the input.

Where you'll see it: in the S4 and Mamba families of language and audio models, and in the Kalman filter that tracks the position of a moving object from noisy readings.

Intuition

A notebook that gets rewritten at every tick

Picture a sound engineer who keeps a short notebook while a song plays. At each moment she reads the new sample, updates a few numbers in the notebook, and then reads off a value from the notebook as her output. The notebook is the state. It is much smaller than the whole song, but it has to carry enough to predict what comes next. The update rule is the same at every moment, and the readout is the same at every moment. That is the whole model.

The reason to want this structure is memory. A recurrent network also keeps a state, but its update is a nonlinear function that is hard to analyse and slow to train on long inputs. A state space model keeps the update linear. A linear update can be unrolled in closed form, and the closed form can be computed in parallel. That is the source of the efficiency claims in the S4 and Mamba papers.

The idea is older than deep learning. Control engineers have used state space form since the early 1960s. Kalman's 1960 filter, which fuses noisy measurements into a running estimate of position and velocity, is a state space model with Gaussian noise on top.

The continuous picture first

A continuous state space model maps an input signal x(t)x(t) to an output signal y(t)y(t) through a hidden state h(t)h(t) that is a vector of NN numbers:

h′(t)=A h(t)+B x(t),y(t)=C h(t)h'(t) = \mathbf{A}\, h(t) + \mathbf{B}\, x(t), \qquad y(t) = \mathbf{C}\, h(t)

Here h′(t)h'(t) is the rate of change of the state, A\mathbf{A} is an N×NN \times N matrix saying how the state evolves on its own, B\mathbf{B} says how the input pushes the state, and C\mathbf{C} says how the state is read out. This is a linear ordinary differential equation with an input forcing term, which is why the ODE lesson is the first prerequisite.

The plot below shows the simplest case, one state with B=1B = 1 and A=−1A = -1 (solid) or A=−2A = -2 (dashed), when the input is a unit step that switches on at time zero. The state rises toward B/(−A)B/(-A), which is 1 and 0.5 here, and levels off. The speed at which it gets there is set by AA.

01234500.20.40.60.81time tstate h(t)
  • state h(t), A = -1
  • state h(t), A = -2
With a unit step input and B = 1, the state rises toward B/(-A). A more negative A makes it settle faster, and at a lower level.

Why we discretise

Computers see sequences as samples, not as continuous curves. Text tokens and audio samples arrive one step at a time. So the continuous rule has to be turned into a rule over steps. Writing the step size as Δ\Delta, a common recipe is the zero-order hold (ZOH), which assumes the input stays constant between samples:

A‾=exp⁡(ΔA),B‾=(ΔA)−1(exp⁡(ΔA)−I) ΔB\overline{\mathbf{A}} = \exp(\Delta \mathbf{A}), \qquad \overline{\mathbf{B}} = (\Delta \mathbf{A})^{-1}\bigl(\exp(\Delta \mathbf{A}) - \mathbf{I}\bigr)\,\Delta \mathbf{B}

Once you have A‾\overline{\mathbf{A}} and B‾\overline{\mathbf{B}}, the model is a discrete recurrence:

ht=A‾ ht−1+B‾ xt,yt=C hth_t = \overline{\mathbf{A}}\, h_{t-1} + \overline{\mathbf{B}}\, x_t, \qquad y_t = \mathbf{C}\, h_t

The notation A‾\overline{\mathbf{A}} with a bar means "the discrete version of A\mathbf{A}". The step size Δ\Delta has a physical meaning: a large Δ\Delta means each sample covers a long stretch of the continuous signal.

flowchart LR
  P[Continuous parameters A, B, C and step size Delta] --> D[Discretise: A-bar, B-bar]
  D --> R[Linear recurrence over steps]
  D --> K[Convolution kernel built from C, A-bar, B-bar]
  R --> Y1[Output y_t, one step at a time]
  K --> Y2[Output y, whole sequence at once]

The math

Definition

Definition: State space model

A discrete state space model is a map from an input sequence x1,…,xLx_1, \ldots, x_L to an output sequence y1,…,yLy_1, \ldots, y_L, defined by a hidden state ht∈RNh_t \in \mathbb{R}^N and fixed matrices A‾∈RN×N\overline{\mathbf{A}} \in \mathbb{R}^{N \times N}, B‾∈RN×1\overline{\mathbf{B}} \in \mathbb{R}^{N \times 1} and C∈R1×N\mathbf{C} \in \mathbb{R}^{1 \times N}, by the recurrence ht=A‾ht−1+B‾xth_t = \overline{\mathbf{A}} h_{t-1} + \overline{\mathbf{B}} x_t with h0=0h_0 = 0, and the readout yt=Chty_t = \mathbf{C} h_t.

Here LL is the sequence length, NN is the state size, and h0=0h_0 = 0 means the state starts empty. The symbols A‾\overline{\mathbf{A}}, B‾\overline{\mathbf{B}} and C\mathbf{C} are the same for every step, which is what makes the model linear time invariant (LTI).

Unrolling the recurrence

Start from h0=0h_0 = 0 and apply the recurrence step by step:

h1=B‾ x1h2=A‾ B‾ x1+B‾ x2h3=A‾2B‾ x1+A‾ B‾ x2+B‾ x3\begin{aligned} h_1 &= \overline{\mathbf{B}}\, x_1 \\ h_2 &= \overline{\mathbf{A}}\, \overline{\mathbf{B}}\, x_1 + \overline{\mathbf{B}}\, x_2 \\ h_3 &= \overline{\mathbf{A}}^2 \overline{\mathbf{B}}\, x_1 + \overline{\mathbf{A}}\, \overline{\mathbf{B}}\, x_2 + \overline{\mathbf{B}}\, x_3 \end{aligned}

Multiply by C\mathbf{C} to get the outputs. The pattern is that yty_t is a weighted sum of all past inputs, where the weight on xt−kx_{t-k} is C A‾k B‾\mathbf{C}\,\overline{\mathbf{A}}^{k}\,\overline{\mathbf{B}}. Write this weight as a kernel K‾k=C A‾k B‾\overline{\mathbf{K}}_k = \mathbf{C}\,\overline{\mathbf{A}}^{k}\,\overline{\mathbf{B}}. Then the whole output is a convolution:

yt=∑k=0t−1K‾k xt−ky_t = \sum_{k=0}^{t-1} \overline{\mathbf{K}}_k\, x_{t-k}

This is the same kernel structure as the ordinary Convolution of signal processing, with a kernel that is fixed and does not depend on the input. Two computation modes follow. The recurrence runs one step at a time and needs only the current state, which is cheap at inference. The convolution computes every output at once with a fast transform, which suits training on whole sequences. This is the dual view the Mamba paper describes: the model can be computed as a linear recurrence or as a global convolution, and the choice is a matter of efficiency.

Scalar example of the discretisation

Take N=1N = 1, A=−1A = -1, B=1B = 1, C=1C = 1, and Δ=0.5\Delta = 0.5.

  1. Compute A‾=exp⁡(ΔA)=exp⁡(−0.5)=0.60653\overline{A} = \exp(\Delta A) = \exp(-0.5) = 0.60653.
  2. Compute B‾=(ΔA)−1(exp⁡(ΔA)−1) ΔB=(−2)(0.60653−1)(0.5)=0.39347\overline{B} = (\Delta A)^{-1}(\exp(\Delta A) - 1)\,\Delta B = (-2)(0.60653 - 1)(0.5) = 0.39347.
  3. Check against the integral: B‾\overline{B} should equal ∫00.5e−s ds=1−e−0.5=0.39347\int_0^{0.5} e^{-s}\,ds = 1 - e^{-0.5} = 0.39347. It does.
  4. Feed a constant input xt=1x_t = 1 from h0=0h_0 = 0.
  5. h1=0.39347h_1 = 0.39347, and h2=0.60653×0.39347+0.39347=0.63212h_2 = 0.60653 \times 0.39347 + 0.39347 = 0.63212.
  6. The closed form 1−e−0.5t1 - e^{-0.5t} gives h2=1−e−1=0.63212h_2 = 1 - e^{-1} = 0.63212. Matching.

The interpretation: the discrete recurrence reproduces the continuous system exactly at the sample times, provided the input is held constant between samples. That is the assumption ZOH makes.

Kernel example

Take A‾=0.5\overline{A} = 0.5, B‾=1\overline{B} = 1, C=2C = 2, and a single impulse input x=(1,0,0,0)x = (1, 0, 0, 0).

  1. The kernel is K‾k=C A‾k B‾=2×0.5k\overline{K}_k = C\,\overline{A}^k\,\overline{B} = 2 \times 0.5^k, so K‾=(2,1,0.5,0.25)\overline{K} = (2, 1, 0.5, 0.25).
  2. Run the recurrence: h1=1h_1 = 1, h2=0.5h_2 = 0.5, h3=0.25h_3 = 0.25, h4=0.125h_4 = 0.125.
  3. Read out: y=Ch=(2,1,0.5,0.25)y = C h = (2, 1, 0.5, 0.25).
  4. The convolution y=x∗K‾y = x \ast \overline{K} with this impulse input gives (2,1,0.5,0.25)(2, 1, 0.5, 0.25) as well.

The two modes agree. The impulse response is the kernel itself, which is why the kernel is the model's memory profile.

Cost of the full state

In the Mamba paper's notation, each channel of a sequence has a state of NN numbers. With DD channels, batch size BB and length LL, the full state over the sequence has BLDNBLDN entries. A realistic setting for illustration: B=1B = 1, L=2048L = 2048, D=1024D = 1024, N=16N = 16. Then BLDN=1×2048×1024×16=33,554,432BLDN = 1 \times 2048 \times 1024 \times 16 = 33{,}554{,}432 numbers. Materialising that in fast GPU memory is the bottleneck the paper's hardware-aware scan is designed to avoid. The formula is from the paper; the numbers are an illustration of it, not a measurement.

Worked examples

Example
Step response of a damped state

A system with A‾=0.8\overline{A} = 0.8, B‾=0.2\overline{B} = 0.2, C=1C = 1 receives input xt=1x_t = 1 for every tt, starting from h0=0h_0 = 0.

  1. h1=0.8×0+0.2×1=0.2h_1 = 0.8 \times 0 + 0.2 \times 1 = 0.2.
  2. h2=0.8×0.2+0.2=0.36h_2 = 0.8 \times 0.2 + 0.2 = 0.36.
  3. h3=0.8×0.36+0.2=0.488h_3 = 0.8 \times 0.36 + 0.2 = 0.488.
  4. h4=0.8×0.488+0.2=0.5904h_4 = 0.8 \times 0.488 + 0.2 = 0.5904.
  5. The steady state is h∗=B‾/(1−A‾)=0.2/0.2=1h^* = \overline{B} / (1 - \overline{A}) = 0.2 / 0.2 = 1. The state approaches 1 but more slowly each step.

Interpretation: the state is a leaky running sum. Its memory of old inputs decays by a factor of 0.80.8 each step, so inputs more than about ten steps back contribute little.

Example
A small sequence model with two state dimensions

Let N=2N = 2, A‾=diag(0.5,0.9)\overline{\mathbf{A}} = \mathrm{diag}(0.5, 0.9), B‾=(1,1)⊤\overline{\mathbf{B}} = (1, 1)^\top, and C=(1,1)\mathbf{C} = (1, 1). This means one state dimension forgets quickly and the other slowly.

  1. Impulse input x=(1,0,0)x = (1, 0, 0).
  2. The kernel entry at lag kk is C A‾k B‾=0.5k+0.9k\mathbf{C}\,\overline{\mathbf{A}}^k\,\overline{\mathbf{B}} = 0.5^k + 0.9^k.
  3. At lag 0 that is 1+1=21 + 1 = 2. At lag 1 it is 0.5+0.9=1.40.5 + 0.9 = 1.4. At lag 2 it is 0.25+0.81=1.060.25 + 0.81 = 1.06.
  4. The output is y=(2,1.4,1.06)y = (2, 1.4, 1.06).

Interpretation: the fast dimension dominates the first step, and the slow dimension keeps the tail alive. Real models use NN in the tens and learn the entries of A‾\overline{\mathbf{A}}, so a mix of time scales is learned rather than hand-picked.

Tradeoffs and failure modes

The basic LTI model is fast and well understood, but it has a specific weakness. Because its parameters are fixed, it treats every input token the same way. It cannot choose to ignore filler and keep a key fact. The Mamba paper calls this a fundamental limitation of LTI models and makes the parameters depend on the input to fix it.

SituationWhat goes wrongBetter choice
Long continuous signals such as audio or sensor dataLTI models do well here; the continuous prior fitsStay with LTI state space models and the convolution mode
Discrete data such as text or DNA, where the model must decide what to keepA fixed kernel cannot select content, so recall of a specific earlier token degradesA selective SSM such as [[selective-state-space-model
Short sequences where a few thousand tokens are enoughOverhead of discretisation and scanning buys nothingA Transformer with ordinary attention
Autoregressive inference with a long promptAttention caches grow with lengthA recurrent form of the SSM, which keeps a fixed-size state
Choosing a step size Δ\Delta without careToo large and the discrete model misses fast dynamics; too small and training wastes stepsLearn Δ\Delta per channel and check with a known impulse response

Other failure modes: the eigenvalues of A‾\overline{\mathbf{A}} must stay inside the unit circle for the state not to blow up, so parameterisations that enforce stability are part of practical work. Initialisation of A\mathbf{A} matters a lot; poorly chosen values give models that forget too fast or too slowly. The stability condition is a direct use of eigenvalues.

History

The state space form is much older than deep learning. Rudolf Kalman's 1960 paper gave a recursive filter for linear systems with noise, and the state space description it used became standard in control engineering during the 1960s. It solved the problem of estimating a hidden state from noisy measurements, which is still the core of navigation and tracking.

Machine learning took up the form slowly. Linear recurrent units and older state space ideas appeared in signal processing and in some sequence models, but they did not match transformers on language. In 2021 Gu, Goel and Re introduced the structured state space sequence model, S4, which parameterised A\mathbf{A} with a special structure and computed the convolution efficiently. It showed strong results on long-range benchmarks. In late 2023 Gu and Dao introduced Mamba, which made the parameters input-dependent, gave up the fixed convolution for those layers, and used a hardware-aware scan to keep the cost linear in sequence length. It caught on because the paper reported transformer-quality language modelling from a model whose cost grows linearly with sequence length.

Check yourself

QuestionRecall: what does the matrix A‾\overline{\mathbf{A}} control in the recurrence ht=A‾ht−1+B‾xth_t = \overline{\mathbf{A}} h_{t-1} + \overline{\mathbf{B}} x_t?Show answer

It controls how the existing state carries over from one step to the next. It is the memory part of the update. B‾\overline{\mathbf{B}} controls how much the new input is written into the state, and C\mathbf{C} controls how the state is read out. A‾\overline{\mathbf{A}} alone decides how quickly old information fades.

QuestionComputation: with A‾=0.5\overline{A} = 0.5, B‾=1\overline{B} = 1, C=3C = 3, and input x=(1,1,0)x = (1, 1, 0), what is y3y_3?Show answer

Use yt=∑kK‾kxt−ky_t = \sum_k \overline{K}_k x_{t-k} with K‾k=3×0.5k\overline{K}_k = 3 \times 0.5^k. For t=3t = 3 the terms are K‾0x3+K‾1x2+K‾2x1=3×0+1.5×1+0.75×1=2.25\overline{K}_0 x_3 + \overline{K}_1 x_2 + \overline{K}_2 x_1 = 3 \times 0 + 1.5 \times 1 + 0.75 \times 1 = 2.25. So y3=2.25y_3 = 2.25. Checking with the recurrence: h1=1h_1 = 1, h2=0.5+1=1.5h_2 = 0.5 + 1 = 1.5, h3=0.75+0=0.75h_3 = 0.75 + 0 = 0.75, so y3=3×0.75=2.25y_3 = 3 \times 0.75 = 2.25.

QuestionWhy: why does the ZOH discretisation use exp⁡(ΔA)\exp(\Delta \mathbf{A}) rather than just 1+ΔA1 + \Delta \mathbf{A}?Show answer

The exact solution of the continuous system over a step of length Δ\Delta, with the input held constant, multiplies the state by exp⁡(ΔA)\exp(\Delta \mathbf{A}). The simpler 1+ΔA1 + \Delta \mathbf{A} is only the first-order approximation of that exponential. It drifts for large Δ\Delta or for large eigenvalues of A\mathbf{A}, and the exact form keeps the discrete model faithful to the continuous one.

QuestionApplied: a team wants a model that must recall a fact stated 5,000 tokens earlier in a text document. Would a fixed-parameter LTI state space model be a good choice, and why?Show answer

Probably not on its own. An LTI model applies the same kernel regardless of content, so it cannot decide that a particular token is worth keeping. It can carry information over long distances in principle, but it cannot select the relevant token. A selective SSM, which makes the update depend on the input, or a hybrid with attention is a better fit. The team should test recall on a retrieval task before committing.

Sources