MISTRAL 7B
Published: October 2023

Mistral 7B: Outperforming Models 3x Its Size

Albert Q. Jiang, Alexandre Sablayrolles, Arthur Mensch, Chris Bamford, Devendra Singh Chaplot, Diego de las Casas, Florian Bressand, Gianna Lengyel, Guillaume Lample, Lucile Saulnier, Lélio Renard Lavaud, Marie-Anne Lachaux, Pierre Stock, Teven Le Scao, Thibaut Lavril, Thomas Wang, Timothée Lacroix, William El Sayed.

TL;DR: Mistral 7B is an extremely efficient 7-billion-parameter language model that outperforms Llama 2 13B on all benchmarks, and Llama 1 34B on reasoning, math, and code. It achieves this using Grouped-Query Attention (GQA) and Sliding Window Attention (SWA) with a rolling buffer cache.

Grouped-Query Attention Sliding Window Attention Rolling Buffer Cache Apache 2.0 License

Why This Matters

In the race for state-of-the-art Natural Language Processing (NLP), the standard playbook has been simple: make models larger. However, scaling up parameter counts introduces a massive bottleneck: computational cost and inference latency. This makes large models highly impractical for real-world, real-time edge deployment.

The holy grail of LLM engineering is to achieve high performance while keeping the model small enough for fast, cost-effective inference. Mistral 7B proves that architectural ingenuity can bypass the raw scaling law. By introducing optimized attention mechanisms, it delivers the reasoning, coding, and mathematical capabilities of models 2x to 5x its size, entirely rewriting the cost-performance equation.

The Big Idea

The core contribution of Mistral 7B is the optimization of the attention layer. While standard transformer architectures suffer from quadratic time complexity and linear memory scaling with sequence length, Mistral uses two complementary attention paradigms: Grouped-Query Attention (GQA) and Sliding Window Attention (SWA). Coupled with a Rolling Buffer Cache, Mistral drastically reduces the memory footprint of the KV Cache, allowing it to handle massive sequence lengths at a fraction of the hardware cost.

How It Works

Mistral 7B Architecture Specifications

Dimensions 4096
Layers 32
Query Heads 32
KV Heads (GQA) 8
Window Size ($W$) 4096
Context Length 8192
Vocab Size 32000
Hidden Dim 14336

1. Grouped-Query Attention (GQA)

Standard Multi-Head Attention (MHA) uses the same number of heads for query, key, and value vectors. This leads to large memory requirements during decoding due to the growing KV Cache. Multi-Query Attention (MQA) uses a single head for key and value, which speeds up inference but hurts model capacity.

Grouped-Query Attention (GQA) is a clever middle ground: it groups query heads into subsets, with each subset sharing a single Key and Value head. Mistral 7B uses 32 query heads and 8 KV heads (a group size of 4), achieving a significant speedup and cache reduction while maintaining high accuracy.

2. Sliding Window Attention (SWA)

Sliding Window Attention exploits the stacked layers of a transformer to attend to information beyond a fixed window size $W$. In SWA, a token at position $i$ in layer $k$ only attends to hidden states from the previous layer $k-1$ that fall within a local window of size $W$:

$$h_i^{(k)} = \text{Attention}\left(q_i^{(k)}, \{k_j^{(k-1)}, v_j^{(k-1)}\}_{j = i-W}^{i}\right)$$

Because this window is stacked across multiple layers, information propagates recursively. At layer $k$, a token can access information up to $k \times W$ tokens in the past. With 32 layers and a window size of 4096, Mistral 7B has a theoretical attention span of approximately 131,072 tokens!

Interactive SWA Receptive Field Simulator

Click any block to see propagation

Adjust the slider to change the local attention window size $W$. Hover or click on a token block in any layer to see which tokens it can recursively "see" from the input layer.

Formula: $\text{Span} = L \times W$
Current theoretical span: 9 tokens.
Layer 3
Layer 2
Layer 1
Input

3. Rolling Buffer Cache

