TAI BUI
Back to curriculum
LearnPython

Jamba — Hybrid SSM-Transformer

State space models (SSMs) and transformers pull in opposite directions. Attention buys quality at quadratic cost. An SSM buys linear-time inference and constant memory through a recurrence, and gives up some quality for it. AI21's Jamba (March 2024) and Jamba 1.5 (August 2024) put both in one model: 1 Transformer layer for every 7 Mamba layers, MoE on every other layer, and a 256k context window that fits on a single 80GB GPU. Mamba-3 (ICLR 2026) tightens the SSM side with complex-valued state updates and MIMO projections. This lesson reads both architectures end to end and works out why the hybrid recipe is a reasonable bet for long context.

TIME: ~60 minutesPREREQUISITES: Phase 10 · 14 (open-model architectures), Phase 10 · 17 (native sparse attention)

Learning Objectives

  • Explain the three primitives in a Jamba block (Transformer layers, Mamba layers, MoE) and the 1:7 interleaving with MoE on every other layer.
  • State what an SSM's recurrence looks like at a high level and why it gives constant-memory inference.
  • Compute the KV cache footprint of a Jamba-shaped model at 256k context and compare it to a pure-Transformer model of the same shape.
  • Name the three Mamba-3 innovations (exponential-trapezoidal discretization, complex-valued state update, MIMO) and the problem each one targets.

The Problem

Attention is quadratic in sequence length, and state space models are linear. The gap widens fast: at 256k tokens a single attention map has about 65B entries per head, while an SSM's recurrent state stays the same size at any sequence length.

Pure-SSM models (Mamba, Mamba-2) match Transformer perplexity at small scale but lag on state-tracking tasks and on some kinds of in-context retrieval. My read is that an SSM compresses history into a fixed state, so when the history is long, some of it gets lost. Attention keeps the keys and values of past tokens around and pays quadratic cost for the privilege.

So use both. Put Transformer layers where exact recall matters, use SSM layers for the rest, and tune the ratio. Jamba is a production-scale model that ships this hybrid recipe (52B total, 12B active, 256k context, single 80GB GPU). Jamba 1.5 extends the family to 398B total and 94B active. Mamba-3 (ICLR 2026) is a recent pure-SSM design that a future hybrid could be rebuilt around.

This lesson reads the three papers and builds the mental model for picking the right ratio.

Warm-up0/3
1. Why does the KV cache of a standard Transformer keep growing as the context gets longer?Warm-up
A row of token boxes, tok 1 through tok N, with an arrow down into a KV cache box whose size after N tokens is left as a question mark. A question asks why this keeps growing as N grows.
Setup only, from the stem; the growth reason is the question.
2. A model switches from full multi-head attention with 32 KV heads to grouped-query attention with 8 KV heads. What happens to its KV cache at the same context length?Warm-up
Two boxes: full multi-head attention with 32 kv heads, and grouped-query attention with 8 kv heads, whose cache size is left as a question mark. A question asks what happens to the cache at the same context length.
Setup only, from the stem; the cache change is the question.
3. In a mixture-of-experts layer, what does 'active parameters' refer to?Warm-up
Two boxes: the experts in the layer, say 8 total, and a token's active parameters, left as a question mark. A question asks what active parameters means for a token passing through this layer.
Setup only, from the stem; the definition is the question.
Check: The Problem0/1
1. Why do pure-SSM models like Mamba and Mamba-2 lag Transformers on state-tracking and some in-context retrieval tasks?Explain
A long history of many past tokens flowing into a fixed-size state h, which stays the same size always, flowing into a box asking how state-tracking and recall compare against attention, left as a question mark. A question asks why pure-SSM models lag Transformers here.
The compression step is given directly in the Problem section; why it causes a lag is the question.

The Concept

An SSM in one page

A state space model processes a sequence x_1, ..., x_N via a fixed-size state h:

h_t = A h_{t-1} + B x_t
y_t = C h_t

At each step the state evolves via a linear dynamics A, takes input B x_t, and emits output C h_t. A, B, C can be learned. The property that matters: computing y_t needs only h_{t-1} and x_t, not any earlier x. Memory is constant and inference is O(1) per token.

