Sliding Window Attention
Sliding Window Attention (SWA) is a sparse attention mechanism in which each token attends only to a fixed-size local neighborhood of preceding and following tokens, reducing the quadratic complexity of full self-attention to where is the window size. It matters because it makes Transformer-based language models tractable on sequences of tens of thousands to millions of tokens without sacrificing the representational power needed for long-document reasoning. The counterintuitive fact: despite each token seeing only neighbors locally, information propagates globally across the full sequence depth in layers — meaning a model with sufficient depth recovers full receptive field coverage with a fraction of the compute.
Full attention is a dense all-pairs comparison that scales catastrophically with sequence length; sliding window attention replaces this dense graph with a sparse banded graph where each token communicates only with its nearest neighbors per layer. The "aha moment" is recognizing that locality is sufficient per layer because stacking layers gives an effective receptive field of tokens on each side — exactly as convolutions build global context from local filters. This means the architectural choice is not "local vs. global" but rather "where should global mixing happen": deep in the stack through propagation, or immediately through explicit global tokens or cross-window connections.
The most widespread mistake is assuming that sliding window attention with window size limits the model's receptive field to tokens permanently. Practitioners read "each token attends to neighbors" and conclude the model cannot capture dependencies longer than tokens — and then cargo-cult excessively large window sizes, negating the efficiency gains. The correct mental model is that receptive field grows linearly with depth: after layers, token has transitively received information from tokens in . In Longformer (Beltagy et al., 2020), a 12-layer model with achieves a receptive field of 3,072 tokens on each side — covering most practical document lengths. The fix: reason about effective receptive field, not per-layer window size.
How It Works
Formal Setup
Let be the input sequence of tokens with embedding dimension . In standard scaled dot-product attention, the attention matrix is fully dense. Sliding window attention imposes a sparsity mask such that only entries within a band of half-width are retained. The masked attention score matrix is:
Explanation: each query only receives finite scores from keys within the window, and pre-softmax effectively zeros out all out-of-window attention weights. Here , are the query and key projections with , and is the key dimension. The output for token is then:
Explanation: the output at position is a locally-normalized weighted sum of value vectors restricted to the window, so attention weights are re-normalized within each window rather than globally.
Complexity Analysis
The total number of attended pairs is at most (compared to for dense attention), giving:
Explanation: for fixed window size , the compute scales linearly with sequence length, making million-token contexts computationally tractable on commodity hardware.
Effective Receptive Field
After attention layers each with window , the maximum token distance over which information can flow is bounded by:
Explanation: this is a worst-case lower bound on receptive field — in practice, residual connections and layer normalization can allow faster information mixing, but gives the strict graph-theoretic propagation depth assuming causal paths of length .
Dilated Sliding Window Attention
To expand the receptive field without increasing , one can apply a dilation factor so that the attended positions form a strided pattern:
Explanation: with dilation , each window covers a span of tokens while still attending to only positions, so the receptive field grows by a factor of at no additional compute cost — analogous to dilated/atrous convolutions in CNNs.
Iz Beltagy is a research scientist at the Allen Institute for AI (AI2), where his work focuses on making large language models efficient and applicable to scientific and long-form text. He led the development of Longformer (2020), which popularized sliding window attention combined with task-specific global tokens as a practical recipe for processing documents up to 4,096+ tokens with near-linear complexity. Surprisingly, Longformer's design drew direct inspiration from WaveNet's dilated convolutions — Beltagy's team explicitly framed the attention pattern as a "dilated sliding window" to build intuition for ML practitioners already familiar with convolutional architectures.
Ashish Vaswani was a senior researcher at Google Brain and is widely recognized as the primary architect of the original Transformer paper "Attention Is All You Need" (2017), which introduced scaled dot-product attention and multi-head attention as the foundation for modern sequence models. Although the original Transformer used full attention, Vaswani's clean formalization of query-key-value attention created the modular scaffolding that made sparse variants like sliding window attention straightforward to derive and implement. He later co-founded Essential AI after a period at Google, and notably the 2017 paper was written during an intensive sprint that Vaswani has described as one of the most collaborative experiences of his research career.
Intuition
Imagine you are a journalist trying to understand a very long document — say, a 500-page legal brief. You don't read the entire document before forming any opinion about page 200; instead, you read locally — the paragraph you're on, the previous few paragraphs, and the next few — and carry a running mental summary forward. After enough pages of this local reading, your understanding of page 200 is informed by everything that came before, but the mechanism was always local. Sliding window attention is exactly this: each token reads a small neighborhood at each layer, but through the residual stream, meaning accumulates and propagates so that deeper layers carry distant context already compressed into the representations of nearby tokens. The global picture emerges from stacked local views — the same principle behind how pixels in a deep CNN "see" the whole image even though each convolutional kernel is tiny.