Skip to content

State-space layers: S4 and Mamba

A linear state-space system turned into a sequence layer. How discretization turns it into a recurrence, why the same layer is also one long convolution, and what Mamba's input-dependent steps add.

Advanced ~14 min7 steps

What runs in CodeSplash

You will be able to

  • Discretize a diagonal state-space system with a step size Δ
  • Explain why a linear recurrence equals a convolution with its impulse response
  • Describe what makes Mamba selective, and what that costs
  • Say where state-space layers stand against attention

Read first

01

A linear system as a layer

Read

A state-space layer runs a small linear system along the sequence: a hidden state h is updated by the input u, and the output y reads the state.

hk=Aˉ hk−1+Bˉ uk,yk=C hkh_k = \bar A\, h_{k-1} + \bar B\, u_k, \qquad y_k = C\, h_k

Ā and B̄ come from a continuous system with matrices A and B and a step size Δ: for a diagonal A, Ā = exp(ΔA) and B̄ = (exp(ΔA) − 1)/A · B. Unlike an RNN there's no nonlinearity inside the recurrence; layers are stacked with nonlinear mixing in between. S4Structured state-space layer (S4)A sequence layer built from a linear state-space system; the same layer runs as a recurrence or as one long convolution. chose A's structure so the state can remember a long history stably.

02

Recurrence or convolution: same numbers

Read

Because the recurrence is linear, unrolling it gives y_k = Σ_j C Ā^j B̄ u_{k−j}: a convolution with the kernel K_j = C Ā^j B̄. Feed in a single 1 followed by zeros and the outputs are the kernel.

The program does that for a two-state system with Δ = 0.1: 0.0588, 0.0563, 0.0540, 0.0517, decaying slowly. Training can compute the whole kernel and convolve every position in parallel, like a TCN; prediction can step the recurrence one token at a time, like an RNN, with constant memory.

Python
import numpy as np

A = np.array([-.5, -1.]); B = np.array([1., .5]); C = np.array([.7, -.2]); dt = .1
Ab = np.exp(dt * A); Bb = np.expm1(dt * A) / A * B        # zero-order-hold discretization
h = np.zeros(2); y = []
for u in [1., 0., 0., 0.]:                                # an impulse
    h = Ab * h + Bb * u; y.append(float(C @ h))
print(y)                                                  # = the convolution kernel C Ā^j B̄
R
A <- c(-.5, -1); B <- c(1, .5); C <- c(.7, -.2); dt <- .1
Ab <- exp(dt * A); Bb <- expm1(dt * A) / A * B
h <- c(0, 0); y <- numeric(0)
for (u in c(1, 0, 0, 0)) { h <- Ab * h + Bb * u; y <- c(y, sum(C * h)) }
print(y)

Check that the scan and the convolution give the same numbers, then try Euler with a big step.

DExplorable · State space models: scan, kernel, selection

How does a state space model remember, and what does making it selective change?

How much of the final output's weight sits on the marked step a quarter of the way in?

How sure?

Model

Selective: the step size depends on the input (Mamba).

4
0.10
0.50

State n decays at rate decay × (n + 1).

Discretization

64

Input (time-invariant)

Selective mode always uses noise with one marked token.

Attention over L = 64 compares 4,096 pairs; the scan does about 256 state updates.

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

03

Mamba: steps that depend on the input

Read

In S4, Ā, B̄ and C are the same for every token, which is what makes the convolution possible. MambaSelective state-space model (Mamba)A state-space layer whose step size and input/output maps depend on the current token, so it can choose what to remember. makes Δ, B and C functions of the current token. A large Δ lets a token overwrite the state; a small one lets the state pass it by. The layer can now choose what to remember, something a fixed kernel can't.

The reference program's selective step, on inputs 1, 0, −1 and a half, gives 0.174, 0.172, 0.037 and 0.103. The price: no single kernel exists, so training needs a parallel scan written for the hardware.

Let the step depend on the input and watch the marked token stay in the state.

DExplorable · State space models: scan, kernel, selection

How does a state space model remember, and what does making it selective change?

How much of the final output's weight sits on the marked step a quarter of the way in?

How sure?

Model

Selective: the step size depends on the input (Mamba).

4
0.10
0.50

State n decays at rate decay × (n + 1).

Discretization

64

Input (time-invariant)

Selective mode always uses noise with one marked token.

Attention over L = 64 compares 4,096 pairs; the scan does about 256 state updates.

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

04

Fixed or selective?

Predict

Predict first

The task: remember the token that follows a special marker, wherever the marker appears in a long sequence. Which layer is built for that?

S4: its fixed kernel sees every tokenMamba: it can choose what to keepNeither can do it
Reveal

Mamba. A fixed kernel weights positions by distance only, the same for every sequence, so it can't single out "the token after the marker". Making Δ, B and C depend on the token is exactly what lets a layer store content selectively.

05

What runs here

Read
EntryIn CodeSplash
S4 (DNN024)Explained here: no method trains a state-space layer
Mamba (DNN025)Explained here

The reference programs are small component demos, not trained models. The time-series track's statistical state-space models share the equations but estimate a few interpretable parameters rather than learning a sequence layer.

Reference · DNN024 · Explained hereStructured State Space Sequence Model (S4)S4 learns long-range sequence transformations using a structured continuous-time state-space model and efficient convolutional computation. Reference · DNN025 · Explained hereMamba selective state-space modelMamba makes selected state-space parameters depend on the input, allowing the model to retain or discard information content-dependently. A hardware-aware scan supports efficient sequence processing;
06

Check: state-space layers

Checkpoint

Check yourself

Why can an S4 layer be trained as one long convolution?

Show the answer

Answer: a

Linearity plus time-invariance gives a fixed impulse response, and that's the convolution kernel.

Check yourself

In the discretized system, what does a smaller step size Δ do to Ā = exp(ΔA) for a negative A?

Show the answer

Answer: a

exp(ΔA) → 1 as Δ → 0. Small steps mean each token changes the state only a little and old information lasts longer.

Check yourself

How does an S4 layer generate one new token at a time after training?

Show the answer

Answer: a

The same layer has two equivalent forms: convolution for parallel training, recurrence for step-by-step use.

Check yourself

What makes Mamba "selective"?

Show the answer

Answer: a

Input-dependent Δ, B and C let the layer decide, token by token, how much to write into or read from its state.

Check yourself

What does Mamba give up by making its parameters depend on the input?

Show the answer

Answer: a

Without time-invariance there's no one kernel to convolve with. A hardware-aware scan recovers parallel training.

Check yourself

Can you train an S4 or Mamba layer in CodeSplash today?

Show the answer

Answer: b

No method trains a state-space layer. The recurrent forecaster's cells are GRU, LSTM and Elman.

07

Recap

Recap
  • A state-space layer is a discretized linear system: h ← Āh + B̄u, y = Ch.
  • Time-invariant (S4): one convolution kernel for parallel training, a recurrence for generation.
  • Selective (Mamba): Δ, B and C depend on the token, so it can choose what to remember, at the cost of the fixed kernel.
  • Both are explained here.

Concepts in this lesson