Search

Search papers and lessons

Selective State Space Model

Level 4 · 14 min read

A selective state space model lets the step size and the input and output projections depend on the current token, so it can choose what to keep in its state instead of applying one fixed rule everywhere.

On this page

  • Colour = topic mix
  • Calculus
  • Linear algebra
  • Architectures
  • Discrete math
  • Sign in to see your progress

TL;DR

A state space model updates a hidden state with the same linear rule at every step, so it cannot decide that one token matters more than another. A selective state space model makes three of its parameters, the input projection, the output projection and the step size, functions of the current input. The update then acts like a learned gate: it can write a token strongly into the state, or let the token pass without changing the state. The price is that the model is no longer a fixed convolution, so it needs a different algorithm to run fast.

Where you'll see it: the Mamba family of language models, which use this layer in place of attention for long sequences.

Intuition

A notebook that decides what to write down

The previous lesson described a notebook that is rewritten at every tick with the same rule. Suppose the rule is fixed, and the notebook holds a short summary. Every word gets the same weight whether it is a name, a date or a filler word. Over a long text, the important facts are overwritten by the stream of filler.

A selective model looks at each word before deciding how to update. For a filler word it writes almost nothing and keeps the old notes. For a key word it overwrites part of the notebook. The notebook is still small, so it still cannot hold everything. What changes is that the choice of what to keep depends on the content.

This is the central idea of the Mamba paper. Gu and Dao call it selectivity. They argue that sequence models trade effectiveness against efficiency through how well they compress context into a state. Attention keeps everything and is slow at generation time. A fixed state space model is fast but cannot pick. A selective one aims for both: a small state that is filled with the right things.

Why a gate is the same as a step size

A recurrent network with gates already decides how much of the old state to keep and how much new input to let in. The paper shows that this gating is a special case of discretising a continuous system with an input-dependent step size. A larger step size means the new input is written more strongly and the old state decays more. A smaller step size means the state is kept and the input barely registers. So making the step size depend on the input gives the same effect as a learned gate, but derived from the discretisation rather than added by hand.

The math

From LTI to selective

A linear time-invariant (LTI) model uses the same continuous parameters A\mathbf{A}, B\mathbf{B}, C\mathbf{C} and step size Δ\Delta at every position. Discretising them gives fixed A‾\overline{\mathbf{A}} and B‾\overline{\mathbf{B}}, as in the previous lesson. The selective version makes the parameters depend on the input token xtx_t:

Δt=τΔ(Linear1(xt)),Bt=LinearN(xt),Ct=LinearN(xt),\Delta_t = \tau_\Delta\big(\mathsf{Linear}_1(x_t)\big), \qquad \mathbf{B}_t = \mathsf{Linear}_N(x_t), \qquad \mathbf{C}_t = \mathsf{Linear}_N(x_t),

where τΔ\tau_\Delta is the softplus function, softplus(z)=log⁡(1+ez)\mathrm{softplus}(z) = \log(1 + e^{z}), which keeps the step size positive. Lineard\mathsf{Linear}_d is a learned projection to dimension dd. The paper also broadcasts Δt\Delta_t across channels, and it keeps A\mathbf{A} fixed as a learned, input-independent matrix. The discrete recurrence then uses the zero-order hold (ZOH) rule for each step:

A‾t=exp⁡(ΔtA),B‾t=(ΔtA)−1(exp⁡(ΔtA)−I) ΔtBt,\overline{\mathbf{A}}_t = \exp(\Delta_t \mathbf{A}), \qquad \overline{\mathbf{B}}_t = (\Delta_t \mathbf{A})^{-1}\big(\exp(\Delta_t \mathbf{A}) - \mathbf{I}\big)\,\Delta_t \mathbf{B}_t, ht=A‾t ht−1+B‾t xt,yt=Ct ht.h_t = \overline{\mathbf{A}}_t\, h_{t-1} + \overline{\mathbf{B}}_t\, x_t, \qquad y_t = \mathbf{C}_t\, h_t.

Every quantity with a tt subscript depends on the input at that step. This is what the paper means by a time-varying model.

The gated special case

Take a single state dimension with N=1N = 1, A=−1\mathbf{A} = -1, B=1\mathbf{B} = 1, and Δt=softplus(zt)\Delta_t = \mathrm{softplus}(z_t) where zt=Linear(xt)z_t = \mathsf{Linear}(x_t) is a scalar. Two identities of softplus give exp⁡(−Δt)=σ(−zt)\exp(-\Delta_t) = \sigma(-z_t) and 1−exp⁡(−Δt)=σ(zt)1 - \exp(-\Delta_t) = \sigma(z_t), where σ\sigma is the logistic function. So the recurrence becomes

