Search

Search papers and lessons

Jamba: A Hybrid Transformer-Mamba Language Model

Opher Lieber, Barak Lenz, Hofit Bata et al. · 2024

Jamba mixes attention, Mamba state space and mixture-of-experts layers, so its 256K-token KV cache is about 4 GB where an attention-only model of similar size needs 32 GB.

On this page

  • Colour = topic mix
  • Architectures
  • Neural network basics
  • Calculus
  • Discrete math
  • Linear algebra
  • Probability
  • ReadReady to readNot yet

TL;DR

Jamba is a language model that mixes three kinds of layers: attention layers, which read the whole context; Mamba layers, which carry a fixed-size state forward; and mixture-of-experts layers, which add capacity while using only a few experts per token. Most of its layers are Mamba, so its key-value cache is about 8 times smaller than an attention-only model's. The released model has 52B total parameters, 12B active per token, and fits on one 80GB GPU with 8-bit weights.

Builds on: Selective State Space Model, Mixture of Experts, KV Cache · Leads to: no lesson in the graph yet; the paper releases its weights and plans to release checkpoints from its ablation runs.

Why it mattered

Transformers became the default for language models, but two costs grow with the context. The first is memory: the KV cache holds the keys and values of every past token, so it grows with the sequence and with the batch. The second is speed: each generated token attends to the whole context. State space models such as Mamba avoid both, since they keep one summary state, but earlier pure state space models still trailed Transformers in quality. Jamba's authors argue that mixing the two families gets the benefits of both.

The paper's central claim is about the design point it reaches. It calls Jamba "the first production-grade Attention-SSM hybrid model" and reports that it performs comparably to Mixtral-8x7B, a model of similar active size, and to the larger Llama-2 70B on many benchmarks, while supporting a 256K-token context and running up to 3 times faster than Mixtral on long inputs. Earlier hybrids, the paper says, were tested at small sizes or lagged pure Mamba or pure attention in quality. The paper also reports that the released model is a base model without instruction tuning or moderation, and should not be used with end users as it is.

The problem

Recall from KV Cache that in an attention layer, every past token leaves a key and a value behind, so the cache has size proportional to the number of attention layers times the sequence length. Recall from Mixture of Experts that a router sends each token to a few experts, so total parameters can grow without the compute per token growing with them.

Three things had to be balanced at once. The attention layers give quality and in-context learning but need the cache. The Mamba layers need no cache but, on their own, showed weaker in-context learning. And the mixture-of-experts layers add capacity but also add memory for the expert weights and communication between GPUs. The paper treats these three choices, the mixing ratio, the expert count and the expert frequency, as a design space and runs ablations to choose one point in it.

How it works

Jamba is a stack of identical Jamba blocks. Each block has l=8l = 8 layers, mixed at a ratio of one attention layer to seven Mamba layers (a:m=1:7a:m = 1:7). Every layer is followed by an MLP, and in every second layer the MLP is replaced by a mixture-of-experts layer with 16 experts and the top 2 used per token (e=2e = 2, n=16n = 16, K=2K = 2). The attention layer is never a mixture-of-experts layer in this architecture, so it keeps a plain MLP. The released configuration stacks 4 such blocks, for 32 layers in total.

Trace one small input through one block. Use 8 tokens and a block of 8 layers, with the layer types as in the paper.

  1. Input. The 8 tokens are turned into vectors by the embedding table. Check: each token is one vector, so the block receives 8 vectors.
  2. Layer 1, Mamba plus MLP. The Mamba layer reads the 8 vectors in order and updates one running state, token by token. Its output is added back to the vectors through a residual connection. The MLP that follows is a plain MLP in this layer. Check: the Mamba layer keeps a state of fixed size, whatever the number of tokens.
  3. Layer 2, Mamba plus mixture-of-experts. The same kind of update runs, followed by an MoE layer. A router scores the 16 experts for each token, and only the top 2 run. Check: for each token, 2 of 16 experts are active, so one eighth of the expert weights are used.
  4. Layers 3 and 4. Mamba plus MLP, then Mamba plus MoE. The state gets updated, and no key or value is stored. Check: no cache grows during these layers.
  5. Layer 5, attention plus MLP. Attention lets each token read all earlier tokens. Its keys and values are stored in the KV cache, one pair per token. Check: this is the only layer type in the block that adds to the cache, and it is followed by a plain MLP, not an MoE.
  6. Layers 6 to 8. Mamba plus MoE, then Mamba plus MLP, then Mamba plus MoE. The state keeps being updated, and no key or value is stored. Check: the cache still holds only the entries written by layer 5.
  7. Output. The last vectors go to the language-model head, which gives a probability for each of the 64K vocabulary tokens. Check: the output size is the vocabulary size per position.
