Mini WorldLaplace AI / Plate F56 — Artificial intelligence

Mamba and State Space Models

Keep every token and memory grows with the text. Keep a fixed-size state instead, and let each token decide what stays.

Scroll

01

What It Does

A language model has to keep something from every token it reads in order to go on writing. Transformer attention keeps the keys and values of every earlier token, and they pile up (see the plate “The KV Cache and PagedAttention”). A state space model does it differently: it folds what it has read into a state of fixed size and updates it once per token.

The 2023 Mamba paper (Gu and Dao) makes that update depend on each token. It reports 4 to 5 times the generation throughput of a Transformer of similar size, and, trained on the same data, a 2.8-⁠billion-parameter Mamba averaging 63.3 on six zero-shot tests, against 59.1 for the same-size Pythia and 61.7 for Pythia-⁠6.9B, more than twice its size.

On the stand, a reading machine. A paper tape printed with the tokens to read runs from the reel on the right, under the reading head, onto the reel on the left. Behind the head is the state: 8 rows of 16 rods, each rod’s height a number in the state, never more rods and never fewer. At the back, a ghosted rack holds what attention would keep: one more pair of pins for every token read.

Generation throughput: 4⁠–⁠5 times a Transformer of similar size · average score: Mamba-⁠2.8B 63.3, Pythia-⁠2.8B 59.1, Pythia-⁠6.9B 61.7

02

A Cache That Keeps Growing

To write each new token, attention uses the keys and values of every token before it, so all of them are kept: the KV cache. It is as long as the text read so far.

Take Pythia-⁠2.8B, the Transformer the Mamba paper compares against: 32 layers, keys and values of 2,560 numbers each per layer, 16 bits a number. One token takes 2 × 32 × 2,560 × 2 = 327,680 bytes; at 2,048 tokens, the longest it was trained on, one text’s cache is 671 MB.

The rack at the back is that cache: each token read drops in a pair of pins, the round head its key, the square head its value. When a text ends the rack empties and the next text starts from nothing. The state in front stays at its 128 rods throughout.

Pythia-⁠2.8B: 327,680 bytes a token · 2,048 tokens: 671 MB · it keeps everything it reads

03

A State of Fixed Size

State space models come from control and signal processing. A system holds a set of internal numbers, its state h; each input x updates it once, h ← Ā·h + B̄·x, and the output is read as y = C·h. Ā sets how much of the old state stays, B̄ how the input is written in, C what is read out.

However many tokens it reads, the state stays the same size. Mamba-⁠2.8B’s state is 6,553,600 numbers per text, 13.1 MB at 16 bits, as large after the first token as after the millionth. It has 64 layers of 5,120 channels, each channel with 16 numbers, plus a short convolution that holds the last 4 inputs. Pythia-⁠2.8B’s cache passes it at the 40th token and is 51 times larger at 2,048.

The state on the stand has 8 rows of 16, 128 rods. A row is a channel and a column one number of its state; 16 is Mamba’s default state size, and a real layer has thousands of channels. A rod above the midline is positive, below it negative.

Mamba-⁠2.8B: 6,553,600 numbers, 13.1 MB, whatever the length · Pythia-⁠2.8B’s cache passes it at token 40 and is 51 times larger at 2,048

04

Every Step the Same

The structured state space models before Mamba, such as S4 (Gu, Goel and Ré, ICLR 2022), used the same Δ, A, B and C for every token. The Mamba paper calls this linear time invariance: the model’s dynamics do not change over time.

Time invariance has a payoff: the whole output can be written as the input convolved with one fixed kernel, computed in one go for training, then run step by step for generation at the same cost per token. The price is that it cannot choose by content: a token to remember and a token that does not matter go into the state the same way, and older content fades at the same rate whatever comes next.

The stand switches to that rule: for every token, the valves on the right open to the same height. The springs under the rods are A, pulling every rod back to the midline, harder towards the right: A runs from −1 to −16, Mamba’s initial values.

The same Δ, A, B and C for every token · trainable as one convolution · no choosing by content

05

Remembering Selectively

The Mamba paper uses two synthetic tasks to show what time-invariant models lack. Selective copying: in a sequence 4,096 long, 16 tokens to remember sit among noise tokens at random places, and must be repeated in order at the end. Induction heads: having seen “Harry Potter”, the next “Harry” should be followed by “Potter”.

On selective copying, two-layer models score 18.3 % with S4 alone, 97.0 % with the selective version (the paper calls it S6), and 99.8 % inside Mamba’s block. On induction heads, trained on sequences of 256, Mamba is perfect at every length tested up to 1,048,576 tokens, 4,096 times longer; the attention models tested had fallen to 26.6⁠–⁠67.6 % by 1,024 tokens and were not tested beyond 16,384 for lack of memory.