Modeling quality comes down to the structure of A. S4 (Gu 2021) used a highly structured matrix that could be evaluated as a long convolution during training. Mamba (Gu, Dao 2023) replaced the fixed A, B, C with data-dependent ones, which is the "selective" part. Mamba-2 (2024) simplified the structure further, and Mamba-3 (2026) adds complexity back in specific places.

For a decoder LLM, an SSM layer can take the place of an attention layer, with a fixed-size per-layer state instead of a growing KV cache.

The Jamba block

A Jamba block interleaves layers according to two numbers:

  • l: the attention-to-Mamba ratio. Jamba uses l = 8, meaning 1 Transformer layer for every 7 Mamba layers (7 Mamba + 1 Attention = 8 layers per group).
  • e: the MoE frequency. Jamba uses e = 2, meaning every other layer applies MoE.

The layer sequence within a block:

M  M  M  M  M  M  M  A    (7 Mamba + 1 Attention)
|  M  |  M  |  M  |  M    (where | marks MoE applied)

Each Jamba block is 8 layers. Stack 4 blocks (32 layers) and you get 28 Mamba layers and 4 attention layers, with MoE on 16 of the 32.

Why the 1:7 ratio

AI21 ran ablations to find the attention-to-Mamba ratio with the best perplexity per parameter and the best in-context recall on their long-context evals.

  • Too much attention (1:1): quality improves, but memory and speed degrade.
  • Too little attention (1:15): memory looks great, but in-context retrieval suffers.
  • Sweet spot: 1:7 or 1:8.

The intuition is that the Transformer layers handle exact recall and state tracking while the Mamba layers do the cheap bulk of the processing.

Positional encoding

Mamba layers are position-aware through the recurrence, so the attention layers in the original Mamba-based hybrids did not use RoPE. The SSM layers supplied position information. Jamba 1.5 adds RoPE to the attention layers to generalize to longer context, a refinement based on long-context evaluation.

The memory budget

For a Jamba-1 shape (32 layers: 28 Mamba + 4 Attention, hidden 4096, 32 attention heads):

  • KV cache (attention layers only): 2 * 4 * 32 * 128 * 256k * 2 = 16 GB at 256k BF16. Only the 4 attention layers contribute.
  • SSM state: 28 * hidden * state_size, fixed per layer and independent of sequence length. With a state of 16 per feature and hidden 4096, that is 28 * 4096 * 16 * 2 = 3.7 MB total.

Compare to a pure Transformer at 32 layers, same hidden, full MHA at 32 heads: 2 * 32 * 32 * 128 * 256k * 2 = 128 GB at 256k BF16. That is an 8x reduction in KV cache. Even against a GQA(8) baseline (2 * 32 * 8 * 128 * 256k * 2 = 32 GB), the 1:7 hybrid at 16 GB is 2x smaller.

This is the arithmetic behind "256k context on a single 80GB GPU." The full-MHA KV cache of a pure Transformer would not fit, and a GQA baseline leaves little room for weights and activations. The hybrid's cache is small enough to leave that room.

Mamba-3: the pure-SSM baseline in 2026

Mamba-3 (ICLR 2026, arXiv:2603.15569) introduces three innovations on the pure-SSM side:

  1. Exponential-trapezoidal discretization. Replaces the Euler-method discretization in Mamba-2 with a more expressive recurrence. A convolution-like operation is applied to the state-input inside the core recurrence, instead of as an outer convolution on x_t.

  2. Complex-valued state update. Earlier Mambas simplified the state matrix from complex (S4) to real diagonal (Mamba) to scaled identity (Mamba-2). Mamba-3 brings complex values back, which is equivalent to a data-dependent rotary embedding on the state. That restores the state-tracking ability the real-valued simplifications cost.

  3. Multi-input multi-output (MIMO) projections. Replaces per-feature scalar projections with matrix-valued ones. This improves modeling power and hardware utilization at inference without increasing decode latency.

At 1.5B parameters, Mamba-3 improves average downstream accuracy by 0.6 points over Gated DeltaNet; the MIMO variant adds 1.2 more for a total 1.8-point gain. At the same state size, Mamba-3 matches Mamba-2 with half the state.

