State Space Models

Dive into Deep Learning · §12.4

Structured State Space Models (S4D)
continuous-time dynamics · zero-order-hold discretization · ODE–recurrence–convolution trinity · multi-timescale initialization

Why Multi-Timescale Memory Needs Structured Decays

Motivation

  • Three Orders of Magnitude: Language spans 10-token syntax, 200-token paragraph topics, and 1,000+ token entity persistence.
  • Zero-Bias Underflow: Gates at \sigma(0) = 0.5 attenuate a 1,000-step signal by 2^{-1000} = 0 in float32.
  • Structured State Bank: Continuous-time SSMs give each channel N log-spaced decay rates \lambda_1, \ldots, \lambda_N scaled by \Delta.

Continuous-Time Linear State Space Models (ODE View)

Continuous-Time Dynamics

\mathbf{h}'(t) = \mathbf{A}\,\mathbf{h}(t) + \mathbf{B}\,x(t), \qquad y(t) = \mathbf{C}\,\mathbf{h}(t) + D\,x(t)

  • Exponential Stability: Negative real parts \textrm{Re}(\lambda_n) < 0 ensure homogeneous solutions e^{\lambda_n t} decay smoothly without explosion.
  • Four Operators: Write vector \mathbf{B}, state evolution matrix \mathbf{A} \in \mathbb{C}^{N \times N}, readout \mathbf{C}, and skip D.

Diagonal State Matrices: N Leaky Integrators/Channel

Diagonal Structure

h_n'(t) = \lambda_n\, h_n(t) + b_n\, x(t), \quad y(t) = \sum_{n=1}^{N} c_n\, h_n(t) + D\, x(t)

  • Decoupled O(N) Bank: Diagonal \mathbf{A} = \textrm{diag}(\lambda_1, \ldots, \lambda_N) turns an O(N^2) matrix ODE into N parallel scalar leaky integrators per channel (d \times N total state).
  • Real vs. Complex Diagonal: Real rates \lambda_n = -e^{\theta_n} give smooth decay windows; complex rates add oscillation.

Exact Discretization via Zero-Order Hold (ZOH)

From Continuous to Discrete

h_t = \bar{a}\, h_{t-1} + \bar{b}\, x_t, \quad \bar{a} = e^{\Delta\lambda}, \quad \bar{b} = \frac{e^{\Delta\lambda} - 1}{\lambda}

  • Zero-Order Hold: Holding x(\tau) \equiv x_t constant on ((t-1)\Delta, t\Delta] integrates the linear ODE in closed form.
  • Numerical Stability: Evaluate \bar{b} via expm1(dt * rate) / rate to avoid cancellation when \Delta|\lambda| \ll 1.

The Step Size Δ Acts as a Timescale Dilation Gate

Step Size vs. Update Gate

h_t = e^{-\Delta}\, h_{t-1} + (1 - e^{-\Delta})\, x_t, \qquad \tau_{1/2} = \frac{\ln 2}{\Delta|\lambda|}

  • Exact Convex Blend (\lambda = -1): Discretized ZOH matches the GRU/minGRU update with keep gate \bar{a} = e^{-\Delta}.
  • Inverse Timescale Law: Shrinking \Delta from 1.0 to 0.001 dilates discrete memory half-life from 0.7 to 693 steps.

Factored Half-Lives and the S4D Initialization Band

Multi-Timescale Initialization

  • Factored half-life: With \lambda_n < 0 and channel step size \Delta_c > 0, state (c, n) has half-life: \tau_{1/2}(c, n) = \frac{\ln 2}{\Delta_c\, |\lambda_n|}
  • Real S4D initialization: Sets \lambda_n = -n for n = 1, \ldots, 16 (the diagonal of the HiPPO Legendre matrix, Gu et al., 2020) and draws \Delta_c log-uniformly on [0.001, 0.1].
  • Spectrum at step 0: Initial half-lives span from \ln 2 / (0.1 \times 16) = \mathbf{0.43\text{ steps}} to \ln 2 / (0.001 \times 1) = \mathbf{693\text{ steps}}.

The Convolutional View of Time-Invariant SSMs

LTI Duality

y_t = \sum_{k=0}^{t-1} K_k\, x_{t-k} + D\, x_t, \qquad K_k = \sum_{n=1}^{N} c_n\, \bar{a}_n^k\, \bar{b}_n

  • Impulse Filter: When (\bar{a}_n, \bar{b}_n, c_n) are time-invariant, unrolling the recurrence yields a length-T causal filter \mathbf{K}.
  • O(T \log T) FFT Training: Zero-padding \mathbf{X} and \mathbf{K} to length 2T evaluates all T outputs in parallel via rfft / irfft.

One System, Three Views: ODE, Recurrence, Convolution

The SSM Trinity