The tape now carries such a sequence: one “moon” to remember, then a row of dots. Under Mamba’s rule, the rods “moon” wrote into stay red.

Selective copying: S4 18.3 % · S6 97.0 % · Mamba 99.8 % · induction heads: trained at 256, perfect up to 1,048,576

06

Chosen by the Token

Mamba’s change is that Δ, B and C are no longer fixed parameters but computed from the current token, each by a small linear projection. A stays fixed, but how much of the old state stays is exp(Δ·A), so when Δ changes, so does that.

The paper reads Δ this way: a large Δ resets the state and focuses on the current input; a small Δ keeps the state and ignores the current input. B decides whether and how a token is written into the state, C how the state is read into the output. With a state of a single number the update is exactly a recurrent network’s gate, h ← (1 − g)·h + g·x (the paper’s Theorem 1).

The row of red handwheels on the right is each row’s Δ: the higher it rises, the longer this step. The comb in front is B, the one behind is C; both change with every token. In the paper’s ablation (about 350 million parameters), perplexity is 8.71 with Δ, B and C all selective, 9.81 with only Δ, and 10.93 with none.

Large Δ: reset and take in this token · small Δ: keep the state and let it pass · with one number of state, a gate

07

A Scan on the Chip

Once Δ, B and C change with each token, the sequence can no longer be one convolution; it has to run as a recurrence. Done directly, the whole state of every channel at every token would be written to the GPU’s main memory, N times larger than the input and output (N is the state size).

Mamba uses three classic techniques. Kernel fusion: Δ, A, B and C are read from main memory into the chip’s fast SRAM, discretised and run through the recurrence there, and only the output is written back. A parallel scan for the recurrence. Recomputation: the intermediate states that training’s backward pass needs are not stored but computed again. (On main memory and SRAM, see the plate “FlashAttention and GPU Memory”.)

The paper measured this scan at 20 to 40 times the speed of an unfused PyTorch scan and, comparing that one operation alone, faster than FlashAttention-⁠2 beyond sequences of 2K (A100 80GB, batch 1). Training memory is about that of a same-size Transformer with FlashAttention.

The full state is never written out · discretised and scanned in SRAM · 20⁠–⁠40 times a PyTorch scan

08

How Much Faster, How Much Better

Generation, on one A100 80GB, after a 2,048-⁠token prompt, 128 tokens out: Mamba 1.4B peaks at 1,814 tokens a second (batch 128); the Transformer 1.3B peaks at 364 (batch 32, beyond which it runs out of memory). At batch 1 it is 140 against 79, 1.8 times. The paper writes 4 to 5 times: with no KV cache, Mamba can run much larger batches.

Quality: Mamba and Pythia share the tokenizer and the Pile data, 300 billion tokens each. At 2.8 billion parameters the six zero-shot tests average 63.3 against 59.1, and perplexity on the Pile is 6.22 against 6.73.

The paper states its limits too: the models it evaluated are small, the largest trained one has 2.8 billion parameters, and whether Mamba still compares favourably at larger sizes remains to be assessed.

Generation: up to 1,814 against 364 tokens a second · batch 1: 140 against 79 · average 63.3 against 59.1 on the same data

09

Mamba-⁠2: Two Algorithms

Mamba-⁠2 (Dao and Gu, ICML 2024) restricts A to a number times the identity. Then what a layer does to a whole sequence can be written as one lower-triangular matrix: entry i, j is C at step i times B at step j times all the decay in between. The paper calls this state space duality.

The same matrix can be computed two ways. Row by row as a recurrence, it is a state space model, with work growing linearly in length; all at once, it looks like masked attention, C as the queries, B the keys, the input the values, with no softmax and quadratic work. Mamba-⁠2 cuts the sequence into chunks: matrix multiplications inside each chunk, which the GPU’s tensor cores do best, and only the state passed from chunk to chunk.

The paper measured this layer at 2 to 8 times the speed of Mamba’s scan, faster than FlashAttention-⁠2 beyond 2K, with room for a much larger state: the released models keep 128 numbers per channel where Mamba keeps 16. It also notes that the duality does not cover standard softmax attention.

One layer = one lower-triangular matrix · run row by row, or multiplied in chunks · state size 16 → 128

10

Mixed Models

