Mamba — Selective State Spaces, Step by Step

  1. 1Mamba — Selective State Spaces, Step by Step
  2. 2Mamba-2 — When a State Space Model Is a Masked Attention Matrix
  3. 3Mamba-3 — A Better Step, a Turning State, More Work per Byte
  4. 4VMamba — Scanning an Image Four Ways
  5. 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. 1. The Whole Paper in Three Steps2
  2. 2. A State Space Model in One Picture2
  3. 3. S4: Fixed Weights, So a Convolution2
  4. 4. Selection: Let the Token Choose2
  5. 5. No Convolution, So How Is It Fast?2
  6. 6. The Mamba Block2
  7. 7. Results2
  8. 8. Conclusion2
  9. 9. References2
  10. 10. Connections2
TrivediInformational[Page 1]
S. TrivediOct 2026

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

  1. Keep a small memory per channel, updated by a linear rule (a state space model).
  2. Let each token set Δ and the vectors B and C that write into and read from that memory (selection).
  3. Compute it fast on a GPU without writing the big state out to memory (the hardware-aware scan).
the state h: kept in fast on-chip memory (SRAM)read from GPU memory (HBM)ht−1htAfixed, not from the inputN numbersof stateBtCtxtytDchannelsonly y goesback to GPUmemoryΔtdiscretiseprojectselection: the input sets Δ, B and C
Fig. 1 The selective state space model, redrawn from Fig. 1 of [GU23]. The input sets Δ, B and C; A stays a parameter. The state lives in fast on-chip memory; only y goes back to GPU memory.

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):

h′(t)=Ah(t)+Bx(t)y(t)=Ch(t)(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).
A¯=exp(ΔA)B¯=(ΔA)−1(exp(ΔA)−I)ΔBht=A¯ht−1+B¯xt,yt=Cht(2)

Ā 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 CA¯jB¯, the same at every t, because nothing depends on time. One kernel slides over the sequence (Eq. 3):

K¯=(CB¯,CA¯B¯,CA¯2B¯,…)y=x*K¯(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:

-1.01 / 4 · −1
One step of the memory. One channel, B = C = 1. Left: Ā (old state kept) and B̄ (new input written) against the step Δ. Right: the weight an input still has k steps later, the kernel of a fixed model. "As a gate" sets Δ = softplus(z) with A = −1, so Ā = 1 − σ(z) and B̄ = σ(z) (Theorem 1), with the paper's rule.

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):

ParameterS4S6 (Mamba)
Aparameterparameter
B, Cparameterfrom the input, shared by all channels
Δparameterfrom the input, one per channel
computed asrecurrence or convolutionrecurrence (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):

ht=(1−gt)ht−1+gtxt,gt=σ(zt)(4)
  • 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:

0.52.00.011 / 2 · the 6-step check
A fixed filter against a choosing one. A toy with one channel (A = −1, B = C = 1). Filled squares are data tokens, empty ones noise. The fixed model uses one Δ everywhere, so every row is the row above shifted by one: a convolution. The choosing model takes a large Δ on data and a small one on noise, so its state holds between data tokens.

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 ht=atht−1+bt is a pair (at,bt), and two stretches combine into one [MART17]:
(a1,b1)•(a2,b2)=(a1a2,a2b1+b2)(5)
  • 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:

1 / 4 · the pairs
for what is kept
2 / 2 · 2560 (2.8B)11
How a recurrence runs in parallel. Eight steps of h_t = a_t · h_(t−1) + b_t with toy values, each a pair (a_t, b_t). Each round combines every slot with the one 1, 2, then 4 places to its left; after three rounds every slot holds h_t, the same as the loop on top. "What is kept": the values one layer holds while generating. Mamba's state does not grow with the length; a Transformer layer's K and V cache does.

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):

normresidualnorm and residual:the released code,around every blockDin-projection D → 2EDxzED eachcausal conv, width 4SiLUselective SSMΔ, B, C from xSiLUgateout-projection ED → DDlinear projectionmixes along the tokensactivation or normmultiply
Fig. 2 The Mamba block, redrawn from Fig. 3 of [GU23]. One branch: short causal convolution, SiLU, selective SSM. The other: a SiLU gate. Their product goes back to width D.
  1. In-projection D → 2ED, split into x and z.
  2. x: a short causal convolution, SiLU, then the selective SSM.
  3. 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):

ModelPile ppl ↓Avg of 6 tasks (%)
Pythia-1.4B7.5155.2
Mamba-1.4B6.8059.7
Pythia-2.8B6.7359.1
Mamba-2.8B6.2263.3
Pythia-6.9B6.5161.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):

SettingPerplexity ↓
Nothing selective10.93
Δ only9.81
Δ, B and C8.71
N = 1 → 16, B and C selective9.73 → 8.71
N = 1 → 16, B and C fixed9.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.

POINTS AT THIS PAGEPAPERMamba-2 — When a State Space Model Is aMasked Attention MatrixPAPERVMamba — Scanning an Image Four WaysREAD · CITEDVision Transformers Today — May 2022 toOctober 2026, Year by YearREAD · CITEDMamba in RT-DETRv4 — What Could Make ItFasterPAPER · CITEDMamba-3 — A Better Step, a TurningState, More Work per ByteThis pageTHIS PAGE POINTS ATREADVision Transformers Today — May 2022 toOctober 2026, Year by YearPAPERAttention Is All You Need — TheTransformer, Block by BlockREFERENCE · CITEDFlashAttention: Fast andMemory-Efficient Exact Attention withIO-AwarenessREFERENCE · CITEDFlashAttention-2: Faster Attention withBetter Parallelism and Work PartitioningREFERENCE · CITEDEfficiently Modeling Long Sequences withStructured State SpacesREFERENCE · CITEDOn the Parameterization andInitialization of Diagonal State SpaceModelsREFERENCE · CITEDSimplified State Space Layers forSequence ModelingREFERENCE · CITEDParallelizing Linear Recurrent NeuralNets Over Sequence LengthREFERENCE · CITEDPythia: A Suite for Analyzing LargeLanguage Models Across Training andScalingREFERENCE · CITEDIn-context Learning and Induction HeadsREAD · LINKED IN THE TEXTQuery, Key, Value — Dot-ProductAttention from ScratchPAPER · LINKED IN THE TEXTMamba-2 — When a State Space Model Is aMasked Attention MatrixPAPER · LINKED IN THE TEXTMamba-3 — A Better Step, a TurningState, More Work per Byte

Related

Go deeper

  1. Start with
    Read
  2. Paper
  3. Paper
  4. Paper
  5. Paper
  6. Read
  7. Read
  8. Paper
  9. Read
  10. Paper
  11. Paper
  12. Paper
  13. Paper
  14. Paper
  15. Read
  16. Paper
  17. Paper
  18. Read
  19. You are here
    Mamba — Selective State Spaces, Step by Step
    Paper
  20. Then
    Paper
  21. Paper
  22. Paper
  23. Read

Go broader

<- Older [Page 2] Newer ->