Mamba-3 is not yet in a production hybrid at scale, but it is a natural candidate for the SSM side of the next Jamba-class model.

When to reach for a hybrid

Hybrids win when:

  • Context is long enough that a pure-Transformer KV cache becomes painful (64k and up).
  • Tasks mix short-range structure (good for SSM) with long-range recall (needs attention).
  • You deploy on a single-GPU memory budget where the Transformer KV cache alone would not fit.

Hybrids lose when:

  • Context is short (under 16k). The SSM machinery buys little, and a pure Transformer is fine.
  • Tasks need dense attention across the whole input, such as deep reasoning or multi-document cross-reference. Having few attention layers in the hybrid hurts there.
  • You are scaling toward frontier models. Pure-Transformer + MoE + MLA (DeepSeek-V3 style) is the recipe I see doing best on capability right now.

The competitive landscape

ModelFamilyScaleUnique claim
Mamba-2pure SSM3Blinear time, constant memory
Jambahybrid52B/12B256k on 80GB
Jamba 1.5 Largehybrid398B/94Benterprise-grade long-context
Mamba-3pure SSM1.5B (paper)state-tracking restored
DeepSeek-V3pure Transformer + MoE671B/37Bfrontier capability

In this landscape, pure-Transformer MoE leads at the frontier and hybrids look strongest in the 256k-plus context niche. Mamba-3's state-tracking gains may push hybrid ratios toward more SSM and less attention in the next generation.

