Search

Search papers and lessons

Efficiently Modeling Long Sequences with Structured State Spaces

Albert Gu, Karan Goel, Christopher Ré · 2021

Gu, Goel and Ré (2021) make state space models fast enough to use as a general sequence model, by writing the state matrix as normal plus low-rank so that its convolution kernel reduces to a Cauchy kernel.

On this page

  • Colour = topic mix
  • Linear algebra
  • Calculus
  • Discrete math
  • Architectures
  • Abstract algebra
  • ReadReady to readNot yet

TL;DR

A state space model (SSM) reads a sequence one step at a time, keeping a hidden state that is updated by a fixed linear rule. With the right state matrix the state can remember inputs thousands of steps back, but the previous way of computing such a model was too slow and memory-hungry to use in practice. S4 reparameterises the state matrix as a "normal plus low-rank" matrix. That makes the model's convolution kernel computable in near-linear time and memory, and the paper reports strong results on long-sequence benchmarks.

Builds on: State Space Model, HiPPO Matrix, Convolution · Leads to: Selective State Space Model, Transformers are SSMs: Generalized Models and Efficient Algorithms (Mamba-2)

Why it mattered

S4 showed that a model built on a linear state space could match or beat attention models on tasks with very long inputs, and it did so with a cost that grows roughly linearly with sequence length. It also made the model usable as a general layer. The paper applies it to image classification from raw pixels, autoregressive generation, speech and time-series forecasting, with the same architecture throughout. Later state space models such as Selective State Space Model and Transformers are SSMs: Generalized Models and Efficient Algorithms (Mamba-2) build on the same view of a sequence model as a linear recurrence that can also be run as a convolution.

The problem

The earlier Linear State Space Layer (LSSL) of Gu et al. had shown that deep SSMs can handle long-range dependencies, especially with the HiPPO Matrix as state matrix. The paper cites a case where that matrix lifted sequential MNIST from 60% to 98% accuracy. But the LSSL was impractical. For state dimension NN and sequence length LL, the paper states its computation as O(N2L)O(N^2 L) operations and O(NL)O(NL) memory, against a lower bound of Ω(L+N)\Omega(L+N). For a typical size of N=256N = 256 this uses orders of magnitude more memory than an RNN or CNN of the same size. The authors also show that the fast algorithm proposed for the LSSL is numerically unstable, because the HiPPO matrix is highly non-normal.

So the question is: can the same model be computed with the efficiency of a convolution, without losing the long-range memory that the HiPPO matrix provides?

How it works

Take the SSM in its three equivalent views and the job of each. A continuous system x′=Ax+Bux' = Ax + Bu, y=Cxy = Cx is discretised with a step size Δ\Delta into a recurrence, and unrolling the recurrence gives a convolution with a kernel K‾\overline{K}. Training wants the convolution, because it parallelises. Inference wants the recurrence, because it costs constant time per step. The bottleneck is computing K‾\overline{K}, which needs repeated powers of a matrix.

flowchart LR
  A[HiPPO matrix A] --> B[normal plus low rank: Lambda minus P Q star]
  B --> C[truncated generating function at roots of unity]
  C --> D[Woodbury identity removes the low-rank term]
  D --> E[Cauchy kernel, near-linear time]
  E --> F[inverse FFT gives kernel K bar]
  F --> G[convolve K bar with the input]
  1. Start with the HiPPO matrix AA and a discrete SSM. Each step of the recurrence multiplies the state by A‾\overline{A}, so computing K‾\overline{K} naively takes LL matrix products.
  2. Diagonalising AA would make powers easy, but the eigenvectors of the HiPPO matrix have entries as large as 24N/32^{4N/3}, so that route is numerically unusable. Check: with N=3N = 3 this bound is about 24=162^4 = 16; at N=300N = 300 it is astronomically large.
  3. Split AA into a normal matrix Λ\Lambda and a rank-one term PQ⊤PQ^\top. Normal matrices diagonalise stably. The HiPPO matrix is not normal, but it is normal plus a rank-one term. Check: the paper's Theorem 1 gives rank r=1r = 1 for the HiPPO matrix of equation (2).
  4. Replace powers by a single inverse: evaluate the generating function ∑jK‾jzj\sum_j \overline{K}_j z^j at the LL roots of unity. This turns the problem into a resolvent, (⋅)−1(\cdot)^{-1}, not a power.
  5. Remove the low-rank term with the Woodbury identity. The inverse of Λ−PQ∗\Lambda - PQ^* becomes a sum of diagonal inverses. Check: the diagonal inverses are now Cauchy matrices, with entries 1/(ωj−λk)1/(\omega_j - \lambda_k).
  6. Evaluate the Cauchy kernel with a fast multipole-type algorithm, then apply an inverse FFT to get K‾\overline{K}. The output is a convolution kernel that is used in the usual way, y=K‾∗uy = \overline{K} \ast u.

