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 progressShort answer: Sliding Window Attention (SWA) restricts each token's attention scope to a fixed local window size (e.g. 4096 tokens), reducing KV cache memory and computation complexity from quadratic to linear , while enabling information to propagate across layers up to a theoretical receptive field of .
Detail
Standard self-attention forces every token to attend to all prior tokens in the sequence length .
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 , stacking layers expands the effective receptive field:
A 32-layer model with a sliding window achieves an effective receptive field of tokens without ever allocating an attention matrix.
| Mechanism | Attention Memory | KV Cache Growth | Max Theoretical Receptive Field |
|---|---|---|---|
| Full Attention | |||
| Sliding Window Attention (SWA) |
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 to keep KV VRAM footprint constant.
Related Concepts
- [[What is prefix caching (prompt caching) and how does it eliminate redundant KV computation?]] (
#152): What is prefix caching (prompt caching) and how does it eliminate redundant KV computation? - [[How does Chunked Prefill prevent decoding latency spikes during concurrent batch processing?]] (
#156): How does Chunked Prefill prevent decoding latency spikes during concurrent batch processing? - [[How does contextual compression reduce context window token usage during RAG?]] (
#128): How does contextual compression reduce context window token usage during RAG?