Check: The Concept0/24
1. What is a state space model (SSM) layer?Define
A formula panel showing h_t equals A h_t-1 plus B x_t, and y_t equals C h_t. A question asks what kind of layer has this recurrence.
The lesson's own opening formula for this layer type; naming it is the question.
2. What makes an SSM 'selective' in Mamba?Define
Two boxes: S4's A, B, C are fixed and learned once, while Mamba's A, B, C are left as a question mark. A question asks what makes Mamba's version selective.
S4's fixed design is given directly in the Concept section; what Mamba changes is the question.
3. In Jamba, what does the attention-to-Mamba ratio l = 8 describe?Define
A row of 8 identical dashed boxes, each marked with a question mark, representing one group of 8 layers under Jamba's ratio l = 8. A question asks which positions are attention and how many are Mamba.
Setup only, the group size the stem gives; the split is the question.
4. What does one Jamba block contain?Define
A box labelled 1 Jamba block, with two dashed sub-boxes below it: composition, left as attention count and Mamba count unknown, and MoE pattern, left as where it is applied unknown. A question asks what one Jamba block contains, layer by layer.
Setup only; the block is given as a unit, its composition and MoE pattern are the question.
5. What is the SSM state in a Mamba layer?Define
A term card reading SSM state. A per-layer buffer inside each Mamba layer, distinct from an attention layer's cache. The question: What is it, and does its size depend on context length?
The term as the lesson names it; its mechanism is the question.
6. What does Jamba's '256k context' claim state?Define
A term card reading Jamba's 256k context claim. A number tied to one GPU, repeated throughout the lesson. The question: What exactly does the claim state?
The term as the lesson names it; its mechanism is the question.
7. Which three innovations define Mamba-3, the 2026 pure-SSM design?Define
Three identical dashed boxes, innovation 1, 2 and 3, each marked with a question mark. A question asks which three innovations define Mamba-3.
Setup only; the count of three is given by every option, the names are the question.
8. What is exponential-trapezoidal discretization in Mamba-3?Define
A term card reading Exponential-trapezoidal discretization. A piece of Mamba-3's recurrence design, named against Mamba-2's Euler-method discretization. The question: What is it, concretely?
The term as the lesson names it; its mechanism is the question.
9. What is MIMO in Mamba-3?Define
A term card reading MIMO. A design choice in Mamba-3's projections, replacing what earlier Mambas used. The question: What does MIMO stand for, and what does it change?
The term as the lesson names it; its mechanism is the question.
10. What is a hybrid architecture in this context?Define
A term card reading Hybrid architecture. A design term distinguishing Jamba's approach from a pure Transformer or a pure SSM. The question: What defines a hybrid architecture here?
The term as the lesson names it; its mechanism is the question.
11. Which entry in the competitive landscape table is Jamba 1.5 Large?Define
Two boxes giving one table row's scale, 398B total and 94B active, and its unique claim, enterprise-grade long-context, with the model name left unstated. A question asks which model this row is.
Transcribed from the lesson's own competitive landscape table, naming only the scale and claim this question asks about.
12. In the SSM recurrence h_t = A h_{t-1} + B x_t with y_t = C h_t, what makes inference cost constant per token?Explain
A box stating the inputs used to compute y_t are h_t-1 and x_t only, with an arrow into a box asking the cost per token, left as a question mark. A question asks why this makes inference cost constant per token.
The two inputs are given directly by the recurrence in the Concept section; why cost stays constant is the question.
13. What did AI21's ablations find about a 1:15 attention-to-Mamba ratio?Explain
Two boxes: 1:1, lots of attention, where quality improves but memory and speed degrade, and 1:15, little attention, where memory and retrieval are both left as question marks. A question asks what the ablations found at 1:15.
The 1:1 finding is given directly in the Concept section; the 1:15 finding is the question.
14. What does Jamba 1.5 change about the attention layers compared with the original Jamba?Explain
Two boxes: original hybrids, where attention layers use no RoPE, and Jamba 1.5, where attention layers are left as a question mark. A question asks what Jamba 1.5 adds to its attention layers.
The original hybrids' no-RoPE design is given directly in the Concept section; what 1.5 adds is the question.
15. Why does Mamba-3 bring complex values back into the state update?Explain
A four-step ladder: S4 uses a complex-valued state, Mamba simplifies to real diagonal, Mamba-2 simplifies further to scaled identity, and Mamba-3 is left as a question mark. A question asks why Mamba-3 would go back toward something closer to S4's complex values.
The S4-to-Mamba-2 simplification chain is given directly in the Concept section; Mamba-3's reason is the question.
16. In a Jamba-1 shape with 4 attention layers and 28 Mamba layers, why do only the attention layers make memory grow with context length?Explain
Two boxes: 4 attention layers, with memory behavior versus context left as a question mark, and 28 Mamba layers, with the same left as a question mark. A question asks why only one of these grows as context length grows.
The layer counts are given directly by the stem; which one grows, and why, is the question.
17. Which situation currently favors a pure Transformer over a hybrid?Explain
Two boxes, pure Transformer and hybrid, each with the situation that favors it left as a question mark. A question asks which situation favors the pure Transformer.
Setup only; no specific situation is named, so no option can be eliminated by reading the picture.
18. At 256k BF16, how does the 1:7 Jamba-1 shape (4 attention layers) compare with a 32-layer full-MHA Transformer and a GQA(8) baseline?Apply
Three dashed boxes: Jamba 1:7 with 4 attention layers and 32 kv heads, full MHA with 32 attention layers and 32 kv heads, and GQA(8) with 32 attention layers and 8 kv heads, all at head dimension 128. A question asks the KV cache at 262,144 tokens for each.
Setup only, the stem's own three configurations; the KV cache for each is the question.
19. At 1.5B parameters, base Mamba-3 gains 0.6 points over Gated DeltaNet and the MIMO variant adds 1.2 more. What is the MIMO variant's total gain?Apply
Two boxes: base Mamba-3, plus 0.6 points, and the MIMO variant, plus 1.2 more points. A question asks the MIMO variant's total gain over Gated DeltaNet.
Both figures are given directly by the stem; the total is the question.
20. Stack 4 Jamba blocks (l = 8, e = 2) into a 32-layer model. How many layers are Mamba, how many are attention, and how many apply MoE?Apply
Four identical dashed blocks, each labelled 8 layers, with three dashed boxes below for the totals across all four: Mamba layers, attention layers, and MoE layers, each left as a question mark.
Setup only; the lesson's own l = 8, e = 2 parameters and the 4-block stack are given, the per-type totals are the question.
21. Your hybrid was trained at a 1:15 attention-to-Mamba ratio. It matches a 1:7 model on perplexity, but fails needle-in-a-haystack retrieval at 128k context. Which change best fits the ratio ablations?Analyze
Two boxes: perplexity, matching a 1:7 model, and retrieval at 128k context, failing. A question asks which change best fits the ratio ablations.
Both outcomes are given directly by the stem; the best fix is the question.
22. A pure-SSM model handles ordinary language modeling but fails tasks that need tracking state across a sequence. Which Mamba-3 change targets that symptom?Analyze
Three identical dashed boxes, candidate 1, 2 and 3, each marked with a question mark. A question asks which of three Mamba-3 changes targets a pure-SSM model that fails state-tracking despite fine language modeling.
The symptom is given directly by the stem; which candidate fixes it is the question.
23. You must serve 256k-token contexts on a single 80GB GPU, and the tasks mix short-range structure with long-range recall. Which design fits best?Design
Two boxes: a constraint of 256k context on a single 80GB GPU, and a task mix of short-range structure with long-range recall. A question asks which design fits best.
Both the constraint and the task mix are given directly by the stem.
24. A product handles prompts under 8k tokens, has ample GPU memory, and needs deep reasoning across the whole input. Which architecture fits?Design
Two boxes: a context under 8k tokens with memory not tight, and a requirement of deep reasoning across the whole input. A question asks which architecture fits.
Both the context and the requirement are given directly by the stem.

