
📺 Today’s recommended deep-dive video: https://www.youtube.com/watch?v=8Q_tqwpTpVU
Mamba: The Linear-Time Architecture Challenging the Transformer’s Throne
For years, the Transformer has dominated artificial intelligence, but its quadratic scaling creates a massive computational bottleneck as sequences grow. Mamba introduces a “Selective State Space” approach that combines the parallel training efficiency of CNNs with the constant-time inference of RNNs. This breakthrough allows for modeling massive contexts without the memory explosion typically seen in attention-based models.
Core Question: How can we model massive sequences with the efficiency of a linear system without losing the context-aware power of attention?
Highlights
- Linear Scaling: Unlike Transformers, Mamba’s computational cost grows linearly with sequence length, enabling million-token contexts.
- Selection Mechanism: Mamba introduces input-dependent parameters, allowing the model to “choose” which information to remember or ignore.
- Hardware-Aware Design: The architecture uses kernel fusion and parallel scans to optimize data movement between GPU SRAM and DRAM.
- Constant-Time Inference: Since it functions as a recurrent model during generation, the cost per token remains identical regardless of sequence length.
⏱️ Reading time: approx. 10 minutes · Saves you about 65 minutes vs. watching.
Want to take notes while watching? Click the image below and let AI Notebook capture the key points for you 👇
The Evolution of Sequence Modeling
Why Transformers Aren’t Enough
Sequence modeling is the art of mapping one stream of data to another, whether it’s audio signals or tokens in a sentence.
Traditional models like Recurrent Neural Networks (RNNs) offer constant-time inference but fail to parallelize during training, making them sluggish on modern hardware. Meanwhile, Transformers revolutionized the field with parallel processing, yet they suffer from quadratic complexity, meaning the cost of computation explodes as sequences grow longer. We have long sought a “Goldilocks” architecture that balances these tradeoffs effectively without sacrificing performance on long-range dependencies.
Convolutional Neural Networks (CNNs) provide another alternative, offering easy parallelization through fixed kernels. However, they are inherently limited by a finite context window, preventing them from capturing the deep, global relationships required for complex language tasks or sophisticated reasoning.

💡 Digging Deeper
Q: Why is “quadratic complexity” such a big deal for Transformers?
A: In a Transformer, every token must “attend” to every other token. If you double the length of your input, the number of dot-product operations quadruples, leading to massive memory requirements.
Q: Can’t we just use RNNs for everything?
A: RNNs are difficult to train because they process tokens one by one. You cannot utilize the thousands of cores in a GPU effectively if each step depends on the completion of the previous one.
From Bunny Populations to State Space Models
The Mathematical Foundation
To understand State Space Models (SSMs), we first look at differential equations, which model how a system changes over time. Imagine a population of bunnies growing at a constant rate; the rate of change depends on the current number of bunnies present in the system at that exact moment.
Solving these equations gives us a function that predicts the system’s state at any point in time.
In the digital realm, we must discretize these continuous equations to work with discrete tokens or sampled signals. By using methods like Euler’s approximation or Zero-Order Hold, we transform continuous differential equations into recurrent formulas. This allows a model to calculate the next hidden state based on the previous one, essentially “compressing” history into a fixed-size vector that evolves as new data arrives, rather than keeping a massive cache of every past token.
The Role of the Hippo Matrix
The “A” matrix in an SSM is responsible for capturing the history of the system. If it is poorly structured, the model “forgets” information too quickly or fails to prioritize recent context over distant noise.
The authors utilize Hippo (High-Order Polynomial Projection Operators) theory to initialize this matrix. Hippo allows the state to mathematically approximate the history of the input signal using Legendre polynomials. This ensures that the model maintains a high-fidelity representation of recent tokens while gracefully decaying the influence of older data, acting much like an advanced version of an exponentially moving average.

