Foundations to Frontier
Tests
E. Deep Learning › E17 State-Space and Recurrent-Revival Models

S4

S4 (Structured State Spaces; Gu, Goel and Ré, ICLR 2022) made state-space layers practical for very long sequences. It initialises the state matrix with HiPPO, a construction that keeps an optimal online summary of the input history in Legendre polynomials. It also parameterises A as normal plus low-rank, so the convolution kernel can be computed quickly. S4 was the first model to solve the 16,384-step Path-X task of the Long Range Arena. Simplified diagonal variants (S4D, DSS) later showed that a well-chosen diagonal initialisation works almost as well. This page covers HiPPO, the structured parameterisation, kernel computation and diagonal variants. In the exercises you build the HiPPO-LegS and S4D-Lin matrices, then measure how well a linear readout of a 32-dimensional state recalls the input from 10 to 200 steps ago. At delays 100 and 200, HiPPO reached R² 0.90 and 0.70, S4D-Lin 0.81 and 0.75, and a random diagonal SSM only 0.25 and 0.19.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

A memory designed to summarise the whole past

A state-space layer’s memory is only as good as its dynamics. Choose them randomly, and the memory fades within a few dozen steps. S4 starts from a special matrix, HiPPO, designed so the state always holds a compact summary of the entire input history, like keeping the first few coefficients of a polynomial fit to everything seen so far. With that start, the layer can remember thousands of steps. The demo shows how a set of decay rates shapes how far back a kernel reaches.

State-space lab
Kernel K_k = Σ C ā^k B̄ over 8 modes; the slowest decay rate 0.02 sets how far back the layer remembers. S4-style layers compute this kernel once and apply it as a long convolution.
Why engineers care

Long sensor recordings, audio, and genomic or telemetry sequences need models that look back thousands of steps; S4-style layers do this at linear cost.

📝 Quick check (Level 0)
0/3 answered
  1. 1. S4's key initialisation is…
  2. 2. HiPPO's state summarises…
  3. 3. S4 first solved…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • HiPPO-LegS: A_nk = −√(2n+1)√(2k+1) for n > k, A_nn = −(n + 1), 0 above the diagonal; B_n = √(2n+1).
    • A is lower triangular, so its eigenvalues are −1, −2, …, −N.
    • The state approximates the coefficients of the input history in Legendre polynomials.
  • S4 parameterisation: A = Λ − PQ* (normal plus low-rank). The kernel’s generating function is computed with Cauchy kernels and the Woodbury identity, in near-linear time.
  • S4D-Lin: diagonal λ_n = −1/2 + iπn. These are oscillating modes at frequencies πn with a common decay, close to HiPPO in practice.
  • Discretisation: bilinear (S4) or ZOH (S4D), with a learnable step Δ setting the timescale.
  • Long Range Arena: Path-X classifies 128 × 128 images as 16,384-step pixel sequences.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Eigenvalues of HiPPO-LegS (N = 3):
  2. 2. S4D-Lin's imaginary parts:
  3. 3. Path-X sequence length:

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

S4 in code

Exercise 1.

  • HiPPO-LegS had the expected entries and eigenvalues.
  • S4D-Lin had real parts −1/2 and imaginary parts πn.
  • The bilinear discretisation was stable.

Practice.

  • Use the state-spaces repository (S4, S4D), or Mamba’s simpler diagonal kernels.
  • Initialise with HiPPO or S4D-Lin, learn log Δ per channel (initialised log-uniform in [0.001, 0.1]), and use a lower learning rate for A and Δ.
  • Stack S4 blocks with normalisation, GLU or MLP mixing and residuals, like Transformer blocks with attention replaced.
# S4D layer (diagonal, ZOH), per channel h: A_h complex diagonal, B, C complex, log_dt real
dt = torch.exp(log_dt)                                       # (H,)
dA = torch.exp(dt[:, None] * A)                              # (H, N)
K = 2 * torch.einsum("hn,hnl->hl", C * (dA - 1) / A * B, dA[..., None] ** torch.arange(L)).real
y = fft_conv(u, K) + D * u                                   # long convolution, then mix channels
🐍 Exercise 1: HiPPO and S4D-Lin
Build the HiPPO-LegS matrices, the S4D-Lin initialisation and the bilinear discretisation.
📝 Quick check (Level 2)
0/3 answered
  1. 1. A typical Δ initialisation:
  2. 2. A and Δ often use…
  3. 3. An S4 block replaces attention with…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