Concrete check: take a scalar toy SSM with A=−1A = -1, B=1B = 1, C=1C = 1, Δ=0.5\Delta = 0.5. Then A‾=0.75/1.25=0.6\overline{A} = 0.75 / 1.25 = 0.6, B‾=0.5/1.25=0.4\overline{B} = 0.5 / 1.25 = 0.4, C‾=1\overline{C} = 1. The kernel is (0.4,0.24,0.144,… )(0.4, 0.24, 0.144, \dots). For the input (1,1,1)(1, 1, 1) the recurrence gives outputs 0.4,0.64,0.7840.4, 0.64, 0.784, and the convolution gives 0.40.4, then 0.4+0.24=0.640.4 + 0.24 = 0.64, then 0.4+0.24+0.144=0.7840.4 + 0.24 + 0.144 = 0.784. The two views agree. S4's contribution is computing this kernel for a large matrix without ever forming the powers.

The math

Definition: Structured state space (S4) layer

A continuous SSM x′(t)=Ax(t)+Bu(t)x'(t) = Ax(t) + Bu(t), y(t)=Cx(t)y(t) = Cx(t) with AA in normal-plus-low-rank form. Discretised with the bilinear rule, its kernel is K‾=(CB‾,CA‾B‾,…,CA‾L−1B‾)\overline{K} = (C\overline{B}, C\overline{A}\overline{B}, \dots, C\overline{A}^{L-1}\overline{B}). S4 parameterises A=VΛV∗−PQ∗A = V\Lambda V^* - PQ^* and learns Λ\Lambda, PP, QQ, BB, CC.

Step 1 in symbols: the recurrence

With bilinear discretisation, A‾=(I−Δ2A)−1(I+Δ2A)\overline{A} = (I - \tfrac{\Delta}{2}A)^{-1}(I + \tfrac{\Delta}{2}A) and B‾=(I−Δ2A)−1ΔB\overline{B} = (I - \tfrac{\Delta}{2}A)^{-1}\Delta B. The discrete system is xk=A‾xk−1+B‾ukx_k = \overline{A}x_{k-1} + \overline{B}u_k, yk=Cxky_k = Cx_k. Unrolling it from x−1=0x_{-1} = 0 gives yk=CA‾kB‾u0+⋯+CB‾uky_k = C\overline{A}^k\overline{B}u_0 + \dots + C\overline{B}u_k.

Step 2 in symbols: why diagonalising fails

For the HiPPO matrix, Ank=−(2n+1)(2k+1)A_{nk} = -\sqrt{(2n+1)(2k+1)} when n>kn \gt k, −(n+1)-(n+1) when n=kn = k, and 00 when n<kn \lt k. The paper shows that the matrix with entries Vij=(i+ji−j)V_{ij} = \binom{i+j}{i-j} diagonalises AA (Lemma 3.2), and that VV has entries up to about 24N/32^{4N/3}. Working with VV therefore cannot be done stably for large NN.

Step 3 in symbols: normal plus low rank

Theorem 1 states that every HiPPO matrix from the earlier work has the form A=V(Λ−(V∗P)(V∗Q)∗)V∗A = V(\Lambda - (V^*P)(V^*Q)^*)V^* with VV unitary and Λ\Lambda diagonal, and rank r=1r = 1 or r=2r = 2. Conjugating by an invertible matrix VV leaves the SSM's input-output map unchanged (Lemma 3.1), so one can work with the diagonal-plus-low-rank form directly.

