Mamba-2 — When a State Space Model Is a Masked Attention Matrix
- 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
Mamba's scan cannot use the matrix units that make attention fast. Mamba-2 makes the state matrix one number per step, and the whole layer can be written as one masked matrix: masked linear attention, not softmax. Computed in chunks, it does more arithmetic than the scan and still runs faster.
Table of Contents
- 1. Two Small Changes2
- 2. The Whole Layer Is One Matrix2
- 3. Which Attention, Exactly?3
- 4. Chunks: Matrix Multiplies Inside, a Scan Between3
- 5. Why More Arithmetic Is Faster3
- 6. The Mamba-2 Block3
- 7. Results3
- 8. Conclusion3
- 9. References3
- 10. Connections3
Mamba's scan Mamba — Selective State Spaces, Step by Step is fast, but it is made of sums and products that a GPU's matrix units cannot run. On an A100 those units do 312 TFLOPS of BF16 matrix multiplies, against 19 TFLOPS of FP32 arithmetic (the authors' blog on [DAO24]). Attention lives on matrix multiplies; Mamba did not.
Think of a relay race. Inside each leg, the runners all run at once; between legs, only the baton changes hands. Mamba-2 does the same: inside a chunk of tokens, matrix multiplies; between chunks, only a small state is handed on.
The whole paper [DAO24] in three steps:
- Write the layer as one matrix: .
- Make the state matrix one number per step. Then M is a mask times a score matrix: masked linear attention.
- Cut M into chunks: matrix multiplies inside, a short recurrence between.
Brush up ;)
- The SSM, selection and the scan: Mamba — Selective State Spaces, Step by Step.
- Masked attention, queries, keys and values: Query, Key, Value — Dot-Product Attention from Scratch.
1. Two Small Changes
Mamba-2's layer is Mamba's selective SSM [GU23] (from the S4 line [GU21]) with "two minor differences" (§2.4, p. 5):
- A becomes a scalar. : one number per head and per step, shared by the whole state.
- Heads get wider. Head dimension P is 64 or 128 instead of 1.
What it buys: a state "8× the size of Mamba or even higher" (§1, p. 2). The blog says N = 64 to 256, against Mamba's 16; the released 2.7B model keeps the code's defaults, N = 128, P = 64.
T is the sequence length, m the chunk length.
2. The Whole Layer Is One Matrix
Unroll , from a zero state. Input is written by , carried by every A in between, and read by (Eq. 3):
- M is T × T and lower-triangular.
- It is N-semiseparable: every block on or below the diagonal has rank at most N (Def. 3.1, Thm. 3.5). Every path from past to future squeezes through a state of N numbers.
Now use the scalar. A product of numbers slides out of the middle:
Two independent pieces, cell by cell:
- L, the mask: how much has decayed between step i and step j.
- C Bᵀ, the scores: how strongly step j reads what step i wrote.
Numbers are always satisfying. With a = (0.5, 0.9, 0.5, 0.8, 1, 0.2):
| row j | i = 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| 0 | 1 | |||||
| 1 | 0.9 | 1 | ||||
| 2 | 0.45 | 0.5 | 1 | |||
| 3 | 0.36 | 0.4 | 0.8 | 1 | ||
| 4 | 0.36 | 0.4 | 0.8 | 1 | 1 | |
| 5 | 0.072 | 0.08 | 0.16 | 0.2 | 0.2 | 1 |
Table. 1 The mask L for T = 6, from the paper's definition (Eq. 6). My numbers.
- Row 4 copies row 3 (); shrinks the whole past fivefold at once. One number per step: the state forgets together.
Move the decay: the mask fades from the diagonal. Switch to M and the scores join in.
3. Which Attention, Exactly?
Rename (C, B, x) as (Q, K, V):
That is masked linear attention, no softmax (Eq. 13, 16). Only the mask changes across the family:
- All-ones causal L: linear attention [KATH20].
- , one fixed decay: RetNet [SUN23].
- Data-dependent a: SSD, the layer of Mamba-2. GLA [YANG23] puts similar gates on linear attention.
Build first (quadratic) or carry the state step by step (linear): same function, "duals of each other" (§5.3).
NOTE: SSD is not softmax attention (§10.3). The exponential in softmax has no finite feature map, so its scores cannot be written as C Bᵀ with a finite N.
Set a = 1 and nothing ever leaves the state: linear attention's mask.
4. Chunks: Matrix Multiplies Inside, a Scan Between
The quadratic order costs T²; the linear one cannot use matrix units. SSD cuts M into m × m blocks and takes both (§6):
- Inside each chunk: outputs from a zero start, as one small masked attention (matrix multiplies).
- Chunk states: each chunk's final state, from a zero start.
- Pass the states along: the scan of Mamba, but over only T/m states.
- States into outputs: what each incoming state adds.
Output = step 1 + step 4.
On the paper's example size, T = 9, m = 3 (my numbers, N = P = 2):
- Step 2 leaves chunk 2 at (2.6, 11.2) in channel 0. Step 3 adds chunk 1's state (3.45, 4), decayed by 0.8 × 1 × 0.2, giving (3.152, 11.84).
- The chunked outputs equal the step-by-step ones exactly.
Play the steps: after step 4 every bar fills its outline. m = 1 is the plain recurrence, m = 9 the plain matrix: only the order of work changes.
5. Why More Arithmetic Is Faster
| Attention | SSM | SSD | |
|---|---|---|---|
| State size | T | N | N |
| Training FLOPs | T²N | TN² | TN² |
| Inference FLOPs | TN | N² | N² |
| Matrix multiplication | ✓ | ✓ |
Table. 2 Costs of the three forms (p. 22 of [DAO24]).
- By my count at the code's defaults (N = 128, P = 64, m = 256) and T = 2048, SSD does about 3.2× the scan's arithmetic.
- It still runs faster: that arithmetic runs at up to 312 TFLOPS, not 19.
- Measured on an A100: "2-8× faster" than Mamba's scan, and past FlashAttention-2 [DAO23] from 2K tokens, "6× faster at sequence length 16K" (§1, §9.3).
Counted, the attention form and SSD break even near T ≈ 342; measured, at 2K: memory traffic matters too.
6. The Mamba-2 Block
- Parallel projections, like Q, K and V in attention: one all-reduce per block instead of two (§8.1).
- An extra norm before the output projection, for stability.
- B and C shared across heads, like multi-value attention.
Both changes help a little (Tab. 4, ~125M): perplexity 11.76 → 11.49, with fewer parameters (129.3M → 126.5M).
7. Results
Same 300B Pile tokens as Mamba and Pythia [BIDE23]:
| Model | Pile ppl ↓ | LAMB. ppl ↓ | Avg acc |
|---|---|---|---|
| Pythia-2.8B | 6.73 | 5.04 | 55.7 |
| Mamba-2.8B | 6.22 | 4.23 | 59.9 |
| Mamba-2-2.7B | 6.09 | 4.10 | 60.2 |
| Pythia-6.9B | 6.51 | 4.45 | 58.3 |
Table. 3 Zero-shot, average over seven tasks in % (Tab. 1 and Tab. 10 of [DAO24]).
- +1.9 over Pythia-6.9B, more than twice its size; only +0.3 over Mamba.
A little attention helps. Swap some of a 48-layer, 350M Mamba-2's layers for attention (Tab. 2):
| Attention layers (of 48) | 0 | 1 | 6 | 24 |
|---|---|---|---|---|
| Perplexity ↓ | 8.60 | 8.38 | 8.26 | 8.50 |
Table. 4 Hybrids, 350M, 7B tokens (Tab. 2 of [DAO24]). Transformer++: 8.68.
- One attention layer gives most of the gain; six (12.5 %) is best; half the stack is worse again.
- At 2.7B (Tab. 3), 6 of 64 layers reach an average of 61.0, against 60.2 for pure Mamba-2 and Transformer++.
8. Conclusion
- An SSM layer is one matrix, , with low-rank blocks below the diagonal.
- Make A a scalar and : masked linear attention with a data-dependent decay. Not softmax.
- Chunks run that matrix as matrix multiplies plus a short scan: more arithmetic, less time.
- The price: a scalar A is less expressive. The authors' verdict: "the jury's still out!" (blog).
Next, Mamba-3 — A Better Step, a Turning State, More Work per Byte asks what the scalar decay cost. Stay tuned.
9. References
- [DAO24] Dao, Gu. Transformers are SSMs: Generalized Models and Efficient Algorithms Through Structured State Space Duality. ICML 2024, arXiv 2405.21060. Blog: "State Space Duality (Mamba-2)", tridao.me.
- [GU23] Gu, Dao. Mamba: Linear-Time Sequence Modeling with Selective State Spaces. 2023, arXiv 2312.00752.
- [GU21] Gu, Goel, Ré. Efficiently Modeling Long Sequences with Structured State Spaces. ICLR 2022, arXiv 2111.00396.
- [KATH20] Katharopoulos et al. Transformers are RNNs: Fast Autoregressive Transformers with Linear Attention. ICML 2020, arXiv 2006.16236.
- [SUN23] Sun et al. Retentive Network: A Successor to Transformer for Large Language Models. 2023, arXiv 2307.08621.
- [YANG23] Yang et al. Gated Linear Attention Transformers with Hardware-Efficient Training. 2023, arXiv 2312.06635.
- [BIDE23] Biderman et al. Pythia: A Suite for Analyzing Large Language Models Across Training and Scaling. 2023, arXiv 2304.01373.
- [DAO23] Dao. FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning. 2023, arXiv 2307.08691.
- Code and checkpoints: github.com/state-spaces/mamba (defaults N = 128, P = 64, chunk 256).
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 · this builds on it (earlier in the series)
- Paper · builds on this (next in the series)
- Read · same series: Mamba
- Read · this builds on it
- Read · same tag: Efficient attention; cites the same paper
- Paper · same series: Mamba
Go deeper
-
Start withRead
-
Paper
-
Paper
-
Paper
-
Paper
-
Read
-
Read
-
Paper
-
Read
-
Paper
-
Paper
-
Paper
-
Paper
-
Paper
-
Read
-
Paper
-
Paper
-
Read
-
Paper
-
You are hereMamba-2 — When a State Space Model Is a Masked Attention MatrixPaper
-
ThenPaper
-
Paper
-
Read
Go broader
- Sits under
- SeriesMambapart 2 of 5 · start from the first part
- Its tags on the map
- Also under these tagsThread Management SystemSystems programmingRT-DETR — DETRs Beat YOLOs, Encoder by EncoderEfficient attentionRT-DETRv2 — A Bag of Freebies for the DecoderEfficient attention
- Ask the mapWhat connects Mamba-2 and …type the other page's name; the map answers with the chain of links between them