Mamba: Linear-Time Sequence Modeling with Selective State Spaces
This research paper explains why prior SSMs struggled on language, how selectivity restores content-based reasoning, and how a simplified Mamba block reaches Transformer-quality modeling without attention.
Transformers won foundation modeling with attention: every token can look at every other token. That power is also the bill — training cost grows with sequence length squared, and autoregressive decode must carry a growing key/value (KV) cache. A long line of subquadratic models (linear attention, gated convolutions, structured state space models) tried to keep a compact state instead. On language, they usually lost.
Mamba’s punchline is simple enough to draw: prior structured SSMs were linear time-invariant (LTI) — their dynamics did not depend on the token content — so they could not selectively remember or forget. Make the SSM parameters functions of the input, compute the resulting time-varying recurrence with a hardware-aware scan, and wrap it in a simplified block. The result is a homogeneous architecture without attention that the authors report matches much larger Transformers on language while scaling linearly in sequence length.
What this paper explains
Structured state space models (S4 and relatives) map a 1-D sequence through a latent state with linear dynamics. After discretization they look like a recurrence
or, when parameters are constant in time, an equivalent global convolution. That dual form made LTI SSMs fast to train. It also capped them: a static kernel cannot decide “this token is noise; skip it” the way attention can.
Gu and Dao ask:
- What principle separates models that compress context well from those that do not?
- Can we add content-aware selection to SSMs without giving up hardware efficiency?
- How simple can the surrounding neural architecture be if the sequence mixer itself is strong?
Their answers are selectivity, a selective scan implementation, and the Mamba block (SSM fused into a gated MLP-style pathway, stacked homogeneously).
Prior limits
- Attention does not compress: decode stores the entire context as KV, so memory and bandwidth grow with length.
- LTI SSMs / global convolutions compress into a fixed-size state and train efficiently, but their do not depend on . Synthetic tasks that require content-aware filtering (selective copying) or associative recall (induction heads) expose the failure.
- Earlier attempts at input-dependent SSM pieces often hit the wall that the convolution trick disappears once parameters vary over time — materializing a huge state tensor is expensive on GPUs.
- Hybrid stacks that bolt weak LTI mixers next to attention inherit the same long-context costs attention was meant to avoid.
The mechanism
Selection as compression control
The paper frames sequence modeling as compressing context into a state. Attention is effective because it refuses to compress; RNNs/SSMs are efficient because they must. Selectivity is the control knob: given , decide how information propagates along the sequence dimension.
Mechanically, Mamba’s selective SSM (sometimes called S6 in the ablations) makes several parameters input-dependent:
- , — linear projections to the state dimension
- with
stays structured/parameterized as in prior SSMs; discretization turns into time-varying . The recurrence is no longer LTI, so the convolution dual is gone.
Gating connection
When , , , and is produced as above, the selective recurrence collapses to classical RNN gating (their Theorem 1):
Large (large ) focuses on the current token and resets the state; small persists the state and ignores the token. That is the teaching picture behind the slider below.
Hardware-aware selective scan
Naively expanding to shape blows memory bandwidth. Mamba keeps the expanded state in fast levels of the GPU memory hierarchy (SRAM), loads from HBM, discretizes, runs a parallel associative scan in recurrent mode, and writes only the smaller outputs back to HBM. Same algorithmic recurrence; different materialization strategy.
Mamba block
Instead of interleaving an H3-style mixer with a separate MLP, Mamba uses one repeated block: expand the model width by factor (experiments fix ), apply a local conv + selective SSM on the main branch, gate with SiLU/Swish, project back, residual + norm. Stacking two such blocks matches the parameter pattern of a Transformer’s attention+MLP pair at width .
Algorithm walkthrough
- Start from a structured SSM with latent width per channel.
- Replace static with input-dependent maps (softplus on ).
- For each timestep, discretize to (e.g. zero-order hold) and update .
- Train with a fused selective scan that avoids writing the full state to HBM; infer autoregressively with constant-time-per-step recurrence (no KV cache growth with history length).
- Embed the selective SSM inside the Mamba block and stack blocks homogeneously — no attention layer required.
The opening figure shows tokens entering a compact state: relevant tokens update the latent cubes; noise tokens are gated out. The mid-article figure contrasts an LTI convolution path with the selective recurrent path.
What to notice when reading
- The synthetic tasks in §3.1 / §4.1 are not toys — they isolate content-aware failure modes that LTI models cannot patch with width alone.
- Selectivity is broader than one architecture: the paper treats it as a principle (what can depend on ), with S6 + Mamba as the concrete instance.
- The efficiency claim is not “RNNs are back”; it is “expand state in SRAM, scan in parallel, keep outputs small.”
- Ablations (Table 6 in the paper) attribute most of the language-model jump to replacing LTI SSM layers with selective ones, not merely to the outer block cosmetics.
Results and evidence
Numbers below are as reported by Gu & Dao — not re-benchmarked here.
Synthetic stress tests
| Setup | Highlight (paper) |
|---|---|
| Selective copying | Mamba + S6 reaches 99.8% accuracy; LTI S4/Hyena variants in the same table stay much lower |
| Induction heads | Selective SSM generalizes to million-length sequences (4000× training length); non-selective methods fail beyond ~2× |
Language modeling (Pile pretraining + zero-shot)
From their Table 3 (same tokenizer/dataset/training length cohort where noted):
| Model | Pile ppl ↓ | LAMBADA acc ↑ | Avg zero-shot ↑ |
|---|---|---|---|
| Pythia-1.4B | 7.51 | 61.7 | 55.2 |
| RWKV-1.5B | 7.70 | 56.4 | 54.3 |
| Mamba-1.4B | 6.80 | 64.9 | 59.7 |
| Pythia-2.8B | 6.73 | 64.7 | 59.1 |
| RWKV-3B | 7.00 | 63.9 | 59.6 |
| Mamba-2.8B | 6.22 | 69.2 | 63.3 |
The authors also report that a Mamba-3B matches Transformers about twice its size on their common-sense average, and that Mamba generation throughput is about 5× a same-size Transformer in their A100 decode setup (prompt 2048, generate 128; Hugging Face Transformer baseline).
Other modalities. On audio waveforms and DNA, they report beating prior SSM/Hyena/Transformer baselines and improving as context grows toward million-length sequences (including a speech FID reduction “by more than half” on a challenging generation set in their abstract summary).
Limitations
Compressed from the paper’s scope and what a careful reader should not over-claim:
- Results depend on their training recipes, tokenizers, and evaluation harness; treat Table 3 as evidence inside that protocol.
- Selective SSMs lose the pure convolution training path — performance hinges on the fused scan implementation quality.
- The homogeneous Mamba block is a design choice; later hybrids (attention + SSM) are outside this paper’s main claim.
- Million-length wins are shown most cleanly on synthetic induction and modality-specific long-range tasks; general chat assistants still need systems work beyond the architecture paper.
- Real-valued vs complex SSM parameterizations and many hyperparameters are ablated at ~350M scale; not every knob is swept at 3B.
How to read the paper
- Abstract + Figure 1 — selectivity + hardware-aware state expansion in one picture.
- §2 — SSM recurrence, discretization, LTI/convolution dual.
- §3.1–3.2 — why selection, Algorithms 1–2 (input-dependent ).
- §3.3 — why the scan must be hardware-aware once LTI is dropped.
- §3.4–3.5 — Mamba block; gating theorem.
- §4.1 synthetics, §4.2 language scaling + Table 3, then modality sections you care about.
- §4.6 ablations — S6 vs LTI is the lever.
Knowledge check
Why do LTI structured SSMs struggle on selective copying even if they can copy at fixed offsets?
What changes when Mamba makes Δ, B, and C functions of the input?
In the gating special case (Theorem 1), what does a large Δ_t / gate do?
Keep reading
- Original paper (arXiv:2312.00752) — Figures 1–3, Algorithms 1–2, Table 3.
- Gu et al., Efficiently Modeling Long Sequences with Structured State Spaces (S4) — arXiv:2111.00396.
- Related Fanout Daily: Attention Is All You Need, FlashAttention, Switch Transformers, GQA.
Sources
- Gu & Dao, Mamba: Linear-Time Sequence Modeling with Selective State Spaces, 2023 — arXiv:2312.00752
- Gu et al., Efficiently Modeling Long Sequences with Structured State Spaces (S4), ICLR 2022 — arXiv:2111.00396
- Official code/checkpoints referenced by the authors: https://github.com/state-spaces/mamba