AI State of the Art
SOTA/Post-Transformer Architectures/28 min
Section 65

State Space Models: S4

Listen as a podcast →

The problem with really long sequences

Transformers are brilliant, but they have a structural flaw: their memory and compute cost explodes quadratically with sequence length. Doubling the length quadruples the cost. For a 16,000-token sequence, it's 256 times more expensive than a 1,000-token sequence.

This isn't a problem when processing tweets or paragraphs. But take a 30-second audio file, at 16,000 samples per second, that's 480,000 data points. Or a genomic sequence of a few million bases. Or even a 100-page legal document. The Transformer, with its quadratic attention, is simply out of the picture.

That's exactly the problem S4 (Structured State Space for Sequences) solves, and its solution came from an unexpected place.

Efficiently Modeling Long Sequences with Structured State Spaces

The inspiration: dynamical systems

S4 draws from a branch of applied mathematics that has nothing to do with natural language processing: control theory, the field behind flying an aircraft or regulating a thermostat. The basic idea is intuitive. When you listen to music, your brain doesn't memorize every millisecond from the start of the song. It maintains an internal state (a compressed summary of what it has heard) and this state evolves continuously as notes come in.

That's exactly the logic of State Space Models (SSM): a system that maintains a hidden state h(t) (a vector summarizing everything the model has retained so far) and updates it continuously based on the input x(t). You don't need to know control theory to understand: an SSM is simply an update rule that says "here's how my internal summary changes when I receive a new input, and here's how I produce an output from this summary."

The fundamental equations

A continuous SSM is defined with four matrices (A, B, C, D):

h′(t)=A⋅h(t)+B⋅x(t)(how the state evolves)y(t)=C⋅h(t)+D⋅x(t)(how the state produces the output)\begin{aligned} h'(t) &= A \cdot h(t) + B \cdot x(t) & \text{(how the state evolves)} \\ y(t) &= C \cdot h(t) + D \cdot x(t) & \text{(how the state produces the output)} \end{aligned}

In plain terms, each matrix has a specific role:

  • A: governs how the internal state evolves over time, it determines what the model retains from the past and how quickly it "forgets." It's the heart of the system.
  • B: controls how the current input influences the state, it's the input gate, deciding how much of each new token is absorbed into internal memory.
  • C: controls how the state is read to produce the output, it's the read gate, extracting useful information from the internal summary.
  • D: a shortcut that sends the input straight to the output without passing through the state, a bypass road around the system. Often set to zero or ignored.
