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 , over the input, where the matrices , and are computed from the current input . 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 and the maps and 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 with a state ,
with , and (equation 1).
Discretization. With a step size , the zero-order hold gives
(equation 4), and the discrete recurrence , (equation 2). is diagonal with real entries by default, so and . The reference code uses the simpler for the input map.
Time-invariant case (S4). If , , and are the same at every step, unrolling the recurrence from gives with the kernel (equation 3), which can be computed with the fast Fourier transform.
Selection (S6). The input is ( steps, channels). Each channel has its own diagonal with entries. At each step,
so are shared by the channels and has one step per channel ( is a learned vector; the rank-1 map can be widened to rank ). Then for each channel and state entry ,
Because the matrices change with , there is no fixed kernel, and the layer is computed as a recurrence only (Algorithm 2).
The scan. Each step is the affine map , with and entrywise. Composing step and then step gives , an associative operation, so all states are the prefix results of a parallel prefix scan: work and depth.
Hardware-aware kernel. The expanded state has numbers for a batch of sequences. The kernel loads , , , and from the GPU main memory into on-chip memory, does the discretization, the scan and the product with there, and writes back only , of size . 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 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 sequences of length with channels and state size per channel:
- Training time. The scan does work. The projections that make , and and the linear maps of the block add . There is no term in .
- Training memory. With the fused kernel, the main memory holds numbers per layer, not the expanded state, which lives only in on-chip memory.
- Generation. Each new token costs per layer and needs only the state , of 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 numbers per layer after tokens and spends per token on attention.
- Depth. With a parallel scan the layer has sequential depth; run step by step, it has steps.
- The time-invariant case. An S4 layer with a fixed kernel costs 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: , and 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, and , the selective recurrence is the gate of a gated recurrent network with (Theorem 1 below). Unlike an LSTM or GRU, the state update is linear in , which is what allows the scan.
- Transformers. Attention keeps every past key and value and can look any of them up exactly, at cost in training and a cache that grows with . 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, , and can be computed with the same kind of scan.
Known results in the model
- Scan correctness (theorem). The operation is associative, and the -th prefix result applied to gives . 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 , , , and , the zero-order hold gives and , so with . A large resets the state to the current input; a small one keeps the state and ignores the input.
- Stability (theorem). If every and , then , 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 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
- 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)
- Gu, Goel and Ré 2022, the S4 layer, a linear time-invariant state space model computed as a long convolution
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag FBSQEB Edit this page History Report an error Markdown