Sliding Window Attention › Dilated Sliding Windows
Language AI HandbookAll chapters Sliding Window Attention › Dilated Sliding WindowsOne section · Sections in this chapter5 / 12 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Standard sliding windows have a limitation: the receptive field grows slowly with window size. To see positions 100 tokens away, you need either a window of 200 tokens (expensive) or many layers for information to propagate (slow). Dilated sliding windows offer a third option: spread the attention across a wider range by skipping intermediate positions.
Dilated sliding window attention attends to positions at regular intervals (the dilation rate) rather than consecutive positions. This expands the effective receptive field while keeping the number of attended positions constant.
The intuition comes from signal processing and computer vision. A dilated convolution samples every th position instead of consecutive positions, covering more ground with the same number of parameters. Dilated attention applies the same principle: instead of attending to your 5 nearest neighbors, attend to 5 positions spaced 2 apart.
How Dilation Works
Let's formalize this idea. In standard attention, the attended positions form a consecutive sequence centered on the query. With dilation, we introduce gaps between attended positions.
With dilation rate and attended positions, token attends to the following set of positions:
where:
- : the query position (the token computing its attention)
- : the dilation rate (the gap between consecutive attended positions)
- : the number of positions to attend to (same as in non-dilated attention)
- : the number of positions on each side of the center
The dilation rate acts as a multiplier on the offset from the center position. With (no dilation), we get the standard consecutive window. With , we skip every other position, doubling the effective range while attending to the same number of positions.
For example, with and , token at position 10 attends to positions rather than the consecutive window . Both patterns attend to exactly 5 positions, but the dilated version covers a range of 8 positions instead of 4.


Multi-Scale Attention with Varying Dilation
A single dilation rate creates gaps in coverage: with , we miss all odd-positioned tokens. The solution is to use different dilation rates across attention heads. Some heads use for fine-grained local attention, others use for medium-range context, and still others use or for long-range dependencies.
This multi-scale approach, used in models like Longformer, creates a powerful combination. The union of all heads covers a broad range of positions, each head specializes in a particular scale, and the total computation remains sparse. Think of it like having multiple photographers at an event: one captures close-ups, another medium shots, and a third wide angles. Together, they document everything without any single photographer doing all the work.
def multi_scale_receptive_field(dilations, k):
"""
Calculate coverage of multi-scale dilated attention.
Args:
dilations: List of dilation rates for different heads
k: Number of positions each head attends to
Returns:
Total range covered by union of all heads
"""
all_positions = set()
center = 1000 # Arbitrary center position
half_k = k // 2
for d in dilations:
for offset in range(-half_k, half_k + 1):
all_positions.add(center + offset * d)
min_pos = min(all_positions)
max_pos = max(all_positions)
return max_pos - min_pos, len(all_positions)
# Example: 4 heads with different dilations
dilations = [1, 2, 4, 8]
k = 5
range_covered, unique_positions = multi_scale_receptive_field(dilations, k)Multi-scale dilated attention (k=5 positions per head): Dilation rates: [1, 2, 4, 8] Total range covered: 32 positions Unique positions attended: 11 Coverage density: 33.3%
The multi-scale approach extends the effective receptive field while maintaining sparse attention. With dilation rates of and 5 positions per head, the model covers a range of 32 positions with 11 unique attention connections, achieving both broad coverage and computational efficiency.