A state of fixed size has a cost. Jelassi and colleagues (ICML 2024) proved that a model whose state has too few bits must err when copying a long random string; and looking up a phone book, models trained on the Pile showed a 410-⁠million-parameter Pythia beating a 2.8-⁠billion-parameter Mamba once the book had 70 entries or more. In NVIDIA’s 2024 study of 8B models trained on the same data, pure Mamba and Mamba-⁠2 also lagged on five-shot MMLU and phone-book lookup.

Hence hybrids: mostly Mamba layers, with a few attention layers. AI21’s Jamba (2024) has one attention layer in every 8, with 52 billion parameters of which 12 billion are used per token; its Table 1, at 256K tokens: a 4 GB KV cache, against 32 GB for Mixtral and 128 GB for Llama-⁠2 7B. NVIDIA’s Nemotron-⁠H-⁠8B has 52 layers, 24 of them Mamba-⁠2 and 4 attention.

The Mamba-⁠2 paper found the same: replacing 6 of a 2.7B model’s 64 layers with attention brought perplexity on the Pile from 6.09 to 5.95. Its guess is that attention layers look earlier tokens up directly, so not everything has to be squeezed into the state.

Jamba: 1 attention layer to 7 Mamba layers · KV cache at 256K tokens: 4 GB against Mixtral’s 32 GB

11

What It Does Not Say

A state of fixed size is not a memory of unlimited length. It keeps only what its update rule picks, compressed, and a token once read cannot be looked at again. The paper’s language models were trained on sequences of 2,048 tokens; its million-token results come from a two-layer synthetic task and from DNA and audio.

Linear time is not faster everywhere. Both kinds of model read a prompt all at once; what Mamba saves is the work and memory per generated token. At batch 1, Mamba 1.4B is 1.8 times as fast as the Transformer 1.3B, not 5.

Nor is selection better everywhere. The paper writes that it fixes time-invariant models’ weakness on discrete data such as text and DNA, but can hold them back where they excel, on continuous signals such as audio.

A fixed state is a lossy one · no looking back at what was read · the saving is in every generated token

12

Taken Apart

The machine comes apart: the tape and its two reels, the reading head, the 128 rods and the springs under them, the frame, the Δ valves on the right, the B comb in front and the C comb behind, the outputs on the left; at the back, the rack of what attention would keep.

The head reads one token and computes Δ, B and C; the valves, the two combs and the rods move once. That is one step.

Tape · reading head · Δ valves · B comb · rods · springs A · frame · C comb · outputs y · key–value rack

13

Laboratory

The tape carries a few ordinary words, then “moon” to remember, then a row of dots, and last a question mark that reads it out. The lever sets how many dots come in between; the other choice is the rule: selective (Δ, B and C set by the token) or fixed (the same Δ, B and C for every token). Each press of Run starts again from the beginning.

Readouts: how much of “moon” is left in the state (against what was written in), how many tokens have been read, and how many key–value pairs attention would hold by now. The state is 128 numbers throughout.

This is the page’s model: Δ, B and C are set by hand after the paper’s reading of them, not learned, and the fixed rule’s Δ is 0.15. A smaller Δ would keep “moon” a little longer, but then every token goes in only a little and older words linger too: a fixed rule cannot treat different tokens differently.

Readouts: how much of “moon” is left · tokens read · KV pairs · a state of 128 numbers

Try thisWith 64 dots in between, how much of “moon” does the selective rule keep? And the fixed rule? How many key–value pairs would attention hold by then, and how many numbers is the state?

What the model leaves out

The machine is a model, not the look of a chip. Its state has 8 rows of 16: 16 is Mamba’s default state size, while Mamba-⁠2.8B has 5,120 channels in each of 64 layers, and the released Mamba-⁠2 models keep 128 numbers per channel. The rule is the paper’s: h ← exp(Δ·A)·h + Δ·B·x and y = C·h, with B̄ simplified to Δ·B as in the official code, and A at Mamba’s initial values, −1 to −16. The page makes Δ, B and C from each token (pseudo-randomly); they are not learned. A real Mamba block also has projections, a short convolution and a gate around this layer, all left out here. One character or one word on the tape counts as a token; real tokenizers split text differently. The pins on the rack only show that attention keeps one key and one value per token, not to scale: in Pythia-⁠2.8B a pair is 327,680 bytes. Each step is slowed to about a second. MB means 10⁶ bytes; the Jamba paper’s GB means 2³⁰ bytes.

Easy to get wrong

