↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
All 38 blocks
Purple: shared transformer, then Mamba · other blocks: Mamba
Loading recorded inference…

Why mix attention and state spaces

Attention can read any earlier token exactly, but it keeps a key and a value for every one of them. A state-space model such as Mamba reads the past only through a state of fixed size, which costs the same at any length but has to compress what it keeps. A hybrid stacks mostly state-space blocks and calls attention only now and then.

This page records Zamba2-1.2B on The capital of France is (six tokens, counting the start token) through all 38 blocks. Every block has a Mamba2 mixer. Six of them, blocks 6, 12, 18, 24, 30 and 36, first call a shared transformer: one set of attention and MLP weights used at all six places. The block selector splits those six into two halves, the shared transformer and then the Mamba mixer. The most likely next token is Paris.

The Mamba2 mixer

Each Mamba2 mixer projects its normalised input with (2048 → 8512) into five parts, in this order: a gate (4096), the input (4096), (128), (128) and a step size for each of the 64 heads. A causal convolution of width 4 followed by SiLU mixes , and with the three tokens before them.

Each head owns 64 channels of and a 64 × 128 state . With a learned , the scan is

decides how the token is written into the state and how the state is read out. Both are computed from the token, which is what makes the model selective. The step size sets the balance: a large forgets more of the old state and writes more of the new token. In Mamba2 the decay is one number per head and token, shared by all 64 channels and 128 state coordinates.

The Gate · RMSNorm · Wout step multiplies the scan output by , normalises it over its 4096 coordinates and projects it back to 2048. The state of one mixer is 64 heads × 64 channels × 128 = 524,288 numbers, whatever the length of the prompt.

The scan as a mixing matrix

Unrolling the scan gives every output as a weighted sum of earlier inputs:

plays the part of a query, of a key and of a value, and the decay becomes a causal mask that shrinks with distance. Dao and Gu call this equivalence structured state-space duality: the same layer can run as a recurrence, token by token, or as attention-like matrix products over chunks of the sequence.

The State-space mixing panel builds in three stages: , then the decay mask , then . The page computes it in the browser from the recorded , , and . Unlike softmax weights, its entries are not normalised and can be negative (drawn orange). The state the page shows is rebuilt token by token and matches the model's own chunked scan to within .

The shared transformer

The shared block reads the concatenation of the current hidden state and the original token embedding, 4096 wide, normalised with RMSNorm. Its 32 heads of 128 dimensions rotate and with RoPE and scale the scores by , which is although the heads are 128 wide. A Gated MLP, from 2048 to 8192 and back, follows.

The six calls share these weights but do not compute the same function. Each adds its own low-rank adapter (LoRA, rank 128) to the shared , , and MLP input projections, and each ends with its own 2048 × 2048 Linear layer. There is no residual inside the shared block. Its output joins only the input of the same block's Mamba mixer:

So only the Mamba output enters the residual stream. The attention still keeps keys and values for every token, but at 6 of the 38 blocks rather than all of them.

Where hybrids came from

Structured state spaces began with S4 (Gu, Goel and Ré), a linear recurrence with a fixed, carefully initialised state matrix that can be computed as one long convolution. Mamba (Gu and Dao, 2023) made , and depend on the token. That rules out the convolution but lets the model choose what to keep, and a parallel scan computes it quickly. Mamba-2 (Dao and Gu, 2024) restricted to one number per head and showed the duality with attention.

Jamba (2024) interleaved attention and Mamba layers and added mixture-of-experts MLPs. Zamba (2024) kept a stack of Mamba blocks and called one shared attention block every few blocks; the Zamba2 report moved to Mamba2 blocks and added the per-call LoRA adapters. The LinA tab draws RWKV-7, a recurrence with a matrix state and a delta rule, as a mixing matrix in the same way, and the Attention explainer's Mamba tabs take the selective scan and its duality further.

Papers

  1. Zamba: A Compact 7B SSM Hybrid Model Paolo Glorioso et al. · arXiv 2024 The design this page shows: a stack of Mamba blocks that calls one shared attention-and-MLP block every few blocks.
  2. The Zamba2 Suite: Technical Report Paolo Glorioso et al. · arXiv 2024 The model family this page records: Mamba2 blocks, and LoRA adapters that let each call of the shared block specialise.
  3. Transformers are SSMs: Generalized Models and Efficient Algorithms Through Structured State Space Duality Tri Dao, Albert Gu · ICML 2024 Mamba-2 and structured state-space duality: the scan written as a masked, attention-like matrix, as in the State-space mixing panel.
  4. Mamba: Linear-Time Sequence Modeling with Selective State Spaces Albert Gu, Tri Dao · COLM 2024 Selective state spaces: Δ, B and C computed from each token, run with a hardware-aware parallel scan.
  5. Efficiently Modeling Long Sequences with Structured State Spaces Albert Gu, Karan Goel, Christopher Ré · ICLR 2022 S4, the structured state-space layer the Mamba line grew from.
  6. Jamba: A Hybrid Transformer-Mamba Language Model Opher Lieber et al. · arXiv 2024 An early large hybrid: attention and Mamba layers interleaved, with mixture-of-experts MLPs.
  7. LoRA: Low-Rank Adaptation of Large Language Models Edward J. Hu et al. · ICLR 2022 Low-rank adapters, which Zamba2 adds to its shared weights at each of the six calls.

See also