ht=(1−σ(zt)) ht−1+σ(zt) xt.h_t = \big(1 - \sigma(z_t)\big)\, h_{t-1} + \sigma(z_t)\, x_t.

This is a gated leaky integrator. The gate σ(zt)\sigma(z_t) is the fraction of the new input written into the state, and 1−σ(zt)1 - \sigma(z_t) is the fraction of the old state kept. A gate near 1 overwrites the state; a gate near 0 keeps it. This is the same form as a classical gated recurrent update.

−6−4−2024600.20.40.60.81z = Linear(x_t)fraction
  • write gate sigma(z)
  • keep fraction 1 - sigma(z)
In the gated case, a token with large z is written into the state and a token with very negative z is ignored. The two fractions always sum to 1.

Parallel computation

A fixed model can be computed as a convolution, because A‾\overline{\mathbf{A}} and B‾\overline{\mathbf{B}} do not change with position. A selective model cannot, since its kernel would depend on every input. The paper keeps the recurrence and computes it with a parallel scan. A linear recurrence of the form ht=atht−1+bth_t = a_t h_{t-1} + b_t can be combined pairwise in any grouping, so a scan computes all states in logarithmic depth. The selective recurrence is a linear recurrence with input-dependent coefficients, so the same scan applies.

The scan still writes large intermediate tensors. The paper fuses the discretisation and the scan into one GPU kernel that keeps the expanded state in fast on-chip memory, and it recomputes intermediate states during the backward pass rather than storing them. The authors report that the resulting layer needs memory comparable to an optimised attention implementation.

Worked examples

Example
Keeping a key token through filler

Use the gated case with two kinds of token. The key token has z=4z = 4, so σ(4)≈0.98201\sigma(4) \approx 0.98201. A filler token has z=−4z = -4, so σ(−4)≈0.01799\sigma(-4) \approx 0.01799 and 1−σ(−4)≈0.982011 - \sigma(-4) \approx 0.98201. Start from h0=0h_0 = 0.

  1. Key token with input x1=1x_1 = 1: h1=(1−0.98201)×0+0.98201×1=0.98201h_1 = (1 - 0.98201) \times 0 + 0.98201 \times 1 = 0.98201.
  2. Filler token with input x2=0x_2 = 0: h2=0.98201×0.98201+0.01799×0=0.96435h_2 = 0.98201 \times 0.98201 + 0.01799 \times 0 = 0.96435.
  3. Filler token with x3=0x_3 = 0: h3=0.98201×0.96435=0.94701h_3 = 0.98201 \times 0.96435 = 0.94701.
  4. Filler token with x4=0x_4 = 0: h4=0.98201×0.94701=0.92998h_4 = 0.98201 \times 0.94701 = 0.92998 (rounded).

After three filler tokens the key is still at about 0.93. For comparison, a fixed model with A‾=0.5\overline{A} = 0.5 and B‾=0.5\overline{B} = 0.5 would write the key as 0.5 and then decay it to 0.5×0.53=0.06250.5 \times 0.5^3 = 0.0625 over the same three steps. The selective model keeps the key because its gate on the filler tokens is near zero.

Interpretation: the difference is not in the size of the state but in what the update does to it. The same state size can hold a key fact across a long gap if the model learns gates that close on filler.

Example
Reading the scan with two steps

Use the linear recurrence ht=atht−1+bth_t = a_t h_{t-1} + b_t with h0=0h_0 = 0 and two steps: (a1,b1)=(0.5,1)(a_1, b_1) = (0.5, 1) and (a2,b2)=(0.8,2)(a_2, b_2) = (0.8, 2).

  1. Sequentially: h1=0.5×0+1=1h_1 = 0.5 \times 0 + 1 = 1, and h2=0.8×1+2=2.8h_2 = 0.8 \times 1 + 2 = 2.8.
  2. Combine the two steps into one pair: the composite coefficient is a2a1=0.4a_2 a_1 = 0.4 and the composite offset is a2b1+b2=0.8×1+2=2.8a_2 b_1 + b_2 = 0.8 \times 1 + 2 = 2.8. Applying the composite to h0=0h_0 = 0 gives 0.4×0+2.8=2.80.4 \times 0 + 2.8 = 2.8.
  3. Both methods give h2=2.8h_2 = 2.8.