Representation Mathematical Form Primary Deep Learning Role
1. Continuous-Time ODE \mathbf{h}'(t) = \mathbf{A}\mathbf{h}(t) + \mathbf{B}x(t) Principled multi-timescale initialization (\lambda_n, \Delta) & resolution invariance
2. Discrete Recurrence \mathbf{h}_t = \bar{\mathbf{a}} \odot \mathbf{h}_{t-1} + \bar{\mathbf{b}} x_t Fast O(1)-state, O(dN)-work autoregressive token generation (step)
3. Causal Convolution y = \mathbf{K} * x + D x Fast O(Td\log T) parallel LTI training via zero-padded length-2T FFT

What N Real Exponentials Can and Cannot Approximate

Kernel Expressivity & Conditioning

Can a sum of N monotone real exponentials K_k = \sum_{n=1}^N c_n \bar{a}_n^k \bar{b}_n (\lambda_n = -n, \Delta = 0.05) peak at lag k = 20 to read a past token? We fit \mathbf{c} \in \mathbb{R}^N by least squares to a sharp lag-20 spike and a smooth lag-20 Gaussian bump:

 N  peak  spike |c|  spike residual  bump |c|  bump residual
 1     0      7e-01            0.99     7e+00           0.90
 4    17      1e+02            0.97     1e+03           0.55
16    20      2e+11            0.89     8e+10           0.03

Look at bump |c| (\mathbf{8 \times 10^{10}}): While N = 16 real exponentials fit the lag-20 bump in least squares (0.03 error), doing so requires readout weights of magnitude \sim 10^{11} with alternating signs that cancel to 10 decimal places.

Pitfall: Ill-Conditioning of Real Exponential Bases

Why Real SSMs Average Windows

  • Vandermonde Ill-Conditioning: Real modes e^{-n\Delta k} decay monotonically; forming a delayed bump at k^*=20 requires subtracting nearly colinear curves with \|\mathbf{c}\|_\infty \sim 10^{11}.
  • Complex Conjugate Rates (S4D-Lin): Adding imaginary parts \lambda_m = -1/2 \pm i\pi m creates orthogonal damped cosines e^{-\Delta k/2}\cos(\omega_m \Delta k) that fit delayed spikes with O(1) weights.

Implementing S4D: Discretization, FFT, and Step

The S4D Code Architecture

S4D parameterizes log_neg_rate (\lambda_n = -e^{\theta_n} < 0) and log_dt (\Delta = e^\phi > 0), fixes \mathbf{B} = \mathbf{1} (since only b_n c_n enters K_k), and supports both FFT training and O(1)-state generation:

def forward(self, X):  # X: (batch_size, num_steps, d)
    T = X.shape[1]
    a_bar, b_bar = self.discretize()
    # All powers a_bar^k for k < T: d * N * T numbers held in memory
    powers = a_bar.unsqueeze(-1) ** torch.arange(T, device=X.device)
    K = ((self.C * b_bar).unsqueeze(-1) * powers).sum(1)  # Kernel, (d, T)
    Y = torch.fft.irfft(torch.fft.rfft(X, n=2 * T, dim=1) *
                        torch.fft.rfft(K.T, n=2 * T, dim=0), n=2 * T, dim=1)
    return Y[:, :T] + self.D * X
  • Memory advantage during LTI training: The FFT path forms only the (d, N, T) power tensor once per batch rather than materializing the 4D (B, T, d, N) state across sequences.

Numerical Equivalence of Convolution, Scan, and Step

The S4D Layer Verified

Our S4D module stores log_neg_rate (\lambda_n = -e^{\theta_n} < 0), log_dt (\Delta = e^{\phi} > 0), readout C, and skip D (fixing \mathbf{B} = \mathbf{1} since only b_n c_n enters K_k). Comparing FFT forward, linear_scan, and token-by-token step on T = 50:

convolution vs scan: 2.4e-07, convolution vs steps: 2.4e-07

All three evaluation algorithms compute the exact same mathematical function up to float32 rounding (\sim 10^{-7}).

Disentangling Timescales from Architecture on Recall

Controlled Ablation Design

  • 2×2 Experimental Matrix: Crosses architecture (minGRU with N=1 vs. S4D with N=16) against initial half-life spectrum (\le 7 steps vs. 693 steps) on delayed recall (T \in \{100, 1{,}000\}).
  • Transferring S4D Timescales to Gates: Setting b_c = -\log(e^{\Delta_c} - 1) aligns minGRU’s initial gate \sigma(b_c) = e^{-\Delta_c} with S4D’s log-uniform horizon.

1,000-Step Delayed Recall: Initial Timescales Decide

Long-Range Recall Results

Test accuracy (chance = 0.125) and post-training slowest half-life (\ln 2 / \min(\Delta|\lambda_n|)) across two seeds at T = 100 and T = 1{,}000:

accuracy                   T=100       T=1000   
minGRU                   0.12  0.12   0.13  0.13
S4D                      1.00  1.00   1.00  1.00
S4D, short timescales    1.00  1.00   0.12  0.12
minGRU, S4D timescales   1.00  1.00   1.00  1.00
slowest half-life after training, in steps
S4D                      1028  1190   3020  2552
S4D, short timescales     119   196      7     7