flowchart LR
  A[tokens] --> B[Mamba + MLP]
  B --> C[Mamba + MoE]
  C --> D[Mamba + MLP]
  D --> E[Mamba + MoE]
  E --> F[Attention + MLP]
  F --> G[Mamba + MoE]
  G --> H[Mamba + MLP]
  H --> I[Mamba + MoE]
  I --> J[next block or output]

The paper also notes two details of the implementation. It adds RMS normalization inside the Mamba layers, because large activations in those layers caused loss spikes at 7B scale. And it uses no explicit position information, such as rotary embeddings, since the Mamba layers that come before each attention layer provide some position information on their own.

Worked examples

Example
1. Tiny: how much cache does the mix save?

Take a model of 32 layers, all attention, and compare it with Jamba's layout of 4 blocks of 8 layers, one attention layer in each block.

  1. The attention-only model has 32 attention layers, so it stores 32 keys and values per token.
  2. Jamba has 4 blocks, each with 1 attention layer, so it has 4 attention layers.
  3. The ratio is 4/32=1/84 / 32 = 1/8.
  4. Check: a 32-layer model and a 4-layer attention set differ by a factor of 8, which matches the paper's claim.

Interpretation: the cache shrinks by the fraction of layers that are attention, and the Mamba layers store nothing per token.

Example
2. Standard: routing one token to two experts

Take one token in one MoE layer. The router gives a score to each of the 16 experts. Suppose the two highest scores are 2.0 for expert 3 and 1.0 for expert 9, and the other 14 scores are lower.

  1. Keep the top 2 experts: expert 3 and expert 9.
  2. Turn their scores into weights that sum to 1: e2/(e2+e1)=7.389/10.107=0.731e^{2}/(e^{2}+e^{1}) = 7.389/10.107 = 0.731 for expert 3, and e1/(e2+e1)=0.269e^{1}/(e^{2}+e^{1}) = 0.269 for expert 9.
  3. Run only those two experts on the token. Call their outputs f3(x)f_3(x) and f9(x)f_9(x).
  4. The layer output is 0.731⋅f3(x)+0.269⋅f9(x)0.731 \cdot f_3(x) + 0.269 \cdot f_9(x).
  5. Check: the weights sum to 0.731+0.269=1.0000.731 + 0.269 = 1.000.

Interpretation: the layer holds 16 experts worth of parameters, but each token pays for only 2. This mechanism is why the model has 52B total parameters but only 12B active ones.

Example
3. Realistic: how many long sequences fit on one 80GB GPU?

These are back-of-envelope numbers from the paper's Table 1 and the parameter counts, not measurements. Assume int8 weights, one byte per parameter, keep the cache at 16 bits as in Table 1, and ignore activations and other overhead.

  1. Jamba has 52B total parameters, so its weights take about 52 GB. That leaves about 28 GB of the 80 GB for the cache.
  2. Table 1 gives Jamba a KV cache of 4 GB for one 256K-token sequence. So about 28/4=728 / 4 = 7 such sequences fit.
  3. Mixtral has 46.7B total parameters, so its weights take about 47 GB. That leaves about 33 GB.
  4. Table 1 gives Mixtral a KV cache of 32 GB for one 256K-token sequence. So about 33/32≈133 / 32 \approx 1 such sequence fits.
  5. Check: the cache is the limiting factor for Mixtral, and Jamba holds several times more long sequences in the same memory.

Interpretation: at very long contexts, the cache decides how many requests a GPU can serve, and the hybrid design is what lets the paper claim a single-GPU, 256K-token model.

Try it: a Jamba-style stack has 6 blocks of 8 layers each, with one attention layer per block. How many attention layers are there in total, and what fraction of all layers is attention? Answer: there are 6 attention layers, out of 6⋅8=486 \cdot 8 = 48 layers, so the fraction is 6/48=1/86/48 = 1/8.

The math

