Back to paper library
Paper notesarXiv 2024

Jamba

An Eighth of Attention

Original paper

Jamba: A Hybrid Transformer-Mamba Language Model

Paper authors

Opher Lieber · Barak Lenz · Hofit Bata …

Show all 22 authors

Opher Lieber · Barak Lenz · Hofit Bata · Gal Cohen · Jhonathan Osin · Itay Dalmedigos · Erez Safahi · Shaked Meirom · Yonatan Belinkov · Shai Shalev-Shwartz · Omri Abend · Raz Alon · Tomer Asida · Amir Bergman · Roman Glozman · Michael Gokhman · Avashalom Manevich · Nir Ratner · Noam Rozen · Erez Shwartz · Mor Zusman · Yoav Shoham

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

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

@misc{mambajamba2024,
  title = {{Jamba: A Hybrid Transformer-Mamba Language Model}},
  author = {Opher Lieber and Barak Lenz and Hofit Bata and Gal Cohen and Jhonathan Osin and Itay Dalmedigos and Erez Safahi and Shaked Meirom and Yonatan Belinkov and Shai Shalev-Shwartz and Omri Abend and Raz Alon and Tomer Asida and Amir Bergman and Roman Glozman and Michael Gokhman and Avashalom Manevich and Nir Ratner and Noam Rozen and Erez Shwartz and Mor Zusman and Yoav Shoham},
  year = {2024},
  eprint = {2403.19887},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2403.19887}
}
On this page

In December 2023, Mamba, a selective state space model that runs in linear time, matched same-size Transformers at language modeling. But the largest model in that paper was 2.8B, and everyone was wondering whether it would hold up at production scale.

Three months later, Israel's AI21 Labs delivered a 52B-parameter model under the Apache 2.0 license, named Jamba. It did use a lot of Mamba layers, but it still kept one attention layer in every eight.

In the Hacker News thread for the release, someone asked point-blank: now that we have Mamba, why include attention layers at all? The reply was short, saying “Mamba is bad with long context. It doesn't remember phone numbers.” Hacker News, 2024-03-28

That reply linked to a study testing tasks that require copying the original text exactly, such as long phone books; we've written a separate note on it. That study also lists situations where state models do well. The Jamba paper gives another, more concrete reason, drawn from its own ablations.

Defining the Problem

When serving long documents, a Transformer's memory use and generation time both grow with the context.

An attention layer keeps a key and a value for every token it has read; the longer the document, the larger this cache grows, and in the end it determines how long a context fits on one GPU. During generation, every new token has to be compared against the entire context again, which drags down throughput on long texts.

Models like Mamba save on both, because they only update a fixed-size state. Yet in AI21's evaluations, at the same scale, pure-Mamba language models still trailed Transformers slightly.

Others had already tried putting both kinds of layers in one model, but only at small scale. Experiments interleaving Mamba and attention stopped at around 1.3B, and StripedHyena, which reached 7B, still lost to the attention-only Mistral-7B.

So this paper sets out to answer several questions that can only be answered after scaling up. In what ratio should the two kinds of layers be mixed? Will adding MoE cause conflicts? And will training stay stable at tens of billions of parameters?

Solving the Problem

The Jamba Block

Jamba architecture diagram: on the left is one Jamba block, made of eight layers from bottom to top: Mamba, Mamba MoE, Mamba, Mamba MoE, Transformer, Mamba MoE, Mamba, Mamba MoE; on the right are the internal structures of the four layer types, each consisting of RMSNorm plus Attention or Mamba, followed by RMSNorm plus MLP or MoE, with a residual connection around each part
Figure 1 from the paper, excerpted from page 3 of the v2 PDF, CC BY-SA 4.0. The left panel is one Jamba block; the right panel shows the internal structure of the four layer types.

Start with the right panel. Every layer has two parts. The first mixes information along the sequence, using Attention or Mamba; the second transforms features token by token, using an ordinary MLP or MoE. Each part has an RMSNorm in front and is wrapped in its own residual connection.

Pairing them up gives the four layer types on the right. One of them, Attention MoE, isn't actually used in Jamba.

Now the left panel. A Jamba block has eight layers. Counting from the bottom, the fifth is the only Transformer layer, and the other seven are Mamba; MoE appears in every other layer.

