Simple Algorithm
Deep Learning · Emergence · 30 min

Structured State Spaces: HiPPO & S4

Continuous-time memory, discretization and long convolution kernels.

S4 began as a question about memory: if a recurrent state must summarize an entire history in NN numbers, what is the best summary, and can it be kept up to date online? HiPPO's answer, project the history onto orthogonal polynomials, produces one specific matrix with remarkable properties. S4 made that matrix usable at scale by finding the right algebra for its convolution kernel. Then the field discovered how much of the machinery was really needed.

What you’ll learn

  • Define the HiPPO online projection problem, state the HiPPO-LegS dynamics exactly, and prove its timescale equivariance.
  • State HiPPO-LegS's guarantees (linear-time updates, gradients of order 1/t1/t, approximation error that falls with smoothness) and explain what changes when the matrix is used in a time-invariant layer.
  • Explain why the HiPPO matrix cannot be diagonalized in practice, and derive S4's kernel algorithm from the generating function, the Woodbury identity and Cauchy kernels.
  • Explain why diagonal state matrices suffice (DSS, S4D), how Δ\Delta and the initialization set the time scales, and what the linear recurrent unit showed about the ingredients that matter.

Memory as online function approximation

A recurrent state of NN numbers must summarize an unbounded history. HiPPO (Gu, Dao, Ermon, Rudra & Ré, 2020) turns "summarize" into a precise approximation problem, and solving that problem produces the state matrices that S4 later used.

Measure μ(t)\mu^{(t)}DynamicsWhat it remembers
LegT: uniform on the window [t−θ,t][t-\theta,t]time-invariant; AA and BB scale with 1/θ1/\thetathe last θ\theta units of time; recovers the earlier Legendre Memory Unit
LagT: e−(t−x)e^{-(t-x)} on (−∞,t](-\infty,t]time-invariantan exponentially fading past; with N=1N=1 and an Euler step, c←(1−Δt) c+Δt fc\leftarrow(1-\Delta t)\,c+\Delta t\,f, a leaky integrator, which becomes a gated RNN when Δt\Delta t depends on the input
LegS: uniform on [0,t][0,t]time-varying, scaled by 1/t1/tthe whole history, with equal weight; no timescale hyperparameter
The three measures of Gu et al. (2020). Choosing the measure is choosing what "memory" means.
DerivationA sanity check, and timescale equivariance
  1. c0(t)=1t∫0tf(s) ds⟹c0′(t)=−1t c0(t)+1t f(t)c_0(t)=\frac1t\int_0^tf(s)\,ds\quad\Longrightarrow\quad c_0'(t)=-\frac1t\,c_0(t)+\frac1t\,f(t)

    With N=1N=1 the only basis function is g0=1g_0=1, so c0(t)c_0(t) is the running mean of the input. Differentiating it gives the theorem with A00=0+1=1A_{00}=0+1=1 and B0=1B_0=1. The higher coefficients follow from the same differentiation together with derivative identities of the Legendre polynomials.

  2. cnh(t)=1t∫0th(s) g^n(s/t) ds=∫01f(αut) g^n(u) du=cnf(αt)■c^h_n(t)=\frac1t\int_0^th(s)\,\hat g_n(s/t)\,ds=\int_0^1f(\alpha ut)\,\hat g_n(u)\,du=c^f_n(\alpha t)\qquad\blacksquare

    Proposition (timescale equivariance). If h(τ)=f(ατ)h(\tau)=f(\alpha\tau) for some α>0\alpha>0, then ch(t)=cf(αt)\mathbf c^h(t)=\mathbf c^f(\alpha t). Proof. Each basis function on [0,t][0,t] is a rescaling of one on [0,1][0,1], gn(t)(s)=g^n(s/t)g^{(t)}_n(s)=\hat g_n(s/t) with g^n(u)=2n+1 Pn(2u−1)\hat g_n(u)=\sqrt{2n+1}\,P_n(2u-1). Substitute s=uts=ut:

  3. ck+1=(I−Ak)ck+1k B fk\mathbf c_{k+1}=\Big(I-\frac{A}{k}\Big)\mathbf c_k+\frac1k\,B\,f_k

    Speeding the input up or slowing it down only re-times the coefficients, so LegS needs no prior on the time scale of the data. Even its Euler discretization contains no step size:

