TL;DR
Training linear RNNs like Mamba from scratch to match Transformer performance is incredibly expensive, requiring trillions of tokens. This paper introduces a breakthrough distillation method that directly initializes Mamba layers using pretrained Transformer attention weights, creating high-performing hybrid models with minimal compute. Combined with a novel hardware-aware speculative decoding algorithm, these hybrid models achieve up to a 2.6x inference speedup while maintaining the generation quality of the original Transformer.
What Problem Does This Paper Address?
While Transformers dominate the large language model (LLM) landscape, they suffer from a massive bottleneck during inference: the Key-Value (KV) cache. As sequence lengths grow, the memory required to store this cache scales quadratically, making long-context generation prohibitively slow and resource-intensive.
Linear State Space Models (SSMs) and linear RNNs, like Mamba, offer a promising alternative with $O(1)$ inference throughput and no KV cache requirements. However, scaling them up from scratch requires massive computational power and trillions of tokens. The authors of this paper ask a compelling question: Can we take an existing, highly optimized Transformer (like Llama-3) and cheaply convert it into an efficient Mamba-based hybrid model?
Methods
The authors propose a recipe to transform a pretrained Transformer into a hybrid Mamba model using limited academic GPU resources:
- Attention-to-Mamba Initialization: By exploiting the mathematical relationship between linearized attention and linear RNNs, the authors directly map the Transformer's $Q, K, V$ projection weights to Mamba’s input and output parameters ($C, B, x$). The original, knowledge-rich MLP layers are kept frozen during the initial phase.
- Stepwise Hybrid Distillation: Instead of completely removing all attention mechanisms, the authors create hybrid models by progressively replacing attention layers with Mamba blocks (e.g., keeping 50% or 25% of the attention layers).
- Multistage Alignment Distillation: To recover performance, the student model undergoes a three-stage post-training pipeline: progressive distillation using teacher pseudo-labels, Supervised Fine-Tuning (SFT), and Direct Preference Optimization (DPO) to align the student's outputs with the teacher's preferences.
Additionally, speculative decoding for RNNs is notoriously difficult because caching hidden states ($h_t$) introduces massive memory overhead. The authors design a hardware-aware multi-step speculative decoding algorithm with a custom GPU kernel that verifies multiple tokens in parallel and rewinds states lazily without materializing intermediate states off fast GPU memory.
Datasets
The models were distilled and evaluated using several open-source datasets:
- Distillation & Fine-Tuning: UltraChat, UltraFeedback, GenQA, InfinityInstruct, OpenHermes 2.5, and SimPO.
- Evaluation Benchmarks: MT-Bench, AlpacaEval 2, GSM8K, CRUX, and the LM Evaluation Harness (including MMLU, HellaSwag, ARC, and TruthfulQA).
- Long-Context Evaluation: Needle-in-a-Haystack.
Key Insights and Ideas
- Weight Reuse is Highly Effective: Pretrained Transformer attention weights contain structural priors that can directly seed a linear RNN. This eliminates the need for expensive pretraining, requiring only 20 billion tokens of distillation compared to the trillions needed from scratch.
- Hybrids Offer the Best of Both Worlds: Retaining a fraction of attention layers (e.g., 25% to 50%) preserves the complex context-matching capabilities of Transformers while significantly reducing the memory footprint of the KV cache.
- Natural Length Extrapolation: Surprisingly, even though the distillation process only utilized a 2k sequence length, the resulting hybrid models naturally extrapolated to show near-perfect accuracy on Needle-in-a-Haystack tests up to 38k tokens—20x the training length.
- DPO as a Distillation Objective: This work represents one of the first successful applications of Direct Preference Optimization (DPO) specifically used to close the alignment gap between a student model and its teacher.
Key Results
- State-of-the-Art Hybrid Performance: The top-performing model, distilled from Llama3-8B-Instruct (retaining 50% attention), achieved a 29.61% length-controlled win rate on AlpacaEval 2 against GPT-4 and a 7.35 on MT-Bench, outperforming open-source Mamba models trained from scratch on trillions of tokens.
- Substantial Inference Speedups: The hardware-aware speculative decoding algorithm delivered a 2.1x to 2.6x speedup for pure Mamba models on NVIDIA 3090 and H100 GPUs, and over a 1.8x speedup when applied to hybrid architectures.
Limitations and Future Work
- The Pure Mamba Gap: While hybrid models perform exceptionally well, completely removing all attention layers (0% attention / pure Mamba) still results in a significant drop in downstream task accuracy. Completely closing this gap remains an open challenge.
- Draft Model Bottlenecks: In speculative decoding, the draft models inherit the massive embedding tables of their parent models (like Llama 3), making them relatively large. The authors note that developing smaller, more compact draft models is a critical next step to unlocking even higher throughput.
Summary updated