💡 Digging Deeper
Q: What does the “Delta” (step size) parameter do?
A: Delta controls how much the model focuses on the current input versus the past state. A small Delta means the model preserves the current state; a large Delta means it incorporates more of the new input.
The Mamba Breakthrough: Selective Scan
Moving Beyond Time-Invariance
Vanilla SSMs are “time-invariant,” meaning they use the same parameters for every token regardless of its importance or content. While efficient, this makes them terrible at “Selective Copying”—the ability to ignore irrelevant information while focusing on key data points. If a model cannot distinguish between a crucial noun and a stray punctuation mark, it lacks the “reasoning” capability required for sophisticated language modeling and complex instructions.
Mamba fixes this by making the B, C, and Delta matrices functions of the input itself.
By allowing the model to change its parameters for each token, Mamba gains the “selection” ability of attention. However, this content-awareness breaks the ability to use traditional convolutions, which rely on a fixed kernel. To solve this, the authors developed a highly optimized “Selective Scan” algorithm that utilizes hardware-aware tricks to maintain the speed of parallel training while allowing the parameters to fluctuate.
Hardware-Aware Optimization
Speed isn’t just about the number of operations; it’s about how efficiently data moves through the GPU’s memory hierarchy.
Mamba achieves its blazing performance by utilizing Kernel Fusion, which minimizes the slow transfer of data between the high-bandwidth memory (DRAM) and the fast on-chip memory (SRAM). By fusing multiple operations into a single Cuda kernel, the model keeps intermediate states on the chip, avoiding the “IO-bound” bottleneck that plagues less optimized architectures. This hardware-aware design is what truly allows Mamba to scale linearly while being competitive with Transformers on modern GPUs.

💡 Digging Deeper
Q: If Mamba is recurrent, how is it parallelized for training?
A: It uses an associative “Parallel Scan” algorithm. As long as the operations are associative (like addition), you can compute segments of the sequence in parallel and then combine them, similar to how a prefix-sum is calculated.
Key Takeaways
Mamba represents a significant shift in how we approach sequence modeling. By moving away from the “all-to-all” attention mechanism of the Transformer and toward a selective, compressed state representation, it solves the quadratic memory problem. This allows the model to process sequences that are orders of magnitude longer than what was previously possible on the same hardware.
The innovation isn’t just in the math, but in the implementation. By grounding the model in differential equations and then optimizing the computation for the specific memory layout of GPUs, the authors created a system that is both theoretically sound and practically fast. Mamba performs as well as the best Transformers on standard benchmarks while offering much faster inference and better scaling for the future of AI.
Q&A
Q1: Is Mamba better than a Transformer?
A: “Better” depends on the use case. Mamba is significantly more efficient for long sequences and offers faster inference. However, Transformers have been tested on much larger scales (trillions of tokens), so Mamba’s performance at the “GPT-4 scale” is still an area of active research.
Q2: How does Mamba handle the “forgetting” problem of RNNs?
A: It uses the Hippo Matrix initialization to ensure that the hidden state captures as much historical information as possible and a selection mechanism to decide what is worth keeping.
Q3: Can Mamba be used for images or just text?
A: While the paper focuses on sequence modeling (text/audio), the underlying architecture is general-purpose and can be adapted for any data that can be linearized into a sequence.
Q4: What is “Kernel Fusion” in the context of Mamba?
A: It is a technique where multiple mathematical operations (like discretization and the scan) are combined into a single GPU command. This prevents the GPU from having to write intermediate results to its slow main memory.
Q5: Why does Mamba use a 1D convolution before the SSM?
A: The convolution helps tokens “talk” to their immediate neighbors before they enter the state space model. This local context mixing compensates for the fact that the SSM dimensions are otherwise independent.
Q6: What is “Activation Recomputation”?
A: It is a memory-saving trick where, instead of saving every hidden state for the backward pass, the model simply recalculates them on the fly. This is often faster than reading them from the GPU’s slow memory.
Q7: Does Mamba have a finite context window?
A: Theoretically no. Because it is an RNN-style model at its core, it has an “infinite” context. Practically, the state can only hold so much information before it becomes crowded, but it doesn’t have the hard cutoff or quadratic cost of a Transformer.