Check your understanding

A LegS memory with N=1N=1 has read the input f(s)=sf(s)=s on [0,10][0,10]. What is its state c0(10)c_0(10)?

  1. 1010
  2. 5050
  3. 55
  4. 11
Show the answer

C. 55

With one coefficient, LegS keeps the mean of the whole history under the uniform measure: 110∫010s ds=5\frac1{10}\int_0^{10}s\,ds=5. Higher coefficients add the history's linear, quadratic, … trends, measured against Legendre polynomials on [0,t][0,t].

What LegS guarantees, and what the time-invariant version means

Part (b) is the contrast that matters for learning. A mode of an LTI system, or a contracting RNN, passes gradient back with a factor like e−(t1−t0)/τe^{-(t_1-t_0)/\tau}, exponentially small in the distance. LegS's gradient depends only on t1t_1 and shrinks polynomially, because every past moment keeps the same weight 1/t11/t_1 in the uniform measure. Part (c) says what NN buys: smoother histories compress better, and the error grows with tt because the same NN coefficients must cover a longer past.

From time-varying to time-invariant. The Linear State-Space Layer (LSSL; Gu, Johnson, Goel, Saab, Dao, Rudra & Ré, 2021) dropped the 1/t1/t and used the HiPPO matrix as the constant AA of an LTI system with a learned step size. On sequential MNIST, replacing a random AA by the HiPPO matrix raised accuracy from 60% to 98%. Without the 1/t1/t, however, the system no longer computes LegS projections, and for a while no one could say what it did compute. Gu, Johnson, Timalsina, Rudra and Ré (2023) answered: the time-invariant system projects the history onto Legendre polynomials under an exponentially warped time axis, and Δ\Delta sets the length of the dependencies it captures.

Think first LegS is equivariant to time scale, yet S4 learns a step size Δ\Delta for every channel. Why does a time-invariant layer need Δ\Delta at all?

Time invariance and scale invariance conflict. LegS achieves scale invariance with the 1/t1/t factor, which makes the system time-varying: its memory window grows with tt. A convolution requires fixed dynamics, and fixed dynamics have fixed time constants, 1/∣Re⁡λn∣1/|\operatorname{Re}\lambda_n|, measured in units of the step Δ\Delta. Learning Δ\Delta, and spreading it across channels, restores a range of time scales that LegS got for free.

The S4 kernel: generating functions, Woodbury and Cauchy

An LTI layer of sequence length LL needs the kernel K=(CBˉ,CAˉBˉ,…,CAˉL−1Bˉ)K=(C\bar B,C\bar A\bar B,\dots,C\bar A^{L-1}\bar B). Powering a dense N×NN\times N matrix costs O(N2L)\order{N^2L} time and O(NL)\order{NL} memory, which is what the LSSL paid. With a diagonal matrix the kernel would be a cheap Vandermonde product. But conjugating to diagonal form (A↦V−1AVA\mapsto V^{-1}AV, B↦V−1BB\mapsto V^{-1}B, C↦CVC\mapsto CV leaves the input–output map unchanged) is numerically impossible for this matrix:

Predict before you look

For N=64N=64, the largest entry of Vij=(i+ji−j)V_{ij}=\binom{i+j}{i-j} over 0≤j≤i≤630\le j\le i\le63 is about 10x10^x. What is xx?

Show the answer

25.3

The largest entry is about 1.8×10251.8\times10^{25}, consistent with the bound 24N/3=285.3≈5×10252^{4N/3}=2^{85.3}\approx5\times10^{25}. Single-precision floats carry about 7 significant digits and double precision about 16, so products with such a matrix lose everything to rounding.

