Transformers are SSMs: Generalized Models and Efficient Algorithms Through Structured State Space Duality

Tri Dao, Albert Gu

TL;DR

This paper introduces the State Space Duality (SSD) framework, proving that State Space Models (SSMs) and linear attention are mathematically equivalent through the lens of structured semiseparable matrices. Leveraging this connection, the authors present Mamba-2, a new architecture that is 2-8x faster than the original Mamba while matching or exceeding Transformer performance on large-scale language modeling.

The Problem: Two Disjointed Worlds

Deep learning is currently a tale of two architectures: Transformers, which dominate the landscape but scale quadratically ($O(T^2)$) with sequence length, and State Space Models (SSMs) like Mamba, which scale linearly ($O(T)$) but have historically been harder to optimize and understand.

Despite Mamba's success, SSMs were developed somewhat in isolation from the massive community effort dedicated to optimizing Transformers. This disjointed development made it difficult to apply hardware-specific tricks—like maximizing GPU Tensor Core utilization—to SSMs. The authors set out to unify these two worlds, seeking a theoretical bridge that allows SSMs to benefit from the algorithmic and systems-level maturity of attention mechanisms.

Methods: The SSD Framework and Mamba-2

The core contribution is the SSD framework, which identifies that both SSMs and attention variants are specific instances of multiplication by structured semiseparable matrices.

  1. Theoretical Duality: The authors demonstrate that a selective SSM has a "dual" quadratic form that looks exactly like a variant of attention without the softmax. Conversely, linear attention can be reframed as a linear recurrence (an SSM).
  2. Mamba-2 Architecture: Inspired by this duality, Mamba-2 modifies the original Mamba block. Instead of generating SSM parameters sequentially as a function of the input inside the block, it produces them in parallel at the beginning, much like the $Q, K, V$ projections in a Transformer.
The Parallel Mamba Block simplifies projections to occur in parallel, mirroring Transformer designs for better hardware utilization.
  1. Hardware-Efficient SSD Algorithm: By viewing the SSM as a block-decomposed semiseparable matrix, the authors developed a new algorithm that uses matrix multiplication as its primary primitive. This allows Mamba-2 to leverage the high-throughput Tensor Cores on modern GPUs, which the original Mamba's "selective scan" could not fully utilize.

Key Insights and Ideas

  • The Matrix Bridge: The fundamental realization is that sequence models are essentially different ways of multiplying by a $T \times T$ matrix. For SSMs, this matrix is "semiseparable," meaning its off-diagonal blocks have low rank.
  • Duality as Computation Order: The difference between an SSM (linear time) and Attention (quadratic time) is often just the order in which tensor contractions are performed. SSD allows models to switch between these "dual" forms depending on what is most efficient for the hardware.
  • Positional Encoding via Masks: SSD replaces the heuristic positional embeddings of Transformers with a data-dependent positional mask ($L$) derived from the SSM's state transitions, providing a more principled way to control information flow across time.
The SSD framework maps various sequence models into a unified space of structured matrices, bridging SSMs and Structured Masked Attention.

Datasets and Key Results

The authors evaluated Mamba-2 using standard pre-training datasets like The Pile (an 800GB text corpus), synthetic benchmarks like MQAR (Multi-Query Associative Recall), and standard zero-shot benchmarks (LAMBADA, HellaSwag, PIQA, ARC).

  • Massive Speedups: The SSD algorithm is 2-8x faster than Mamba's original selective scan. It outpaces the highly-optimized FlashAttention-2 at sequence lengths as short as 2,000 tokens.
  • Scaling Dominance: In scaling law experiments up to 1.3B parameters, Mamba-2 Pareto-dominates both Mamba-1 and a strong "Transformer++" baseline, achieving better perplexity for the same amount of training compute and wall-clock time.
  • Superior Recall: On the MQAR task, Mamba-2 significantly outperforms Mamba-1. Because the SSD algorithm is highly efficient, Mamba-2 can utilize much larger state sizes (8x larger or more), allowing the model to memorize significantly more information from the context.

Limitations and Future Work

  • Expressivity Trade-offs: To achieve this efficiency, Mamba-2 uses a slightly more restrictive structure for its state transitions (scalar-times-identity) compared to Mamba-1's diagonal structure. While compensated for by larger state sizes, the theoretical impact on certain tasks requires further exploration.
  • Non-Causal Models: The current framework is optimized for causal (autoregressive) modeling. Extending these duality insights to non-causal settings, such as BERT-style encoders or Vision Transformers, remains a major open direction.
  • Hybrid Architectures: The authors suggest that future foundation models might be hybrids, combining SSD layers with standard softmax attention to capture the best of both paradigms.

Summary updated

BibTeX
@inproceedings{dao2024transformers,
  author = {Tri Dao and Albert Gu},
  title = {{Transformers are SSMs: Generalized Models and Efficient Algorithms Through Structured State Space Duality}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=ztn8FCR1td},
}
Figure