Step 4 in symbols: generating function

Define K^(z)=∑i=0L−1C∗A‾iB‾ zi\hat{K}(z) = \sum_{i=0}^{L-1} C^*\overline{A}^i\overline{B}\,z^i. Then K^(z)=21+z C~∗(2Δ1−z1+z−A)−1B\hat{K}(z) = \frac{2}{1+z}\,\tilde{C}^*\left(\frac{2}{\Delta}\frac{1-z}{1+z} - A\right)^{-1}B with C~=(I−A‾L)∗C\tilde{C} = (I - \overline{A}^L)^*C, and the kernel values are recovered from K^\hat{K} at the roots of unity ωk=exp⁡(2πik/L)\omega_k = \exp(2\pi i k/L) by one inverse DFT (Lemma C.2), which costs O(Llog⁡L)O(L\log L).

Step 5 in symbols: Woodbury and the Cauchy kernel

For A=Λ−PQ∗A = \Lambda - PQ^*, the Woodbury identity gives (Λ−PQ∗)−1=Λ−1+Λ−1P(1−Q∗Λ−1P)−1Q∗Λ−1(\Lambda - PQ^*)^{-1} = \Lambda^{-1} + \Lambda^{-1}P(1 - Q^*\Lambda^{-1}P)^{-1}Q^*\Lambda^{-1}, so the low-rank part only needs small corrections. With R(z)=(2Δ1−z1+z−Λ)−1R(z) = (\tfrac{2}{\Delta}\tfrac{1-z}{1+z} - \Lambda)^{-1}, the generating function is K^(z)=21+z[C~∗RB−C~∗RP(1+Q∗RP)−1Q∗RB]\hat{K}(z) = \frac{2}{1+z}\left[\tilde{C}^*R B - \tilde{C}^*RP(1 + Q^*RP)^{-1}Q^*RB\right]. The needed quantities are entries of the Cauchy matrix Mij=1/(ωi−λj)M_{ij} = 1/(\omega_i - \lambda_j), whose matrix-vector product costs O((M+N)log⁡2(M+N))O((M+N)\log^2(M+N)) in exact arithmetic and O~(M+N)\tilde{O}(M+N) numerically (Proposition 5).

Step 6 in symbols: the cost

Theorem 3 gives the kernel with four Cauchy products in O~(N+L)\tilde{O}(N+L) operations and O(N+L)O(N+L) space. The paper notes that its current implementation uses the naive O(NL)O(NL) Cauchy product on GPUs, which is simple and parallel but not the asymptotically fastest. Trainable parameters per S4 layer are 5N5N (Λ\Lambda, PP, QQ, BB, CC), as stated in Section 3.4.

Results

All numbers are from the paper. Where the paper reports two versions, both are shown, because Appendix D.5 states that later reruns superseded some tables.

  • Efficiency. Compared with the LSSL, S4 is "up to 30×30\times faster with 400×400\times less memory" (Section 1).
  • Long Range Arena. The original paper reports an average of 80.48 for S4, against less than 60 for every baseline in Table 2 of that version. The updated S4 row (Appendix D.5, which replaces the original Table 2 numbers) averages 86.09, with Path-X 96.35, against 53.66 for the original Transformer and 59.37 for Luna-256. The introduction quotes the original Path-X result, 88%, against 50% for random guessing.
  • Raw speech. S4 reaches 98.3% on the 10-class Speech Commands subset with raw length-16000 audio, and 96.3% when the sampling frequency is halved, without retraining. Appendix D.5 asks readers to cite a follow-up paper's table, on the full 35-class dataset, for Speech Commands results.
  • Sequential CIFAR-10. Without data augmentation S4 gets 91.12% against 89.46% for a ResNet-18 with 11.0M parameters. With augmentation it gets 93.16% against 95.62%. Swapping batch norm for layer norm, S4 gets 90.46% and the ResNet gets 79.52%.
  • Generative modelling. On CIFAR-10 density estimation S4 reaches 2.85 bits per dimension. On WikiText-103 it comes within 0.8 perplexity of a strong Transformer and sets a state of the art for attention-free models.
  • Generation speed. S4 generates about 60×60\times faster than standard autoregressive models on both CIFAR-10 and WikiText-103.
  • Forecasting. On the Weather dataset with horizon 720, S4 has MSE 0.245 against 0.359 for Informer. That is a reduction of about 32%. The text's figure of 37% matches the LogTrans number (0.388), not the Informer one.
  • Ablations. With AA trained, every initialisation reaches perfect training accuracy, but validation accuracies differ by over 15%. With dropout 0.1 and 100K parameters the full S4 reaches 84.27% test accuracy on sequential CIFAR-10. The authors conclude that the HiPPO initialisation, not the low-rank parameterisation, is the source of the effect.

