Sliding Window Attention
Fixed-window local attention: derivation, effective receptive field across layers, dilated variants, global+local hybrid patterns (Longformer), and memory-complexity analysis for long-context models.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- The Local Window Formulation
- Effective Receptive Field
- Dilated Sliding Window
- Global + Local Hybrid (Longformer)
- Alternating Window Patterns (Mistral)
- Memory and Compute Analysis
- Comparison with Full Attention
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Derive the sliding window attention mask and its complexity.
- Prove the effective receptive field grows linearly with depth.
- Explain how dilated windows extend the receptive field without extra compute.
- Describe the Longformer hybrid pattern combining local, global, and sliding attention.
- Analyze when sliding window is preferable to full attention.
Notation
- — window size (number of tokens each position attends to)
- — number of layers
- — dilation factor
- — sequence length
- — sliding window mask
Core Intuition
Most information in natural language is local: a word's meaning depends primarily on its neighbors. Full attention is overkill for most positions — distant tokens contribute negligibly to the softmax distribution. Sliding window attention exploits this locality by restricting each token to attend only within a fixed window, achieving linear complexity in sequence length.
Sliding Window Attention
The Local Window Formulation
Each token at position attends to positions in the window :
where .
Mask matrix: The window mask is a banded matrix:
Applied as: .
For causal models: The window is one-sided: position attends to .
Effective Receptive Field
Theorem. After layers of sliding window attention with window , position can be influenced by any position in (causal case).
Proof. At layer 1, position sees positions . At layer 2, each of those positions has already incorporated information from their own window at layer 1. Position at layer 1 saw positions . Thus after 2 layers, position is influenced by . By induction, after layers: .
Example: Mistral with and : effective receptive field = tokens — sufficient for 128K contexts even with local attention.
Important caveat: While information can propagate positions, the signal attenuates exponentially with distance (each hop through a residual connection adds noise).
Dilated Sliding Window
Instead of attending to consecutive neighbors, skip every positions:
Receptive field per layer: positions (covers a wider range with the same number of attended tokens).
Multi-head dilation: Different heads use different dilation factors:
- Head 1: (local)
- Head 2: (skip one)
- Head 4: (skip three)
This gives each layer access to both fine-grained local and coarse-grained distant information.
Global + Local Hybrid (Longformer)
Architecture: Combine three attention patterns:
- Sliding window (all tokens): Local context, complexity.
- Global tokens (selected positions): Attend to/from all positions. Typically: [CLS] token, question tokens, or every -th position.
- Random attention (optional): Attend to a few random positions for diversity.
Formulation for token :
Complexity: — linear in if is fixed.
Use cases: Document classification (global = [CLS]), QA (global = question tokens).
Alternating Window Patterns (Mistral)
Mistral's approach: Use sliding window attention in every layer, but the effective context comes from stacking layers:
- Every layer: causal sliding window of size .
- No global tokens, no dilated attention.
- Effective context: .
KV-cache optimization: Only need to cache tokens per layer (older tokens are evicted). Fixed KV-cache size regardless of sequence length:
Comparison with Llama: Llama uses full attention (KV-cache grows with ). Mistral's sliding window gives constant memory but relies on information propagation through layers.
Memory and Compute Analysis
Standard attention: compute, memory.
Sliding window: compute, memory.
Speedup factor: . For : faster attention.
Implementation: Can be implemented as a block-sparse matrix multiply, or via FlashAttention with masking (FlashAttention-2 supports arbitrary causal masks including sliding window).
Comparison with Full Attention
- Quality at short context (): Identical (window covers everything).
- Quality at long context (): Sliding window loses direct access to distant tokens. Quality depends on task:
- Summarization, local understanding: minimal degradation.
- Fact retrieval from specific distant position: significant degradation.
- Reasoning chains: moderate (info propagates through layers).
Empirical finding: For most language modeling tasks, sliding window with and 32 layers achieves comparable perplexity to full attention, at dramatically lower cost.
Common Pitfalls
Pitfall 1. Setting window too small. With and : effective context is only tokens. For long-document tasks, this is insufficient.
Pitfall 2. Forgetting that information must traverse multiple layers. A token at distance influences position only through a chain of intermediate representations — the signal is heavily processed and potentially distorted.
Pitfall 3. Applying sliding window to cross-attention. In encoder-decoder models, the decoder should typically attend to the full encoder output, not a window (the "relevant" encoder positions aren't necessarily nearby).
Summary
- Sliding window restricts attention to nearest tokens: complexity.
- Effective receptive field = across layers.
- Dilated windows extend range without additional compute.
- Longformer adds global tokens for long-range connections.
- Mistral uses pure sliding window with layer stacking for 128K context.
- Fixed KV-cache size () regardless of sequence length.
Exercises
Exercise 1. For : compute the sliding window attention FLOPs and compare to full attention FLOPs.
Exercise 2. Prove that the effective receptive field after layers of dilated attention with dilation and window is .
Exercise 3. Design a hybrid attention pattern for a 32-layer model processing 100K tokens: specify which layers use sliding window, which use full attention, and justify your choices.
Exercise 4. Compute the maximum KV-cache memory for Mistral () in FP16, and compare to Llama at .
Exercise 5. Explain why simply increasing to equal doesn't recover full attention performance if the model was trained with small .