Samba: Simple Hybrid State Space Models for Efficient Unlimited Context Language Modeling

Liliang Ren, Yang Liu, Yadong Lu, yelong shen, Chen Liang, Weizhu Chen

TL;DR

Samba is a novel hybrid language model architecture that combines the linear-time efficiency of Mamba (a selective State Space Model) with the precise memory recall of Sliding Window Attention (SWA). By interleaving these layers, Samba achieves state-of-the-art performance at the 3.8B parameter scale. It maintains perfect memory recall up to 256K tokens and processes 128K-length prompts nearly 4x faster than traditional Transformers, offering a highly efficient path toward unlimited context modeling.

The Context Length Dilemma

Large Language Models (LLMs) currently face a fundamental trade-off between performance and efficiency when scaling context windows:

  1. Transformers rely on the Attention mechanism, which allows for "perfect recall" of any token in a sequence. However, Attention suffers from quadratic complexity ($O(N^2)$). As the context window grows, the computational and memory costs explode, making "infinite context" practically impossible.
  2. State Space Models (SSMs), such as Mamba, offer linear complexity ($O(N)$), making them incredibly fast and memory-efficient. However, because they compress the entire history into a fixed-size recurrent state, they often struggle with "needle-in-a-haystack" retrieval tasks—effectively "forgetting" specific details over long distances.

The authors of Samba seek to break this deadlock by creating a hybrid that retains the speed of SSMs while using a limited "window" of attention to ensure the model doesn't lose its place or forget crucial specific details.

Samba's Hybrid Architecture

The core of Samba is a simple but highly effective layer-wise hybridization strategy. Instead of relying entirely on one type of layer, the architecture interleaves three distinct functional blocks to balance compression and precision.

Comparison of the Samba architecture block against other baseline configurations.
  • Mamba Layers: These selective SSMs act as the backbone of the model. They capture time-dependent semantics and efficiently compress the sequence into recurrent hidden states.
  • Sliding Window Attention (SWA): Unlike full attention, SWA only looks at a fixed number of previous tokens (in this case, a window of 2,048). This provides the "high-definition" signal needed for precise memory retrieval without the quadratic cost of looking at the entire history.
  • MLP (Multi-Layer Perceptron): Standard SwiGLU layers are used to store and recall factual knowledge.

By alternating these layers (e.g., Mamba → MLP → SWA → MLP), Samba ensures that information is both compressed for long-term efficiency and accessible for short-term precision.

Training and Evaluation

The researchers evaluated Samba across several scales and data regimes to prove its viability:

  • Pre-training: The model was trained on the Phi-2 (230B tokens) and Phi-3 (3.2T tokens) datasets to compare Samba against industry-standard baselines.
  • General Benchmarks: It was tested on standard suites like MMLU (reasoning), GSM8K (math), and HumanEval (code).
  • Long Context: The Proof-Pile dataset was used to test perplexity scaling up to 1M tokens.
  • Retrieval Tasks: The Passkey Retrieval task and the more challenging Phonebook task (retrieving specific phone numbers from thousands of entries) were used to test memory extrapolation capabilities.

Key Insights

  • The "Specialization" of Layers: Through entropy analysis, the authors found that Samba’s layers naturally specialize during training. The middle layers exhibit low attention entropy, meaning they focus intensely on specific, precise retrieval. Meanwhile, the top and bottom layers have higher entropy, focusing on integrating global information.
  • Efficiency in Attention: Interestingly, Samba requires far fewer attention heads than a standard Transformer. The authors found that even with just one or two Key-Value (KV) heads, Samba outperformed models with more complex attention setups. This suggests that the Mamba layers are doing the heavy lifting of sequence modeling, leaving the attention layers to focus purely on targeted retrieval.
  • Linear-Time Extrapolation: Because the attention uses a sliding window and the backbone is an SSM, the model's inference time stays linear. Yet, the recurrent state allows it to maintain context far beyond its 2,048-token attention window—achieving improved perplexity even at 1 million tokens in a zero-shot setting.

Key Results

  • Superior Performance: At 3.8B parameters, Samba-IT achieved a 71.9 on MMLU and 87.6 on GSM8K, significantly outperforming the Phi-3-mini model trained on the exact same data.
  • Massive Speedups: Samba achieved a 3.73× higher throughput than Transformers (equipped with Grouped-Query Attention) for 128K context prompts, and a 3.64× speedup when generating 64K tokens.
  • Perfect Recall: After minimal fine-tuning on 4K-length sequences, Samba demonstrated perfect memory recall on the Passkey Retrieval task up to a 256K context length.
  • Phonebook Mastery: On the challenging Phonebook task, Samba closed the gap with full-attention models, outperforming other linear-complexity models that typically fail when the "phonebook" exceeds their training length.

Limitations and Future Work

While Samba represents a major step forward for efficient long-context modeling, the authors note a few areas for future improvement:

  • Zero-shot Retrieval: While Samba's perplexity (predictive ability) is excellent at long ranges zero-shot, its ability to perform specific retrieval (finding a needle) at 256K still requires a small amount of fine-tuning.
  • Hybridization Search: The paper explored a few static hybridization strategies, but the authors suggest that more sophisticated, task-adaptive dynamic architectures could further optimize the balance between SSMs and Attention.
  • Transferability: The authors left the question of whether a model fine-tuned for one long-context task (like Passkey) can automatically transfer that ability to others (like Phonebook) for future research.

Summary updated

BibTeX
@inproceedings{ren2025samba,
  author = {Liliang Ren and Yang Liu and Yadong Lu and yelong shen and Chen Liang and Weizhu Chen},
  title = {{Samba: Simple Hybrid State Space Models for Efficient Unlimited Context Language Modeling}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=bIlnpVM4bc},
}
Figure