XYZNOT

Papers · arXiv 2312.00752

Mamba: Linear-Time Sequence Modeling with Selective State Spaces

Published cs.LGcs.AI arXivPDF

Read with translation and AI

Summary in three lines

  1. 🎯 Transformers scale quadratically with sequence length and require large KV caches during autoregressive inference, while prior subquadratic models like LTI state space models struggle on discrete, content-reasoning tasks like language modeling.
  2. 🔧 Makes SSM parameters input-dependent (selective) to dynamically filter or propagate information, and pairs this with a hardware-aware fused parallel associative scan and recomputation algorithm to avoid materializing the expanded hidden state in GPU HBM.
  3. 📈 Mamba-3B matches or exceeds Transformers twice its size on downstream zero-shot tasks (e.g., 63.3% average vs. 59.1% for Pythia-2.8B and 61.7% for Pythia-6.9B) while providing up to 5× higher inference throughput.

Abstract

Foundation models, now powering most of the exciting applications in deep learning, are almost universally based on the Transformer architecture and its core attention module. Many subquadratic-time architectures such as linear attention, gated convolution and recurrent models, and structured state space models (SSMs) have been developed to address Transformers' computational inefficiency on long sequences, but they have not performed as well as attention on important modalities such as language. We identify that a key weakness of such models is their inability to perform content-based reasoning, and make several improvements. First, simply letting the SSM parameters be functions of the input addresses their weakness with discrete modalities, allowing the model to selectively propagate or forget information along the sequence length dimension depending on the current token. Second, even though this change prevents the use of efficient convolutions, we design a hardware-aware parallel algorithm in recurrent mode. We integrate these selective SSMs into a simplified end-to-end neural network architecture without attention or even MLP blocks (Mamba). Mamba enjoys fast inference (5 higher throughput than Transformers) and linear scaling in sequence length, and its performance improves on real data up to million-length sequences. As a general sequence model backbone, Mamba achieves state-of-the-art performance across several modalities such as language, audio, and genomics. On language modeling, our Mamba-3B model outperforms Transformers of the same size and matches Transformers twice its size, both in pretraining and downstream evaluation.

Reading guide

Problem and motivation

Foundation models overwhelmingly rely on the Transformer architecture and self-attention. While dense attention routes information effectively across a sequence context window, it incurs two fundamental limitations: quadratic computational cost with respect to sequence length , and the requirement of storing past keys and values (the KV cache), leading to linear-time inference per step and memory bottlenecks.

Prior subquadratic approaches—such as linear attention, recurrent architectures, and structured state space models (SSMs) like S4—scale linearly or pseudo-linearly in time. However, structured SSMs have historically failed to match Transformer quality on discrete, information-dense modalities such as text. The authors identify the root cause: prior SSMs are linear time-invariant (LTI). Because their transition dynamics do not change depending on input content, they cannot selectively compress context, filter out irrelevant noise, or modulate retention across variable spacing.

Key ideas

  • Selection Mechanism as Context Compression: Sequence modeling requires compressing history into a state. Prior LTI SSMs use fixed transition parameters, forcing them to treat all timesteps uniformly. By parameterizing SSM matrices based on the current input token, the model gains the content-awareness needed to filter or remember information selectively.
  • Overcoming the Non-Convolving Bottleneck: Making parameters input-dependent breaks the mathematical equivalence between linear recurrences and global convolutions. Without convolutions, naive recurrence would require materializing an expanded latent state of size in GPU high-bandwidth memory (HBM). Mamba solves this by fusing discretization, parallel associative scan, and output projection into fast GPU on-chip SRAM, keeping HBM traffic minimal.
  • Unified Architecture: Mamba replaces interleaved Multi-Head Attention (MHA) and MLP blocks with a single homogeneous block that combines the gated design of H3 with the SwiGLU-style gated MLP.

Method

Continuous state space models map an input to via an implicit latent state :

Using zero-order hold (ZOH) discretization with step size , the continuous system transforms into discrete recurrence parameters:

The Selection Mechanism (S6)

Instead of keeping constant over time, Mamba makes them input-dependent:

  • , with and

This generalizes classical RNN gating: when , the recurrence simplifies directly to a gated update , where .

Hardware-Aware State Expansion

To maintain high throughput without materializing the state in HBM:

  1. Parameters are loaded from HBM directly into on-chip SRAM.
  2. Discretization into and a work-efficient parallel associative scan are computed entirely inside SRAM.
  3. The latent state is contracted with to shape before writing back to HBM.
  4. Intermediate states are recomputed in SRAM during the backward pass instead of being saved in HBM.