The authors describe this design space with five parameters:

  • ll: the number of layers in a block.
  • a:ma:m: the ratio of attention layers to Mamba layers.
  • ee: the interval, in layers, at which an MLP is replaced with MoE.
  • nn: the number of experts in each MoE layer.
  • KK: the number of experts chosen per token.

The released version uses l=8l=8, a:m=1:7a:m=1:7, e=2e=2, n=16n=16, K=2K=2. Four blocks stacked together give 32 layers, of which only 4 are attention. The model has 52B total parameters, with 12B active per token. With int8 weights, it fits on a single 80GB GPU.

Most other details follow the standard recipe of the time, with GQA for attention, SwiGLU activations, a 64K vocabulary, and each digit split into its own token. Two choices stand out, and we'll come back to them in the discussion. First, RMSNorm is added inside the Mamba layers; second, the model uses no positional encoding such as RoPE.

Working Out the KV Cache by Hand

So how much does keeping only 4 attention layers actually save? Table 1 of the paper gives the KV cache for a 256K context in 16-bit:

Table: Table 1 of the paper.
ModelTotal paramsActive paramsKV cache (256K)
Llama-26.7B6.7B128GB
Mistral7.2B7.2B32GB
Mixtral46.7B12.9B32GB
Jamba52B12B4GB

Let's work it out. Every attention layer stores one key and one value per token; Mixtral and Jamba both have 8 KV heads of 128 dimensions each (according to their configuration files on Hugging Face), and bf16 takes 2 bytes per number. Mixtral's 32 attention layers need 128 KiB per token, or 32 GiB for 256K tokens; Jamba's 4 layers need 16 KiB per token, 4 GiB in total. Both numbers match Table 1 (the table says GB; here everything is in power-of-two GiB). The eightfold difference is exactly the difference between 32 and 4 attention layers.

On the Mamba side, what has to be carried along is the SSM state: 28 layers, an inner width of 8,192 and a state dimension of 16 per channel, about 3.67 million numbers, roughly 7 MiB in bf16. It stays this size whether the context is 1K or 256K. (Only the SSM state is counted here; the convolution cache comes on top and is also small.)

Discussion

How It Stacks Up Against Mixtral

First, the standard benchmarks. The authors mainly compare against Mixtral 8x7B, whose total and active parameter counts are both close to Jamba's. A few selected results:

Table: Excerpted from Table 2 of the paper.
BenchmarkMixtralJamba
HellaSwag (10-shot)86.787.1
WinoGrande (5-shot)81.282.5
GSM8K (3-shot CoT)60.459.9
HumanEval (pass@1)34.829.3
MMLU (5-shot)70.667.4
BBH (3-shot)50.345.4

In the authors' words, “in most tasks, Jamba performs comparably.” Item by item, it is slightly higher on HellaSwag and WinoGrande in this table; in the full table, it trails by a few points on ARC-E and ARC-C, as well as MMLU, BBH, and HumanEval.

Jamba's advantage lies mainly in the cost of long contexts. Figure 2 of the paper shows that the context length that fits on a single A100 80GB is, for Jamba, about twice Mixtral's and seven times Llama-2-70B's, around 140K tokens. Figure 3 measures end-to-end throughput for encoding plus decoding, generating 512 tokens in both settings. On a single A100 with int8 quantization and an 8K context, Jamba fits a batch of 16 while Mixtral doesn't, and the throughput gap is around threefold; on four A100s, unquantized, at batch size 1 with a 128K context, Jamba is again about three times faster.

The authors also caution that these throughput numbers are only meaningful as relative comparisons, because the community has spent six years optimizing inference for pure Transformers, and Jamba had none of that yet.

For long-context ability, the authors ran two kinds of tests. On needle-in-a-haystack, the needle is found almost every time within 256K; they stress that this relies on just 4 attention layers. They also converted the five longest datasets in L-Eval to a 3-shot format: the average F1 is 0.44, versus 0.43 for Mixtral.

One to Three, or One to Seven?

To choose the ratio, the authors experimented with small models of 1.3B parameters trained on 250B tokens:

Table: Excerpted from Table 4 of the paper; none of these use MoE. OLLM is the authors' reproduction of the Hugging Face Open LLM Leaderboard aggregate score.
ModelOLLMHellaSwagWinoGrandeNQ
Pure attention36.462.459.614.5
Pure Mamba36.162.659.414.5
Hybrid 1:337.265.161.716.5
Hybrid 1:737.265.161.716.0

