Mamba — Selective State Spaces, Step by Step
- 1Mamba — Selective State Spaces, Step by Step
- 2Mamba-2 — When a State Space Model Is a Masked Attention Matrix
- 3Mamba-3 — A Better Step, a Turning State, More Work per Byte
- 4VMamba — Scanning an Image Four Ways
- 5Mamba in RT-DETRv4 — What Could Make It Faster
Summary
Attention remembers everything and pays for it; older linear recurrent models remember a fixed amount and cannot choose what. Mamba lets each token decide how much to write into a small memory, and then computes that recurrence fast on a GPU. A short walk through discretisation, selection, the parallel scan, the block and the results.
Table of Contents
- 1. The Whole Paper in Three Steps2
- 2. A State Space Model in One Picture2
- 3. S4: Fixed Weights, So a Convolution2
- 4. Selection: Let the Token Choose2
- 5. No Convolution, So How Is It Fast?2
- 6. The Mamba Block2
- 7. Results2
- 8. Conclusion2
- 9. References2
- 10. Connections2
Mamba [GU23] starts from one line: sequence modelling is "compressing context into a smaller state" (§3.1). Attention [VASW17] does not compress at all, and the older linear recurrent models compress every token the same way.
Picture two students taking notes in a lecture.
The first writes down every word. Nothing is lost, and the notebook grows with every sentence. That is attention. The second keeps one page and decides, word by word, whether to rewrite it: filler barely touches it, a definition overwrites half of it. That is Mamba. How hard her pen presses is the step Δ.
Brush up ;) Attention in Attention Is All You Need — The Transformer, Block by Block and Query, Key, Value — Dot-Product Attention from Scratch; the short version of Mamba in Vision Transformers Today — May 2022 to October 2026, Year by Year §2.
1. The Whole Paper in Three Steps
- Keep a small memory per channel, updated by a linear rule (a state space model).
- Let each token set Δ and the vectors B and C that write into and read from that memory (selection).
- Compute it fast on a GPU without writing the big state out to memory (the hardware-aware scan).
2. A State Space Model in One Picture
One channel: input x(t), output y(t), and a state h(t) of N numbers in between (Eq. 1):
- A moves the memory on its own, B writes the input in, C reads the output out.
- Tokens come in steps, so Mamba discretises with a step Δ by the zero-order hold: hold x fixed for one step and solve exactly (Eq. 4).
Ā is how much of the old state survives a step, B̄ how much of the input is written. The numbers, for A = −1, B = 1, Δ = 0.1:
- Ā = 0.904837, B̄ = 0.095163. They add up to 1: a mix of old and new.
- The memory halves every 6.93 steps; at Δ = 0.01, every 69.31. Small Δ, long memory.
NOTE: the authors' released code uses B̄ = ΔB, Eq. 4 to first order in Δ; the paper does not say so. Mamba-3 — A Better Step, a Turning State, More Work per Byte comes back to it.
3. S4: Fixed Weights, So a Convolution
Unroll the recurrence from h = 0. The weight on the input j steps back is , the same at every t, because nothing depends on time. One kernel slides over the sequence (Eq. 3):
- This is linear time invariance (LTI). S4 [GU21] trained with the convolution and generated with the recurrence.
- Later work made A diagonal [GU22, SMIT22]; Mamba follows. The D channels run independently, so the state is D·N numbers per token (§2).
Slide Δ and watch the half-life and the kernel:
4. Selection: Let the Token Choose
A fixed kernel keeps a token by how long ago it came, never by what it was. It cannot skip an "um". So Mamba computes three of the four parameters from the input (Alg. 2, §3.2):
| Parameter | S4 | S6 (Mamba) |
|---|---|---|
| A | parameter | parameter |
| B, C | parameter | from the input, shared by all channels |
| Δ | parameter | from the input, one per channel |
| computed as | recurrence or convolution | recurrence (scan) only |
Table. 1 The SSM without and with selection, from Alg. 1 and 2 of [GU23].
- B and C are small linear maps of x; Δ = softplus(parameter + a low-rank linear map of x) (§3.6).
- A stays fixed. Ā still changes per token through Δ: "selectivity in Δ is enough" (§3.5.2).
- The price: no single kernel any more, so "this loses the equivalence to convolutions" (§3.2).
Δ is a gate. Take N = 1, A = −1, B = 1, Δ = softplus(z). Then Ā = 1 − σ(z), B̄ = σ(z), and the recurrence is a gated RNN (Theorem 1):
- z = −2: Ā / B̄ = 0.881 / 0.119. Keep the state, ignore the token.
- z = 0: 0.5 / 0.5. Half and half.
- z = 2: 0.119 / 0.881. Overwrite with the token.
On a toy sequence the fixed model fades every token at the same rate; the choosing one holds data tokens and lets noise slide past:
Does it matter? Two synthetic tests:
- Selective Copying (16 data tokens at random spots in 4,096, Tab. 1): S4 18.3 %, S6 alone 97.0 %, full Mamba 99.8 %.
- Induction heads [OLSS22]: trained at length 256, Mamba stays perfect up to 2²⁰, 4,096× longer; no other method goes beyond 2× (§4.1.2).
5. No Convolution, So How Is It Fast?
The state is N times larger than the input and output (§3.3.1). Written out to GPU memory, it is the bottleneck. Three tricks (§3.3.2):
- Kernel fusion. Load Δ, A, B, C into the small fast on-chip memory, discretise and scan there, write back only y: "20-40 times" faster than an unfused scan (App. D).
- Parallel scan. Each step is a pair , and two stretches combine into one [MART17]:
- The operator is associative, so L steps combine as a tree in log₂ L rounds.
- Recomputation. States are recomputed for the backward pass, not stored, so memory is about that of a Transformer with FlashAttention [DAO22].
Eight steps, three rounds. Press ▶ until the last slot reaches the loop's 3.886:
for what is kept
Switch it to "what is kept": while generating, a Mamba-2.8B layer keeps 102,400 values at any length; a Transformer layer of the same width keeps 10,485,760 at 2,048 tokens, and growing. (Mamba has twice the layers; §6.)
The speed claims measure different things (A100 80GB, §4.5 and App. D):
- The scan beats FlashAttention-2 [DAO23] beyond 2K tokens, 7× at 32K: one operation against another, not a whole layer.
- 4–5× generation throughput: whole models, because with no KV cache Mamba fits larger batches.
6. The Mamba Block
Earlier SSM models alternated an SSM block with an MLP block. Mamba merges them into one (§3.4):
- In-projection D → 2ED, split into x and z.
- x: a short causal convolution, SiLU, then the selective SSM.
- Multiply by SiLU(z), project ED → D, add the residual.
The numbers: E = 2, N = 16, and about 6D² parameters per block against 12D² for a Transformer layer. So Mamba stacks twice the layers: the released 130M has 24, GPT-3's 125M has 12 (Tab. 12).
7. Results
Language. Mamba and Pythia [BIDE23] share the data, the 300B tokens and the tokenizer (§4.2.2):
| Model | Pile ppl ↓ | Avg of 6 tasks (%) |
|---|---|---|
| Pythia-1.4B | 7.51 | 55.2 |
| Mamba-1.4B | 6.80 | 59.7 |
| Pythia-2.8B | 6.73 | 59.1 |
| Mamba-2.8B | 6.22 | 63.3 |
| Pythia-6.9B | 6.51 | 61.7 |
| GPT-J-6B | — | 63.0 |
Table. 2 Zero-shot results, from Tab. 3 of [GU23]. Avg is the mean of LAMBADA, HellaSwag, PIQA, Arc-E, Arc-C and WinoGrande.
- At each size Mamba wins every task; Mamba-2.8B is +4.2 on the average over Pythia-2.8B.
- It beats GPT-J-6B on the average only; GPT-J is higher on four of the six tasks.
What each piece buys (≈350M models, Pile perplexity, §4.6):
| Setting | Perplexity ↓ |
|---|---|
| Nothing selective | 10.93 |
| Δ only | 9.81 |
| Δ, B and C | 8.71 |
| N = 1 → 16, B and C selective | 9.73 → 8.71 |
| N = 1 → 16, B and C fixed | 9.88 → 9.81 |
Table. 3 Ablations from Tab. 7 and 10 of [GU23].
- Δ alone gets half the gain; all three together get the rest.
- My favourite rows: more state helps only when it is selective (1.0 against 0.07).
8. Conclusion
- A fixed SSM is a convolution: fast, and blind to content.
- Token-dependent Δ, B, C turn Δ into a gate; the convolution is lost, and a fused parallel scan pays for it.
- Up to 2.8B, one block type beats Pythia at each size, from a state that never grows.
The catch: selection can hurt where LTI models are strong, like raw audio, and nothing above 2.8B was trained (§5). Next, Mamba-2 makes A a single number per head, so the same kind of layer runs on the GPU's matrix units: Mamba-2 — When a State Space Model Is a Masked Attention Matrix. Stay tuned :)
9. References
[GU23] [GU21] [GU22] [SMIT22] [MART17] [DAO22] [DAO23] [BIDE23] [OLSS22] [VASW17]
10. Connections
What this page builds on, cites or links to, and what points back at it. A plain box is a “builds on”; “cited” and “linked in the text” are the lighter links. Hover a box for a preview.
Related
- Paper · builds on this (next in the series)
- Read · this builds on it
- Read · same series: Mamba
- Paper · same series: Mamba
- Paper · builds on this (next in the series)
- Read · linked from the text
Go deeper
-
Start withRead
-
Paper
-
Paper
-
Paper
-
Paper
-
Read
-
Read
-
Paper
-
Read
-
Paper
-
Paper
-
Paper
-
Paper
-
Paper
-
Read
-
Paper
-
Paper
-
Read
-
You are hereMamba — Selective State Spaces, Step by StepPaper
-
ThenPaper
-
Paper
-
Paper
-
Read
Go broader
- Sits under
- SeriesMambapart 1 of 5 · start from the first part
- Its tags on the map
- Also under these tagsEfficientDet — A Comprehensive ReviewEfficient networksMixConv — Mixed Depthwise Convolutional Kernels from Google BrainEfficient networksSNIPER: Efficient Multi-Scale TrainingEfficient networksDynamic Convolution — An Exciting Innovation over Convolution Kernels from Microsoft ResearchEfficient networksText2SQL — Part 1: IntroductionLanguage models
- Ask the mapWhat connects Mamba and …type the other page's name; the map answers with the chain of links between them