Mamba and State-Space Models: Sequence Modeling Without the Quadratic Bill

Self-attention has one bill that always comes due: it scales quadratically with sequence length. Doubling the context quadruples the compute of the attention matrix, and at generation time the KV cache grows with every token produced. For most current workloads this is manageable, which is why the transformer remains the default architecture. But the quadratic curve is a hard ceiling, and for domains like genomics, audio, and very long documents, a series of papers proposed a different bargain: give up exact recall over the entire history in exchange for linear-time training and a fixed-size memory. The most successful of these is Mamba, and understanding it requires going back to an idea from control theory — the state-space model.

A state-space model (SSM) describes a sequence with two equations borrowed straight from linear systems theory. A hidden state evolves through time, and an output is read off that state:

h'(t) = A · h(t) + B · x(t)     (state equation)
y(t)  = C · h(t) + D · x(t)     (output equation)

The input x(t) enters through the matrix B, the state remembers and decays through A, and C projects the state into the output. This is the same skeleton as an RNN — but where classic RNNs hit the wall of sequential training, SSMs from the S4 generation of models came with a trick: after discretizing the continuous equations with a step size Δ, the model has two equivalent computational forms. As a recurrence, it is O(1) per generated token at inference. As a global convolution with a computable kernel, it can be evaluated over the whole sequence in parallel during training — a reference implementation exists in the Mamba repository. Train with the convolution, generate with the recurrence, and you sidestep the RNN’s training problem entirely.

The Time-Invariance Problem

That duality only exists because the parameters A, B, and C are fixed for every timestep — the model is linear time-invariant (LTI). And this is precisely its limitation. Fixed parameters mean every token is processed identically: the model cannot look at the content of a token and decide to remember it or discard it. On synthetic tasks this failure is easy to demonstrate. In selective copying, where the model must reproduce meaningful tokens from a noisy input, LTI models cannot tell which tokens deserve retention. On induction heads — reproducing a pattern seen once earlier in the context — they fail because recalling an arbitrary position from history requires content-dependent addressing, which a fixed linear recurrence cannot express. Transformers solve both trivially, because attention weights are computed from the tokens themselves.

The insight in the Mamba paper is that you can restore content-awareness by making the parameters functions of the input. In what the paper calls a selective SSM (S6), the matrices B and C and the step size Δ are produced by small linear projections of the current token, so they change at every position. A stays learned and fixed — it still defines how the state decays — but how strongly each token writes into the state, and which parts of the state each output reads, now depend on the content.

The step size Δ is where the selectivity really lives. After discretization, Δ acts like a gate: a large Δ means the current token overwrites the state — focus on the present, forget the context; a small Δ means the input is ignored and the previous state passes through nearly untouched. The paper draws an explicit analogy to RNN gating mechanisms, and the effect is a learned, token-by-token decision about what the fixed-size state should hold. Stop words wash through; a user’s name or a key-value pair gets written and kept.

The Selective Scan

Selectivity has an immediate cost: with input-dependent parameters, the convolution form disappears, because a convolution assumes a kernel that does not change along the sequence. Training would fall back to a sequential loop over timesteps — the old RNN problem. Mamba’s answer is the selective scan, and it has two parts.

The first part is mathematical. The recurrence involves only elementwise multiplication and addition, so computing the whole sequence is a prefix scan, and scans are associative: states computed over the first half and the second half of the sequence can be merged, in any grouping, into the same result. That associativity allows a parallel scan algorithm — a tree of partial computations that runs in logarithmic depth on a GPU instead of one sequential loop.

The second part is memory-aware engineering. The naive implementation would materialize a tensor of shape (batch, length, dimensions, state_size) — the expanded hidden state at every timestep — which is far too large for the GPU’s fast SRAM and would thrash in and out of the slow high-bandwidth memory. The selective scan kernel instead keeps the scan running entirely in SRAM: only the small inputs (Δ, B, C projections and the fixed A) move through memory, and the backward pass recomputes intermediate states rather than storing them, trading cheap recomputation for memory traffic. Combined with kernel fusion — one fused kernel instead of many materialized intermediates — the paper reports the scan running roughly 40× faster than a straightforward implementation, and about 5× higher inference throughput than a comparable transformer, because generation never has to read a growing KV cache.

What the Fixed State Costs

The honest accounting is that a fixed-size state is lossy compression. Attention keeps every token available at full fidelity and pays quadratic cost for it; an SSM compresses history into a constant-size vector and pays in recall. Tasks demanding exact associative recall over long contexts — retrieving a specific needle from a long haystack — remain transformer territory, and language models are full of exactly such tasks. This is why production hybrids exist: architectures like Jamba interleave attention layers among Mamba blocks, getting linear scaling on most of the network while retaining attention where exact lookup matters. A follow-up paper, “Transformers are SSMs,” formalized the connection from the other direction by showing that structured SSMs and variants of attention are just two computational forms of one underlying class of semiseparable matrices — two literatures that looked opposed turned out to be points on one spectrum.

For practitioners, the takeaway is not that Mamba replaces the transformer, but that the transformer’s cost model is a choice, not a law. When sequences are extremely long, tokens arrive as a stream, or inference must scale across many concurrent sessions with modest memory, a selective SSM’s profile — linear training compute, constant state at generation time — is genuinely better suited. Whether the ecosystem converges on hybrids or pure SSMs, the selective scan’s lesson already holds: the memory hierarchy of the GPU is as much a design constraint for sequence models as the math is.

Leave a Reply

Your email address will not be published. Required fields are marked *