Third ML Deep Dive post, and the first one that isn't really about attention. Mixture of Experts and grouped-query attention both took attention-based Transformers as the fixed starting point and optimized something around them — which experts activate, how many K/V heads exist. Mamba (Gu and Dao, 2023/2024) and the state space models it's built on start from a different baseline entirely: a linear recurrence, descended from classical control theory, that processes a sequence in time linear in its length instead of attention's quadratic cost.
What a state space model actually is
A state space model starts from a genuinely old idea: a continuous
system where a latent state h(t) evolves according to
h'(t) = Ah(t) + Bx(t), and the output is read off that state as
y(t) = Ch(t). Discretized for a sequence of real tokens — using a
step size Δ and a zero-order hold — this becomes a linear
recurrence that should look familiar: h_t = Āh_{t-1} + B̄x_t,
y_t = Ch_t. That's an RNN. The interesting part is what happens when
A, B, and C are held fixed across the whole sequence: the
same linear recurrence can also be computed as one global
convolution, y = x * K̄, with a convolution kernel built once from
A, B, C. Prior structured SSMs, like S4, depend on exactly this:
train using the convolution form (parallelizable, the whole sequence
visible at once), then switch to the recurrence form for efficient
one-token-at-a-time inference. The paper calls this property linear
time invariance (LTI) — "the model's dynamics are constant through
time" — and is explicit that it wasn't optional: "thus far, all
structured SSMs have been LTI... because of fundamental efficiency
constraints."
The weakness that fixed dynamics can't fix
Mamba's starting observation is that fixed, input-independent dynamics
are also a ceiling on what the model can do: "a key weakness of such
models is their inability to perform content-based reasoning." An LTI
system applies the identical transformation to every token regardless
of what that token actually is — it has no mechanism to decide "forget
this, keep that" based on content, because A, B, and C never
change their behavior in response to what's flowing through them.
Attention doesn't have this problem; a state space model, as built so
far, does.
Selection: let the parameters depend on the input
Mamba's fix is to stop treating Δ, B, and C as constants:
"simply letting the SSM parameters be functions of the input addresses
their weakness with discrete modalities," which lets the model
"selectively propagate or forget information along the sequence length
dimension depending on the current token." This is the paper's
selection mechanism, and the name Mamba comes from the resulting
architecture built around it.
It isn't free. Making Δ, B, C input-dependent means "these
parameters now have a length dimension... meaning that the model has
changed from time-invariant to time-varying," and the paper is direct
about the consequence: "this loses the equivalence to convolutions,
with implications for its efficiency." The one trick that made fixed
SSMs fast — compute the whole sequence as one precomputed convolution
— is gone the moment the dynamics depend on content. A selective SSM
is stuck with the recurrence, computed one step at a time, which is
naively far slower.
A FlashAttention-shaped fix, for a non-attention problem
Mamba's answer to that is a hardware-aware parallel scan, and the
paper draws the comparison explicitly: the goal is to "materialize the
state h only in more efficient levels of the memory hierarchy,"
because, same as in FlashAttention,
"most operations (except matrix multiplication) are bounded by memory
bandwidth." Concretely: "we load the SSM parameters (Δ, A, B,
C) directly from slow HBM to fast SRAM, perform the discretization
and recurrence in SRAM, and then write the final outputs... back to
HBM," recomputing intermediate states during the backward pass instead
of storing them, rather than ever materializing the full
sequence-length state in slow memory. The recurrence itself stays
parallelizable despite being sequential in principle, via a
work-efficient parallel scan algorithm. The paper's own framing makes
the parallel to this blog's first Deep Dive post explicit: "the fused
selective scan layer has the same memory requirements as an optimized
transformer implementation with FlashAttention." Same playbook —
respect the GPU's memory hierarchy instead of fighting it — applied to
a completely different kind of computation.
What it buys
Mamba reports 5x higher generation throughput than Transformers of similar size, scaling linearly in sequence length against attention's quadratic cost, and — on a synthetic induction-heads task — "generalizes perfectly to million-length sequences, or 4000x longer than it saw during training, while no other method goes beyond 2x." On language modeling, trained on 300B tokens of the Pile with the same tokenizer and setup as its Transformer baselines, "Mamba-3B outperforms Transformers of the same size and matches Transformers twice its size." The paper's own summary of where that leaves the field: "Mamba is the first linear-time sequence model that truly achieves Transformer-quality performance."
Mamba-2: SSMs and attention turn out to be related after all
A follow-up paper, Mamba-2 (Dao and Gu, ICML 2024), finds something that reframes the whole comparison: state space models and a form of attention aren't two unrelated architecture families competing for the same job. The paper develops a "rich framework of theoretical connections between SSMs and variants of attention," built on showing "an equivalence between state space models and a well-studied family of structured matrices called semiseparable matrices" — the two architectures turn out to be dual views of the same underlying structured-matrix computation, which the paper names structured state space duality (SSD). It's worth being precise about scope here: the paper's own footnote says "these connections only relate to certain flavors of attention" — specifically linear, kernel-style attention, not the softmax attention this blog's other posts have covered.
That theoretical connection pays off practically: the SSD-based Mamba-2 architecture "is 2-8x faster than the optimized selective scan implementation of Mamba, while simultaneously allowing for much larger recurrent state sizes," and is reported as "highly competitive" with FlashAttention-2, "crossing over at sequence length 2K and 6x faster at sequence length 16K." The speedup comes partly from shifting the computation onto matrix-multiplication hardware rather than a scan — the same hardware Transformers already lean on heavily.
No KV cache to manage, because there's no KV cache
This closes out something that's been implicit in every GPU Deep Dive post this blog has covered on the KV cache: PagedAttention allocates it, prefix caching reuses it, grouped-query attention shrinks how many heads produce it, and quantization shrinks how many bits each value in it costs — but all four of those are managing a structure that grows linearly with sequence length by construction, because attention has to keep every past token's K and V around to attend back to it. A state space model's recurrent state never grows: it's a fixed-size summary, updated in place at every step, holding no explicit memory of individual past tokens at all. That's not a fifth optimization on the same problem — it's a different architecture that doesn't have this particular problem in the first place, at the cost of compressing all of history through a fixed-size bottleneck instead of keeping it addressable.
References
- Mamba: Linear-Time Sequence Modeling with Selective State Spaces — Gu and Dao (2023/2024, arXiv:2312.00752)
- Transformers are SSMs: Generalized Models and Efficient Algorithms Through Structured State Space Duality — Dao and Gu (ICML 2024, arXiv:2405.21060)
KV-cache-vs-fixed-state diagram above is original artwork made for this post.