Sliding Window Attention Patterns

Sliding Window Attention is a sparse attention pattern that restricts each token to attending only to nearby tokens within a fixed local window — reducing the computational complexity from O(n²) to O(n × w) where w is the window size (e.g., 512 or 4096 tokens), enabling processing of much longer sequences with bounded memory while capturing the local dependencies that dominate most natural language and code understanding tasks.

What Is Sliding Window Attention?

  • Definition: An attention pattern where each token at position i can only attend to tokens in the range [i-w, i] (for causal/autoregressive) or [i-w/2, i+w/2] (for bidirectional), where w is the window size. Tokens outside the window receive zero attention weight.
  • The Motivation: Full attention is O(n²) — for a 100K token sequence, that's 10 billion attention computations per layer. But most relevant context for any given token is nearby (within a few hundred to a few thousand tokens). Sliding window exploits this locality.
  • The Key Insight: Even with local-only attention, information can propagate across the full sequence through multiple layers. With window size w=4096 and L=32 layers, the effective receptive field is w × L = 131,072 tokens — covering the full context through cascading local interactions.

Complexity Comparison

Attention TypeMemoryComputeEffective Receptive Field
Full AttentionO(n²)O(n²)Full sequence (every token sees all others)
Sliding WindowO(n × w)O(n × w)w per layer, w × L across L layers
Global + SlidingO(n × (w + g))O(n × (w + g))Full (via global tokens)

For n=100K, w=4096: Full attention = 10B operations; Sliding window = 410M operations (24× less).

How It Works

PositionAttends To (w=4, causal)Cannot See
Token 1[1]—
Token 2[1, 2]—
Token 3[1, 2, 3]—
Token 5[2, 3, 4, 5]Token 1 (outside window)
Token 10[7, 8, 9, 10]Tokens 1-6
Token 1000[997, 998, 999, 1000]Tokens 1-996

Combining Sliding Windows with Other Patterns

CombinationHow It WorksUsed In
Sliding + Global tokensSpecial tokens (CLS, task tokens) attend to ALL positionsLongformer, BigBird
Sliding + DilatedAdditional attention to every k-th token for long-rangeLongformer (upper layers)
Sliding + RandomRandom attention connections for probabilistic global coverageBigBird
Different window sizes per layerLower layers: small window (local); Upper layers: large window (broader)Many efficient transformers
Sliding + Full attention layersEvery N-th layer uses full attentionMistral design choice

Models Using Sliding Window Attention

ModelWindow SizeApproachMax Context
Mistral 7B4,096Sliding window in every layer32K (via rolling KV-cache)
Longformer256-512Sliding + global + dilated16K
BigBird256-512Sliding + global + random4K-8K
Gemma-24,096 (alternating)Alternating sliding/full layers8K

Sliding Window Attention is the foundational sparse attention pattern for efficient transformers — exploiting the locality of language by restricting each token to attend only within a fixed neighborhood, reducing memory and compute from quadratic to linear in sequence length, while maintaining full-sequence information flow through multi-layer receptive field expansion and combination with global attention tokens.

Take sliding window attention patterns further

Ask the copilot about this term, or have our engineers assess it against your process.