Can Mamba Always Enjoy the "Free Lunch"?

Ruifeng Ren, Zhicong Li, Yong Liu · Submission to ICLR 2025 · Rejected

TL;DR

Mamba achieves impressive inference speeds by compressing context into a constant-size hidden state, but this paper shows there is no "free lunch." Through theoretical analysis and experiments, the authors prove that constant-sized Mamba fundamentally struggles with token copying and retrieval over long contexts. Consequently, solving arbitrary dynamic programming (DP) reasoning problems with Chain of Thought (CoT) requires Mamba to scale its size linearly with sequence length—matching the computational cost of Transformers unless the problem possesses strict local structure.

What Problem Does This Paper Address?

Transformer models dominate natural language processing, but their inference cost grows linearly with sequence length ($O(T)$ time and memory per step, yielding $O(T^2)$ total computation). Modern State Space Models (SSMs) like Mamba replace quadratic attention with selective recurrence, promising constant-level ($O(1)$) memory footprint during autoregressive inference and linear-time training.

However, recent empirical evidence indicates that Mamba lags behind Transformers on associative recall and long-context retrieval. This work investigates whether Mamba can truly match the expressive reasoning power of Transformers without sacrificing its signature efficiency. Specifically:

  1. Under what conditions can Mamba reliably perform a basic COPY operation (retrieving a specific token from history)?
  2. What are Mamba’s computational and memory requirements when solving complex, step-by-step reasoning tasks—formalized as dynamic programming (DP) problems equipped with Chain of Thought (CoT)?

Methods

  • Linear Attention Reformulation: The authors view Mamba's selective SSM module through the lens of unnormalized linear attention: $$y_i = \sum_{j=1}^i \alpha_j (c_i^T b_j) (\Delta_j \odot x_j)$$ Here, $c_i, b_j,$ and $\Delta_j \odot x_j$ act as queries, keys, and values, while $\alpha_j \in [0, 1]$ represents state-decay coefficients that exponentially discount older tokens.

  • Theoretical Analysis of COPY:

  • Constant Size: The authors analyze the attention score gap $\rho$ needed to isolate a target token $v_{\text{pos}(i)}$ from historical noise.
  • Linear Size: They construct an existence proof showing how an $O(N)$-sized Mamba state space can act as an exact key-value buffer to reproduce arbitrary tokens.
  • Expressive Power with CoT: Following theoretical frameworks that model algorithmic multi-step reasoning as dynamic programming (DP), the authors evaluate the parameter size and FLOPs required for Mamba to compute next-token intermediate states.

Datasets

The authors evaluated models on two empirical retrieval and copying benchmarks:

  • Synthetic Copy Task: An existing benchmark (adapted from Jelassi et al., 2024) where models are trained to duplicate randomly sampled character strings of lengths $N \in [N_{\min}, N_{\max}]$. Evaluated on lengths up to $N = 40$.
  • Phonebook Task: A realistic in-context retrieval benchmark (adapted from Waleffe et al., 2024; Jelassi et al., 2024) where models read a phone directory containing $N$ names and numbers, then retrieve a specific person's number. Evaluated using pre-trained Mamba checkpoints (370M, 1.4B, and 2.8B) pre-trained on the Pile dataset.

Key Insights and Ideas

  • The Exponential Bottleneck of Constant-Sized Mamba: To accurately copy a token located $L$ steps in the past, a constant-sized Mamba requires the target token's relevance score to scale exponentially with distance: $$\rho = \Omega\left(\left(\frac{1}{a_{\min, >}}\right)^L\right)$$ As the copy distance grows, distinguishing the signal from the accumulated decaying state requires unrealistically large attention margins.

  • $O(N)$ State Space Restores Perfect Copying: If Mamba's hidden state dimension is allowed to scale linearly ($O(N)$) with sequence length, it can allocate orthogonal dimensions for each step—functioning like an uncompressed KV cache and achieving perfect token retrieval.

  • No Free Lunch on General DP Reasoning: Because solving general DP problems requires querying arbitrary previous subproblem results, Mamba must grow its state size to $O(T)$ across $T$ reasoning steps. This brings Mamba’s per-step inference cost to $O(T)$ and its total inference cost to $O(T^2)$—putting it on equal footing with standard and efficient Transformers.

  • Locality Brings True Savings: If a DP problem exhibits $m$-locality (each step depends only on the preceding $m$ states), Mamba only requires an $O(m)$ state size. When $m \ll T$, inference drops to $O(m)$ per step ($O(mT)$ total), genuinely outperforming standard Transformers.