Definition: KV cache size
For a model with LaL_a attention layers, nkvn_{kv} key-value heads of size dhd_h, a context of TT tokens and bb bytes per number, the cache holds 2⋅La⋅nkv⋅dh⋅T⋅b2 \cdot L_a \cdot n_{kv} \cdot d_h \cdot T \cdot b bytes. The factor 2 is for keys and values. Mamba layers do not appear, because they store a fixed-size state instead of one entry per token.

Step 1 in symbols: the cache per token

For a block of ll layers with ratio a:ma:m, the number of attention layers is La=(number of blocks)⋅aL_a = (\text{number of blocks}) \cdot a. With a=1a = 1 per block of 88 layers and 44 blocks, La=4L_a = 4, against L=32L = 32 layers in total. The fraction of layers that store a cache is La/L=1/8L_a / L = 1/8.

Step 2 in symbols: the cache at 256K tokens

Table 1 reports 32 GB for Mixtral at 256K tokens in 16 bits. Check that the formula reproduces it. Using La=32L_a = 32, T=262,144T = 262{,}144 and b=2b = 2, and assuming Mixtral uses nkv=8n_{kv} = 8 heads of size dh=128d_h = 128, the formula gives

2⋅32⋅8⋅128⋅262,144⋅2≈34.4×109 bytes≈32 GiB.2 \cdot 32 \cdot 8 \cdot 128 \cdot 262{,}144 \cdot 2 \approx 34.4 \times 10^{9}\ \text{bytes} \approx 32\ \text{GiB}.

With the same settings but La=4L_a = 4, the Jamba cache is 4 GiB4\ \text{GiB}, which matches the table. The head settings are assumptions that reproduce both Table 1 entries.

Step 3 in symbols: the router

For a token with input xx and router weights WrW_r over n=16n = 16 experts, let s=Wrxs = W_r x be the vector of scores. Keep the indices of the K=2K = 2 largest scores. Let wiw_i be the softmax over those two scores. The layer output is

MoE(x)=∑i∈top-Kwi fi(x),\text{MoE}(x) = \sum_{i \in \text{top-}K} w_i \, f_i(x),

where fif_i is the MLP of expert ii. Only two of the sixteen functions fif_i are evaluated for each token.

Step 4 in symbols: the Mamba state

A Mamba layer updates a state ht\mathbf{h}_t from the current token's vector xt\mathbf{x}_t. The update is a recurrence, so the state does not grow with tt. The exact update is the subject of Selective State Space Model; in this lesson it is enough that the state has a fixed size and is updated once per token.

A note on the sizes in the paper

The paper uses 1:71{:}7 attention to Mamba layers, 16 experts and top-2 routing. The total parameter count of 52B and the active count of 12B come from these choices together with the width of each layer, which the paper does not reproduce in full in the main text.

Results

Quality against similar models. On the paper's academic benchmarks, Jamba is close to Mixtral-8x7B, a model with a similar number of active parameters. The paper's Table 2 shows HellaSwag 87.1 for Jamba against 86.7 for Mixtral, and WinoGrande 82.5 against 81.2. On MMLU, Jamba scores 67.4, below Mixtral's 70.6 and Llama-2 70B's 69.8. On GSM8K, it scores 59.9 against Mixtral's 60.4. The paper's summary is that Jamba performs comparably to leading models of similar or larger size.

Long-context quality. On five long-context question-answering datasets, with a 3-shot format and F1 scoring, the average is 0.44 for Jamba against 0.43 for Mixtral. The paper reports that Jamba beats Mixtral on most of the datasets, and its needle-in-a-haystack test holds up to 256K tokens with only 4 attention layers.

Efficiency. On a single A100 80GB with int8 weights and 8K-token contexts, the paper reports a throughput 3 times that of Mixtral, because Jamba can process larger batches. Mixtral cannot fit a batch of 16 in that setting. At 128K tokens on four A100 GPUs, Jamba again reaches about 3 times Mixtral's throughput. The paper notes that these numbers come without the optimizations that Transformers have had for years.

Ablations at small scale. At 1.3B parameters trained on 250B tokens, the hybrid with a:m=1:3a:m = 1{:}3 or 1:71{:}7 beats both pure attention and pure Mamba on HellaSwag, WinoGrande, NQ and the log-probability tests. In-context learning is the main gap: on IMDB, pure Mamba scores 48.8 against 84.1 for attention and 90.9 for the hybrid, because it often does not follow the answer format. The paper finds an induction-style head in the attention layer of the hybrid, which attends from the last token to the labels of the few-shot examples.

