The Mamba in the Llama: Distilling and Accelerating Hybrid Models

Junxiong Wang, Daniele Paliotta, Avner May, Alexander M. Rush, Tri Dao

Abstract

Linear RNN architectures, like Mamba, can be competitive with Transformer models in language modeling while having advantageous deployment characteristics. Given the focus on training large-scale Transformer models, we consider the challenge of converting these pretrained models for deployment. We demonstrate that it is feasible to distill large Transformers into linear RNNs by reusing the linear projection weights from attention layers with academic GPU resources. The resulting hybrid model, which incorporates a quarter of the attention layers, achieves performance comparable to the original Transformer in chat benchmarks and outperforms open-source hybrid Mamba models trained from scratch with trillions of tokens in both chat benchmarks and general benchmarks. Moreover, we introduce a hardware-aware speculative decoding algorithm that accelerates the inference speed of Mamba and hybrid models. Overall we show how, with limited computation resources, we can remove many of the original attention layers and generate from the resulting model more efficiently. Our top-performing model, distilled from Llama3-8B-Instruct, achieves a 29.61 length-controlled win rate on AlpacaEval 2 against GPT-4 and 7.35 on MT-Bench, surpassing the best 8B scale instruction-tuned linear RNN model. We also find that the distilled model has natural length extrapolation, showing almost perfect accuracy in the needle-in-a-haystack test at 20x the distillation length.

Introduction

While Transformers currently dominate the landscape of Large Language Models (LLMs), they carry a significant "memory tax" known as the Key-Value (KV) cache. As sequence lengths increase, the KV cache grows linearly, and the attention mechanism's computational cost grows quadratically. This scaling behavior makes long-context reasoning and generation prohibitively expensive in terms of memory and latency.

Linear RNNs, such as Mamba, offer a promising alternative with linear complexity and a constant-sized state. However, the barrier to entry for these architectures is high: training a Mamba model from scratch to match the quality of a state-of-the-art Transformer like Llama-3 typically requires trillions of tokens and massive compute. This research presents a more efficient path: "transplanting" the knowledge of a pretrained Transformer into a Mamba-based hybrid model through distillation and weight reuse.

Methodology: The Transformer-to-RNN Transition

The authors propose a three-pronged approach to bridge the gap between Transformers and Linear RNNs without requiring a supercomputer-level training budget.

1. Attention-to-Mamba Initialization

Instead of starting with random weights, the researchers initialize Mamba layers by reusing the $Q, K,$ and $V$ projection weights from the original Transformer's attention layers. This "warm-start" strategy preserves much of the teacher model's learned knowledge, allowing the student model to reach high performance with only 20 billion tokens of training—a tiny fraction of the data used for training from scratch.

2. Hybrid Architecture

While pure Mamba models offer the best theoretical efficiency, they often exhibit a performance gap compared to Transformers. The authors found a "sweet spot" in a hybrid architecture: keeping one attention layer for every four layers (25% attention). This configuration maintains the global reasoning capabilities of attention while offloading the majority of the computation to Mamba layers, drastically reducing the KV cache footprint.

3. Multi-stage Distillation Pipeline

The training process follows a standard alignment pipeline but leverages the teacher model at every step:

  • Progressive Distillation: The student learns to match the teacher's output distributions.
  • Supervised Fine-Tuning (SFT): The model is trained on high-quality instruction data.
  • Direct Preference Optimization (DPO): The teacher model is used to score and align the student's preferences, marking one of the first successful uses of DPO as a distillation objective to close the gap between student and teacher.

Accelerating Inference

To further improve deployment characteristics, the researchers developed a hardware-aware speculative decoding algorithm and a fast GPU kernel. This allows Mamba models to verify multiple tokens in parallel, overcoming the serial nature of traditional RNN inference. On H100 GPUs, this algorithm provides up to a 2x speedup over standard autoregressive generation for pure Mamba models.

Experimental Results

Performance Benchmarks

The hybrid model distilled from Llama-3-8B-Instruct demonstrated exceptional performance, outperforming existing 8B-scale linear RNNs (like Falcon Mamba) that were trained on trillions of tokens from scratch.

  • AlpacaEval 2: 29.61% length-controlled win rate against GPT-4.
  • MT-Bench: A score of 7.35, surpassing existing instruction-tuned linear RNNs.
  • General Benchmarks: The model remained competitive across MMLU, ARC, HellaSwag, and GSM8K.

Length Extrapolation

One of the most striking findings was the model's ability to handle contexts far beyond its training data. Despite being distilled on 2k-length sequences, the model showed almost perfect accuracy in "Needle-in-a-Haystack" tests at 38k tokens—a 20x extrapolation of the distillation length.

Throughput

Using the proposed speculative decoding implementation, the researchers achieved a throughput of over 300 tokens/second for a 7B model on a single GPU.

Conclusion and Future Work

This work demonstrates that we do not need to choose between the high performance of Transformers and the efficiency of Linear RNNs. By using weight reuse and hybrid architectures, we can convert existing high-quality Transformers into efficient models that are easier to deploy.

Future research directions include refining the size of draft models for speculative decoding and using automated neural architecture search to find even more optimal ratios of attention to Mamba layers. The code and checkpoints for this project are open-sourced, providing a foundation for further exploration of hybrid architectures.

Summary updated

Abstract

Linear RNN architectures, like Mamba, can be competitive with Transformer models in language modeling while having advantageous deployment characteristics. Given the focus on training large-scale Transformer models, we consider the challenge of converting these pretrained models for deployment. We demonstrate that it is feasible to distill large Transformers into linear RNNs by reusing the linear projection weights from attention layers with academic GPU resources. The resulting hybrid model, which incorporates a quarter of the attention layers, achieves performance comparable to the original Transformer in chat benchmarks and outperforms open-source hybrid Mamba models trained from scratch with trillions of tokens in both chat benchmarks and general benchmarks. Moreover, we introduce a hardware-aware speculative decoding algorithm that accelerates the inference speed of Mamba and hybrid models. Overall we show how, with limited computation resources, we can remove many of the original attention layers and generate from the resulting model more efficiently. Our top-performing model, distilled from Llama3-8B-Instruct, achieves a 29.61 length-controlled win rate on AlpacaEval 2 against GPT-4 and 7.35 on MT-Bench, surpassing the best 8B scale instruction-tuned linear RNN model. We also find that the distilled model has natural length extrapolation, showing almost perfect accuracy in the needle-in-a-haystack test at 20x the distillation length. Code and pre-trained checkpoints are open-sourced at https://github.com/jxiw/MambaInLlama and https://github.com/itsdaniele/speculative_mamba.
BibTeX
@inproceedings{wang2024mamba,
  author = {Junxiong Wang and Daniele Paliotta and Avner May and Alexander M. Rush and Tri Dao},
  title = {{The Mamba in the Llama: Distilling and Accelerating Hybrid Models}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=uAzhODjALU},
}
Figure