Results

  • Synthetic Extrapolation: On the Induction Heads task, Mamba trained on sequence length 256 extrapolates perfectly (100% accuracy) up to length ( sequence length), whereas attention baselines fail or OOM past . On Selective Copying, Mamba reaches 99.8% accuracy compared to 57.0% for H3 and 18.3% for S4.
  • Language Modeling: Evaluated under Chinchilla scaling up to 1.3B parameters on the Pile, Mamba matches or outperforms Transformer++ (rotary embeddings, SwiGLU, RMSNorm). On downstream zero-shot tasks (e.g., LAMBADA, PIQA, HellaSwag, WinoGrande, ARC), Mamba-2.8B attains an average accuracy of 63.3%, outperforming Pythia-2.8B (59.1%), Pythia-6.9B (61.7%), and matching or exceeding 6B–7B parameter models.
  • Genomics and Audio: On Great Apes DNA classification at sequence length 1M (), Mamba achieves 71.67% (1.4M params) and 81.31% (7M params) accuracy compared to 54.87% for HyenaDNA. On SC09 speech generation, Mamba reduces FID from 1.99 (SaShiMi) to 0.94 (6.1M params) and 0.67 (24.3M params).
  • Efficiency: The fused scan kernel executes 20–40× faster than standard PyTorch scan implementations and runs up to 7× faster than FlashAttention-2 at 32K context. Inference throughput is up to 5× higher than comparable Transformers.

Limitations and open questions

  • Continuous Signal Performance: While input-dependent selection improves discrete reasoning (text, DNA), ablations on raw audio waveform pretraining show that purely linear time-invariant (LTI) models (like standard S4) perform better on uniformly sampled, continuous data.
  • Model Scale: Evaluated models are capped around 3B parameters and 300B pretraining tokens; empirical performance at 7B–70B+ scales and beyond requires further investigation.
  • Downstream Ecosystem: Transformers possess extensive tooling for prompt engineering, in-context manipulation, KV-cache quantization, and adapter tuning. Understanding how SSMs adapt across these interaction modes remains an active exploration.

Key terms

Structured State Space Models (SSMs)
A class of deep sequence models derived from continuous linear dynamical systems that map 1D inputs through an implicit latent state.
Linear Time Invariance (LTI)
A mathematical property where system transition parameters remain static across all timesteps, enabling fast global convolution representations but restricting content-dependent reasoning.
Selection Mechanism
Parameterizing SSM transition dynamics (Δ, B, C) as functions of the input token, allowing the model to dynamically propagate, reset, or ignore context along the sequence length.
S6
An abbreviation for selective structured state space models computed via an associative parallel scan rather than convolution.
Parallel Associative Scan
A work-efficient prefix computation algorithm that evaluates linear recurrence across sequence lengths in parallel on accelerator hardware.
Hardware-Aware State Expansion
A GPU kernel fusion and recomputation strategy that performs discretization and scan in fast on-chip SRAM to avoid round-tripping large intermediate states to high-bandwidth memory (HBM).
Induction Heads
A synthetic associative recall mechanism that evaluates whether a model can reproduce a previously observed token following its preceding context token.
Mamba Block
A simplified, homogeneous sequence model block that integrates input projections, 1D convolution, selective SSMs, and multiplicative gating without separate attention or MLP layers.

Questions

Why can't selective SSMs be trained using fast FFT convolutions?

Fast global FFT convolutions require the system dynamics (matrices A, B, and C) to be constant across time (linear time invariance). Because Mamba makes B, C, and Δ functions of the input token at each timestep, the convolution kernel varies at every position, breaking the convolutional equivalence and requiring recurrent parallel scans instead.

How does Mamba avoid the memory blowup of recurrent state expansion during training?

Naive recurrence with state expansion dimension N requires materializing an intermediate state of shape (B, L, D, N) in GPU HBM. Mamba uses kernel fusion to load only low-dimensional inputs into SRAM, computes the discretization and parallel scan within fast SRAM, writes only the contracted (B, L, D) output back to HBM, and recomputes intermediate activations during backpropagation.

What is the relationship between Mamba's Δ parameter and recurrent gating?

The step size parameter Δ acts as a generalized gating mechanism. When discretized under zero-order hold (ZOH) with N=1 and A=-1, the selective recurrence reduces mathematically to a standard RNN gate: large Δ causes the model to focus on the current input and reset the state, whereas small Δ persists historical state and ignores the input.

Does Mamba use a KV cache during generation?

No. Because Mamba operates as an autoregressive state space recurrence during generation, it only maintains a fixed-size hidden state (dimension D × N per layer) rather than caching keys and values for all past tokens, resulting in constant-time O(1) inference per step.

The overview on this page was written by AI from the paper and may contain mistakes. Read the paper for the details.

Read with translation and AI