AI Grimoire
Sheet
statuspromising
difficultyadvanced
timeO(n·d·N)
memoryO(d·N)
described2021
revised2w ago

Selective State Space Models

A continuous linear system, discretised and run over the sequence. Constant state per step rather than a cache that grows with context — and, since Mamba, dynamics that depend on the input.

Standing

PromisingPromising and actively moving. The results are real but narrow — one lab, one model family, or one benchmark suite — and the picture may look different in a year.

Strong results in hybrid stacks. Nobody has yet shipped a frontier model without attention in it.

judged as of 2026-09 · what the labels mean

Theory

Start from a linear time-invariant system mapping a scalar input stream to an output through an NN-dimensional latent state.

h(t)=Ah(t)+Bx(t),y(t)=Ch(t)h'(t) = A\,h(t) + B\,x(t), \qquad y(t) = C\,h(t)
eq. 1 — continuous form

To run it on tokens you discretise with a step size Δ\Delta. Zero-order hold gives the matrices the recurrence actually uses.

Aˉ=exp(ΔA),Bˉ=(ΔA)1(exp(ΔA)I)ΔB\bar{A} = \exp(\Delta A), \qquad \bar{B} = (\Delta A)^{-1}\bigl( \exp(\Delta A) - I \bigr)\, \Delta B
eq. 2 — zero-order hold discretisation

With AA, BB, CC constant the recurrence is a convolution, computable for the whole sequence in one FFT. That is S4, and it is also its ceiling: a time-invariant system cannot choose what to remember, so it cannot solve selective copying or induction.

Selection

Mamba makes BB, CC and Δ\Delta functions of the current token. The system stops being time-invariant, the convolution disappears, and what remains is a recurrence that must be scanned.

Bt=WBxt,Ct=WCxt,Δt=softplus(WΔxt)B_t = W_B x_t, \qquad C_t = W_C x_t, \qquad \Delta_t = \mathrm{softplus}(W_\Delta x_t)
eq. 3 — the parameters become projections of the inputGu & Dao §3.2

Δt\Delta_t is the interesting one. Large Δ\Delta makes Aˉ0\bar{A} \to 0 and the state resets to the current input; small Δ\Delta makes AˉI\bar{A} \to I and the token is ignored. It is a gate in the LSTM sense, arrived at from the discretisation rather than bolted on.

The cost model inverts the transformer’s. Decoding is O(1)O(1) per token in context length with a fixed-size state, but that state is a lossy summary — exact retrieval from far back is the thing attention buys and this does not. Hybrids that interleave a few attention layers among many SSM layers are the current answer.

Implementation

python · torch — recurrence, not the scan kernel
import torch
from torch import Tensor


def selective_scan(
    x: Tensor,        # [B, L, D]   input
    A: Tensor,        # [D, N]      log-negative, learned
    B: Tensor,        # [B, L, N]   input-dependent
    C: Tensor,        # [B, L, N]   input-dependent
    delta: Tensor,    # [B, L, D]   input-dependent, softplus'd
) -> Tensor:
    batch, length, dim = x.shape
    state = torch.zeros(batch, dim, A.size(-1), device=x.device)
    out = []

    for t in range(length):
        dt = delta[:, t].unsqueeze(-1)                  # [B, D, 1]
        a_bar = torch.exp(dt * A)                       # [B, D, N]
        b_bar = dt * B[:, t].unsqueeze(1)               # [B, 1, N] -> broadcast
        state = a_bar * state + b_bar * x[:, t].unsqueeze(-1)
        out.append((state @ C[:, t].unsqueeze(-1)).squeeze(-1))

    return torch.stack(out, dim=1)

AA is stored as exp(Alog)-\exp(A_{\log}) so the eigenvalues stay negative and the system stays stable; Aˉ=exp(ΔA)\bar{A} = \exp(\Delta A) then lies in (0,1)(0, 1) and the state cannot blow up. The Bˉ\bar{B} above drops the (ΔA)1(exp(ΔA)I)(\Delta A)^{-1}(\exp(\Delta A) - I) factor for the first-order approximation ΔB\Delta B, which is what Mamba itself uses.

KV cache @ 32k
512 MB
SSM state
0.5 MB
Scaling in n
O(1)
decode state per layer, d = 4096, batch 1

Related

References

[1]Gu & Dao — Mamba: Linear-Time Sequence Modeling with Selective State Spaces (2023)arXiv:2312.00752
[2]Gu et al. — Efficiently Modeling Long Sequences with Structured State Spaces (2021)arXiv:2111.00396
[3]Dao & Gu — Transformers are SSMs: State Space Duality (2024)arXiv:2405.21060