Abstract
Efficiently modeling sequences with infinite context length has been a long-standing problem. Past works suffer from either the quadratic computation complexity or the limited extrapolation ability on length generalization. In this work, we present Samba, a simple hybrid architecture that layer-wise combines Mamba, a selective State Space Model (SSM), with Sliding Window Attention (SWA). Samba selectively compresses a given sequence into recurrent hidden states while still maintaining the ability to precisely recall memories with the attention mechanism. We scale Samba up to 3.8B parameters with 3.2T training tokens and show that Samba substantially outperforms the state-of-the-art models based on pure attention or SSMs on a wide range of benchmarks. When trained on 4K length sequences, Samba can be efficiently extrapolated to 256K context length with perfect memory recall and show improved token predictions up to 1M context length. As a linear-time sequence model, Samba enjoys a 3.73x higher throughput compared to Transformers with grouped-query attention when processing user prompts of 128K length, and 3.64x speedup when generating 64K tokens with unlimited streaming.
Introduction
Modern Large Language Models (LLMs) face a fundamental trade-off between efficiency and memory. Transformers, while highly performant, suffer from quadratic complexity, where computational costs quadruple as input sequences double. This "quadratic wall" makes processing massive contexts, such as entire codebases or long books, prohibitively expensive. Conversely, State Space Models (SSMs) like Mamba offer linear scaling but often struggle with precise memory recall—the ability to retrieve specific information from a vast context—due to their fixed-size recurrent state compression.
Samba was designed to bridge this gap, offering a model that maintains the speed of an SSM while achieving the sharp recall capabilities of a Transformer.
The Samba Architecture
The core innovation of Samba is a simple yet effective layer-wise hybridization. Instead of relying on a single layer type, Samba interleaves three distinct functional blocks to balance global context compression with local high-resolution retrieval:
- Mamba Layers: These selective SSMs capture time-dependent semantics and provide the backbone for efficient, recurrent-style decoding.
- Sliding Window Attention (SWA) Layers: By focusing only on a fixed window of recent tokens (e.g., 2048), SWA provides precise retrieval of high-definition signals without the quadratic cost of full attention.
- MLP Layers (SwiGLU): Standard feed-forward networks are utilized to store and recall factual knowledge.
By stacking these layers, the model uses Mamba to compress broad context and SWA to maintain a high-resolution view of immediate history. Because the SWA window is fixed, the entire architecture maintains linear time complexity.
Experimental Results
The researchers evaluated Samba at various scales, with the flagship 3.8B parameter model trained on 3.2 trillion tokens of high-quality "textbook" data. The results demonstrate that Samba is not just efficient, but highly capable:
- General Knowledge: Samba 3.8B achieved a score of 71.9 on the MMLU benchmark, significantly outperforming the similarly sized Phi-3-mini (68.8).
- Reasoning and Math: On the GSM8K math benchmark, Samba scored 87.6, proving that the hybrid architecture maintains strong reasoning capabilities.
- Coding: The model also showed competitive performance on HumanEval, matching or exceeding state-of-the-art models in its parameter class.
Long-Context Efficiency and Extrapolation
One of Samba's most striking features is its ability to handle massive contexts. Despite being trained on sequences of only 4,000 tokens, it demonstrates remarkable extrapolation capabilities:
- Perfect Recall: After minimal fine-tuning (500 steps), Samba achieved 100% accuracy on the Passkey Retrieval task at context lengths up to 256,000 tokens. Pure SWA models typically fail beyond their fixed window size.
- Predictive Accuracy: Samba showed improved perplexity on contexts up to 1 million tokens in zero-shot settings.
- Throughput Gains: Due to its linear scaling, Samba is significantly faster than standard Transformers for long prompts. It achieves a 3.73x higher throughput for 128K token prompts and a 3.64x speedup during generation for unlimited streaming.
Conclusion
Samba represents a significant advancement in efficient language modeling, proving that the specific interleaving of SSMs and attention can overcome the limitations of both architectures. While the authors note that zero-shot retrieval still lags slightly behind full-attention models without minor fine-tuning, the massive gains in throughput and context handling make Samba a powerful candidate for next-generation, long-context AI applications.
Summary updated