The three pillars of S4: continuous state space, long-range dependencies via the HiPPO matrix, fast discrete representations
The three pillars of S4: a continuous system, a structured A matrix for long range (HiPPO), and fast discrete computationSource : Hazy Research, Stanford (S4 authors' blog)

From continuous to discrete: processing text

The equations above describe a continuous system, time flows smoothly, like an audio signal. But text is a discrete sequence: one token after another, not a continuous stream. So we need to convert the continuous system into a discrete one that advances in steps.

Why discretize?

Imagine a thermometer that measures temperature continuously. If you only record the value every 5 minutes, you've discretized the signal. The time step Δ\Delta (delta) controls the fineness of this conversion: a small Δ\Delta captures rapid variations, a large Δ\Delta only sees slow trends.

The bilinear method (Tustin)

To convert the continuous matrices AA and BB into their discrete versions Aˉ\bar{A} and Bˉ\bar{B}, S4 uses the bilinear method, a classic signal processing technique that preserves system stability well:

Aˉ=(I−Δ2A)−1(I+Δ2A)Bˉ=(I−Δ2A)−1Δ⋅B\begin{aligned} \bar{A} &= \left(I - \frac{\Delta}{2} A\right)^{-1} \left(I + \frac{\Delta}{2} A\right) \\ \bar{B} &= \left(I - \frac{\Delta}{2} A\right)^{-1} \Delta \cdot B \end{aligned}

Let's decode Aˉ\bar{A} term by term:

  • II is the identity matrix, the neutral starting point (change nothing)
  • Δ2A\frac{\Delta}{2} A is the matrix AA (state evolution) scaled to half the time step
  • (I+Δ2A)(I + \frac{\Delta}{2} A) says "take the current state and advance it by half a step"
  • (I−Δ2A)−1(I - \frac{\Delta}{2} A)^{-1} compensates by "stepping back" half a step, it's this forward/backward combination that makes the bilinear method accurate and stable
  • The result Aˉ\bar{A} is the discrete version of AA: it says exactly how the state transforms from one token to the next

For Bˉ\bar{B}, it's simpler: take BB (how the input influences the state), multiply it by Δ\Delta (to account for the time step), and apply the same stability correction.

The discrete system: a recurrent network

Once discretized, the SSM works exactly like a recurrent neural network (RNN): a network that reads a sequence one element at a time while keeping a memory of what it has already seen. At each token kk, the state is updated:

hk=Aˉ⋅hk−1+Bˉ⋅xkyk=C⋅hk+D⋅xk\begin{aligned} h_k &= \bar{A} \cdot h_{k-1} + \bar{B} \cdot x_k \\ y_k &= C \cdot h_k + D \cdot x_k \end{aligned}

First line: the new state hkh_k is a blend of what was already retained (Aˉ⋅hk−1\bar{A} \cdot h_{k-1}, the previous state transformed by Aˉ\bar{A}) and what's arriving now (Bˉ⋅xk\bar{B} \cdot x_k, the current token filtered by Bˉ\bar{B}).

Second line: the output yky_k is read from the state (C⋅hkC \cdot h_k) with optionally a direct shortcut from the input (D⋅xkD \cdot x_k).

It's a simple recurrence, but what makes S4 special is that it can also be computed in a completely different way: as a convolution.

The duality: recurrence and convolution

SSMs have a remarkable property: they can be computed in two mathematically equivalent ways, which give exactly the same result but do not cost the same.

In recurrence mode, one token at a time: ideal when the model is answering, since the cost per token does not depend on how much has already been read and the memory stays constant.

In convolution mode, the whole sequence at once: ideal during training, where you already have the entire text and want to keep every processor busy in parallel.

That is the SSM advantage: the efficiency of a recurrent network when answering, and the parallelism of a Transformer when learning, without having to choose.

Convolution mode: why it matters

By unrolling the recurrence, you can pre-compute a convolution kernel of length L, a filter, in other words: a list of coefficients saying how much weight to give each offset into the past:

Kˉ=[CBˉ,  CAˉBˉ,  CAˉ2Bˉ,  …,  CAˉL−1Bˉ]\bar{K} = \left[C\bar{B},\; C\bar{A}\bar{B},\; C\bar{A}^2\bar{B},\; \ldots,\; C\bar{A}^{L-1}\bar{B}\right]

The output is obtained by sliding that filter across the whole input, which is what a convolution is. Done naively, the sliding is expensive: for each of the L positions you have to combine L coefficients, so on the order of L × L operations. For 100,000 tokens that is ten billion operations, and nothing has been gained over attention.

The Fast Fourier Transform

This is where the Fast Fourier Transform comes in, almost always abbreviated FFT. It is worth two minutes, because it is what makes S4 cheap.

A signal can be described in two ways. The first is in terms of time: its value at each instant, point by point. The second is in terms of frequency: which elementary vibrations the signal is made of, and how strong each one is. For a piano chord, the first description gives the shape of the wave at every microsecond; the second simply says "a C, an E, a G, at these intensities."

Both descriptions hold the same information. But an operation that is painful in one becomes trivial in the other: sliding a filter across an entire signal is, in the world of frequencies, a plain frequency-by-frequency multiplication. No sliding, no combining, you multiply two lists term by term.

The FFT is the algorithm that travels between the two worlds, and does it fast. So the full recipe becomes: take the input and the filter into the frequency world, multiply them, come back. The cost collapses.

What the detour buys, in numbers

A word on the notation. Saying a computation costs O(L²) means the time grows like the square of the length: double the sequence and you quadruple the work. O(L log L) grows barely faster than the length itself: double the sequence and you roughly double the work.

For a 100,000-token sequence, going from L² to L log L divides the number of operations by about 6,000. That is the difference between an impossible computation and a routine one, and it is what lets S4 handle an audio file or a genome where quadratic attention gives up.

The three equivalent views of an SSM: continuous, recurrent and convolutional
One system, three views: continuous (theory), recurrent (token-by-token inference), convolutional (parallel training)Source : charleneleong-ai.github.io

The real challenge of S4: the matrix A

Having these equations is the starting point. Making them work in practice on long sequences is the real challenge. The central problem is the matrix A: when computing the convolution kernel, you take successive powers of A (A, A^2, A^3, ..., A^L). If A is poorly initialized, these powers explode to infinity or collapse to zero exponentially fast. In both cases, the model captures no long-range dependencies.

The HiPPO solution: a mathematically grounded initialization

S4's major intellectual contribution is the HiPPO matrix (High-Order Polynomial Projection Operator). This is not an arbitrary choice or an empirical recipe, it is a matrix derived from polynomial approximation theory.

The idea: we want the state h(t) to contain, at all times, the best possible approximation of the entire input history. HiPPO constructs a matrix A that projects this history onto a basis of Legendre polynomials, mathematical functions optimal for approximation. Each component of the state h encodes a coefficient of this polynomial approximation, capturing the input signal at increasingly fine resolution.

HiPPO: the theoretical foundation of S4

The HiPPO-LegS matrix guarantees that the hidden state is an optimal approximation of the history over a temporal window. Unlike classic recurrent networks, LSTMs and GRUs, two gated architectures designed in the 1990s and 2010s to hold on to information longer, which forget the past exponentially, unable to capture dependencies beyond a few hundred steps, S4 with HiPPO can capture dependencies over thousands of time steps. It's this solid theoretical foundation that allows S4 to succeed where RNNs have failed for years.

Making the computation practical: the diagonal route

The HiPPO matrix is dense: none of its elements is zero. That is what makes it expensive, because raising a dense matrix to successive powers takes an enormous number of multiplications. For this S4 introduces the NPLR parameterization (Normal Plus Low-Rank), which exploits HiPPO's particular shape to speed the kernel computation up. Effective, but hard to implement.

Later work (S4D and DSS (Diagonal State Spaces)) showed that a radical simplification works almost as well: making the matrix A diagonal.

What a diagonal matrix is, and why it changes everything

A matrix, applied to a vector, mixes its components: every output value depends on every input value. It is a telephone switchboard where each line can talk to all the others.

A diagonal matrix is a special case: all its elements are zero except those on the diagonal. It mixes nothing. Each component of the state evolves on its own account, simply multiplied by its own number:

A=diag(λ1,λ2,…,λN)A = \text{diag}(\lambda_1, \lambda_2, \ldots, \lambda_N)

Instead of a switchboard, you have N separate wires that never cross. And that has two decisive consequences.

Powers become free. Recall that the convolution kernel requires computing A, A², A³, and so on. Multiplying two dense matrices of size N takes on the order of N³ operations, repeated L times. With a diagonal matrix there is no matrix multiplication left at all: raising to the power k means raising each number on the diagonal to the power k, that is, N independent operations. A heavy computation becomes an immediate one.

The system decomposes. A state of dimension N governed by a diagonal matrix is no longer one system with N entangled dimensions: it is N little one-dimensional systems, independent of each other. So they can all be computed in parallel, and each can be reasoned about separately.

What you lose, what you keep

By forbidding any mixing between components of the state, a diagonal matrix is in principle less expressive than a dense one: it represents a narrower family of behaviours.

The empirical result, and this is what makes the work interesting, is that the loss is negligible on long sequences. What mattered in HiPPO was not the mixing between components, but the choice of the numbers governing how fast each one forgets. A diagonal matrix keeps that choice entirely.

Hence the trade-off adopted by almost everything that followed in this lineage, Mamba included: keep HiPPO's intuition, drop its complexity.

Results: S4 on long sequences

The Long Range Arena benchmark

This benchmark was created specifically to test models' ability to capture long-range dependencies. Before S4, no model achieved convincing results, Transformers and RNNs alike plateaued.

Long Range Arena results

S4 achieves ~86% average accuracy on the Long Range Arena, compared to ~54% for standard Transformers and ~59% for the best efficient-attention variant tested (the paper's numbers: 86.09, 53.66 and 59.37). The most striking result: the Path-X task, which requires reasoning over 16,000-token sequences, is solved at ~96% by S4. Attention-based models? At chance level. This result is what put SSMs on the map.

Complexity comparison

OperationTransformerS4 (convolution)S4 (recurrence)
TrainingO(L^2)O(L log L)O(L)
Inference per tokenO(L) via KV cacheN/AO(1)
MemoryO(L^2)O(L)O(N)

The gap widens with length. When the sequence gets 10x longer, the Transformer's cost is multiplied by 100 (the square of 10), while S4's grows by only about 13 (10x the length, times a logarithm that barely moves). At 100x longer: 10,000 versus roughly 130. It's exactly on long sequences that S4 shines.

Variants and evolutions of S4

S4D, for "S4 diagonal": simplifies the matrix A to a diagonal form, making implementation much more accessible without significantly sacrificing performance. It's this simplified version that allowed many teams to experiment with SSMs.

S5: multi-input multi-output (MIMO) version with a shared state matrix across dimensions, reducing the total number of parameters.

H3 (Hungry Hungry Hippos): combines two SSM layers with a multiplicative gate between them. By adding non-linearity between sequential layers, H3 boosts the model's expressiveness, it's the intermediate step that led directly to Hyena and Mamba.

From S4 to Mamba

S4 laid the foundations, but it had a fundamental limitation: its parameters are fixed, the same filter applies to every token, regardless of its content. Mamba, its successor, solves this problem by making the matrices B, C, and the discretization step delta input-dependent. Instead of a fixed audio filter applied uniformly, Mamba is an intelligent equalizer that adjusts its settings to each note based on the musical context. It's this selectivity that allows Mamba to rival Transformers on language tasks.

Conclusion

S4 is a fundamental breakthrough that opened a new paradigm. By combining dynamical systems theory with efficient computation techniques (HiPPO for initialization, NPLR for acceleration, FFT for convolution), it demonstrated that attention isn't the only way to model sequences, and that you can do much better on long sequences. The concepts it introduces, the recurrence/convolution duality, HiPPO initialization, structured parameterization of the state matrix, are the foundations on which the entire SSM family (Mamba, RWKV, RetNet, Hyena) is built.

Check your understanding0 / 5

1. An SSM like S4 can be computed in two mathematically equivalent ways. How are they used in practice?

2. Why is the Fast Fourier Transform (FFT) decisive for S4?

3. What is the central danger around the matrix A, and how does S4 solve it?

4. What do you gain by making the matrix A diagonal, as in S4D?

5. What fundamental limitation of S4 motivated the creation of Mamba?