Algorithm Search⌘K

Modelresearch

Selective state space model

Also called Mamba.

A linear recurrent layer whose state matrices depend on the input, computed by a parallel scan in time linear in sequence length.

A selective state space model is a sequence layer that runs a linear recurrence ht=Aˉtht−1+Bˉtxth_t = \bar A_t h_{t-1} + \bar B_t x_t, yt=Cthty_t = C_t h_t over the input, where the matrices Aˉt\bar A_t, Bˉt\bar B_t and CtC_t are computed from the current input xtx_t. Earlier structured state space models such as S4 use the same matrices at every step, which lets them run as one long convolution but stops them from choosing what to keep: they cannot ignore a filler token or reset on a boundary. Gu and Dao made the step size Δ\Delta and the maps BB and CC functions of the input (the S6 layer), gave up the convolution, and computed the recurrence with a parallel prefix scan in a fused GPU kernel. The cost is linear in the sequence length, and generation keeps a state of fixed size instead of a growing key-value cache. Their Mamba network stacks blocks built around this layer, with no attention.

The machine

Continuous system. For one input channel x(t)x(t) with a state h(t)∈RNh(t) \in \mathbb{R}^N,

h′(t)=A h(t)+B x(t),y(t)=C h(t),h'(t) = A\, h(t) + B\, x(t), \qquad y(t) = C\, h(t),

with A∈RN×NA \in \mathbb{R}^{N \times N}, B∈RN×1B \in \mathbb{R}^{N \times 1} and C∈R1×NC \in \mathbb{R}^{1 \times N} (equation 1).

Discretization. With a step size Δ>0\Delta > 0, the zero-order hold gives

Aˉ=exp⁡(ΔA),Bˉ=(ΔA)−1(exp⁡(ΔA)−I) ΔB\bar A = \exp(\Delta A), \qquad \bar B = (\Delta A)^{-1}(\exp(\Delta A) - I)\, \Delta B

(equation 4), and the discrete recurrence ht=Aˉht−1+Bˉxth_t = \bar A h_{t-1} + \bar B x_t, yt=Chty_t = C h_t (equation 2). AA is diagonal with real entries a1,…,aN<0a_1, \ldots, a_N < 0 by default, so Aˉ=diag(eΔan)\bar A = \mathrm{diag}(e^{\Delta a_n}) and Bˉn=(eΔan−1) bn/an\bar B_n = (e^{\Delta a_n} - 1)\, b_n / a_n. The reference code uses the simpler Bˉ=ΔB\bar B = \Delta B for the input map.

Time-invariant case (S4). If Δ\Delta, AA, BB and CC are the same at every step, unrolling the recurrence from h0=0h_0 = 0 gives y=x∗Kˉy = x * \bar K with the kernel Kˉ=(CBˉ,  CAˉBˉ,  CAˉ2Bˉ,…)\bar K = (C \bar B,\; C \bar A \bar B,\; C \bar A^2 \bar B, \ldots) (equation 3), which can be computed with the fast Fourier transform.

Selection (S6). The input is x∈RL×Dx \in \mathbb{R}^{L \times D} (LL steps, DD channels). Each channel cc has its own diagonal A(c)A^{(c)} with NN entries. At each step,

Bt=LinearN(xt),Ct=LinearN(xt),Δt=softplus(p+BroadcastD(Linear1(xt))),B_t = \mathrm{Linear}_N(x_t), \qquad C_t = \mathrm{Linear}_N(x_t), \qquad \Delta_t = \mathrm{softplus}\big(p + \mathrm{Broadcast}_D(\mathrm{Linear}_1(x_t))\big),

so Bt,Ct∈RNB_t, C_t \in \mathbb{R}^N are shared by the channels and Δt∈RD\Delta_t \in \mathbb{R}^D has one step per channel (pp is a learned vector; the rank-1 map can be widened to rank RR). Then for each channel cc and state entry nn,

ht,c,n=eΔt,c an(c) ht−1,c,n+Bˉt,c,n xt,c,yt,c=∑n=1NCt,n ht,c,n.h_{t,c,n} = e^{\Delta_{t,c}\, a^{(c)}_n}\, h_{t-1,c,n} + \bar B_{t,c,n}\, x_{t,c}, \qquad y_{t,c} = \sum_{n=1}^{N} C_{t,n}\, h_{t,c,n}.

Because the matrices change with tt, there is no fixed kernel, and the layer is computed as a recurrence only (Algorithm 2).

The scan. Each step is the affine map h↦αth+βth \mapsto \alpha_t h + \beta_t, with αt=Aˉt\alpha_t = \bar A_t and βt=Bˉtxt\beta_t = \bar B_t x_t entrywise. Composing step ss and then step tt gives (αtαs,  αtβs+βt)(\alpha_t \alpha_s,\; \alpha_t \beta_s + \beta_t), an associative operation, so all states h1,…,hLh_1, \ldots, h_L are the prefix results of a parallel prefix scan: Θ(L)\Theta(L) work and Θ(log⁡L)\Theta(\log L) depth.

Hardware-aware kernel. The expanded state has BLDNB L D N numbers for a batch of BB sequences. The kernel loads Δ\Delta, AA, BB, CC and xx from the GPU main memory into on-chip memory, does the discretization, the scan and the product with CC there, and writes back only yy, of size BLDB L D. In the backward pass the states are computed again instead of stored, as FlashAttention does with the attention matrix.

The block. The Mamba block widens the channels with a linear map, applies a short causal convolution, a nonlinearity and the selective SSM, multiplies the result by a gated branch of the input, and maps back to DD channels. Blocks are stacked with normalization and residual connections, and a language model puts a softmax over the vocabulary on top, trained by causal language modeling.

What it counts