The two hybrid ratios are nearly identical, and both beat either single architecture. The authors chose 1:7 because it is more compute-efficient. In a second comparison scaled up to 7B and 50B tokens, pure Mamba's OLLM of 35.3 is slightly below pure attention's 36.1, while the hybrid scores highest at 36.6 and has the lowest training loss throughout.

One caveat: the two ablations trained on 250B and 50B tokens respectively, and the paper doesn't disclose the total training budget of the released model. For now, the conclusions about the ratio are limited to these ablation settings.

Where Does Pure Mamba Get Stuck?

This is the most interesting part of the paper.

Pure Mamba's perplexity and general benchmark scores aren't bad, but it drops sharply on three datasets:

Table: Table 6 of the paper; 1.3B models, 250B tokens.
ModelIMDBQuACNarrativeQA
Pure attention84.127.945.8
Pure Mamba48.820.227.7
Hybrid90.926.643.7

IMDB is sentiment classification of movie reviews, with only two possible answers: Positive and Negative. When the authors looked at pure Mamba's outputs, they found that it often ignored the format, writing “Very Good,” “Funny,” “Bad,” or even “3/10.”

Some of these answers may have the right sentiment; they just weren't given in the format of the earlier examples.

In the few-shot prompt, every example is “review + Sentiment: label.” One way to stick to this format is, at the final colon, to look back at how the earlier examples wrote their labels. Earlier research has linked this kind of “look back and copy” ability to the induction head in Transformers, an attention head that finds an earlier occurrence of the same pattern and copies the token that came after it.

The authors' conjecture is that pure Mamba has no attention, so this mechanism doesn't easily emerge on its own during training. Switch to the 1:7 hybrid (this 1.3B model has three attention layers in total), and the problem disappears. (The “pure Mamba” here actually interleaves Mamba layers with MLPs; it just has no attention.)

A fragment of an IMDB few-shot prompt: three examples each end with a Sentiment label, Negative, Negative, Positive, highlighted in different shades of blue; the prompt ends at the colon after Sentiment
Figure 8 from the paper, excerpted from page 12 of the v2 PDF, CC BY-SA 4.0. One head in the hybrid model's first attention layer, looking out from the final colon; darker colors mean stronger attention.

The passage in the figure is an example that pure Mamba gets wrong and the hybrid gets right. Looking out from that final colon, the attention lands almost entirely on the labels Negative, Negative, Positive from the earlier examples. Across the three attention layers of the 1.3B hybrid, the authors found 12 heads like this.

The authors themselves call this anecdotal evidence. It shows that heads resembling induction heads appear in the hybrid model, but it falls short of proving that SSMs can never acquire in-context learning. From an engineering standpoint, though, a small number of attention layers is enough to close this gap.

Three More Findings

  1. MoE helps. Adding MoE (top-2 of 16, every other layer) to the 7B hybrid trained on 50B tokens raises OLLM from 36.6 to 38.1 and NQ from 15.4 to 18.9.
  2. Mamba needs extra normalization at scale. Training is stable at 1.3B and below; when scaling up to the full model, large loss spikes appeared, caused by overly large activations inside the Mamba layers. Adding RMSNorm inside them stabilized training.
  3. Positional encoding is optional. Adding RoPE to the attention layers or leaving it out gives similar results (1.3B, 250B tokens). The authors' conjecture is that the Mamba layers placed before the attention layers already supply implicit positional information.

Conclusion

AI21 positions Jamba as the first production-scale hybrid of attention and Mamba; what was released in March 2024 was a pretrained base model that had not yet been instruction-tuned or aligned. The paper's results come down to a few points:

  1. The ratio can be very low. Hybrids at 1:3 and 1:7 work almost equally well, and both beat pure attention and pure Mamba; keeping only 4 attention layers out of 32 cuts the KV cache for a 256K context from 32GB to 4GB.
  2. Pure Mamba's weakness lies in format and copying. General benchmarks barely show a difference; it surfaces in tasks like IMDB that require answering in the few-shot format, and the 1:7 hybrid recovers it.
  3. Performance close to Mixtral, at a much lower cost for long texts. Jamba still trails Mixtral on some benchmarks, and its throughput advantage appears mainly at long contexts.

Back to the question: with Mamba here, why keep attention at all? This paper takes a pragmatic line, using Mamba for most layers and reserving a few for attention, which works best at an acceptable cost. Three months later, NVIDIA ran a larger-scale comparison with 8B models and reached much the same conclusion.