Use It

code/main.py is a memory calculator for hybrid architectures. Given the number of attention layers, a hidden size, a head config, and an SSM state size, it computes:

  • KV cache at a target context length.
  • SSM state memory.
  • The sum of the two at 8k, 65k, 131k, and 262k context (8192, 65536, 131072, and 262144 tokens), for each configuration.

It covers these configurations:

  • A pure-Transformer baseline (full MHA and GQA 8), where the KV cache grows with context.
  • Jamba-style hybrids at 1:7, 1:3, and 1:15.
  • A pure SSM with no KV cache.

The numbers are illustrative. They count KV cache and SSM state only, with no weights or activations, so treat them as a way to compare shapes and not as a deployment budget. The closing takeaway is computed from the same functions as the table, so it prints the same numbers: 128 GB for the full-MHA Transformer and 16 GB for the 1:7 hybrid at 256k.

Integration considerations for a real deployment:

  • Production inference servers such as vLLM and SGLang support Jamba and Mamba. Check the specific version.
  • At 256k context, Jamba's memory advantage shows up in concurrent-request throughput, because the same VRAM holds more Jamba sequences than Transformer sequences.
  • Mamba-3 as a standalone model is a research preview at 1.5B.
Check: Use It0/6
1. What is the production support status of these models in inference servers such as vLLM and SGLang?Define
Three identical dashed boxes, Jamba, Mamba and Mamba-3, each with its support status left as a question mark. A question asks what each model's production support status is.
Setup only, the three model names from Use It; their statuses are the question.
2. In the calculator, the 1:3 hybrid has 8 attention layers with 32 KV heads of dimension 128. What KV cache does it report at 262,144 tokens in BF16?Apply
A panel of given values: 8 attention layers, 32 kv heads, head dimension 128, context 262144, with KV cache left as a question mark.
Setup only, the stem's own figures; the KV cache is the question.
3. At 256k in BF16 the 1:7 row totals about 16 GB against 128 GB for the full-MHA baseline. What savings does the calculator's headline table print for the 1:7 row?Apply
A panel giving the 1:7 row total as 16 GB and the baseline full MHA as 128 GB, with savings left as a question mark.
Both figures are given directly by the stem; the savings percentage is the question.
4. The pure Mamba row has 32 layers, hidden 4096, SSM state size 16 and no attention layers, in BF16. What total does it print at 8k and at 262k context?Apply
A panel asking for the total memory at context 8,192 and at context 262,144, for a config with 32 layers, hidden 4096, SSM state size 16, and 0 attention layers, both left as question marks.
Setup only, the stem's own config; both totals are the question.
5. An engineer reads the calculator's 1:7 row and expects the whole row total to double when context goes from 131k to 262k. What is wrong with that assumption?Analyze
Two boxes: attention KV cache and SSM state, each with how it scales with context left as a question mark. A question asks whether the whole row total doubles when context goes from 131,072 to 262,144.
The context change is given by the stem; which component scales is the question.
6. The calculator's closing takeaway is computed from the same functions as its table. What does it report for the KV cache of the pure full-MHA Transformer and the 1:7 hybrid at 256k?Analyze
A terminal panel reading: pure Transformer at 256k equals an unstated amount just for KV cache, and Jamba 1:7 equals an unstated amount of KV cache plus 3.50MB SSM state. A question asks what the takeaway reports for these two figures.
Real printed lines from the lesson's closing takeaway, with the two KV cache figures masked; the masked figures are the question.