For a batch of BB sequences of length LL with DD channels and state size NN per channel:

  • Training time. The scan does Θ(BLDN)\Theta(B L D N) work. The projections that make BtB_t, CtC_t and Δt\Delta_t and the linear maps of the block add Θ(BLD2)\Theta(B L D^2). There is no term in L2L^2.
  • Training memory. With the fused kernel, the main memory holds Θ(BLD)\Theta(B L D) numbers per layer, not the Θ(BLDN)\Theta(B L D N) expanded state, which lives only in on-chip memory.
  • Generation. Each new token costs Θ(DN+D2)\Theta(D N + D^2) per layer and needs only the state hth_t, of DND N numbers per layer (plus a few inputs for the short convolution), whatever the number of tokens seen. A transformer with a key-value cache stores 2td2 t d numbers per layer after tt tokens and spends Θ(td)\Theta(t d) per token on attention.
  • Depth. With a parallel scan the layer has Θ(log⁡L)\Theta(\log L) sequential depth; run step by step, it has LL steps.
  • The time-invariant case. An S4 layer with a fixed kernel costs Θ(Llog⁡L)\Theta(L \log L) per channel by FFT; the selective layer cannot use this route.

Relation to other models

  • State space models and the Kalman filter. The same linear state equation, but here it is deterministic, discretized from continuous time, and learned as a layer of a network, not a model of noisy observations.
  • Structured SSMs (S4 and its diagonal forms). These are the time-invariant special case: Δ\Delta, BB and CC do not depend on the input. Selection gives up the convolution form for the ability to act on content.
  • Gated recurrent networks. With one state entry, A=−1A = -1 and B=1B = 1, the selective recurrence is the gate ht=(1−gt)ht−1+gtxth_t = (1 - g_t) h_{t-1} + g_t x_t of a gated recurrent network with gt=σ(Linear(xt))g_t = \sigma(\mathrm{Linear}(x_t)) (Theorem 1 below). Unlike an LSTM or GRU, the state update is linear in hh, which is what allows the scan.
  • Transformers. Attention keeps every past key and value and can look any of them up exactly, at Θ(L2)\Theta(L^2) cost in training and a cache that grows with tt. A selective SSM compresses the past into a fixed-size state, at linear cost; Section 3.1 of Gu and Dao frames this as the trade-off between efficiency and the size of the state.
  • Hidden Markov models. The forward algorithm of an HMM is also a linear recurrence in a state vector whose matrix depends on the current input, αt=diag(b(xt)) T⊤αt−1\alpha_t = \mathrm{diag}(b(x_t))\, T^\top \alpha_{t-1}, and can be computed with the same kind of scan.

Known results in the model

  • Scan correctness (theorem). The operation (α2,β2)∘(α1,β1)=(α2α1,  α2β1+β2)(\alpha_2, \beta_2) \circ (\alpha_1, \beta_1) = (\alpha_2 \alpha_1,\; \alpha_2 \beta_1 + \beta_2) is associative, and the tt-th prefix result applied to h0h_0 gives hth_t. So any parallel prefix algorithm computes the same states as the sequential loop, in exact arithmetic.
  • Gating as a special case (theorem). Theorem 1 of Gu and Dao: with N=1N = 1, A=−1A = -1, B=1B = 1, Δt=softplus(zt)\Delta_t = \mathrm{softplus}(z_t) and zt=Linear(xt)z_t = \mathrm{Linear}(x_t), the zero-order hold gives Aˉt=e−Δt=1/(1+ezt)=1−σ(zt)\bar A_t = e^{-\Delta_t} = 1/(1 + e^{z_t}) = 1 - \sigma(z_t) and Bˉt=1−e−Δt=σ(zt)\bar B_t = 1 - e^{-\Delta_t} = \sigma(z_t), so ht=(1−gt)ht−1+gtxth_t = (1 - g_t) h_{t-1} + g_t x_t with gt=σ(zt)g_t = \sigma(z_t). A large Δt\Delta_t resets the state to the current input; a small one keeps the state and ignores the input.
  • Stability (theorem). If every an<0a_n < 0 and Δt>0\Delta_t > 0, then 0<eΔtan<10 < e^{\Delta_t a_n} < 1, so the state cannot grow without bound when the inputs are bounded.
  • Selective copying (measurement). On the task of copying tokens placed at random gaps among filler tokens, a time-invariant S4 layer reached 18.3% accuracy and the selective S6 layer 97.0%; the full Mamba network with S6 reached 99.8% (Section 4.1.1).
  • Induction heads (measurement). Trained on sequences of 256 tokens, the Mamba network solved the induction-head task on test sequences up to 2202^{20} tokens, about 4000 times longer; the other methods tested did not go beyond about twice the training length (Section 4.1.2).
  • Language modeling (measurement). Gu and Dao report that their 3B-parameter Mamba model beats transformers of the same size and matches transformers twice its size, in pre-training perplexity and downstream tasks, with 4 to 5 times higher generation throughput than a transformer of similar size (Sections 4.2 and 4.5).

References

  1. Gu and Dao 2024, Section 2 (equations 1 to 4, zero-order hold, linear time invariance), Section 3.1 (selection as compression), Section 3.2 (Algorithms 1 and 2, s_B, s_C, s_Delta, softplus), Section 3.3.2 (the hardware-aware scan), Section 3.4 (the block), Section 3.5.1 (Theorem 1), Section 3.6 (real diagonal A), Sections 4.1.1 and 4.1.2 (selective copying, induction heads), Sections 4.2 and 4.5 (language modeling, speed)
  2. Gu, Goel and Ré 2022, the S4 layer, a linear time-invariant state space model computed as a long convolution
  3. Blelloch 1990, the work-efficient parallel scan for an associative operator, and its use for first-order linear recurrences

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/FBSQEB/text.yaml