Part II · SSM Core Week 5 Published s4d.py test_s4d.py Notebook

Diagonal SSMs (S4D) and Stability Analysis

S4D — the diagonal simplification of S4 that drops kernel cost from O(N²L) to O(NL) and makes stability guaranteed by construction via log-parameterized eigenvalues.

On this page
  1. From Full to Diagonal
  2. Initialization variants
  3. S4D-Lin (default)
  4. S4D-Inv
  5. S4D-LegS
  6. Complex-valued state and stability by construction
  7. Element-wise computation
  8. Stability region analysis
  9. Reflections
  10. What transfers
  11. What requires adaptation
  12. Forward map

From Full to Diagonal

Week 4’s S4 stores A\statemat as a full N×N\statedim \times \statedim matrix (HiPPO-LegS initialization). The kernel computation K[k]=CAˉkBK[k] = \outputmat \discA^k \inputmat requires matrix powers — O(N2L)\bigO{\statedim^2\seqlen} even with a naive implementation.

S4D’s insight: any diagonalizable SSM with distinct eigenvalues can be transformed to an equivalent diagonal form. “Diagonal” here means the state matrix A\statemat is a diagonal matrix (all off-diagonal entries zero), not a reference to any architectural name. The key result comes from the DSS (Diagonal State Space) paper Gupta et al. (2022) : the change-of-basis VV that diagonalizes A\statemat via eigendecomposition can be absorbed into B\inputmat and C\outputmat without changing the system’s input-output behavior.

Proposition 5.1 (Diagonal expressiveness).

If A=VΛV1\statemat = V \Lambda V^{-1} with distinct eigenvalues Λ=diag(λ1,,λN)\Lambda = \diag(\lambda_1, \ldots, \lambda_\statedim), then the SSM (A,B,C)(\statemat, \inputmat, \outputmat) has the same transfer function as (Λ,V1B,CV)(\Lambda, V^{-1}\inputmat, \outputmat V). The transfer function G(z)=C(zIA)1BG(z) = \outputmat(zI - \statemat)^{-1}\inputmat captures the complete input–output behavior of the system — two SSMs with the same transfer function produce identical outputs for any input.

Proof.

The transfer function is G(z)=C(zIA)1BG(z) = \outputmat(zI - \statemat)^{-1}\inputmat. Substituting A=VΛV1\statemat = V\Lambda V^{-1}: G(z)=CV(zIΛ)1V1B=C~(zIΛ)1B~G(z) = \outputmat V(zI - \Lambda)^{-1}V^{-1}\inputmat = \tilde{\outputmat}(zI - \Lambda)^{-1}\tilde{\inputmat} where C~=CV\tilde{\outputmat} = \outputmat V and B~=V1B\tilde{\inputmat} = V^{-1}\inputmat.

Initialization variants

S4D stores eigenvalues as complex numbers: λk=ak+ibk\lambda_k = a_k + i \cdot b_k where ak<0a_k < 0 (decay) and bkb_k (oscillation frequency). Three initialization schemes:

S4D-Lin (default)

λk=12+iπk\lambda_k = -\tfrac{1}{2} + i \pi k for k=1,,N/2k = 1, \ldots, \statedim/2. [Convergence] S4D-Lin is the default initialization in the S4D, S5, and mamba-minimal reference codebases; other schemes are empirical variants.

Constant decay rate 1/2-1/2, linearly spaced frequencies. The simplest formula — one line of code. Each eigenvalue captures a different frequency band of the input signal. S4D-Lin is not the best performer on long-range tasks, but its simplicity makes it ideal for understanding the diagonal structure.

Example 5.2 (S4D-Lin at N = 8).

Four complex-conjugate pairs {0.5+ikπ:k=1,2,3,4}\{-0.5 + i k\pi : k = 1, 2, 3, 4\}. All share decay rate 0.5-0.5 but span frequencies π\pi through 4π4\pi. With Δ=0.1\stepsize = 0.1,

λˉk=exp(λkΔ)=e0.050.951(same for every k),\abs{\bar\lambda_k} = \abs{\exp(\lambda_k\stepsize)} = e^{-0.05} \approx 0.951 \quad \text{(same for every } k\text{)},

