Mamba-2 — When a State Space Model Is a Masked Attention Matrix

  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

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. 1. Two Small Changes2
  2. 2. The Whole Layer Is One Matrix2
  3. 3. Which Attention, Exactly?3
  4. 4. Chunks: Matrix Multiplies Inside, a Scan Between3
  5. 5. Why More Arithmetic Is Faster3
  6. 6. The Mamba-2 Block3
  7. 7. Results3
  8. 8. Conclusion3
  9. 9. References3
  10. 10. Connections3
TrivediInformational[Page 1]
S. TrivediOct 2026

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:

  1. Write the layer as one matrix: y=Mx.
  2. Make the state matrix A one number per step. Then M is a mask times a score matrix: masked linear attention.
  3. Cut M into chunks: matrix multiplies inside, a short recurrence between.

Brush up ;)

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. At=atI: 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 ht=Atht−1+Btxt, yt=Ct⊤ht from a zero state. Input xi is written by Bi, carried by every A in between, and read by Cj (Eq. 3):

y=MxMji=Cj⊤Aj⋯Ai+1Bi(1)
  • 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:

Mji=(aj⋯ai+1)Cj⊤BiM=L∘(CB⊤)(2)

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 ji = 012345
01
10.91
20.450.51
30.360.40.81
40.360.40.811
50.0720.080.160.20.21

Table. 1 The mask L for T = 6, from the paper's definition (Eq. 6). My numbers.

  • Row 4 copies row 3 (a4=1); a5=0.2 shrinks the whole past fivefold at once. One number per step: the state forgets together.
0.91 / 3 · none2 / 3 · 3
The matrix behind the layer. The whole layer as one 9 × 9 matrix, a toy at the size of the paper's chunk example (T = 9, state size N = 2; the decays, B and C are my numbers). Row j is an output step, column i an input step. Blue cells are positive, rust negative, darker is larger. The mask L holds the decays, the scores hold C·B, and the layer's matrix M is the two multiplied cell by cell. The lines cut it into chunks; every block below the diagonal has rank at most 1 in L and at most N = 2 in M.

Move the decay: the mask fades from the diagonal. Switch to M and the scores join in.

TrivediInformational[Page 1]
S. TrivediOct 2026

3. Which Attention, Exactly?

Rename (C, B, x) as (Q, K, V):

Y=(L∘QK⊤)V(3)

That is masked linear attention, no softmax (Eq. 13, 16). Only the mask changes across the family:

  • All-ones causal L: linear attention [KATH20].
  • Lji=γj−i, one fixed decay: RetNet [SUN23].
  • Data-dependent a: SSD, the layer of Mamba-2. GLA [YANG23] puts similar gates on linear attention.

Build QK⊤ 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.

1.01 / 3 · none2 / 3 · 3
The matrix behind the layer. The whole layer as one 9 × 9 matrix, a toy at the size of the paper's chunk example (T = 9, state size N = 2; the decays, B and C are my numbers). Row j is an output step, column i an input step. Blue cells are positive, rust negative, darker is larger. The mask L holds the decays, the scores hold C·B, and the layer's matrix M is the two multiplied cell by cell. The lines cut it into chunks; every block below the diagonal has rank at most 1 in L and at most N = 2 in M.

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

  1. Inside each chunk: outputs from a zero start, as one small masked attention (matrix multiplies).
  2. Chunk states: each chunk's final state, from a zero start.
  3. Pass the states along: the scan of Mamba, but over only T/m states.
  4. 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.
4 / 4 · states into outputs2 / 3 · 3
Four steps, one answer. A toy at the size of the paper's chunk example (my numbers): nine steps cut into chunks, a state of 2 × 2 numbers. Step through the four parts of the chunked algorithm. The bars are the outputs it has so far (blue: from inside the chunk; rust: from the state handed in), the dashed outlines the outputs computed step by step. After step 4 they are the same numbers. Chunks of 1 are the plain recurrence, one chunk of 9 is the plain matrix form.

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

