Skip to content
AI360Xpert
Core ML

Mamba

Mamba makes state space models input-dependent: instead of using the same fixed transition for every token, it learns separate selection gates that let each token control how much it updates the state and how much of the output to reveal.

Mamba replaces the fixed A and B matrices of a standard SSM with input-dependent selection gates that decide at each step how much to update the state and how much to reveal in the output, letting the model selectively remember or forget
Mamba replaces the fixed A and B matrices of a standard SSM with input-dependent selection gates that decide at each step how much to update the state and how much to reveal in the output, letting the model selectively remember or forget

Why Does This Exist?

State space models achieve linear-time inference by compressing the past into a fixed-size state updated per token. The problem is their selection is "dumb": the matrices AA and BB that control how each input modifies the state are fixed — the same for every token, regardless of content. That's the same limitation RNNs have, and it's why information that entered the state many tokens ago tends to decay regardless of whether it was important.

Attention solves this by having every query look back at every key directly — expensive but precise. Gu and Dao (2023) asked: can you keep the linear-time budget of SSMs while adding content-aware selection? Mamba is their answer. The key change is to make the discretisation step size Δ\Delta, which controls how strongly a new input modifies the state, a function of the input itself. A "boring" token produces a small Δ\Delta — the state barely changes, old information is preserved. A "salient" token produces a large Δ\Delta — it rewrites the state strongly. The model learns which tokens matter.

This one change closes much of the quality gap between SSMs and attention, particularly on recall-intensive tasks, while preserving linear inference cost.

Think of It Like This

A highlighter you only press hard on important sentences

Imagine reading a document and updating your notes as you go. A vanilla SSM is like pressing the highlighter with exactly the same pressure on every sentence, so every sentence fades at the same rate. Mamba is like having a smart grip that tightens when the sentence is important — important sentences get pushed hard into the notes, unimportant ones barely leave a mark. The notebook (state) ends up with the salient sentences prominent and the filler already faded.

How It Actually Works

The selection mechanism

In a standard SSM, the step size Δ\Delta is a learned scalar — fixed across all inputs. Mamba makes Δ\Delta input-dependent:

Δt=softplus(WΔxt+bΔ)\Delta_t = \text{softplus}(W_\Delta \, x_t + b_\Delta)

where WΔW_\Delta is a small learned linear projection and xtx_t is the current input. Then:

Aˉt=exp(ΔtA),BˉtΔtB\bar{A}_t = \exp(\Delta_t A), \quad \bar{B}_t \approx \Delta_t B

When Δt\Delta_t is large, Aˉt0\bar{A}_t \approx 0 (old state forgotten, new input dominates). When Δt\Delta_t is small, AˉtI\bar{A}_t \approx I (state preserved, new input barely absorbed). This is exactly attention's selectivity, but implemented through state transitions rather than a score matrix.

Why this breaks the parallel form

Standard SSMs can be parallelised with a fast convolution during training because the kernel is fixed — the same for every position. Mamba's input-dependent Δ\Delta means the kernel changes at every position, so the standard FFT trick doesn't apply. Gu and Dao wrote a hardware-efficient parallel scan algorithm that exploits the associativity of the state transitions, achieving O(NlogN)O(N \log N) on GPU with careful use of SRAM — a different but similarly IO-aware trick to FlashAttention.

What the Mamba block looks like

A Mamba block wraps the SSM with a gated MLP: the input is split into two branches, one goes through the SSM, the other through a pointwise gate (SiLU), and the two are multiplied. This is a gated linear unit pattern, similar to SwiGLU in transformers.

Show Me the Code

Implementing the selective recurrence with input-dependent Δ\Delta and comparing to a fixed-Δ\Delta SSM.

