Mistral Architecture › Inference Efficiency

Michael BrenndoerferUpdated August 6, 20254 min read
Language AI HandbookAll chapters
Part 29 of 67Modern Decoder Models
Chapter 211Mistral Architecture › Inference Efficiency
Reading section 10 of 14 · Inference Efficiency · 4 minSection 10 of 14 · 4 min

Book contents

Section 10 of 14 · 4 min read

Mistral Architecture › Inference EfficiencyOne section · Sections in this chapter10 / 14 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The practical impact of Mistral's design becomes most apparent during inference. Let's quantify the throughput improvements:

In[34]:
Code
def estimate_inference_memory(batch_size, seq_len, config, dtype_bytes=2):
    """
    Estimate peak memory during inference.

    Includes: model weights, KV cache, activations
    """
    hidden = config["hidden_size"]
    layers = config["num_layers"]
    heads = config["num_attention_heads"]
    kv_heads = config["num_kv_heads"]
    intermediate = config["intermediate_size"]
    vocab = config["vocab_size"]
    window = config.get("sliding_window", seq_len)
    head_dim = hidden // heads

    # Model weights (approximate)
    embed_params = vocab * hidden
    attn_params = layers * (hidden * hidden * 4)  # Q, K, V, O projections
    ffn_params = layers * (hidden * intermediate * 3)  # up, gate, down
    total_params = embed_params + attn_params + ffn_params
    weights_bytes = total_params * dtype_bytes

    # KV cache
    cache_len = min(seq_len, window) if window else seq_len
    kv_cache_bytes = (
        2 * layers * cache_len * kv_heads * head_dim * dtype_bytes * batch_size
    )

    # Activations (current layer only)
    activation_bytes = (
        batch_size * seq_len * hidden * dtype_bytes * 4
    )  # Rough estimate

    return {
        "weights_gb": weights_bytes / (1024**3),
        "kv_cache_gb": kv_cache_bytes / (1024**3),
        "activations_gb": activation_bytes / (1024**3),
        "total_gb": (weights_bytes + kv_cache_bytes + activation_bytes)
        / (1024**3),
    }


# Compare at different sequence lengths
seq_lengths = [1024, 4096, 8192, 16384]
batch_size = 1
Out[35]:
Console
Inference Memory Comparison (batch_size=1, fp16):

                               LLaMA 2 7B                     Mistral 7B          
  Seq Length     KV Cache        Total     KV Cache        Total
----------------------------------------------------------------------------
       1,024       0.50 GB      12.84 GB       0.12 GB      14.90 GB
       4,096       2.00 GB      14.43 GB       0.50 GB      15.37 GB
       8,192       4.00 GB      16.56 GB       0.50 GB      15.49 GB
      16,384       8.00 GB      20.81 GB       0.50 GB      15.74 GB

The comparison reveals Mistral's memory efficiency advantage. At 4K tokens, Mistral's KV cache is already 4x smaller due to GQA alone. At 8K tokens and beyond, the sliding window provides additional savings by capping cache growth. The total memory difference becomes substantial for long-context applications.

At 8K tokens, Mistral's KV cache is 8x smaller than LLaMA 2's would be (due to 4x from GQA and 2x from the context being capped at window size). This translates directly to higher throughput, as the memory bandwidth required to read the KV cache during each generation step is proportionally reduced.

Out[36]:
Visualization
Stacked bar chart showing LLaMA 2 memory breakdown with large KV cache portion.
LLaMA 2 7B inference memory breakdown at 8K context. KV cache dominates at longer sequences due to full attention and per-head storage.
Stacked bar chart showing Mistral memory breakdown with much smaller KV cache.
Mistral 7B inference memory breakdown at 8K context. GQA and rolling buffer reduce KV cache by 8x, shifting the memory profile.

The memory breakdown visualizations show the impact of Mistral's design choices. While model weights are similar between the two architectures, the KV cache differs by nearly an order of magnitude. For LLaMA 2, the KV cache grows to dominate memory usage at long sequences. Mistral's combination of GQA (4x reduction) and rolling buffer (2x reduction at 8K context) keeps the KV cache manageable, freeing memory for larger batch sizes or longer generations.