Limits. The authors say S4 still trails Transformers on language modelling. The released code uses the naive O(NL)O(NL) Cauchy product through pykeops. The authors expect a dedicated CUDA version to be faster. Several headline results were later revised, so cite the updated tables when quoting.

Reading guide

  1. Abstract and Section 1. The LRA and speech motivation. Needs State Space Model and Transformer as the point of comparison.
  2. Section 2.1 to 2.3. The continuous SSM, its discretisation and the recurrence. Needs Ordinary Differential Equation and State Space Model.
  3. Section 2.2. HiPPO and equation (2). Needs HiPPO Matrix and Legendre Polynomials.
  4. Section 2.4. The convolution kernel. Needs Convolution.
  5. Section 3.1 to 3.2. Diagonalisation, normal plus low rank, and the three techniques. Needs Low-Rank Matrix and Discrete Fourier Transform for the roots of unity.
  6. Section 3.3 to 3.4. Complexity and the layer. Table 1 compares the models. Needs Recurrent Neural Network.
  7. Section 4. Experiments. Use the appendix tables for exact figures.
  8. Appendix C. Full proofs of the kernel algorithm.

Check yourself

QuestionIn the scalar toy example with A = -1, B = 1, C = 1 and step size 0.5, what is the discrete state matrix A-bar?Show answer

Using A-bar = (I - (Delta/2) A)^(-1) (I + (Delta/2) A). Here (Delta/2) A = 0.25 times -1 = -0.25, so the first factor is 1 / 1.25 and the second is 0.75. Then A-bar = 0.75 / 1.25 = 0.6.

QuestionWhy does the paper not compute the kernel by diagonalising the HiPPO matrix?Show answer

The eigenvector matrix V of the HiPPO matrix has entries of magnitude up to about 2^(4N/3). For large state size N these numbers cannot be represented accurately, so the diagonalisation is numerically infeasible even though it is mathematically valid.

QuestionExplain in one sentence what the Woodbury identity buys in S4.Show answer

It converts the inverse of the diagonal-plus-low-rank matrix into the inverse of its diagonal part plus a correction on a rank-one term, so that the remaining work is a Cauchy kernel, which has fast stable algorithms.

QuestionThe paper reports 91.12% for S4 and 89.46% for ResNet-18 on sequential CIFAR-10 without augmentation. Compute the gap in percentage points, and say whether the paper's claim of competitiveness is consistent with it.Show answer

91.12 minus 89.46 is 1.66 percentage points in S4's favour. Without augmentation S4 is ahead. With augmentation it trails by 95.62 minus 93.16, which is 2.46 points. So the paper's word "competitive" fits both results, and S4 has fewer parameters (7.9M against the ResNet's 11.0M).

QuestionA user quotes the original Path-X result of 88% and the updated result of 96.35%. Which should a reader cite, and why?Show answer

Cite the updated table, but say which version. Appendix D.5 states that the Long Range Arena results in Table 2 were updated from follow-up work with minor hyperparameter changes, and the original numbers are kept in Table 4. Quoting one without the other can mislead.

Sources

Read the full paper

Albert Gu, Karan Goel, Christopher Ré, 2021. arXiv:2111.00396