Back to paper library

Mamba-2-Hybrid

Fuzzy Memory

Original paper

An Empirical Study of Mamba-based Language Models

Paper authors

Roger Waleffe · Wonmin Byeon · Duncan Riach …

Show all 16 authors

Roger Waleffe · Wonmin Byeon · Duncan Riach · Brandon Norick · Vijay Korthikanti · Tri Dao · Albert Gu · Ali Hatamizadeh · Sudhakar Singh · Deepak Narayanan · Garvit Kulshreshtha · Vartika Singh · Jared Casper · Jan Kautz · Mohammad Shoeybi · Bryan Catanzaro

Publication
Publication venue not yet verified
First submitted to arXiv
arXiv:2406.07887
Cite this paper · BibTeX

The citation identifies the linked source version. It cites the original paper and its authors.

@misc{mambamamba2hybrid2024,
  title = {{An Empirical Study of Mamba-based Language Models}},
  author = {Roger Waleffe and Wonmin Byeon and Duncan Riach and Brandon Norick and Vijay Korthikanti and Tri Dao and Albert Gu and Ali Hatamizadeh and Sudhakar Singh and Deepak Narayanan and Garvit Kulshreshtha and Vartika Singh and Jared Casper and Jan Kautz and Mohammad Shoeybi and Bryan Catanzaro},
  year = {2024},
  eprint = {2406.07887},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2406.07887}
}
On this page

By mid-2024, the news about Mamba was somewhat polarized.

On one side were the original authors' experiments at 2.8B and below, in which Mamba and Mamba-2 matched or even beat same-size Transformers on common benchmarks. On the other, more and more studies pointed out weaknesses in copying, retrieval, and in-context learning. AI21's Jamba simply kept a few attention layers between its Mamba layers.

The trouble was that these comparisons were run under quite different conditions. The original authors' experiments stayed under 3B parameters and 1T tokens; large models like Jamba, on the other hand, had no control Transformer trained on the same data.

This technical report from NVIDIA sets out to fill that gap by training 8B-parameter Mamba, Mamba-2, and Transformer models, plus a hybrid, with the same data, the same hyperparameters, and the same evaluation pipeline on up to 3.5T tokens, then comparing them across 35 tasks.

Besides NVIDIA researchers, the author list includes Mamba's two authors, Tri Dao and Albert Gu.

Defining the Problem

The authors want to know whether Mamba can keep up with Transformers once the training budget grows, and, where it can't, whether adding back a few attention layers is enough.

To keep the comparison fair, the four models share:

  • The same data: two versions, 1.1T and 3.5T tokens, both precursors of the Nemotron-4 training data, made up of 70% English, 15% other languages, and 15% code.
  • The same hyperparameters: the authors found at small scale that Mamba's best hyperparameters were close to the Transformer's, so all models share them.
  • The same evaluation: a version-pinned open-source evaluation suite with the same set of prompts.

The model configurations:

Table: Compiled from §3.1 and Table 6 of the paper. All four have about 8B parameters and a hidden dimension of 4096.
ModelLayersNotes
Transformer32Each layer is attention + MLP; 32 attention heads, RoPE
Mamba56Two Mamba layers have about as many parameters as one attention + MLP layer; state dimension 128
Mamba-256Same as above, with Mamba-2 blocks; head dimension 64, 8 groups
Mamba-2-Hybrid5624 Mamba-2 layers, 4 attention layers, 28 MLP layers

The 3.5T-token group trained only Mamba-2, the Transformer, and the hybrid. The authors' reason: pure Mamba's state is so large that, already in the 1.1T group, it was nearly three times slower than Mamba-2.

Solving the Problem

First, How Pure SSMs Score

The 1.1T-token results contain one jarring number:

Table: Excerpted from Table 2 of the paper; 8B models, 1.1T tokens. The five tasks are WinoGrande, PIQA, HellaSwag, ARC-E, and ARC-C.
ModelAvg. of five commonsense tasksMMLU 0-shotMMLU 5-shot
Transformer67.8738.3246.28
Mamba68.0728.6328.00
Mamba-268.5628.9429.19

On commonsense tasks, both Mambas edge out the Transformer. On MMLU 5-shot, though, they trail by more than 17 points, falling almost to the chance level of guessing among four options.

Trained to 3.5T tokens, Mamba-2's MMLU 5-shot score recovers to 48.7, only 1.37 points below the Transformer's 50.07; its average score even pulls ahead. So is more training all it takes?

The authors didn't stop there. They posed MMLU in three different ways:

  1. Standard format: four options, A, B, C, and D, are listed after the question, and the comparison is which letter the model is most likely to output next.
  2. Choice text as the target: the options are listed the same way, but the comparison is over the full option text, letter included: for example, the probability of “B. sorted('ab')”.
  3. Cloze: no options are listed after the question; instead, the comparison is the probability of each answer text coming right after the question.