so every channel’s state decays by ~5% per step. The phase rotation is λˉk=0.1πk\angle\bar\lambda_k = 0.1\pi k radians per step: 0.1π0.1\pi, 0.2π0.2\pi, 0.3π0.3\pi, 0.4π0.4\pi. Each channel acts as a bandpass filter centered on its frequency, and their sum reconstructs any bandlimited signal the eight-dimensional basis can resolve.

S4D-Inv

λk=12+i/k\lambda_k = -\tfrac{1}{2} + i/k for k=1,,N/2k = 1, \ldots, \statedim/2.

Inverse frequency spacing concentrates resolution at low frequencies, which is better for long-range dependencies.

S4D-LegS

Extract the diagonal of the HiPPO-LegS eigendecomposition from Week 4: compute eigvals(AHiPPO)\operatorname{eigvals}(\statemat_{\text{HiPPO}}) and use the N/2\statedim/2 eigenvalues with most negative imaginary part.

Eigenvalue patterns for the three S4D initialization variants. S4D-Lin: uniformly spaced frequencies with constant decay. S4D-Inv: inverse frequency spacing concentrating resolution at low frequencies. S4D-LegS: eigenvalues extracted from HiPPO decomposition (approximately real).
Eigenvalue patterns for the three S4D initialization variants. S4D-Lin: uniformly spaced frequencies with constant decay. S4D-Inv: inverse frequency spacing concentrating resolution at low frequencies. S4D-LegS: eigenvalues extracted from HiPPO decomposition (approximately real).

Complex-valued state and stability by construction

S4D uses N/2\statedim/2 complex eigenvalue conjugate pairs. A conjugate pair means: if λk=a+bi\lambda_k = a + bi is an eigenvalue, then λk=abi\lambda_k^* = a - bi is also an eigenvalue (same real part, negated imaginary part). We only need to store one of each pair and reconstruct the other. This halves the parameter count and, crucially, guarantees real-valued output through the identity z+z=2Re(z)z + z^* = 2\operatorname{Re}(z). The output uses this conjugate symmetry trick:

yt=2Re ⁣(k=1N/2Ckhk,t)+Dut.y_t = 2 \cdot \operatorname{Re}\!\left(\sum_{k=1}^{\statedim/2} \outputmat_k \cdot \statevec_{k,t}\right) + \feedmat \cdot u_t.
Proposition 5.3 (Stability by construction).

With the parameterization Re(λk)=exp(log_w_realk)\operatorname{Re}(\lambda_k) = -\exp(\texttt{log\_w\_real}_k) and Im(λk)=w_imagk\operatorname{Im}(\lambda_k) = \texttt{w\_imag}_k, all continuous eigenvalues satisfy Re(λk)<0\operatorname{Re}(\lambda_k) < 0 for any finite parameter values. ZOH discretization then maps these inside the unit disk: λˉk=eRe(λk)Δ<1\abs{\bar{\lambda}_k} = e^{\operatorname{Re}(\lambda_k) \cdot \stepsize} < 1.

Proof.

exp(x)<0-\exp(x) < 0 for all xRx \in \R. The ZOH stability preservation follows from [?w4:prop:zoh-stability].

This is a crucial difference from Week 4: S4’s HiPPO initialization happens to be stable, but training could push eigenvalues out of the left half-plane. S4D’s log-parameterization makes continuous eigenvalue instability impossible by construction under this parameterization. [Official] The log-parameterization (λk)=exp(log_w_realk)\Re(\lambda_k) = -\exp(\texttt{log\_w\_real}_k) originates in the S4D paper Gu et al. (2022) and reproduces across S5, Mamba-1, and Mamba-2 reference code. Finite-precision accumulation, gradient overflow, and extreme-Δ\stepsize artifacts are still possible; stability-by-construction fixes only the sign of Re(λk)\operatorname{Re}(\lambda_k).

Element-wise computation

With diagonal A\statemat, all matrix operations collapse to element-wise:

Proposition 5.4 (Kernel complexity reduction).

For diagonal Aˉ=diag(λˉ1,,λˉN/2)\discA = \diag(\bar{\lambda}_1, \ldots, \bar{\lambda}_{\statedim/2}), the convolution kernel is:

K[]=2Re ⁣(k=1N/2C~keλkΔ),K[\ell] = 2 \cdot \operatorname{Re}\!\left(\sum_{k=1}^{\statedim/2} \tilde{\outputmat}_k \cdot e^{\lambda_k \stepsize \cdot \ell}\right),