The controls swap places: At T = 1{,}000, minGRU initialized with S4D’s timescales achieves 1.00 accuracy, whereas S4D initialized with short timescales (\le 7 steps) collapses to 0.12 (chance).

Why 7-Step Initial Half-Lives Succeed at 100, Not 1000

Floating-Point Gradient Horizon

Look closely at the bottom rows of the output table (slowest half-life after training):

  • At T = 100 (S4D, short timescales succeeds): With slowest initial rate \Delta|\lambda_1| = 0.1, the initial signal attenuation over 99 filler steps is e^{-0.1 \times 99} = e^{-9.9} \approx \mathbf{5 \times 10^{-5}}. Because 5 \times 10^{-5} is well above float32 precision (\sim 10^{-7}), Adam receives a nonzero gradient and stretches the slowest half-life from 7 steps to 107–170 steps.
  • At T = 1{,}000 (S4D, short timescales freezes): Over 999 steps, initial attenuation is e^{-0.1 \times 999} \approx \mathbf{4 \times 10^{-44}}—underflowing float32 gradient accumulation completely, so the slowest half-life stays frozen at 7 steps.

The Blind Spot of LTI Models: Content Blindness

From LTI to Selectivity

Why did we use identical blank fillers (blank_fillers=True) in the 1{,}000-step S4D recall test?

  • Fixed lag weights: In a time-invariant convolution y_t = \sum_{k=0}^{t-1} K_k x_{t-k} + D x_t, the weight K_k depends only on the distance k, never on the token x_{t-k} itself.
  • Cannot ignore noisy fillers or variable lags: If salient tokens appear at unpredictable positions among varying filler tokens, any fixed filter K_k blends target tokens and filler noise with fixed weights.

Up next (§12.5 Selective State Space Models): Make the step size \boldsymbol{\Delta}_t, write vector \mathbf{B}_t, and read vector \mathbf{C}_t functions of the input token \mathbf{x}_t—combining S4D’s multi-timescale state bank with minGRU’s content-based gating.

Concept Check: ZOH Discretization and FFT Padding

Check Your Understanding

Test your grasp of continuous-time state space models with these three questions:

  1. For a continuous-time state with rate \lambda = -2, what step size \Delta yields a discrete half-life of 500 token steps under zero-order hold?
  2. Why does S4D.forward pad both the input sequence \mathbf{X} and the kernel \mathbf{K} from length T to length 2T before calling rfft and irfft?
  3. How can you transfer S4D’s initial decay spectrum \bar{a} = e^{-\Delta} (\Delta \in [0.001, 0.1]) to the update-gate bias b of a standard sigmoid gate \sigma(b)?

Answers:

  1. \tau_{1/2} = \ln 2 / (\Delta|\lambda|) = 500 \implies \Delta = \ln 2 / (2 \times 500) \approx \mathbf{0.000693}.
  2. Multiplication in the DFT frequency domain computes circular convolution of period N_{\text{FFT}}; without zero-padding to at least 2T - 1 (rounded to 2T), late tokens at t \approx T wrap around and corrupt early outputs at t \approx 0 (violating causality).
  3. Solve \sigma(b) = 1/(1 + e^{-b}) = e^{-\Delta} \implies e^{-b} = e^\Delta - 1 \implies \mathbf{b = -\log(e^\Delta - 1)} (computed via -log(expm1(dt))).

Summary: Structured State Space Models (S4D)

Key Takeaways

Concept / Component Mathematical Form Practical & Systems Role
Diagonal ODE Bank h_n'(t) = \lambda_n h_n(t) + x(t), \lambda_n = -e^{\theta_n} < 0 N independent rates per channel; O(dN) state and per-step work
Zero-Order Hold (ZOH) \bar{a}_n = e^{\Delta \lambda_n}, \bar{b}_n = (e^{\Delta \lambda_n} - 1)/\lambda_n Exact discrete recurrence for piecewise-constant inputs over step \Delta
Factored Half-Life \tau_{1/2} = \ln 2 / (\Delta|\lambda_n|) Rates \lambda_n = -n separate states; \Delta \in [10^{-3}, 10^{-1}] scales half-lives to 693 steps
Convolutional Duality K_k = \sum_{n=1}^N c_n \bar{a}_n^k \bar{b}_n, y = \mathbf{K} * x + D x Zero-padded length-2T FFT evaluates LTI training pass in O(Td\log T)
Real vs. Complex Rates \lambda_n \in \mathbb{R}_- vs. \lambda_n = -\alpha + i\omega Real rates form smooth window averages; complex rates fit sharp delay spikes
LTI Limitation Fixed lag weights K_k independent of x_{t-k} Solves 1{,}000-step fixed-lag recall, but fails when target positions vary (§12.5)