“5 times” is generation throughput, each model at its own best batch size, against the standard Hugging Face Transformer; at batch 1 it is 1.8 times. “20 to 40 times” compares the scan with an unfused PyTorch scan, not with a Transformer. “Matches Transformers twice its size” means open models such as Pythia-⁠6.9B, GPT-⁠J and OPT-⁠6.7B, trained on around 300 billion tokens. The million-token result comes from a two-layer synthetic task with 16 symbols; the attention models were not tested beyond 16,384 tokens, which is not the same as failing there. The Mamba paper appeared at COLM 2024. Mamba-⁠2’s duality concerns attention without softmax and with a decaying mask; it does not make Mamba-⁠2 a Transformer. How much of a hybrid is attention depends on how layers are counted: about 7 % to 12.5 % of all layers.

Common questions · 5
How does a state space model differ from attention?

A state space model does it differently: it folds what it has read into a state of fixed size and updates it once per token.

01What It Does
How large is Mamba-2.8B’s state?

Mamba-⁠2.8B’s state is 6,553,600 numbers per text, 13.1 MB at 16 bits, as large after the first token as after the millionth.

03A State of Fixed Size
What does “selective” mean in Mamba?

Mamba’s change is that Δ, B and C are no longer fixed parameters but computed from the current token, each by a small linear projection.

06Chosen by the Token
What does Δ do in Mamba?

The paper reads Δ this way: a large Δ resets the state and focuses on the current input; a small Δ keeps the state and ignores the current input.

06Chosen by the Token
How does Jamba mix attention and Mamba?

AI21’s Jamba (2024) has one attention layer in every 8, with 52 billion parameters of which 12 billion are used per token; its Table 1, at 256K tokens: a 4 GB KV cache, against 32 GB for Mixtral and 128 GB for Llama-⁠2 7B.

10Mixed Models

Sources

  1. Gu, Dao (2023): Mamba: Linear-Time Sequence Modeling with Selective State Spaces (arXiv version)
  2. The same paper at COLM 2024 (OpenReview)
  3. Gu, Goel, Ré (2022): Efficiently Modeling Long Sequences with Structured State Spaces, ICLR 2022
  4. Mamba’s official code: mamba_ssm/modules/mamba_simple.py
  5. The state-spaces/mamba-⁠2.8b model configuration (Hugging Face)
  6. The EleutherAI/pythia-⁠2.8b model configuration (Hugging Face)
  7. Biderman et al. (2023): Pythia: A Suite for Analyzing Large Language Models Across Training and Scaling
  8. Dao, Gu (2024): Transformers are SSMs: Generalized Models and Efficient Algorithms Through Structured State Space Duality, ICML 2024
  9. The state-spaces/mamba2-2.7b model configuration (Hugging Face)
  10. Jelassi, Brandfonbrener, Kakade, Malach (2024): Repeat After Me: Transformers are Better than State Space Models at Copying, ICML 2024
  11. Waleffe et al. (2024): An Empirical Study of Mamba-based Language Models (NVIDIA)
  12. Lieber et al. (2024): Jamba: A Hybrid Transformer-Mamba Language Model (AI21)
  13. The ai21labs/Jamba-v0.1 model configuration (Hugging Face)
  14. NVIDIA (2025): Nemotron-⁠H: A Family of Accurate and Efficient Hybrid Mamba-Transformer Models
  15. The nvidia/Nemotron-⁠H-⁠8B-Base-⁠8K model configuration (Hugging Face)

Data

The machine is a model of one selective state-space layer as Gu and Dao describe it in “Mamba: Linear-Time Sequence Modeling with Selective State Spaces” (COLM 2024): a state of 8 channels × 16 numbers (16 is Mamba’s default state size), A = −1 … −16 (Mamba’s initialisation), h ← exp(ΔA)·h + Δ·B·x and y = C·h. The values of Δ, B and C on the tape are made by the page from each token, not learned; every count the page shows from the machine comes from its own arithmetic, not from measurements. The text on the tape: four Tang quatrains (Chinese page) and the openings of public-domain books by Lewis Carroll, Charles Darwin, Mary Shelley and Henry David Thoreau (English page), one character or one word to a token. Material presets, parts and the softbox studio of the site’s model kit are ported from AirsupHQ/machine-skill (MIT, © 2026 Air Supply Technologies Inc.).

The text, pictures and models on this page are licensed CC BY-NC 4.0: free to use and adapt for teaching and other non-commercial purposes, with credit to world.lapla.si. Third-party material listed above keeps its own licence.

Cite this plate

Laplace’s Mini World, Plate F56, “Mamba and State Space Models”, https://world.lapla.si/en/mamba/, CC BY-NC 4.0, updated 2026-10-28.

Embed & API

AI · all plates