Mamba: letting each token control what the state keeps
Mamba uses selective state-space models (SSMs) to carry a fixed-size memory. Could each token control what it writes and keeps?
Linear attention showed how a model can carry information forward in a fixed state instead of keeping every key/value pair. A token writes to that state, then reads information from it.
But the simple update keeps adding every write to the old state. As the conversation moves on, could each token control how much to write and how much old information to keep?
Mamba lets the current input control both.
Start from the write and read we already have
Keep the state matrix H. Mamba uses different names for the vectors involved in the familiar write and read:
| Role | Linear-attention example | Mamba |
|---|---|---|
| Values to write | v | x |
| Pattern that spreads the write across the state | k | B |
| Pattern that reads an output from the state | q | C |
The write has the form xBᵀ, and the read has the form HC. Learned projections produce these vectors from the current input. Different inputs can leave overlapping patterns in the same state; there is still no private slot for each token.
The write/read structure is familiar, but the full rules differ. Mamba does not keep the contribution vector z or divide the read by a total weight as our introductory linear-attention example did. Its update adds the writing and retention controls we will build next.
These are snapshots of the same array: Hₜ₋₁ before a token’s update and Hₜ after it. Only the updated state carries forward to the next token.
Let the input control the update
Writing every token equally strongly would let incidental words disturb important information. Give each token a step Δ: a positive value that scales its write. A small step adds little; a large step adds more.
The token produces this step through learned weights. Softplus makes the result positive.
But a stronger write alone would keep piling information into the same memory. We also need to decide how much of the old memory to carry forward.
Give each state value a negative decay parameter A. These numbers are learned directly during training and stay fixed when the model runs: the token does not pass through A as a projection matrix. By contrast, Δ is calculated afresh from each token, using the learned step weights and Softplus shown above.
Combine A with the current step to produce a retention factor between 0 and 1, using exp(A × Δ). A factor of 0.9 keeps 90% of an old value; 0.2 keeps 20%. Multiply each old state value by its factor, then add the scaled new write.
This is why Δ has two paths: a small step preserves more old memory and writes less; a larger step reduces the old memory more and writes more. A is learned during training; Δ changes with the current input. The write and read patterns B and C also change with the input. Together, these controls let Mamba choose how to use its fixed memory at each token.
Put Mamba-1 together
The controls now have a place in the full layer. First make the value stream and an output gate. A short convolution mixes nearby inputs in the value stream; that stream also produces B, C and Δ through learned projections. A remains a directly learned parameter table.
Mamba-1
After updating and reading the state, add a learned shortcut D × x so current information can reach the output directly. The other branch supplies an output gate: apply SiLU to it, then multiply it with the result, as with the gated feed-forward networks. Finally, project back to the model width and add the residual.
Make that update cheaper: Mamba-2
Original Mamba can fade every state value at a different rate. That is flexible, but much of the computation consists of small element-wise operations. GPUs are especially good at large matrix multiplications. Can we simplify the rule to use them?
Mamba-2 divides the value channels into heads. Each head owns a state matrix: one row per value channel, with several memory values in each row. Its write pattern B fills those columns and its read pattern C combines them back into output values.
The simplification is inside that boundary: a head has one A and one token-dependent Δ, giving one retention factor for its entire state matrix. Different heads can still keep information at different rates.
The stack repeats the state update and read for each group of channels. It does not repeat the whole layer: the input projection and convolution prepare the head inputs, and the head outputs join before the final gate and output projection. B and C patterns can be shared across groups of heads.
Turn the memory rule into a layer
We now have the memory operation. To put it inside a network, we need to prepare its inputs and return an update at the model’s width.
Draw the input weights as three blocks, each receiving the same normalised 2,688-value input: 6,144 × 2,688 produces x/B/C, 64 × 2,688 produces the step inputs, and 4,096 × 2,688 produces the gate. These are row blocks of the same 10,304 × 2,688 projection, shown separately so we can follow each branch.
A short learned convolution gives x, B and C local context. The convolution and SiLU keep that branch at 6,144 values; Softplus keeps the step branch at 64; SiLU keeps the gate branch at 4,096. As in Mamba-1, the read multiplies the updated state by C. A separate D × x branch scales the current values with learned weights and adds them to the readout. The output gate is another multiplication, using SiLU(z): each head’s 64 output values multiply its 64 gate values. Join the 64 gated heads into a 4,096-value vector, then normalise and project back to the model width for the residual update.
The left branch’s 6,144 values are shown as three stacks: 64 heads × 64 x values, 8 groups × 128 B values, and 8 groups × 128 C values. These account for 4,096 + 1,024 + 1,024 values. Each head uses one x vector and its group’s B/C vectors; eight heads share each B/C group. The stacks show one vector at the front, not a projection that shrinks the whole branch to 128 values.
Mamba-2
The internal state carries information between tokens. The residual stream carries a token’s representation between layers.
Where attention still helps
Recurrent layers keep a fixed state as the conversation grows. Hybrid models also have attention layers, whose caches still grow with the number of tokens. The memory plot includes both.
A fixed state can lose distinctions as its contents are updated. Keeping a few attention layers gives a hybrid model a way to look back at separately stored keys and values too. Granite 4.0 H Small, for example, uses 36 Mamba-2 layers and four attention layers.
Next: combining these two kinds of memory.
Sources and diagram conventions
- S4, Gu and colleagues, 2021: structured state-space sequence layers.
- Mamba, Gu and Dao, 2023: input-dependent selection, selective scan and the original block.
- Transformers are SSMs, Dao and Gu, 2024: Mamba-2, scalar-identity state dynamics and the SSD algorithm.
- The authors’ model walkthrough connects the recurrent and attention-like forms.
- Mamba-2 reference implementation: parallel input projection, convolution of x/B/C, positive steps, shortcut, output gating and normalisation.
- Nemotron 3.5 Lightning config: the final block’s width 2,688, 64 heads of 64 channels, state size 128 and eight B/C groups. Its input projection has 10,304 outputs: two 4,096-wide branches, two sets of 8 × 128 grouped patterns, and 64 steps.
The construction sketches use four channels and eight state values. In our row-per-channel convention, a write is xBᵀ and a read is HC. Mamba also scales the write by Δ. B and C are produced as columns; the write diagram shows B as a row. Patterns are learned features, not literal addresses or token slots.
Original Mamba has a diagonal decay operator for each channel. Mamba-2 restricts it to a scalar within each head; B and C may be shared by groups of heads. This restriction enables the SSD computation. It is not simply a change in the number of heads.
Grids sample a few illustrative values. Their displayed aspect ratio preserves which axis is longer; it is not proportional to memory consumption. The initial snapshots show original Mamba 2.8B (5,120 × 16 per layer). The full Mamba-1 diagram uses Mamba 2.8B sizes; the Mamba-2 head and full-layer diagrams use Nemotron 3.5 Lightning sizes. The memory plot follows the selected model and includes attention caches for hybrids; it assumes 16-bit storage, although some runtimes keep recurrent state in 32-bit.