import numpy as np
rng = np.random.default_rng(13)n, d, d_in = 12, 4, 8   # seq len, state dim, input dim
A = -0.5 * np.ones(d)           # stable diagonal A (continuous)B = rng.standard_normal((d, d_in)) * 0.1C = rng.standard_normal((d_in, d)) * 0.1W_delta = rng.standard_normal((d_in,)) * 0.01  # simplified scalar projectionx_seq = rng.standard_normal((n, d_in))
def softplus(x):    return np.log1p(np.exp(x))
# Mamba: input-dependent deltah = np.zeros(d)y_mamba = np.zeros((n, d_in))for t in range(n):    delta = softplus(W_delta @ x_seq[t] + 0.5)   # varies per token    A_bar = np.exp(delta * A)    B_bar = (1 - A_bar) / (-A) * (B @ x_seq[t])  # simplified ZOH    h = A_bar * h + B_bar    y_mamba[t] = C @ h
# Fixed SSM: constant deltadelta_fixed = 0.3A_bar_f = np.exp(delta_fixed * A)h_f = np.zeros(d)y_fixed = np.zeros((n, d_in))for t in range(n):    B_bar_f = (1 - A_bar_f) / (-A) * (B @ x_seq[t])    h_f = A_bar_f * h_f + B_bar_f    y_fixed[t] = C @ h_f
# The variance of delta shows how much it varies with inputdeltas = [softplus(W_delta @ x_seq[t] + 0.5) for t in range(n)]print(f"Delta range: {min(deltas):.3f}{max(deltas):.3f}")# Varies meaningfully across tokens — the selection is workingprint(f"Output MSE (Mamba vs fixed): {((y_mamba - y_fixed)**2).mean():.4f}")# Non-zero — the two models differ, confirming selection changes the result

The varying delta is the model's content-aware gate — some tokens are absorbed strongly, others nearly pass through.

Watch Out For

Expecting Mamba to fully replace attention on all tasks

Mamba's selectivity is better than standard SSMs but still operates through a fixed-size compressed state. Tasks requiring precise recall of a single specific token from thousands of steps ago — needle-in-a-haystack retrieval, exact memorisation — can still exceed what the state can faithfully store. Attention handles these by direct lookup; Mamba must store the needle in the state, which is harder when the state has been modified by many subsequent tokens. For these tasks, hybrid architectures that pair Mamba with periodic attention layers tend to do better.

Assuming Mamba's parallel scan is equivalent to FFT convolution

Standard SSMs parallelize training with FFT convolution, O(NlogN)O(N \log N). Mamba's input-dependent transitions break that — the kernel is different at every position. The parallel scan algorithm Mamba uses is also O(NlogN)O(N \log N) on a GPU but has different memory access patterns and implementation requirements. If you naively try to adapt standard SSM training code to Mamba's selective transitions, you won't get the parallel speedup.

The Quick Version

  • Mamba makes the SSM step size Δ\Delta a learned function of the input, so each token controls how strongly it modifies the state.
  • Large Δ\Delta: the old state is largely forgotten, the new input dominates. Small Δ\Delta: state is preserved, input barely registers.
  • This single change closes much of the quality gap between SSMs and attention, especially on recall-heavy tasks.
  • The input-dependent transitions break the FFT parallelism trick; Mamba replaces it with a hardware-efficient parallel scan.
  • For tasks that need precise long-range retrieval, hybrid attention+Mamba architectures outperform Mamba alone.
  • State Space Models covers the SSM foundation Mamba builds on — the fixed-state recurrence and the parallel training form.
  • Hybrid Attention–SSM Architectures interleave Mamba layers with attention to get both selectivity and precise long-range recall.
  • Attention Complexity establishes what Mamba avoids: the O(N2)O(N^2) cost of maintaining a full attention score matrix.
  • Linear Attention is the attention-family analogue of Mamba's approach — also running-state, also O(N)O(N), but from a kernel rather than a selection mechanism.
  • Mixture of Experts is the other architectural scaling trick active in current models, often combined with Mamba layers.

Related concepts