Sliding-window attention (SWA): a cache that stops growing
A conversation never stops growing, and neither does its cache. Does every token need to see all of it?
A new token stands at the end of a long shelf. Every entry it has read is still there, and each token adds one more.
The shelf never gets shorter.
The shelf only grows
We have made each entry smaller by sharing heads and compressing them. The shelf still grows by one entry per token.
On our reference model (see the KV cache) that is about 43 GB at 128K tokens, and it keeps climbing.
Does it need the whole shelf?
Most of what a word needs is close by. So what if the new token looked only at the last few entries?
Keep a window of recent entries, and compare it with ordinary full attention, which reads everything. Move the current token and watch which entries each one can reach.
This is sliding-window attention. The window slides forward with every new token, so it always covers the most recent entries.
A layer that works this way is also called a local layer. A layer with full attention is global.
Some models cut the text into fixed blocks instead, and let each token read only the earlier entries of its own block. This chunked attention starts afresh at every block boundary rather than moving with the token.
An entry that falls out of the window is never read again by a local layer, so it can be thrown away. The shelf stops growing.
But the beginning is gone
A token that cannot see the beginning can never use it. Or can it?
Layer 2 reads the outputs of layer 1, and each of those has already read its own window. Step through the layers below. Each highlighted state is something the last token’s state was built from.
Information hops back one window per layer. Across 32 layers of 4,096, that reaches about 131K positions.
That is a bound on where information can travel. It does not promise the detail arrives intact.
Give a few layers the long view
Hopping is lossy. So let a few layers read everything, and keep the rest local. Here is how the model chosen at the top does it.
Only the green layers need a cache that grows. Drag the context and see what that saves for this model.
Reach far, pay little?
A sliding window keeps the cache small, but each layer can only reach back one window.
Full attention reaches everything and pays for it.
Is there a layer that reaches far and still pays little?
A window always reads the recent entries, whatever the question. What if a layer chose what to read for each question, wherever it sits on the shelf? That is sparse attention with a lightning indexer.
Sources and assumptions
Longformer put local windows into attention in 2020, and Mistral 7B showed them with a rolling cache in a language model.
- Mistral 7B, for its window, rolling cache and theoretical receptive field.
- Gemma 2 and Gemma 3, for local/global schedules and window ablations.
- gpt-oss config, for alternating layer types and the 128 window.
- Sparse Transformer and Longformer, for earlier sparse masks. Child and colleagues’ Sparse Transformer (2019) used fixed sparse patterns; Longformer is by Beltagy, Peters and Cohan.
The model picker at the top lists only models with sliding-window layers. Cache sizes use each model’s own layers, key/value heads, head sizes and window, at 16 bits per number, in decimal GB. Window conventions differ over whether the window includes the current position. Global caches still grow in hybrid models; rolling local-cache eviction is an implementation requirement.