With the third format, the 1.1T-token models score almost identically across both Mambas and the Transformer (about 38 to 39 points at 5-shot).

From this, the authors infer that pure SSMs may have learned knowledge similar to the Transformer's and stumble on the format. The model has to understand that “four options are listed above, and the answer is a single letter,” then carry each option's content over to the matching letter. Moving information between tokens like this is exactly what attention is good at and what pure SSMs find harder. This is the authors' interpretation; the scores across the three formats support it, but they don't yet amount to direct proof.

In addition, for the 3.5T Mamba-2, going from 0-shot to 5-shot raises its MMLU score by only 1.45 points, while the Transformer gains 4.38. Mamba-2 gets less out of the extra examples.

The Phone Book and Fuzzy Memory

Another test is the phone book, in which the prompt contains a list of “name: phone number” entries and two example questions with answers, then asks for one person's number.

Three line charts, each with phone-book length (thousands of tokens) on the x-axis and a vertical line marking the 4K pretraining length. (a) Standard phone book: the Transformer is near 100% within 4K and drops rapidly to zero beyond it; Mamba and Mamba-2 begin to decline at around 0.5K and are mostly below 20% after 2.5K. (b) Number of correct tokens in the answer: both Mambas fall gradually from 12 to between 4 and 6. (c) Reversed phone book, where the model is first told whom to remember: Mamba does somewhat better but still declines with length, Mamba-2 falls below 10% at around 3K, and the Transformer stays at 100% within 4K
Figure 3 from the paper, excerpted from page 8 of the v1 PDF. All three panels use the 8B models trained on 1.1T tokens; the vertical line marks the pretraining length of 4,096.

In the left panel, the Transformer gets almost everything right within its pretraining length of 4,096 and breaks down beyond it. Both Mambas start answering wrong at around 500 tokens. The authors stress that, unlike MMLU, this gap persists in the Mamba-2 trained on 3.5T tokens.

The middle panel is particularly interesting. Instead of only checking right or wrong, the authors compared each answer token by token to count how many were correct. A phone number is about 12 tokens, and even when the two Mambas answered wrong, they still often got 4 to 8 of them right.

The authors call this fuzzy memory, in which every entry has been squeezed into the state, just not precisely enough, so it comes back out distorted.

The right panel changes the setup: the model is first told whom it will be asked about, then given the phone book. In principle, Mamba could keep its eye on that one name the whole way through. Scores do improve somewhat, but they still fall with length. The authors think that even when it knows the question in advance, the model still struggles to decide which information to keep precisely and which it can forget.

How Much Attention to Add Back?

Next, the authors add a few attention layers back. To keep it fair, they adjust the number of attention heads and the MLP expansion ratio so that every type of layer has about the same number of parameters; that way, the ratio of layers equals the ratio of parameters.

Line chart: the x-axis is the percentage of all layers that are attention layers, from 0 to 100; the y-axis is validation loss. At 0% it is about 2.615, falling to a minimum of about 2.554 at around 8%, then rising gradually as the share increases, to about 2.63 at 100%
Figure 4 from the paper, excerpted from page 9 of the v1 PDF. Hybrid models with 130 million parameters and 24 layers.

In a small 130-million-parameter, 24-layer model, validation loss is lowest when about 8% of the layers are attention; all attention and no attention are both worse than a mix. A model with 840 million parameters confirms the same trend.

A few other design choices:

  • Share of MLP layers: with attention fixed at 8%, an MLP share anywhere from 30% to 50% doesn't hurt the loss. The higher the share, the faster training runs; 50% is about 20% faster than 5%.
  • Positional encoding: for the 840M hybrid, adding RoPE or leaving it out makes little difference in the base model; after extending to a 16K context, the version without RoPE actually averages higher, so RoPE is left out from then on.
  • Layer ordering: a Mamba layer goes first, so that it naturally learns positional information; the attention and MLP layers are spread evenly through the rest. The authors found no better ordering, and there's no need to repeat a fixed block.
  • GQA: switching the attention layers to GQA raises validation perplexity by only about 0.04% and saves a good deal of memory at inference time.

The final 8B hybrid has 56 layers, comprising 24 Mamba-2 (42.9%), 4 attention (7.1%), and 28 MLP (50%). The ordering is:

Text
M+M+M++M+M*+M+M+M+M++M*+M+M+M+M+M*++M+M+M+M+M*+M++M+M+M+

M is Mamba-2, * is attention, and + is MLP. The four asterisks are spread evenly through the middle section.

