Skip to content

How does sliding window attention (SWA) reduce memory usage in long-context models?

Intermediate · 2 min read · LLM Fundamentals

Open in study mode to save this question and track progress

Short answer: Sliding Window Attention (SWA) restricts each token's attention scope to a fixed local window size WW (e.g. 4096 tokens), reducing KV cache memory and computation complexity from quadratic O(N2)O(N^2) to linear O(N⋅W)O(N \cdot W), while enabling information to propagate across LL layers up to a theoretical receptive field of L×WL \times W.

Detail

Standard self-attention forces every token to attend to all prior tokens in the sequence length NN.

Full Attention (N=16K):    Token 16,000 attends to ALL 16,000 previous tokens. (Heavy Memory!)
Sliding Window (W=4K):     Token 16,000 attends ONLY to window [12,000 ... 16,000]. (Linear Memory!)

Receptive Field Propagation Across Layers

Even though layer 1 has a restricted window WW, stacking LL layers expands the effective receptive field:

Receptive Field=L×W\text{Receptive Field} = L \times W

A 32-layer model with a sliding window W=4096W = 4096 achieves an effective receptive field of 32×4096=131,07232 \times 4096 = 131,072 tokens without ever allocating an N×NN \times N attention matrix.

MechanismAttention MemoryKV Cache GrowthMax Theoretical Receptive Field
Full AttentionO(N2)O(N^2)O(N)O(N)NN
Sliding Window Attention (SWA)O(N⋅W)O(N \cdot W)O(W)O(W)L×WL \times W

Example

PyTorch concept for Sliding Window Attention mask generation:

python
import torch

def create_sliding_window_mask(seq_len: int, window_size: int = 4) -> torch.Tensor:
    # Create causal mask
    causal_mask = torch.tril(torch.ones(seq_len, seq_len))
    # Create sliding window mask (zero out tokens beyond window W)
    window_mask = torch.triu(causal_mask, diagonal=-window_size + 1)
    # Positions outside window set to -infinity
    mask = torch.full((seq_len, seq_len), float('-inf'))
    mask[window_mask == 1] = 0.0
    return mask

print("Sliding Window Mask (0.0 = Attend, -inf = Blocked):\n", create_sliding_window_mask(6, window_size=3))

Interview tips

  • Discuss Mistral 7B as a prime example of Sliding Window Attention architecture.
  • Highlight rolling buffer KV cache implementations that overwrite old tokens outside window WW to keep KV VRAM footprint constant.

Related Concepts


⬅ Back to LLM Fundamentals · All topics