Home Knowledge Base Sliding Window Attention

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?

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.

sliding window attention patternsllm architecture

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.