Because SWA only requires access to the last $W$ tokens, Mistral introduces a Rolling Buffer Cache. The cache has a fixed capacity of size $W$. When generating token $i$, its key and value vectors are stored at position $i \pmod W$ in the cache.

Once the sequence length exceeds $W$, new keys and values overwrite older ones. For a sequence length of 32k tokens, this reduces KV cache memory consumption by up to 8x without degrading the model's generation quality.

Rolling Buffer Cache Simulator

Simulate token generation with a fixed-size Rolling Buffer Cache ($W = 4$). Watch how older tokens are overwritten as the index wraps around using $i \pmod W$.

Token Stream
Physical KV Cache Memory (Fixed Size = 4)
Current Generation Step ($i$): 0
Target Cache Slot ($i \pmod 4$): 0

4. Pre-fill and Chunking

When processing a prompt, the keys and values are known in advance. Mistral chunks long prompts into smaller pieces of size $W$. The model then computes attention over both the current chunk and the active cache. This prevents memory spikes during massive prompt pre-filling.

Results & Benchmarks

Mistral 7B was evaluated against Llama 1 & 2 models across a wide battery of tasks. Despite its smaller footprint, it consistently outperforms Llama 2 13B and matches or exceeds the mathematics and coding capabilities of Llama 1 34B.

Benchmark Performance Dashboard

Compare Mistral 7B against Llama models. Click the tabs to filter by specific benchmark domains. Notice how Mistral (orange) matches or beats much larger models.

The "Equivalent Size" Multiplier: In terms of cost-performance, Mistral 7B performs like a Llama 2 model with 3.3x its size on MMLU, and 5.4x its size on Reasoning tasks.
Model MMLU HellaSwag HumanEval GSM8K
LLaMA 2 7B 44.4% 77.1% 11.6% 16.0%
LLaMA 2 13B 55.6% 80.7% 18.9% 34.3%
Mistral 7B 60.1% 81.3% 30.5% 52.2%

Guardrails & Moderation

Mistral 7B - Instruct showcases impressive generalization when fine-tuned on public instruction datasets. To ensure safe deployment, developers can leverage a system prompt to enforce safety constraints.

Additionally, Mistral 7B can function as a highly precise content moderator. By utilizing a self-reflection prompt, the model can classify user queries or its own responses into safety categories (e.g., illegal activities, hateful content, unqualified advice) with 99.4% precision and 95.6% recall.

Limitations & Open Questions

  • World Knowledge Compression: While Mistral 7B excels at reasoning, math, and code, its performance on pure knowledge-based benchmarks (like TriviaQA) is capped by its absolute capacity (7 billion parameters). It achieves a lower compression rate of 1.9x on world knowledge compared to Llama 2.
  • Window Size Bounds: Although SWA theoretically scales context length recursively, fine-tuning or specialized attention masking is required to prevent degradation on extremely long, multi-turn contexts.

Glossary

KV Cache

Key-Value Cache. A technique used to speed up generation in autoregressive models. By storing the key and value representations of previous tokens, the model avoids recalculating them at every step.

Grouped-Query Attention (GQA)

An attention mechanism that groups multiple query heads to share a single key/value head pair. It balances the high speed of Multi-Query Attention with the high capacity of Multi-Head Attention.

Sliding Window Attention (SWA)

An attention pattern where each token only attends to a fixed number of tokens in the immediate past. Stacked layers allow information to travel further down the sequence.

Citation

@misc{jiang2023mistral,
      title={Mistral 7B}, 
      author={Albert Q. Jiang and Alexandre Sablayrolles and Arthur Mensch and Chris Bamford and Devendra Singh Chaplot and Diego de las Casas and Florian Bressand and Gianna Lengyel and Guillaume Lample and Lucile Saulnier and Lélio Renard Lavaud and Marie-Anne Lachaux and Pierre Stock and Teven Le Scao and Thibaut Lavril and Thomas Wang and Timothée Lacroix and William El Sayed},
      year={2023},
      eprint={2310.06825},
      archivePrefix={arXiv},
      primaryClass={cs.CL}
}
Made with Flash Papers — make your own