Limits. The released model is a base model, not instruction-tuned, and the paper warns against using it as is with end users. Some ablations are at 1.3B or 7B scale and 50B to 250B training tokens, so the effect of the ratio at larger scale is not measured in the paper. The MoE benefit at 7B scale is shown in its Table 7 with a single comparison.

Common mistakes and tradeoffs

It is tempting to think that a hybrid model is just a Transformer with a few cheaper layers. In fact the Mamba layers change what the model can do. Pure Mamba struggled with in-context learning in the paper's IMDB test, and the attention layers are what fixed it, even at a ratio of one in eight.

It is tempting to think that total parameters decide memory. In fact the cache decides memory at long contexts, and the expert weights decide the weight memory. Jamba's 52B total parameters are cheaper to serve than a dense 52B model because only 12B are active per token, but the weights still have to sit in memory.

It is tempting to think that more experts always help. In fact the number of experts trades capacity for memory, and the paper picks 16 experts with top-2 routing as a balance, since more experts increase memory and communication between GPUs.

It is tempting to think that the speed gain holds at short contexts. In fact the paper shows that with short sequences, attention takes a small share of the compute, so the advantage of Mamba layers appears mainly at long contexts.

When to use this design. Use it when long contexts or large batches make the KV cache the bottleneck, and when you can accept a model that needs its own kernels and training pipeline. When not to use it. Do not use it when short contexts dominate, since the cache is not the limit there, or when a mature pure-Transformer serving stack is already in place. Alternatives. Grouped-query attention, as in Grouped-Query Attention, shrinks the cache inside every attention layer, and a pure Mamba model removes the cache but is weaker at in-context learning. Jamba combines the two ideas instead of choosing one.

Reading guide

  • Section 1, Introduction. The motivation: the KV cache and the limits of RNNs and pure state space models. Needs KV Cache and State Space Model.
  • Section 2, Model architecture. The Jamba block, the ratio a:ma:m, the MoE frequency ee, and Table 1 on cache size. Needs Mixture of Experts and Selective State Space Model.
  • Section 3, Reaping the benefits. The single-GPU configuration and the throughput comparisons in Figures 2 and 3. Read it with the efficiency paragraph in Results.
  • Section 4, Training and dataset. Only the outline, since the data and framework are proprietary. Needs no new prerequisites beyond the training ideas in Transformer.
  • Section 5, Evaluation. The benchmark tables and the long-context tests. Needs Perplexity for the log-probability measures and In-Context Learning for the few-shot tasks.
  • Section 6, Ablations. The hybrid tests, the in-context learning study with the induction-head figure, MoE, stabilisation with RMS normalization, and the positional information test. Needs In-context Learning and Induction Heads, RMS Normalization and Rotary Position Embedding.

Check yourself

QuestionRecall: which layers in a Jamba block add to the KV cache, and why do the Mamba layers not?Show answer
Only the attention layers add to the KV cache, because they store one key and one value per token. A Mamba layer keeps a state of fixed size that is updated token by token, so it stores nothing per token.
QuestionCompute: a stack has 48 layers in total, with 1 attention layer for every 8 layers. The cache per attention layer is 1 GB at a given length. What is the total cache at that length, and what would it be for an all-attention stack of 48 layers?Show answer
There are 48/8=648 / 8 = 6 attention layers, so the total cache is 6⋅1=66 \cdot 1 = 6 GB. An all-attention stack has 48 attention layers, so its cache would be 48⋅1=4848 \cdot 1 = 48 GB. The ratio is 6/48=1/86/48 = 1/8.
QuestionWhy: why does the paper use 16 experts with top-2 routing rather than more experts?Show answer
More experts add total parameters, which raises the memory needed for weights and the communication between GPUs, while top-2 routing keeps the active parameters and compute per token low. The paper's configuration balances capacity against memory and communication so the model fits on one 80GB GPU with int8 weights.
QuestionApplied: a team wants to serve 256K-token requests on one 80GB GPU. Which design fares better on the cache, and what does the paper's Table 1 say?Show answer
The hybrid design fares better. Table 1 gives Jamba a 4 GB cache at 256K tokens and Mixtral a 32 GB cache, so with the weights taking most of the memory, the hybrid can hold several long sequences while the attention-only model can barely hold one.

Sources

Read the full paper

Opher Lieber, Barak Lenz, Hofit Bata et al., 2024. arXiv:2403.19887