HiPPO: memory as online function approximation.

  • Ask: what dynamics keep the best polynomial approximation (under a chosen measure) of the input history up to now?
  • The answer is a linear ODE whose state is the vector of approximation coefficients. For the scaled Legendre measure (LegS), this gives the matrix above.
  • The state then compresses the entire history with graceful degradation instead of exponential forgetting.

Why structure matters. A random stable A has eigenvalues of arbitrary speeds and no coordination between modes, so most information decays quickly and the rest is redundant. HiPPO and S4D-Lin spread timescales and frequencies systematically, giving a well-conditioned basis for the past.

Efficient kernels. Computing K = (CB̄, CĀB̄, …) naively costs O(N²L). S4’s normal-plus-low-rank structure reduces this to near O(N + L) using Cauchy kernels, and diagonal variants (S4D) make it a simple Vandermonde product.

📝 Quick check (Level 3)
0/3 answered
  1. 1. HiPPO derives A from…
  2. 2. Random stable A tends to…
  3. 3. Diagonal SSM kernels reduce to…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Does structured initialisation give longer memory?

History.

  • A. Gu, T. Dao, S. Ermon, A. Rudra and C. Ré, “HiPPO: recurrent memory with optimal polynomial projections” (NeurIPS 2020).
  • A. Gu, K. Goel and C. Ré, “Efficiently modeling long sequences with structured state spaces” (S4, ICLR 2022).
  • A. Gu et al., “On the parameterization and initialization of diagonal state space models” (S4D, NeurIPS 2022).

Exercise 2.

  • State: a 32-dimensional SSM state, bilinear with Δ = 0.01, initialised with HiPPO-LegS, a random stable diagonal, or S4D-Lin.
  • Input: a smooth signal (20-step moving average of noise).
  • Measurement: a linear readout of the state is fitted (ridge least squares) to recall the input from 10, 50, 100 or 200 steps ago, and R² is measured on held-out time steps.
initialisation delay 10 50 100 200
HiPPO-LegS 0.969 0.928 0.904 0.696
random diagonal 0.900 0.621 0.253 0.188
S4D-Lin 0.873 0.821 0.812 0.753
  • Structure gives long memory: HiPPO still recalls the input from 100 steps ago with R² 0.90, and S4D-Lin keeps 0.75 at 200 steps. The random diagonal state has largely forgotten by 100 steps (0.25).
  • The two structured choices trade off: HiPPO is best up to 100 steps, while S4D-Lin’s evenly spread oscillating modes hold up slightly better at 200.
  • Seeds: seeds 1 and 2 gave the same ranking (delay 200: HiPPO 0.71 and 0.65, S4D-Lin 0.80 and 0.79, random 0.06 and 0.08).
🐍 Exercise 2 (research): does structured initialisation give longer memory?
Measure how well a linear readout of HiPPO, random-diagonal and S4D-Lin SSM states recalls past inputs at several delays.

S4 in AI, robotics and aerospace

  • Long time series: vibration, telemetry and audio classification over tens of thousands of steps.
  • Continuous-time data: irregular sampling is handled by adjusting Δ per step.
  • Design lesson: initialisation can encode a memory principle (here, optimal polynomial projection); random initialisation of recurrent dynamics usually forgets.

References

  1. A. Gu, K. Goel and C. Ré (2022). “Efficiently modeling long sequences with structured state spaces.” ICLR.
  2. A. Gu, T. Dao, S. Ermon, A. Rudra and C. Ré (2020). “HiPPO: recurrent memory with optimal polynomial projections.” NeurIPS.
  3. A. Gu, A. Gupta, K. Goel and C. Ré (2022). “On the parameterization and initialization of diagonal state space models.” NeurIPS.