The Mamba in the Llama: Distilling and Accelerating Hybrid Models

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.

Paper

References (85)

Scroll for more · 38 remaining

Similar papers

Peer review

Reviewer J8xQ4/10 · confidence 5/52024-07-12

Summary

The paper focuses on distilling transformers into SSMs for fast inference after training. Specifically, the authors initialize states of SSMs by pretrained transformer components and utilize speculative decoding for multi-step predictions. Experiments show that the distilled hybrid model can achieve competitive performance compared to the teacher network, i.e. Zephyr-7B on standard Chat benchmarks after training with 3B tokens only. The distilled hybrid model can outperform Mamba 7B which is trained from scratch with 1.3T tokens.

Strengths

How to accelerate the training process of Mamba-based hybrid models is a good research problem. In the paper, the authors propose to utilize distillation from pretrained transformer-based models. The results show the Mamba-based hybrid model can learn efficiently with fewer iterations and samples.

Weaknesses

1. The writing is not well-organized and involves a lot of unclear parts. In Section 2.1, the derivations from softmax-based attention to linear RNNs (Mamba) are not clearly stated, e.g. unexplained symbols in equations. Also the derivations should obey some assumptions. Please refer to Mamba-2. 2. Some settings of the proposed method are not clear. For example, in Section 2.2, Why is A set as fixed over time? In Section 2.3, which components in the model are from attention and which parts are trainable. Figure 1 is also not informative without explanations of the colors and arrows. In section 4, what are the differences between speculative decoding used in transformer and that used in Mamba here? What are the details of hare-aware designs? 3. The experiments are not enough. For comparison methods, Hybrid Mamba which is trained from scratch should be included. Besides Mamba, other SSMs and linear RNNs should be included. The benchmarks are only on chat and academic tasks.

Questions

see the weaknesses.

Rating

4

Confidence

5

Soundness

2

Presentation

1

Contribution

2

Limitations

The paper should be generalized to all SSM models (including Mamba-1/2) or linear RNN models, instead of only focusing on Mamba-1.

Reviewer J8xQ2024-08-13

My main concern is the narrow scope of the methods and experiments of this paper. The paper’s story seems to rely on a biased assumption that Mamba will replace transformers—an appealing idea, but speculative at this stage. The focus on distilling and accelerating the Mamba hybrid model, while potentially applicable to other models (like linear RNN or linear attention), still requires further demonstration. And the paper does not propose any new methods or theory, it is just a application of proven techniques on mamba. Given the limited scope and novelty, I don’t believe the paper meets the NeurIPS standard in its current form.

Authorsrebuttal2024-08-13

We are very grateful for all the encouraging and constructive reviews. Please find our responses below. $\mathcal{Q}.$ The paper’s story seems to rely on a biased assumption that Mamba will replace transformers—an appealing idea, but speculative at this stage. $\mathcal{A}.$ We appreciate the reviewer's concerns. We do not claim in the paper that Mamba will replace transformers and in fact, believe that people will continue mainly training transformers. We instead think that some organizations may want prioritize fast and lower-memory inference, and therefore want to distill their transformer models to Mamba for some applications. $\mathcal{Q}.$ Additionally, the paper does not introduce any new method or theory. $\mathcal{A}.$ We think there are two new methods introduced by this paper. 1) Our distillation approach is novel. To the best of our knowledge, this is the first work demonstrating that transformations between large transformers and linear RNN models is possible through a mathematical transformation. And in our experiments, we distill different large transformers to support this claim. 2) We introduce a new algorithm for speculative decoding in the Mamba model which is efficient in practice. We agree that there is no new theory introduced in this work, although that is generally true for most deep learning papers. $\mathcal{Q}.$ We don't understand that it is just an application of proven techniques on mamba. $\mathcal{A}.$ While distillation is a proven technique, as far as we know there is no work published on distilling from transformer to Mamba or related linear RNNs. The details of what distillation approaches work and how they can reuse parameters is a the contribution. We are a bit perplexed by this comment, and it would be great help if you can point us any thing related to this. (Previous work on this topic does not do anything like distillation.)

Reviewer pZLW5/10 · confidence 4/52024-07-14

Summary

The paper presents a mechanism to distill from llama to mamba.

Strengths

(1) Good paper writing with clear figures. (2) The method is easy to understand. (3) Good experiment design on speculative decoding and other distillation strategy. (4) Good performance (Table 1, 2)