DerivationProof sketch: from powers to a Cauchy kernel
  1. K^(ζ)=∑j=0L−1CAˉjBˉ ζj=C(I−AˉLζL)(I−Aˉζ)−1Bˉ=C~(I−Aˉζ)−1Bˉ\hat K(\zeta)=\sum_{j=0}^{L-1}C\bar A^j\bar B\,\zeta^j=C\big(I-\bar A^L\zeta^L\big)\big(I-\bar A\zeta\big)^{-1}\bar B=\tilde C\big(I-\bar A\zeta\big)^{-1}\bar B

    Evaluate the kernel's generating function at the LL-th roots of unity ζ\zeta. The geometric series of matrices sums to a resolvent, and ζL=1\zeta^L=1 removes the dependence on LL from everything but a modified output vector C~=C(I−AˉL)\tilde C=C(I-\bar A^L):

  2. (I−Aˉζ)−1Bˉ=21+ζ (z I−A)−1B,z=2Δ 1−ζ1+ζ\big(I-\bar A\zeta\big)^{-1}\bar B=\frac{2}{1+\zeta}\,\big(z\,I-A\big)^{-1}B,\qquad z=\frac{2}{\Delta}\,\frac{1-\zeta}{1+\zeta}

    With the bilinear discretization, the discrete resolvent becomes a continuous one at a point z(ζ)z(\zeta), as direct substitution of Aˉ=(I−Δ2A)−1(I+Δ2A)\bar A=(I-\frac\Delta2A)^{-1}(I+\frac\Delta2A) and Bˉ=(I−Δ2A)−1ΔB\bar B=(I-\frac\Delta2A)^{-1}\Delta B shows:

  3. (zI−Λ+PQ∗)−1=R−RP (1+Q∗RP)−1Q∗R\big(zI-\Lambda+PQ^*\big)^{-1}=R-RP\,\big(1+Q^*RP\big)^{-1}Q^*R

    For A=Λ−PQ∗A=\Lambda-PQ^* the Woodbury identity reduces the resolvent to the diagonal one, R(z)=(zI−Λ)−1R(z)=(zI-\Lambda)^{-1}:

  4. u∗R(z) w=∑n=1Nuˉn wnz−λn\mathbf u^*R(z)\,\mathbf w=\sum_{n=1}^N\frac{\bar u_n\,w_n}{z-\lambda_n}

    Every term is now a sum of the form below, a Cauchy kernel evaluated at the LL points z(ζj)z(\zeta_j): O(NL)\order{NL} directly, O~(N+L)\tilde{\mathcal O}(N+L) with fast algorithms for Cauchy matrices. An inverse FFT of K^\hat K at the roots of unity returns KK.

With this kernel, S4 solved Path-X of the Long Range Arena (88% where every earlier model was at chance), reached 91% on pixel-by-pixel CIFAR-10 without data augmentation, and generated autoregressively about 60 times faster than a transformer in the paper's benchmark. Its own ablations pointed at the source: random NPLR matrices trained poorly, so the gains came from the HiPPO initialization, not from the NPLR parameterization.

Simpler is enough: diagonal SSMs, initialization and Δ

Gupta, Gu and Berant (2022) simply dropped the low-rank term and kept the diagonal part Λ\Lambda of LegS's normal component. The resulting diagonal state space (DSS) model matched S4 on the Long Range Arena. Gu, Gupta, Goel and Ré (2022) explained why: the diagonal restriction recovers S4's kernel in the limit of infinite state size. Their S4D computes its kernel in a few lines, as a Vandermonde product, and needs only a good initialization of the eigenvalues, for example

S4D-Lin:  λn=−12+iπn,S4D-Inv:  λn=−12+i Nπ(N2n+1−1),\text{S4D-Lin:}\ \ \lambda_n=-\tfrac12+i\pi n,\qquad\text{S4D-Inv:}\ \ \lambda_n=-\tfrac12+i\,\frac N\pi\Big(\frac{N}{2n+1}-1\Big),

the first a damped Fourier basis and the second an approximation of LegS's diagonal. Mamba later used real eigenvalues, λn=−(n+1)\lambda_n=-(n+1) (S4D-Real), for text.