Ship It

This lesson produces outputs/skill-hybrid-picker.md. Given a workload specification (context length profile, task mix, memory budget), it recommends between a pure Transformer, a Jamba-style hybrid, and a pure SSM, with explicit reasoning about the memory and quality tradeoffs.

Author's Note

I'd run main.py first and read the table before the prose. It prints total cache memory at 8k, 65k, 131k, and 262k context for six configurations, so you can see the 1:7 hybrid land at 16.00GB against 128.00GB for full-MHA at 256k, with the GQA 8 baseline at 32.00GB in between. The ratio argument gets a lot more concrete once those numbers are in front of you.

The part I'd poke at is how little the SSM side contributes. In the script the pure Mamba row stays at 4.00MB from 8k to 262k, because its state does not depend on context length. Nearly all of the memory in the hybrid rows comes from the attention layers, so the memory question reduces to how many attention layers you keep. The 1:3 and 1:15 rows are the quickest way to feel that.

I'd also keep the calculator in proportion. It counts KV cache and SSM state and nothing else, so weights, activations, and MoE expert memory are missing, and the 1:7 row gives the attention layers full MHA heads. Before trusting a ratio for a real workload I'd want a quality eval at that ratio, since memory is only half of what the ablations were measuring.

Exercises

  1. Run code/main.py to compute KV cache at 256k context for a 32-layer pure Transformer (hidden 4096, 32 heads) and for a Jamba-1 hybrid of the same shape. Verify the ~8x memory reduction the AI21 paper claims.

  2. Modify the calculator to model a 1:3 hybrid (3 Mamba : 1 Attention) and a 1:15 hybrid (15 Mamba : 1 Attention). Plot KV cache vs ratio. At what ratio does the KV cache equal the SSM state memory?

  3. Read Section 3 of the Jamba paper (arXiv:2403.19887). Explain why AI21 uses Mamba-1 rather than Mamba-2 despite Mamba-2 being faster. Hint: the hybrid ablation section documents this.

  4. Compute the parameter overhead of MoE-every-other-layer in Jamba 1.5 Large (398B total, 94B active). Compare the active ratio to DeepSeek-V3 (37B/671B) and explain why Jamba's architecture pushes the active ratio higher.

  5. Read Section 3 of the Mamba-3 paper (arXiv:2603.15569). Explain in three sentences why a complex-valued state update is equivalent to a data-dependent rotary embedding. Tie the answer to Phase 7 · Lesson 04's RoPE derivation.

Key Terms

TermWhat people sayWhat it actually means
State space model (SSM)"Recurrence with a fixed state"A layer with a learned recurrence h_t = A h_{t-1} + B x_t; constant memory per token
Selective SSM"Mamba's trick"Data-dependent A, B, C parameters that give the model gating-like selectivity at linear time
Attention-to-Mamba ratio"How many attention layers"In Jamba, l = 8 means 1 attention layer per 7 Mamba layers
Jamba block"The 8-layer group"One attention + seven Mamba + MoE on alternate positions
SSM state"The hidden buffer"Fixed-size per-layer state that replaces the KV cache for Mamba layers
256k context"Jamba's flagship number"The sequence length Jamba-1 fits on a single 80GB GPU; pure Transformer cannot at that size
Mamba-3"2026 pure SSM"Current-best pure-SSM architecture with complex state + MIMO; the baseline hybrids rebuild around
MIMO"Multi-input multi-output"Mamba-3 innovation using matrix-valued projections instead of scalar per-feature
Exponential-trapezoidal discretization"Mamba-3's recurrence"More expressive recurrence that subsumes Mamba-2's Euler-method discretization
Hybrid architecture"Mix attention and SSM"Any model that interleaves Transformer and SSM layers; Jamba is the production archetype

Further Reading

Code

Ships With