Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Explainers/Research Papers

Mamba explained: an alternative to Transformers

Mamba lets a state space model choose what to remember, matching Transformers on language with linear cost.

Derpx0612 Oct · 5 min read
Contents
The idea in one sentenceHow it worksStep 1: a state space model keeps a small running stateStep 2: "selective" means the rule depends on the inputStep 3: the training shortcut breaks, so the scan is rebuiltStep 4: cost compared with attentionWhat the paper foundWhat to be careful aboutSources
0% read · 5 min leftBack to top ↑
On this page0%
Contents
The idea in one sentenceHow it worksStep 1: a state space model keeps a small running stateStep 2: "selective" means the rule depends on the inputStep 3: the training shortcut breaks, so the scan is rebuiltStep 4: cost compared with attentionWhat the paper foundWhat to be careful aboutSources

Today's language models use attention, which compares every token with every other token. Double the text and the work grows four times, and the memory keeps growing too. Faster alternatives existed, but they were weaker at language. This paper builds one that matches Transformers on language while costing far less on long inputs.

Paper

Mamba: Linear-Time Sequence Modeling with Selective State Spaces. Albert Gu (Carnegie Mellon University) and Tri Dao (Princeton University). arXiv:2312.00752, December 2023 (v2 May 2024). Read it at arxiv.org/abs/2312.00752.

The idea in one sentence

Let the model's memory update depend on the token it is reading, so it can keep what matters and ignore the rest, then write the code so this runs fast on a GPU.

The paper's key sentence: efficient models need a small state, effective models need a state that holds the needed information, so what matters is how well the state compresses the context.

How it works

Step 1: a state space model keeps a small running state

A state space model (SSM) reads a sequence one step at a time and keeps a hidden state hhh, a fixed-size list of numbers that summarises everything so far. At each step it does:

ht=Aˉ ht−1+Bˉ xt,yt=C hth_t = \bar{A}\,h_{t-1} + \bar{B}\,x_t, \qquad y_t = C\,h_tht​=Aˉht−1​+Bˉxt​,yt​=Cht​

Here xtx_txt​ is the current input, yty_tyt​ is the output, and Aˉ\bar{A}Aˉ, Bˉ\bar{B}Bˉ, CCC are learned numbers. Aˉ\bar{A}Aˉ says how much of the old state to keep. Bˉ\bar{B}Bˉ says how much of the new input to write in. The state never grows, so each step costs the same, however long the text is.

Toy example (made-up numbers, one-number state). Rule: ht=0.5 ht−1+0.5 xth_t = 0.5\,h_{t-1} + 0.5\,x_tht​=0.5ht−1​+0.5xt​, inputs x=5,1,1,1x = 5, 1, 1, 1x=5,1,1,1 where 5 is the important token and 1 is filler. The state goes 2.5, 1.75, 1.375, 1.188. The important 5 fades away and the filler mixes in. Same rule for every token, so the model cannot protect what matters.

Step 2: "selective" means the rule depends on the input

Earlier SSMs used the same Aˉ\bar{A}Aˉ, Bˉ\bar{B}Bˉ for every token (they are "time-invariant"). The paper shows two small tasks they fail. In Selective Copying the model must copy certain tokens and skip noise tokens placed at random positions. In Induction Heads it must spot an earlier pattern and complete it. Both need to look at what a token is.

Mamba makes the step size Δ\DeltaΔ and the matrices BBB and CCC functions of the current input (small learned linear layers; Δ\DeltaΔ goes through softplus, a function that keeps it positive). Aˉ\bar{A}Aˉ and Bˉ\bar{B}Bˉ come from Δ\DeltaΔ. A large Δ\DeltaΔ means reset and write the input in. A small Δ\DeltaΔ means keep the state and ignore the input.

Toy example (one-number state, A=−1A=-1A=−1, B=1B=1B=1, so Aˉ=e−Δ\bar{A}=e^{-\Delta}Aˉ=e−Δ and Bˉ=1−e−Δ\bar{B}=1-e^{-\Delta}Bˉ=1−e−Δ). Same inputs 5, 1, 1, 1. Choose Δ=5\Delta = 5Δ=5 for the important token and 0.010.010.01 for filler. The state goes 4.97, 4.93, 4.89, 4.85. The important value is kept and the filler barely touches it.