Model with CoT Storage Inference (Each Step) Total Inference (All Steps)
Standard Transformer $O(1)$ $O(T)$ $O(T^2)$
Sparse / Linear Transformer $O(\sqrt{T})$ $O(T)$ $O(T^2)$
Mamba (General DP) $O(T)$ $O(T)$ $O(T^2)$
Mamba ($m$-locality DP) $O(m)$ $O(m)$ $O(mT)$

Key Results

  • Synthetic Copying:
  • While a 126M Transformer mastered the copy task rapidly, a 135M Mamba required nearly $7\times$ more training examples to reach similar accuracy.
  • When increasing the sequence length $N_{\max}$ from 10 to 20 to 30, Mamba's required training samples grew super-linearly ($1.36 \times 10^4 \to 3.52 \times 10^4 \to 1.15 \times 10^5$). At $N_{\max} = 40$, Mamba failed completely within $5.8 \times 10^5$ samples, whereas Transformers learned stably.
  • Reducing Mamba's hidden dimension severely harmed copy performance, indicating that hidden state capacity—rather than layer depth—is the primary bottleneck.
  • Phonebook Retrieval:
  • Retrieval accuracy dropped consistently across all pre-trained Mamba checkpoints as phonebook length grew.
  • Adding a prompt instruction at the very beginning of the prompt (e.g., "Remember the phone number of Joseph Perry") notably boosted accuracy, confirming that Mamba performs significantly better when its selective gating can filter relevant keys upfront.

Limitations and Future Work

  • Simplified Theoretical Architecture: The theoretical derivations focus on the core selective SSM and gated MLP blocks, leaving out Layer Normalization, 1D convolutions, and the input SiLU non-linearity.
  • Existence vs. Learnability: The $O(N)$ capacity results prove that a parameter configuration capable of exact copying exists, but do not guarantee that gradient descent will find these weights in practice.
  • Hybrid Architectures: Given that Mamba excels at structured transformations and parity learning while Transformers dominate arbitrary retrieval, the authors emphasize hybrid models (interleaving attention and SSM layers) as the most promising path forward.

Summary updated

Abstract

Transformers have been the cornerstone of current Large Language Models (LLMs); however, its linear growth in overhead during inference with respect to sequence length poses challenges for modeling long sequences. In this context, Mamba has gradually attracted attention due to its constant-level size during inference and existing empirical results have shown that it can perform comparably to Transformers in sequence modeling while offering significant savings. However, one may ask that, can Mamba always enjoy the ``free lunch"? In this paper, we focus on analyzing the expressive ability of Mamba from a theoretical standpoint. First, inspired by the connection between Mamba and linear attention, we investigate potential shortcomings of the Mamba when performing the COPY operation. Our results indicate that Mamba with constant size may encounter bottlenecks when handling COPY, while it can achieve perfect performance when the size scales linearly with sequence length. Based on this observation, we analyze Mamba's ability to tackle DP problems when equipped with Chain of Thought (CoT). Our findings suggest that to solve arbitrary DP problems, the total cost of Mamba is comparable to standard and efficient Transformers. However, similar to efficient Transformers, when facing DP problems with favorable properties such as locality, Mamba can provide savings in overhead. Our results contribute to a deeper understanding of Mamba.
BibTeX
@misc{ren2024can,
  author = {Ruifeng Ren and Zhicong Li and Yong Liu},
  title = {{Can Mamba Always Enjoy the "Free Lunch"?}},
  year = {2024},
  eprint = {2410.03810},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2410.03810},
}

Loading related content...

Figure