Weaknesses

The experiments are a bit limited, only performed on chat corpora. Further data mixture is needed to understand the effectiveness of the approach. (Yet due to other strength, the reviewer thinks it should receive a positive score).

Questions

Please see the weakness section.

Rating

5

Confidence

4

Soundness

3

Presentation

3

Contribution

3

Limitations

Please see the weakness section.

Reviewer NYcb6/10 · confidence 4/52024-07-14

Summary

The paper proposes a way to convert a Transformer architecture to a Mamba architecture by first initializing a Mamba layer based on a pre-trained attention layer in order to facilitate faster convergence followed by knowledge distillation from the Transformer to the Mamba model. The training procedure only involves post-training on task-specific data. The paper also uses DPO for the knowledge distillation and the authors emphasize the novelty of this method for knowledge distillation. Following their proposed method, the authors demonstrate how to quickly train a hybrid architecture with some attention and some Mamba layers based on existing LLMs, particularly Zephyr 7B. Finally, the paper proposes a way to perform speculative decoding and gain speed up using Mamba architectures.

Strengths

The paper's writing is mostly clear and can be followed. The idea of initializing a Mamba layer based on pre-trained Transformer layers is novel and interesting. The authors evaluate the models on several benchmarks.

Weaknesses

Looking at the performance in Table 1, increasing the attention percentage always improves performance in a significant way (most clear in MMLU). As such, I am not convinced that the method is actually working. There is also a lack of proper baselines. For example, no comparison is performed with random initialization. Similarly, the results for 100% Mamba architecture is not included. While this might not be as good as 50% architecture, it is still very much needed to understand the trade-off. The idea of converting a pre-trained Transformer to Mamba is quite generic. In particular, there does not seem to be any specific scale needed to get the method to work. As such, it is not clear why the authors decided to mainly test the method on such a large scale. For example, a small Transformer model can be trained from scratch, used to create a Mamba model and then compared in terms of language modeling benchmarks such as perplexity. Indeed this experiment is done for one experiment in Section 7 and shows poor performance of the method in terms of perplexity. As a side note, the paragraph starting in line 275 is titled Does PPL correspond to ability?. However, the discussion in that paragraph does not answer this question at all. Moreover there is actually a correspondence and we see degradation in task specific performance similar to PPL. Moreover, the authors mention that they do not target pre-trained language model capabilities. Why is that the case? Furthermore, the comparison is done on a hybrid architecture which further makes it confusing to determine the success of the method. Note that large models have a lot of redundancies as suggested by recent work in pruning and this makes it harder to see whether other attention layers are stepping in to make up for the poor performance of Mamba layers or something else is happening. Overall I find the results not convincing enough and not supporting the claims made in the paper about the success of the method.

Questions

1. The definition of # Gen in Figure 2 (right) is very vague and unclear. Can you please elaborate? 2. In line 182 it is mentioned that the parallel mode of Mamba is faster than a Transformer. Is there a reference for this? If so I think it would be useful to include it. 3. In Algorithm 2, the verify part may return the state from an earlier point, i.e. $h_j$ instead of $h_{k^\prime}$. How is this ok for the rest of the algorithm? This means a set of tokens will be ignored as the algorithm will never go over $j + 1, \ldots, k^\prime$ to incorporate them into the state.

Rating

6

Confidence

4

Soundness

1

Presentation

2

Contribution

2

Limitations

The authors mention that the performance is only measured after fine-tuning on chat corpora instead of full pre-training due to resource limitations. While at 7B scale this is acceptable, as mentioned above, there is no need to focus on that scale from the start. Further limitations, such as degradation of performance when switching from attention to Mamba layers should be more deeply discussed.

Authorsrebuttal2024-08-11