Token 5 (important)
Token 1 (filler)
Token 1 (filler)
Token 1 (filler)

The important token arrives with a large step, so 5 is written into the state (state 4.97)

Step 1 of 4

The paper shows that, in the simplest case, this is exactly the gate of an RNN (a recurrent network), which is the same kind of switch.

Step 3: the training shortcut breaks, so the scan is rebuilt

Fixed-rule SSMs can be computed as one big convolution, which is fast on GPUs. With input-dependent rules that shortcut is gone, so the paper computes the recurrence directly but in a hardware-aware way, using three known techniques:

  • Kernel fusion: the expanded state is created only in fast on-chip memory (SRAM), and only the final output is written to slow GPU memory (HBM).
  • Parallel scan: a method to compute the step-by-step recurrence in parallel instead of strictly one step after another.
  • Recomputation: the in-between states are not saved, they are recalculated when needed for the backward pass. This gives memory use equal to an optimised Transformer with FlashAttention.

The paper reports the efficient scan is 40 times faster than a standard implementation.

Step 4: cost compared with attention

For length LLL, attention compares about L×LL \times LL×L pairs. For 1,000 tokens that is about 1,000,000 pairs, while a recurrence does 1,000 steps. At generation time Mamba keeps a fixed-size state, while a Transformer keeps a cache that grows with the text.

What the paper found

  • Speed. 5 times higher inference throughput than Transformers, and cost that grows linearly with length.
  • Synthetic tasks. Mamba solved the induction-heads task perfectly and kept working at sequence length 1,048,576, which is 4,000 times longer than the 256 it was trained on.
  • Language. Zero-shot average accuracy over several tasks (Table 3) is shown below.
ModelParametersAverage accuracy
Pythia (Transformer)2.8B59.1
Mamba2.8B63.3
Pythia (Transformer)6.9B61.7

Mamba-2.8B beat a Transformer of the same size and also one more than twice as large.

The paper also reports results on DNA and audio modelling, and quality that kept improving with context up to 1 million tokens.

These results show Mamba is competitive at the sizes tested. They do not show how it compares to the biggest models.

What to be careful about

  • Scale. The paper says its tests are limited to small models, below the size of strong open models like Llama, and it is unknown if Mamba still compares well at larger sizes.
  • State size limits memory. A fixed state can hold only a fixed amount, and the paper says effectiveness depends on how well that state compresses the context.
  • No free lunch. The selection helps on text and DNA but can hurt on continuous signals such as audio, where fixed-rule SSMs do well. The authors discuss this trade-off.
  • Ecosystem. The paper leaves open whether SSMs support the features Transformers have, like in-context learning, fine-tuning and quantization.
  • Hardware. The speed depends on the custom GPU kernel. A naive version would be slow.
Key takeaways
  • An SSM keeps a fixed-size state, so each step costs the same however long the input is.
  • Fixed-rule SSMs treat every token the same; Mamba lets the input choose the step size and matrices, so it can keep or ignore tokens.
  • A hardware-aware scan (fusion, parallel scan, recomputation) makes that input-dependent recurrence fast.
  • Mamba-2.8B scored 63.3 against 59.1 for a same-size Transformer, with 5 times the generation throughput, but was tested only at small scale.

Sources

  • Gu and Dao, Mamba: Linear-Time Sequence Modeling with Selective State Spaces, arXiv 2023 (PDF read)
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
Inference · 7 minWhat is quantization? Running AI models in 4 bitsResearch Papers · 6 minLoRA explained: fine-tune a big AI model cheaplyResearch Papers · 7 minChinchilla explained: how big should an AI model be?Research Papers · 6 minFlashAttention explained: making AI attention much faster
Start here
01Why do AI chatbots make things up?02How does Raft work? Getting computers to agree03What is quantization? Running AI models in 4 bits