Working Out the Memory by Hand

Let's work out, with the paper's configurations, how much each model has to carry at inference. The Transformer has 32 layers of 32-head multi-head attention; the hybrid has 4 layers of GQA with 8 KV groups, both with 128 dimensions per head in bf16. The Transformer needs 512 KiB of KV cache per token and the hybrid only 16 KiB, a 32-fold difference: 8 times from cutting the attention layers from 32 to 4, and another 4 times from GQA. At a 4K context, the Transformer needs 2 GiB and the hybrid 64 MiB; Mamba-2's 24-layer state is about 48 MiB regardless of length, about as large as the KV cache at 4K, and the longer the context, the less it matters.

This counts the memory for a single sequence only. Figure 5 of the paper projects that, when generating one token at batch size 32, the two run at about the same speed for short contexts, and at contexts beyond a hundred thousand tokens the hybrid is nearly 8 times faster. These are projections supplied by the report; it doesn't include matching measurements on an inference system or a detailed latency model. Training efficiency, on the other hand, was measured: on 1,024 H100s, the hybrid's MFU is 29.9%, versus 30.7% for the Transformer.

Discussion

12 Standard Tasks

At 3.5T tokens, the hybrid beats the Transformer on all 12 standard tasks. The paper's average column counts MMLU 0-shot and 5-shot as separate columns, 13 in all, and on that average the hybrid is 2.65 points higher:

Table: Excerpted from Table 7 of the paper.
ModelMMLU 5-shotSQuAD v2Paper average (13 columns)
Transformer50.0753.453.17
Mamba-248.751.954.69
Mamba-2-Hybrid53.6058.6755.82

In the MMLU training curves (Figure 6 in the paper), the hybrid leads pure Mamba-2 throughout, but early in training it still trails the Transformer; only after catching up at 1.5T tokens does it stay in front for good.

Long Context: Mostly on Par, Behind on Multi-Document QA

The authors continued pretraining the 4K base models on 50B tokens, extending them to 16K and 32K. The four attention layers use full global attention.

The results fall into three groups:

  1. Phone book: the hybrid is not only perfect within its pretraining length but can also extrapolate a little beyond it. The 4K hybrid stays at 100% up to 5.5K, and the 16K and 32K versions also go past their respective training lengths. A hybrid extended separately to 128K behaves the same way.
  2. RULER synthetic tasks: on tasks like needle-in-a-haystack, multi-key lookup, and variable tracking, the 16K hybrid averages about 12 points higher than the 16K Transformer (74.19 vs. 62.33). On variable tracking, the Transformer often just copies the example answer from the prompt.
  3. Real long-document tasks: on the average of nine tasks, the 16K and 32K Transformers actually come out about 1 point higher, with the gap coming mainly from multi-document QA. On HotpotQA, for example, it's 48.61 vs. 42.17 at 16K.

The authors attribute the third point to LongBench's multi-document QA, which mixes the relevant passages with lots of random Wikipedia passages and puts the question at the end. The SSM layers read without knowing the question yet and may get led astray by irrelevant documents. They also suspect that during context extension, training on unrelated documents simply concatenated together may have led the SSM to treat them as related.

The hybrid is also more sensitive to prompt format. In the test of the 4K base models on Musique, for example, the hybrid's score swung between 10.63 and 16.16 when the prompt was changed, while the Transformer's only moved between 15.25 and 17.68. The authors also point out that LongBench's prompts were originally tuned for Transformers.

Conclusion

Using the same data and pipeline, this report sets the various 2024 claims about Mamba side by side:

  1. Pure SSMs hold their own against Transformers on most language tasks; at 3.5T tokens, pure Mamba-2's average over the five commonsense tasks is even slightly higher, and other tasks are a mix of wins and losses.
  2. The weaknesses of pure SSMs are concentrated in format, copying, and in-context learning. Most of the MMLU gap can be closed with more training, but the phone-book gap remains at both the 1.1T and 3.5T budgets, and the 3.5T pure Mamba-2 also trails on the average of the nine natural long-document tasks. When it answers wrong, it “remembers fuzzily” rather than not remembering at all.
  3. About 8% attention layers is enough. The 8B hybrid has only 4 attention layers out of 56, beats the Transformer on all 12 standard tasks, has a KV cache 32 times smaller, and is more reliable at long-context lookup.
  4. The hybrid still has open questions: multi-document QA, prompt sensitivity, and how to train for long contexts.

Mamba was first discussed as a replacement for the Transformer. Going by this report, most layers are better off handling the sequence with Mamba-2, while a few attention layers take care of precise lookback. NVIDIA's later Nemotron-H and Nemotron 3 series both follow this path.