State-Space Models and S4
S4 revived a decades-old control-theory idea, the linear state-space model, and showed it can be trained as a neural network layer that processes very long sequences without the quadratic cost of attention or the vanishing gradients of a recurrent network.
Prerequisites: Backpropagation Through Time, Convolutional Neural Networks
Attention scales quadratically with sequence length — double the sequence and the compute needed roughly quadruples — which makes it expensive on sequences of tens of thousands of steps, like tick-level order flow over a full session. Recurrent networks scale linearly, which is better, but their gradients through Backpropagation Through Time decay over long spans, so they struggle to actually use information from thousands of steps back even when the compute is cheap enough to try. S4 (Structured State Space Sequence model) asks whether there's a way to process very long sequences that is both linear in cost like an RNN and free of the gradient decay that cripples RNNs over long spans. Its answer borrows an idea from control engineering that predates deep learning by half a century.
The analogy: a thermostat with memory of the whole day
A building's temperature at any moment isn't a random number — it's the outcome of a continuous underlying process: heat leaking in and out, the heating system responding, all evolving smoothly over time according to a small number of physical rules. A control engineer models this with a state: a compact summary (say, "current temperature and its rate of change") that evolves according to fixed equations and that, together with external inputs (heater on/off), fully determines the future. Crucially, you don't need every past reading in memory to predict tomorrow — the current state already summarizes everything relevant about the past that matters for what happens next. A state-space model treats a data sequence the same way: a hidden state evolves smoothly according to a small, fixed set of linear equations, and the output at each moment is read off that state. S4's contribution was finding a way to parameterize and initialize those equations so the state can actually carry information across thousands of steps without decaying — and to compute the whole thing efficiently.
The maths
A continuous-time linear state-space model describes a hidden state evolving under an input :
In words: the state's rate of change is a fixed linear combination of its current value (via matrix ) plus the current input (via ); the output is a fixed linear readout of the state (via ), plus a direct pass-through of the input (via , often set to zero). , , , are the model's learned parameters — a small, fixed set, regardless of how long the sequence is.
For a discrete sequence (which is what any real dataset is), this gets discretized with a step size into a recurrence:
In words: this looks exactly like an RNN's update rule — new state from old state plus new input — but with one crucial difference: and come from a fixed, structured continuous-time system rather than being an arbitrary learned matrix passed through a nonlinearity. That structure is what S4 exploits.
Because the recurrence is linear (no or sigmoid squashing it at each step, unlike an RNN or LSTM), it can be unrolled into an explicit formula for in terms of all past inputs:
In words: the output at step is a weighted sum of every past input , where the weight on an input from steps ago is — a single fixed formula, not a chain of separate multiplications each passed through a saturating nonlinearity. This is exactly a convolution, with a kernel of length built directly from , , . S4's key engineering insight is choosing a special structured form for (derived from a mathematical object called the HiPPO matrix, designed specifically to compress a long history into a fixed-size state with minimal information loss) that makes this convolution kernel computable efficiently — in roughly for a sequence of length , rather than the that plain attention costs.
Worked example 1: unrolling a tiny state-space recurrence by hand
Take a 1-dimensional toy system with , , , and inputs , starting from .
Now verify the convolution formula gives the same directly, without stepping through :
Both routes give . The recurrence and the convolution are two ways of computing the identical answer — the recurrence is cheap for generating one step at a time (inference), the convolution form is cheap for processing a whole sequence in parallel (training), and S4's engineering is precisely about making that convolution kernel fast to compute for very long even though is not this simple a scalar in practice.
Worked example 2: why the decay rate doesn't compound the way an RNN's does
In worked example 1, the weight on an input from steps back is — decaying, but not catastrophically:
Compare this to the earlier RNN vanishing-gradient calculation (in Backpropagation Through Time) where a chain of -saturated Jacobians around 0.6 gave — roughly 300 times smaller. The gap exists because S4's comes from a structured, carefully-designed continuous system (via the HiPPO initialization) chosen specifically so its eigenvalues support slow decay across long spans, rather than from an arbitrary trained matrix squashed through a saturating nonlinearity at every step. This is a deliberate design choice, not a coincidence — the entire point of the HiPPO-based initialization is to make long-range memory the default behavior of the untrained model, something gradient descent then only has to preserve rather than discover from scratch.
Picture the kernel weight as a function of distance back in time — it settles toward zero but far more gently than a saturating-nonlinearity chain, similar to how a running average's envelope narrows gradually rather than collapsing sharply; that gentle decay is what lets an S4 layer keep a usable signal from thousands of steps back.
S4 replaces attention's expensive "compare every pair of positions" and an RNN's fragile "chain a nonlinearity at every step" with a linear recurrence whose transition matrix is specially structured so it can be (a) unrolled into a single convolution kernel for fast, parallel training, and (b) initialized to decay gently, preserving long-range signal by construction rather than by luck.
What this means in practice
State-space layers are a strong fit for very long, densely-sampled sequences where attention's quadratic cost is genuinely prohibitive — full-session tick data, long macro time series, or any pipeline where "look back thousands of steps efficiently" is a hard requirement rather than a nice-to-have. Because the underlying system is linear, an S4 layer alone can't represent input-dependent, content-based routing the way attention or a gated RNN can — it treats every sequence with the same fixed dynamics regardless of what's actually in it, which is precisely the limitation Mamba and Selective State Spaces was built to address.
The trap: assuming a state-space model is "just an RNN with extra math." The practical payoff is specifically that its linearity permits two equivalent computation modes — recurrent (cheap, sequential, good for streaming inference one new tick at a time) and convolutional (parallel, good for training on a full historical batch at once) — and switching between them is what makes S4 both trainable at scale and deployable in real time. Naively implementing only the recurrent form for training throws away the entire efficiency advantage that makes S4 practical on long sequences in the first place; naively implementing only the convolutional form makes streaming, step-by-step inference on live data awkward. A correct implementation needs both.
Related concepts
Practice in interviews
Further reading
- Gu, Goel & Ré, Efficiently Modeling Long Sequences with Structured State Spaces (2021)
- Kalman, A New Approach to Linear Filtering and Prediction Problems (1960)