Notes

Sliding Window Attention

Attention cost grows with the square of the sequence, because every token looks at every other one. Sliding window attention questions whether it needs to: most of what a token depends on is close by.

A fixed neighbourhood per token

Each token attends only to the last w tokens before it — 4096 in Mistral 7B, 512 in many Longformer configurations — so the cost per token stops growing with the document and the whole pass becomes linear in length. Reach is recovered by depth: a token sees its own window, each of those saw theirs, and after L layers information can travel roughly L × w positions, the same widening receptive field a stack of convolutions gives.

What changes in practice
  • The KV cache stops growing. Only the most recent w keys and values need keeping, so a long chat runs on a rolling buffer with a fixed memory bill instead of one that creeps up until the GPU is full.
  • Anything further back than that L × w horizon is unreachable: a name introduced on page one cannot influence page four hundred.
  • So real designs interleave — a few global tokens in Longformer, alternating local and full-attention layers in Gemma — keeping cheap locality for most layers and long reach in a handful.
Why it matters

Much of what is sold as a long context window rests on this one trick, and the trade is worth understanding before trusting it: a model advertising 128k tokens of local attention is not promising that token 1 and token 120,000 can interact. Implementations live in vLLM and llama.cpp, where the fixed-size cache is also what makes concurrent long sessions affordable to serve.

Sliding window attention restricts each token to attending over a fixed number of preceding tokens rather than the entire sequence, making the cost linear in length instead of quadratic. Longer-range information still propagates, because each layer shifts the window forward and depth compounds its reach. It also caps the KV cache at a constant size, which is what lets a long conversation be served without memory growing without bound — at the price of a hard horizon beyond which tokens cannot interact.

When you read a sentence, you do not consciously compare every word with every other word on the page. You mostly hold the last few lines in mind, and the gist of earlier pages carries forward on its own.

Sliding window attention gives an AI language system the same habit. Instead of letting each word check against the entire text — which gets ruinously slow as the text grows — each word only looks back over a fixed stretch, say the last few thousand words. Because the system is built in many stacked layers, each passing what it noticed to the next, information still travels a long way. The trade is a limit: something mentioned very far back can slip out of reach entirely.