Rúben GalvãoThe Rabbit Hole

The Rabbit Hole / AI and machine learning / Language models / Working with longer context

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

The cache grows with every tokenThree shelves of cache entries: a short chat with five entries, a longer chat with twelve, and a long chat with twenty-five. A label under the longest shelf gives the selected model's maximum context in tokens.Short chata small cacheLonger chata bigger cacheLong chata huge cache...up to the model's maximum context
What to notice: The entries can be made smaller, but every new token still adds one. The shelf only gets longer, up to the longest context the selected model supports.

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.

What a window keeps

Interactive

Green: the entries the current token keeps. Blue: the window each earlier token kept.

A toy sequence of 24 tokens.

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.

What the last token is built from

Interactive

A toy stack of 16 tokens and five layers.

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.

What to notice: Only the green layers read the whole context. Every blue layer keeps just its window. This is the pattern of the model chosen at the top.

Only the green layers need a cache that grows. Drag the context and see what that saves for this model.

A cache that stops growing

Interactive

every layer global this model

If every layer were global
This model
Share of all-global

From the selected model's own shape, at 16 bits per number.

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.

How far a layer reaches against what its cache costsA chart with cache cost along the bottom and reach along the side. A sliding window sits at low cost and short reach. Full attention sits at high cost and long reach. The corner with long reach and low cost is empty, marked with a question mark.reach backcache cost as the chat growsslidingwindowfullattention?
What to notice: A window is cheap but short-sighted. Full attention sees everything but pays for it. The empty corner is the one we want.

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.

← Browse all subjectsBack to the beginning ↑