where C~k=CkBkeλkΔ1λk\tilde{\outputmat}_k = \outputmat_k \inputmat_k \cdot \frac{e^{\lambda_k \stepsize} - 1}{\lambda_k} absorbs the ZOH correction. This is O(NL)\bigO{\statedim \seqlen} vs. O(N2L)\bigO{\statedim^2 \seqlen} for full A\statemat.

Proof.

Since Aˉ\discA is diagonal, Aˉk=diag(λˉ1k,)\discA^k = \diag(\bar{\lambda}_1^k, \ldots). The matrix product CAˉkB\outputmat \discA^k \inputmat decomposes into N/2\statedim/2 independent scalar products. The log-space computation eλkΔe^{\lambda_k \stepsize \cdot \ell} avoids explicit power iteration.

The recurrence also simplifies:

hk,t+1=λˉkhk,t+B~kut(element-wise, not matrix multiply).\statevec_{k,t+1} = \bar{\lambda}_k \cdot \statevec_{k,t} + \tilde{\inputmat}_k \cdot u_t \qquad \text{(element-wise, not matrix multiply)}.
def ssm_recurrent_diag(Ab_diag, B, C, D, u):
    def step(h, u_t):
        h_new = Ab_diag * h + B * u_t     # element-wise
        y_t = 2 * jnp.real(jnp.sum(C * h_new)) + D * u_t
        return h_new, y_t
    h0 = jnp.zeros_like(Ab_diag)
    _, y = jax.lax.scan(step, h0, u)
    return y

Stability region analysis

The [DYN] exercise: compare learned eigenvalue positions against integrator stability boundaries.

S4D training dynamics and stability analysis. Top-left: training loss (log scale). Top-right: discrete eigenvalues inside the unit disk (pre- and post-training). Bottom-left: continuous eigenvalues in the left half-plane. Bottom-right: phase portrait mapping eigenvalue position to decay half-life and oscillation period.
S4D training dynamics and stability analysis. Top-left: training loss (log scale). Top-right: discrete eigenvalues inside the unit disk (pre- and post-training). Bottom-left: continuous eigenvalues in the left half-plane. Bottom-right: phase portrait mapping eigenvalue position to decay half-life and oscillation period.

Reflections

What transfers

  • Diagonal = element-wise: The single most impactful simplification in the S4 lineage. Every subsequent architecture (S5, Mamba) uses diagonal state.
  • Stability by construction: The log-parameterization eliminates a class of training failures. This pattern reappears in LRU Orvieto et al. (2023) and Mamba.
  • Phase portrait analysis: Eigenvalue position → memory timescale is the key diagnostic for understanding what an SSM has learned.

What requires adaptation

  1. Complex arithmetic overhead. The conjugate-pair trick halves parameters but introduces complex-valued computation throughout the forward pass. JAX handles complex64 natively, but a common failure mode is dtype mismatch: a function expecting float32 receiving complex64 (or vice versa) produces silent wrong results or cryptic shape errors. Always check dtypes at module boundaries.

  2. Loss of the HiPPO interpretation. Full S4’s state vector has a clean theoretical meaning: each entry is a Legendre coefficient of the input history. Going diagonal discards this — the state becomes a collection of independently decaying oscillators with no polynomial approximation guarantee. This is an explicit theory-for-speed trade-off: the output is empirically similar, but the interpretability of the state is lost. S4D-LegS partially recovers the connection by initializing from HiPPO eigenvalues, but training immediately drifts away from the Legendre structure.

  3. Initialization choice becomes empirical. Unlike Week 4’s HiPPO (one principled answer derived from approximation theory), S4D offers three variants with different frequency-resolution trade-offs: S4D-Lin (uniform resolution), S4D-Inv (low-frequency emphasis), and S4D-LegS (HiPPO-derived). [Practitioner] Choosing between them requires benchmarking on your target task — there is no single “correct” initialization. This is the first point in the curriculum where design becomes empirical rather than principled.

Forward map

  • Week 6: S5 replaces lax.scan with associative scan for O(logL)\bigO{\log \seqlen} parallel depth. The diagonal structure from this week is required.
  • Week 7: Mamba makes B,C,Δ\inputmat, \outputmat, \stepsize input-dependent, breaking the convolutional view. The diagonal state and log-parameterization carry over.
  • Week 18: Lyapunov analysis of the eigenvalue dynamics during training — tracking how the learned eigenvalues drift over optimization steps.