Interpretation: because the pairs (a,b)(a, b) compose associatively, the steps can be grouped in a tree and computed in parallel. That is what makes the time-varying recurrence trainable on a GPU.

Tradeoffs and failure modes

Selectivity buys content-aware memory, but it costs the structure that made state space models simple. The table lists the main trade-offs.

SituationWhat goes wrongBetter choice
Long natural language where a few facts must be recalledA fixed-size state still loses information; selection helps but cannot store everythingKeep some attention layers in a hybrid, or enlarge the state
Exact copying or lookup of arbitrary earlier tokensThe state is compressed, so verbatim retrieval is weaker than with a KV cacheAttention, which stores the full context
Smooth continuous signals such as audio where content gating adds littleExtra parameters and a more complex kernel buy littleAn LTI state space model with the convolution mode
Implementations without a fused scanMaterialising the expanded state in memory makes it slow and memory-hungryUse the fused kernel from the reference implementation
Poor initialisation of the step size or A\mathbf{A}Gates may close too early or never close, so the model forgets or ignores inputInitialise on a range that gives a spread of time scales, and test on a synthetic recall task first

Two further points matter in practice. Because the model is no longer a convolution, the fast FFT-based training used by S4 does not apply, and you need a scan kernel. And the paper's speed claims are about specific hardware and sizes. The authors report that their scan is about 40 times faster than a standard PyTorch scan in training, and that Mamba generates text with about 5 times the throughput of a Transformer of comparable size. Those are their measurements on an A100 GPU, not general facts.

History

The state space line began in control and signal processing, and it reached sequence modelling through the S4 model of Gu, Goel and Re in 2021. S4 gave a structured parameterisation of A\mathbf{A} and an efficient convolution, and it did well on long-range benchmarks. Its parameters were still fixed across positions.

In late 2023 Gu and Dao published Mamba. Its key change was to make Δ\Delta, B\mathbf{B} and C\mathbf{C} depend on the input, which the paper calls the selection mechanism. Making the parameters input-dependent removes the convolution, so they replaced it with a hardware-aware parallel scan and recomputation. The paper also simplified the surrounding architecture by stacking one repeated block without separate attention or MLP blocks. The authors place the idea in the same family as the gating of recurrent networks, long known from the LSTM and GRU, and argue that discretisation is the principled basis for that gating.

Check yourself

QuestionRecall: which parameters of the state space model become functions of the input in the selective version?Show answer

The step size Δt\Delta_t, the input projection Bt\mathbf{B}_t and the output projection Ct\mathbf{C}_t depend on the current token. The matrix A\mathbf{A} stays a learned, input-independent parameter. Making the step size input-dependent is what gives the update its gate-like behaviour.

QuestionComputation: with N=1N = 1, A=−1\mathbf{A} = -1, B=1\mathbf{B} = 1, and a gate value zt=0z_t = 0 at every step, start from h0=0h_0 = 0 and feed the input x=(2,0)x = (2, 0). What are h1h_1 and h2h_2?Show answer

At z=0z = 0 the gate is σ(0)=0.5\sigma(0) = 0.5, so A‾=0.5\overline{A} = 0.5 and B‾=0.5\overline{B} = 0.5. Then h1=0.5×0+0.5×2=1h_1 = 0.5 \times 0 + 0.5 \times 2 = 1, and h2=0.5×1+0.5×0=0.5h_2 = 0.5 \times 1 + 0.5 \times 0 = 0.5. Both values are the state after the update, with the old state halved at each step.

QuestionWhy: why can a selective SSM not be computed as a single fixed convolution the way an LTI SSM can?Show answer

A convolution uses one kernel for every position, and the kernel is built from the fixed matrices A‾\overline{\mathbf{A}} and B‾\overline{\mathbf{B}}. In the selective model each step has its own A‾t\overline{\mathbf{A}}_t and B‾t\overline{\mathbf{B}}_t, so the effective kernel depends on the input sequence and cannot be precomputed. The recurrence is still linear in the state, so it is computed with a parallel scan instead.

QuestionApplied: a team is building a model for DNA sequences, where long stretches are mostly repetitive and a few short motifs matter. Why might a selective state space model suit this better than a fixed one?Show answer

A fixed model applies the same update to repetitive filler and to the motifs, so the motifs can be diluted in the state. A selective model can learn gates that close on the repetitive stretches and open on motifs, so it writes the motifs in and keeps them. The team should still check recall on a task with planted motifs, because the state size limits how much can be kept, and the gains depend on whether the gates learn that split.

Sources