Δ\Delta sets the time scales. With Re⁡λn=−12\operatorname{Re}\lambda_n=-\tfrac12, a mode remembers for about 2/Δ2/\Delta steps. S4D initializes Δ\Delta across channels in a range such as [0.001,0.1][0.001,0.1], so different channels start with memories from about 20 to about 2,000 steps; for Path-X the range was moved toward smaller Δ\Delta, longer memories.

Predict before you look

An S4D-Lin mode with Re⁡λ=−12\operatorname{Re}\lambda=-\tfrac12 runs with step Δ=0.01\Delta=0.01. What percentage of its magnitude, ∣Aˉ∣k=e−kΔ/2|\bar A|^k=e^{-k\Delta/2}, remains after k=500k=500 steps?

Show the answer

8.2 %

e−500×0.01/2=e−2.5≈0.082e^{-500\times0.01/2}=e^{-2.5}\approx0.082, about 8%. The memory is 2/Δ=2002/\Delta=200 steps to fall by a factor ee, so 500 steps is 2.5 memories. With Δ=0.001\Delta=0.001 the same mode would keep e−0.25≈78%e^{-0.25}\approx78\%.

The view from recurrent networks. S5 (Smith, Warrington & Linderman, 2023) used one multi-input, multi-output diagonal SSM per layer and computed it with a parallel scan instead of a convolution. Orvieto et al. (2023) then started from a plain RNN and asked which changes close the gap to S4 on the Long Range Arena. Their linear recurrent unit keeps the recurrence linear and diagonal over C\mathbb C; writes each eigenvalue as λ=exp⁡(−eν+iθ)\lambda=\exp(-e^{\nu}+i\theta), so that ∣λ∣<1|\lambda|<1 for every value of the trained parameters; initializes the eigenvalues uniformly on a ring just inside the unit circle, with radii up to about 0.99; and rescales the input of each mode. The rescaling follows from one line: for white-noise input, a mode with ∣λ∣=r|\lambda|=r accumulates variance

E∣x∞∣2=∑k≥0r2k E∣Bu∣2=E∣Bu∣21−r2,\E|x_\infty|^2=\sum_{k\ge0}r^{2k}\,\E|Bu|^2=\frac{\E|Bu|^2}{1-r^2},

which explodes as r→1r\to1, so the input is multiplied by γ=1−∣λ∣2\gamma=\sqrt{1-|\lambda|^2}. With a small initial phase for Path-X, the linear recurrent unit matched S4, S4D and S5 on every task. The continuous-time derivation explains good initializations and the role of Δ\Delta, but it is not needed to obtain them.

Key takeaways

  • HiPPO maintains the optimal polynomial approximation of the input history under a chosen measure; for the uniform measure on [0,t][0,t] (LegS) the coefficients obey c′=−1tAc+1tBf\mathbf c'=-\frac1tA\mathbf c+\frac1tBf with an explicit lower-triangular AA.
  • LegS is equivariant to time scale, updates in O(N)\order{N}, has gradients of order 1/t1/t rather than exponentially small ones, and approximates smooth inputs with error falling in NN; used as a constant matrix, it projects onto exponentially warped Legendre polynomials, with Δ\Delta setting the time scale.
  • The HiPPO matrix's eigenvectors are exponentially ill-conditioned, so S4 wrote it as normal plus rank one and computed the kernel through its generating function at roots of unity, the Woodbury identity and Cauchy kernels, in O~(N+L)\tilde{\mathcal O}(N+L).
  • Diagonal state matrices suffice when well initialized (DSS, S4D), Δ\Delta sets each channel's memory, and a stable, normalized linear RNN (the LRU) matches S4: the essentials are linearity, eigenvalues near the unit circle and careful parameterization.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

The same signal is played twice as fast: h(τ)=f(2τ)h(\tau)=f(2\tau). How are the HiPPO-LegS coefficients of hh related to those of ff?

  1. ch(t)=2 cf(t)\mathbf c^h(t)=2\,\mathbf c^f(t)
  2. ch(t)=cf(t/2)\mathbf c^h(t)=\mathbf c^f(t/2)
  3. They are unrelated: the step size must be retuned
  4. ch(t)=cf(2t)\mathbf c^h(t)=\mathbf c^f(2t)
