Sliding-Window Attention

Definition

Sliding-window (local) attention restricts each token to attend only to itself and the previous tokens, masking everything else before softmax. Popularized at scale by Mistral 7B (Jiang et al., 2023; arXiv:2310.06825), it drops the attention core from to — linear in sequence length — and bounds the decode-time KV cache at via a rolling buffer. Global reach is recovered through depth.

Key math / How it works

The causal window mask is

Allowed entries per row , so core FLOPs are instead of .

Effective receptive field via depth (the CNN analogy). Layer 1 lets token see back to ; those tokens already pulled from their windows, so after layers is influenced by tokens as far as . An -layer stack thus reaches

Mistral: , → tokens of theoretical reach, far beyond any single layer’s 4096 window.

Decode memory. Only the last tokens’ K/V are ever needed, so the cache is bounded at (Mistral’s fixed-size rolling buffer) rather than growing . Switching a 32k layer to : scores , and the cache smaller and constant as generation continues.

Why it matters

Sliding-window is the cheap workhorse long-context primitive: no new parameters, just a masked SDPA and a rolling KV buffer. Its failure mode is precise — dependencies farther than cannot be bridged — which is exactly why production “hybrid” stacks interleave a few full-attention (or global-token) layers among mostly-local ones for one-hop long-range reach. It underlies both training-time locality (Mistral) and inference-time streaming (see StreamingLLM).

Taught in

See also