What is sliding window attention?
Sliding window attention is an attention layout in which each token attends only to tokens within a fixed-size window around its position (before it, in decoders) rather than the whole sequence, so attention cost grows linearly with sequence length.
Sparse attention is the general name for approaches skipping part of the attention matrix; sliding window attention is a specific form based on a local window. Some models combine it with global attention, where a few tokens see the whole sequence.
- Topic
- Machine learning, models and training
- Also known as
- Local attention, SWA
How sliding window attention works
- A window size is chosen; each token computes attention scores only with tokens inside it.
- Full attention cost grows with the square of sequence length, whereas here it grows with sequence length times window size.
- As layers stack, information moves layer by layer, so upper layers can be influenced indirectly by more distant tokens.
- In decoding only a window's worth of keys and values needs storing, so the cache can stay bounded.
In more detail
Effective receptive field across layers
Across stacked layers each layer can carry information one window further, so the theoretical effective receptive field is the layer count times the window size. In practice the signal weakens across hops, so distant dependencies fade faster than in full attention.
The rolling KV cache and memory in hybrid models
With pure causal sliding window attention the KV cache can live in a fixed-size rolling buffer the length of the window instead of growing with the sequence. In hybrid models that interleave local and global layers, only the local layers get this bounded memory; global layers still need a cache that grows with length.
Commonly confused terms
| Term | How it differs |
|---|---|
| Dilated attention | Sliding window attention is a specific local sparse pattern where each token attends to a fixed nearby range; dilated (strided) attention skips tokens at regular intervals to span a wider range with the same compute budget. |
| FlashAttention | FlashAttention computes exact full dense attention without dropping any tokens, reducing memory from quadratic to linear through GPU SRAM tiling; sliding window attention is a structural pattern that restricts which tokens attend to each other. |
| Block-sparse attention | Chunked or block-sparse attention divides sequences into fixed blocks and selects relevant blocks across the context, whereas sliding window attention strictly keeps a contiguous local band around each position. |
Why sliding window attention matters for on-premises AI
Sliding window attention lets long documents be processed with less GPU memory and compute, a direct capacity gain for in-house systems handling contracts, regulations or technical documents. It does not make context unlimited: access beyond the window is indirect, so distant links can weaken. Models are therefore tested on their own data for tasks such as linking a definition at a document's start to a clause at its end.
Choosing a model to review long contracts in-house, a law firm tries one with sliding window attention, measuring long-range performance with test questions linking clauses spread across a document.
Common questions
Does sliding window attention make the context window unlimited?
No. It localises computation and lowers cost; information beyond the window is reached only indirectly through layers.
How does sliding window attention differ from sparse attention?
Sparse attention is the general approach of skipping part of the attention matrix. Sliding window attention is a specific form based on a local window.
Related terms
- Sparse attention
- Attention mechanism
- Long-context model
- Self-attention
- Context window
- Key-value cache
- Attention sink
Sources
- Beltagy, Peters and Cohan, Longformer: The Long-Document Transformer, 2020
- Child et al., Generating Long Sequences with Sparse Transformers, 2019
In the glossary: Glossary, R to Z