Show the answer

D. ch(t)=cf(2t)\mathbf c^h(t)=\mathbf c^f(2t)

Substituting s=uts=ut turns cnh(t)c^h_n(t) into ∫01f(2ut)g^n(u) du=cnf(2t)\int_0^1f(2ut)\hat g_n(u)\,du=c^f_n(2t): by time tt the fast signal has played what the slow one plays by 2t2t, and LegS sees the same history, compressed in time.

Question 2

How does ∥∂c(t1)/∂f(t0)∥\lVert\partial\mathbf c(t_1)/\partial f(t_0)\rVert behave for HiPPO-LegS, for t0<t1t_0<t_1?

  1. It decays exponentially in t1−t0t_1-t_0, like a vanilla RNN's gradient
  2. It is Θ(1/t1)\Theta(1/t_1), independent of how far back t0t_0 lies
  3. It grows with t1t_1, because the state accumulates the history
  4. It is exactly zero once t1−t0t_1-t_0 exceeds a window length
Show the answer

B. It is Θ(1/t1)\Theta(1/t_1), independent of how far back t0t_0 lies

Under the uniform measure on [0,t1][0,t_1] every past moment has weight 1/t11/t_1, so the sensitivity to f(t0)f(t_0) shrinks only polynomially with the current time and not with the lag. That is why LegS-based memories avoid the exponential vanishing of gradients.

Question 3

Why did S4 not simply diagonalize the HiPPO matrix and compute its kernel as a Vandermonde product?

  1. Its eigenvector matrix has entries growing like 24N/32^{4N/3}, so the change of basis is numerically useless
  2. The HiPPO matrix has no eigenvalues
  3. A diagonal state matrix cannot produce a convolution kernel
  4. Diagonalization would change the input–output map of the layer
Show the answer

A. Its eigenvector matrix has entries growing like 24N/32^{4N/3}, so the change of basis is numerically useless

In exact arithmetic diagonalization is fine: conjugation leaves the layer's input–output map unchanged. But the eigenvectors Vij=(i+ji−j)V_{ij}=\binom{i+j}{i-j} are so badly conditioned that CVCV and V−1BV^{-1}B cannot be computed accurately. S4 instead conjugated by a unitary matrix to a diagonal-plus-rank-one form.

Question 4

Which conclusion is best supported by S4's ablations and the diagonal models that followed (DSS, S4D, LRU)?

  1. The normal-plus-low-rank parameterization is essential: without the low-rank term, SSMs fail on long-range tasks
  2. Any linear recurrence works on long-range tasks, whatever its initialization
  3. Initialization and parameterization of the eigenvalues matter most; diagonal state matrices suffice when initialized well
  4. Only continuous-time models can learn dependencies thousands of steps long
Show the answer

C. Initialization and parameterization of the eigenvalues matter most; diagonal state matrices suffice when initialized well

Random NPLR matrices trained poorly in S4's ablations, DSS and S4D matched S4 with diagonal matrices, and the linear recurrent unit matched them as a discrete-time linear RNN with a stable parameterization, an initialization near the unit circle and input normalization.

Further reading

  • A. Gu, T. Dao, S. Ermon, A. Rudra & C. Ré, "HiPPO: Recurrent memory with optimal polynomial projections," NeurIPS (2020).
  • A. Gu, K. Goel & C. Ré, "Efficiently modeling long sequences with structured state spaces," ICLR (2022).
  • A. Gu, A. Gupta, K. Goel & C. Ré, "On the parameterization and initialization of diagonal state space models," NeurIPS (2022).
  • A. Gu, I. Johnson, A. Timalsina, A. Rudra & C. Ré, "How to train your HiPPO: State space models with generalized orthogonal basis projections," ICLR (2023).
  • J. T. H. Smith, A. Warrington & S. W. Linderman, "Simplified state space layers for sequence modeling," ICLR (2023).
  • A. Orvieto, S. L. Smith, A. Gu, A. Fernando, C. Gulcehre, R. Pascanu & S. De, "Resurrecting recurrent neural networks for long sequences," ICML (2023).