AttentionSSMSSD
State sizeTNN
Training FLOPsT²NTN²TN²
Inference FLOPsTNN²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).
3 / 4 · 1281 / 2 · 643 / 3 · 25611
FLOPs, not time. Arithmetic for one head as the sequence grows: the attention form (the whole T × T matrix), the plain scan, and the chunked algorithm (SSD). Counts, not time: SSD does about three times the scan's arithmetic and still runs faster, because it is all matrix multiplies. The dashed rule is the paper's measurement.

Counted, the attention form and SSD break even near T ≈ 342; measured, at 2K: memory traffic matters too.

6. The Mamba-2 Block

SSMYSiLUconvSiLUAXBCMamba block (sequential)SSMYNSiLUconvSiLUAXBCMamba-2 block (parallel)linear projectionmixes along the tokensNnorm
Fig. 1 Mamba's block and Mamba-2's. Mamba-2 makes A, X, B and C in one projection at the start and adds a norm before the output. Redrawn from Fig. 6 of [DAO24].
  • 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]:

ModelPile ppl ↓LAMB. ppl ↓Avg acc
Pythia-2.8B6.735.0455.7
Mamba-2.8B6.224.2359.9
Mamba-2-2.7B6.094.1060.2
Pythia-6.9B6.514.4558.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)01624
Perplexity ↓8.608.388.268.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, y=Mx, with low-rank blocks below the diagonal.
  • Make A a scalar and M=L∘(CB⊤): 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

  1. [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.
  2. [GU23] Gu, Dao. Mamba: Linear-Time Sequence Modeling with Selective State Spaces. 2023, arXiv 2312.00752.
  3. [GU21] Gu, Goel, Ré. Efficiently Modeling Long Sequences with Structured State Spaces. ICLR 2022, arXiv 2111.00396.
  4. [KATH20] Katharopoulos et al. Transformers are RNNs: Fast Autoregressive Transformers with Linear Attention. ICML 2020, arXiv 2006.16236.
  5. [SUN23] Sun et al. Retentive Network: A Successor to Transformer for Large Language Models. 2023, arXiv 2307.08621.
  6. [YANG23] Yang et al. Gated Linear Attention Transformers with Hardware-Efficient Training. 2023, arXiv 2312.06635.
  7. [BIDE23] Biderman et al. Pythia: A Suite for Analyzing Large Language Models Across Training and Scaling. 2023, arXiv 2304.01373.
  8. [DAO23] Dao. FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning. 2023, arXiv 2307.08691.
  9. 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.

POINTS AT THIS PAGEPAPERMamba-3 — A Better Step, a TurningState, More Work per ByteREAD · CITEDMamba in RT-DETRv4 — What Could Make ItFasterPAPER · LINKED IN THE TEXTMamba — Selective State Spaces, Step byStepThis pageTHIS PAGE POINTS ATREADQuery, Key, Value — Dot-ProductAttention from ScratchPAPERMamba — Selective State Spaces, Step byStepREFERENCE · CITEDFlashAttention-2: Faster Attention withBetter Parallelism and Work PartitioningREFERENCE · CITEDEfficiently Modeling Long Sequences withStructured State SpacesREFERENCE · CITEDPythia: A Suite for Analyzing LargeLanguage Models Across Training andScalingREFERENCE · CITEDTransformers are RNNs: FastAutoregressive Transformers with LinearAttentionREFERENCE · CITEDRetentive Network: A Successor toTransformer for Large Language ModelsREFERENCE · CITEDGated Linear Attention Transformers withHardware-Efficient TrainingPAPER · 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. Paper
  20. You are here
    Mamba-2 — When a State Space Model Is a Masked Attention Matrix
    Paper
  21. Then
    Paper
  22. Paper
  23. Read

Go broader

<- Older [Page 3] Newer ->