Mamba and Selective State Spaces
S4's linear state-space layers process every sequence with the same fixed dynamics regardless of content. Mamba makes those dynamics depend on the current input itself, letting the model choose what to remember and what to skip — while keeping the linear-time efficiency that made S4 attractive over attention.
Prerequisites: State-Space Models and S4, LSTM Gate Mechanics: Forget, Input, Output
An S4 layer's transition matrices are fixed after training — every input sequence, whatever it contains, gets pushed through the exact same linear dynamics. That is precisely what makes S4 fast (the whole sequence can be pre-computed as one convolution), but it is also a real limitation: the model cannot look at an input and decide, on the fly, "this particular token matters a lot, keep it in memory" versus "this one is noise, let it decay quickly." An LSTM's gates can do exactly that — decide, input by input, what to keep — but at the cost of a sequential, step-by-step recurrence that can't be pre-computed as a parallel convolution. Mamba's contribution is making the state-space transition matrices themselves depend on the current input, recovering LSTM-style content-aware selectivity, while finding a different computational trick (not the convolution trick, since input-dependent dynamics break that) to keep training fast anyway.
The analogy: a highlighter that knows what matters
An S4 layer is like reading a document with a fixed rule: highlight every fifth word, no matter what the document says. It's fast and mechanical, but it can't tell a critical number from a filler word — both get the same treatment. An LSTM is like a careful human reader who highlights based on judgment, deciding word by word what's important, but who has to read strictly left to right, one word at a time, which is slow over a long document. Mamba is a reader who highlights based on judgment — content-aware, like the human — but who has found a way to do that highlighting so efficiently that reading a whole book still runs at nearly the speed of the mechanical fixed-rule approach. The selectivity of an LSTM's gates, at close to the speed of S4's parallel scan.
The maths
Recall S4's discretized recurrence, where , are fixed matrices shared across every timestep:
Mamba's core change is to make the parameters that control this recurrence functions of the current input , rather than constants:
In words: instead of one fixed and used at every step, small learned linear layers , , look at the current input and produce input-specific versions of these parameters at every timestep. (the discretization step size) is the most important of the three — it directly controls how much the current input is allowed to affect the state versus how much of the old state is preserved.
The recurrence becomes:
In words: at each step, the effective transition and input matrices are recomputed based on what the input actually is, right before being applied. When is small for a given input, stays close to the identity — the state barely updates, meaning this input is effectively skipped, its effect on memory minimal. When is large, the state updates substantially — this input is treated as important and strongly written into memory. This is functionally the same job an LSTM's forget and input gates do, but arrived at through a different mechanism: a continuous discretization step size rather than a discrete sigmoid gate.
The catch: because now changes at every timestep (it depends on , which depends on ), the neat trick that made S4 fast — collapsing the whole sequence into one fixed convolution kernel computed once — no longer works, since there is no single fixed kernel anymore. Mamba instead uses a hardware-aware parallel scan: a way of computing the sequential recurrence that still parallelizes across the sequence length (it's mathematically an associative operation, so partial results can be combined out of order and merged), implemented to minimize slow memory transfers on a GPU. The output is the same sequential-recurrence answer, computed with much better hardware utilization than a naive step-by-step loop.
Worked example 1: input-dependent Δ changes the outcome
Take a 1-D toy state (as in the S4 page), with (a standard discretization form) and , , starting from . Feed two inputs, (an unusual, informative spike) and (an identical value, but suppose the model has learned it's routine noise in this context, so it produces a much smaller for it).
Suppose (large, "this matters") and (small, "skip this"):
But with small, the input contribution is also scaled down proportionally in a real Mamba implementation (via , not just ) — the point being that the same raw value produces a very different state update depending on what the model assigns it. An S4 layer, by contrast, would apply the exact same , to both inputs regardless of their context, because those matrices are fixed constants, not functions of .
Worked example 2: a selective copy task
Consider a synthetic test that's standard in this literature: a sequence with a handful of meaningful tokens scattered among many "filler" tokens, where the task is to recall the meaningful tokens later in the sequence, in order, ignoring the filler entirely — say, positions hold real values and everything else is a repeated filler symbol.
A fixed-dynamics model (plain S4, or a plain RNN) processes filler and meaningful tokens identically — every position updates the state by the same rule, so the filler dilutes the state's capacity right alongside the meaningful signal, since nothing in the recurrence itself knows to treat position 3's content differently from position 4's. A selective model can learn to assign near-zero to filler tokens (barely updating state on them) and a large to the three meaningful ones (writing them cleanly into state), so the state ends up holding close to just the three values that matter, undiluted by the hundred-plus filler positions in between. This filtering behavior — telling signal from filler based on content — is exactly the class of task where selective state spaces measurably outperform plain S4, and it's a direct, testable consequence of making input-dependent rather than fixed.
Selectivity is a routing decision made per input, similar in spirit to how a classifier's decision boundary treats different regions of input space differently rather than applying one uniform rule everywhere — Mamba's is effectively learning where in input space to "turn the state update on" versus "leave it alone."
Mamba's one change from S4 — making , , and especially the step size functions of the current input rather than fixed constants — restores the content-aware selectivity that gating architectures like the LSTM have, without falling back to a slow, unparallelizable step-by-step recurrence. The trade is a more complex training algorithm (a parallel scan) in exchange for a strictly more expressive model at close to the same asymptotic cost.
What this means in practice
Selective state-space models are attractive wherever S4-scale sequence lengths matter (very long tick histories, full order-book sessions) and the sequence genuinely contains a mix of important and irrelevant content that a fixed-dynamics model can't tell apart — for instance, a session dominated by quiet, low-information ticks punctuated by a handful of information-bearing prints. Where a sequence is fairly uniform in importance throughout, plain S4's simplicity and pure-convolution training speed may still be the more practical choice, since Mamba's added selectivity has more moving parts to tune and debug.
The trap: treating purely as a hyperparameter to tune globally, the way you might tune a fixed discretization step for a numerical ODE solver. In Mamba, is learned per input, per timestep — it is a function of the data, not a single number set before training. Fixing it to a constant collapses the model back toward plain S4 and throws away the entire selectivity mechanism that is Mamba's reason for existing. A second common confusion: selective state-space models are often pitched as "attention-free," which is true in the sense that there's no explicit query-key-value comparison — but the selective gating still gives the model a form of content-based routing that plays a similar functional role to attention's content-based lookup, just computed very differently and at linear rather than quadratic cost.
Practice in interviews
Further reading
- Gu & Dao, Mamba: Linear-Time Sequence Modeling with Selective State Spaces (2023)
- Gu, Goel & Ré, Efficiently Modeling Long Sequences with Structured State Spaces (2021)