mlgenerate.comVisual Generative ML
All Models/text/Mamba (Selective SSM)

Mamba: Selective State Space Model

Autoregressive2023 · Carnegie Mellon & Together AI (Gu & Dao)

Linear-time sequence modeling with input-selective recurrent state spaces

Execution Paradigm:Constant O(1) Memory Recurrent State Step
Constant O(1) Memory (Zero KV-Cache Growth)

Intuition & Key Idea

ELI5 Guide

Why this model was invented and how it transforms generation

The 30-Second Core Idea

"Processes millions of words in linear time and fixed memory by continuously filtering incoming data like an audio equalizer instead of saving every past word."

Real-World Metaphor

A person listening to a lecture who updates a concise set of running mental notes, rather than recording every word and re-reading the entire transcript every sentence.

Key Superpowers & Values
Time ComplexityLinear Scaling
O(N) Linear Time

Compute scales linearly with document length instead of quadratic O(N²).

Inference MemoryFlat Memory
O(1) Constant VRAM

State size never grows; no exploding KV cache on 100k+ tokens.

Hardware KernelSRAM Optimized
Hardware-Aware Scan

Executes associative prefix scan directly in fast GPU SRAM.

Long ContextInfinite Context
1M+ Tokens Native

Ingests massive books and codebases without memory crashes.

1. Input-Dependent Parameter Discretization (Δ, B, C)

Feed single new token x_t. Compute timescale Δ_t, input matrix B_t, and output matrix C_t via linear projection.

Key Insight:

Calculated instantly for single input vector without loading past sequence context.

Interactive Stage (INFERENCE)Projects single new token into dynamic SSM transition parameters.
Phase 1: Prompt PrefillProcessing prompt tokens in one parallel batch to initialize KV cache
Step 1 of 3
The Autoregressive Loop Explained Simply
1
Read KV Cache
Fetch previous tokens
→
2
Load 16GB Weights
HBM memory bottleneck
→
3
Emit 1 Token
Sample from logits
↺
Repeat for next word
Active Token Stream & Memory AllocationTotal Tokens: 3
Prompt"The"K, V cached
Prompt"cat"K, V cached
Prompt"sat"K, V cached
GPU High Bandwidth Memory (HBM) — KV Cache Pool
Avoids O(T²) recomputation · Memory Bandwidth Bound
Slot 0
"The"
[K_cache, V_cache]
Slot 1
"cat"
[K_cache, V_cache]
Slot 2
"sat"
[K_cache, V_cache]
Slot 3
"on"
Unallocated
Slot 4
"the"
Unallocated
Slot 5
"mat"
Unallocated
⚠️ Memory Bandwidth Bottleneck:During autoregressive decoding, the GPU must stream tens of gigabytes of weights and cached KV vectors from VRAM to compute the next token distribution for a single token query. Generating 1,000 tokens requires 1,000 serial rounds of memory reads.
Hardware & Weights InspectorStep 1: Input-Dependent Parameter Discretization (Δ, B, C)
Execution Bottleneck
Compute-Bound (Tensor Cores)
Compute-bound in Streaming Multiprocessors
Arithmetic Intensity
15 FLOPs/Byte (Register math)
Ratio of math operations (FLOPs) to memory traffic (Bytes)
Dominant GPU Kernel
Selective Discretize CUDA Kernel
Optimized CUDA / Triton kernel launched

GPU Memory Hierarchy & HBM Traffic for this Step

HBM ↔ SRAM on-chip

Parameters kept in on-chip registers

Hardware Insight: Zero KV-cache memory traffic: no past keys or values are read from VRAM.

Active Tensor Dimensions & Memory Shapes

x_t[B, 1, D]
Single current token representation
Delta_t[B, 1, D]
Current step timescale
generate_mamba_constant_mem.py
1
def generate_mamba_stream(prompt, model, max_new_tokens=500):
2
# 1. Warm up fixed-size state with prompt in parallel
3
state = model.init_state() # Fixed 16.8 MB total memory!
4
_, state = model.step_parallel(prompt, state)
5
# 2. Pure O(1) constant-time recurrent loop (Zero KV cache!)
6
curr_tok = prompt[:, -1:]
7
for _ in range(max_new_tokens):
8
logits, state = model.step_recurrent(curr_tok, state) # Constant time
9
curr_tok = sample_top_p(logits[:, -1, :])
10
yield curr_tok
Click code line to jump to corresponding stepPyTorch Reference
Why This Architecture Matters

Solves the fundamental memory and compute scaling limits of Transformers: scales linearly O(T) with context length rather than quadratically O(T²), maintaining throughput on 100k+ token sequences where standard Transformers run out of VRAM.

Runtime Complexity & Bottlenecks

Training Objective:Next-Token Cross-Entropy Loss via Parallel Associative Scan: L = -∑ log P_θ(x_t | x_<t)

Inference Bottleneck:O(1) memory footprint and O(1) compute per step (Zero KV Cache)