We thank again the reviewer for the helpful feedback. $\mathcal{Q}.$ I could not find the new PPL results. Could you please share them as well? $\mathcal{A}.$ Since our models are alignment using DPO, we evaluated ppl of our final models on lambada_openai. Here are ppl of models in the ablation results. | Model | PPL | |-------------------------------|-----| | Hybrid Mamba (50%) w/o Attention init | 55.01 | | Hybrid Mamba (50%) w Attention init | 6.20 | | Model | PPL | |-------------------------------|-----| | 50% Attention w/o Mamba Block | 151.98 | | 50% Attention w Mamba Block | 6.20 | $\mathcal{Q}.$ Just to confirm, you still keep the FFN parts of the layers, and only remove the attention, then do fine-tuning? I think if this is the case, this is an important result as it shows the efficacy of using Mamba blocks and the projection. Combined with the obtained speedup I think this makes the results quite interesting. However, the trend that using Mamba blocks lead to decreased accuracy is completely observable in all the results. As such, I think it is paramount that this limitation be clearly specified and discussed in the paper. Do authors agree on this or have I misinterpreted the results? $\mathcal{A}.$ Yes, we still keep those three Gated MLPs (FFN components) and only remove half of the attention layers before fine-tuning. We acknowledge that changing attention to Mamba may still lead to decreased performance as more Mamba layers are introduced. At the same time, we believe that as computational resources scale up, this impact will be mitigated. Additionally, the inductive bias learned from attention can help Mamba scale up, as our model has already outperformed other hybrid Mamba models trained from scratch with significantly more tokens. We definitely will address and discuss this limitations in the paper. Additionally, we will update the speculative decoding section to improve and correct the explanation of the algorithm.

Reviewer NYcb2024-08-13

Dear Authors, Thank you for the additional results. One of my concerns remain and I think it would be a great addition if the authors could show whether the model relies on the language models being large scale or is applicable on smaller scales as well. As I mentioned originally in my review, it is possible to train a smaller transformer from scratch for a reasonable number steps, then do the same approach for converting layers to Mamba layers and compare the two models in terms of perplexity (or any other metric). Currently the paper is only evaluating on 7B models but it is not clear whether there is a limitation for smaller sizes or not. Either way I think it should be discussed and would be a nice addition. Still, given the new results provided in the rebuttal, I think there is sufficient evidence to show the method has merit and can be useful. As such, I have increased my score.

Authorsrebuttal2024-08-14

We deeply appreciate all the supportive and constructive feedback. We are running a smaller scale (110M) ablation experiment to address your concerns. We will definitely discuss this and include it in the next version.

Reviewer NYcb2024-08-11

Dear Authors, Thank you very much for the additional experiments and your clarifications and replies. I have asked for some additional clarifications below. > In the new ablations that we ran, we found that initializing using attention weights not only gives lower PPL, but also significantly improves task-specific performance. I could not find the new PPL results. Could you please share them as well? > About Random Init: We added a comparison with random initialization. Please refer to 3a in the general response. With the Mamba default initialization and the same training tokens, the model gets a very low score in MT-bench and AlpacaEval. > Regarding no Mamba layers: This is a good question, and we were curious as well. Please refer to 3b in our general response. This result confirms that adding Mamba layers is very critical and that the results are not just from the remaining attention. Thank you for providing these results. Just to confirm, you still keep the FFN parts of the layers, and only remove the attention, then do fine-tuning? I think if this is the case, this is an important result as it shows the efficacy of using Mamba blocks and the projection. Combined with the obtained speedup I think this makes the results quite interesting. However, the trend that using Mamba blocks lead to decreased accuracy is completely observable in all the results. As such, I think it is paramount that this limitation be clearly specified and discussed in the paper. Do authors agree on this or have I misinterpreted the results? > We are sorry for the confusion. Yes you are correct in that in speculative coding we may throw away generated tokens. In the case where a state from an earlier point is returned by the verify part, the draft model will also resume decoding from a previous snapshot of the state, recomputing the states for the accepted tokens from its cache. Could you please fix this in the next revision of the paper? As I think the current Algorithm is incorrectly specified and does not include the recomputation part which can be confusing. Assuming the above are addressed, I will be increasing my score as I think the new results provide enough supporting evidence that the method is working.

Reviewer J8xQ2024-08-13

Thanks for the effects. I appreciate the mamba2 experiments provided, but I am still doubt the generalization of the proposed method to other models like linear RNN or linear attention. The paper should not only focus on mamba. I would like to raise my score to 4.

Authorsrebuttal2024-08-13

We thank the reviewer for raising his score. However, we wonder why the reviewer believes we shouldn't focus on Mamba, since it's clear from the title that this is the goal and scope of our work. We would like to ask the reviewer to reconsider their score of rejection, unless there are other doubts or issues about our work, which we are happy to engage with.

Program Chairsdecision2024-09-25

Decision

Accept (poster)

© 2026 NYSGPT2525 LLC