Sliding Window Attention: Extending Context Efficiently

Sliding Window Attention (SWA), a core component in Mistral models, tackles the quadratic computational and memory costs of traditional attention by restricting each token's attention to a fixed-size local window. This reduces complexity from O(n²) to O(n*w), making long context windows practical. Mistral 7B, for instance, achieves a 2x speed improvement at 16K tokens with a 4K window. Gemma 3 and 4 further enhance this by using a hybrid approach, interleaving local SWA layers (e.g., 1024-token windows) with occasional global attention layers to effectively manage KV cache and support very long contexts up to 128K tokens.

Sources

Open the full topic