Glossary · Position and context

Sliding-window attention

Also: local attentionSWAchunked attentioninterleaved attention

Read it at the depth you need: one sentence · a paragraph · the math · in the models here

1In one sentence

Attention limited to the last W tokens on some or all layers, which caps those layers' KV cache and attention cost at W however long the context gets.

2In a paragraph

Full attention looks at every earlier token, so its cost and its cache grow with the context. A sliding window looks only at the most recent W tokens. Most models that use it mix the two: Gemma 3 has five windowed layers for each full one, gpt-oss alternates them, so long-range information still flows through the global layers. Llama 4 does something similar with fixed chunks: a token sees only earlier tokens in its own 8K chunk.

3The math, shapes and parameters

With Ls windowed layers of window W and L − Ls global ones, the KV cache for T tokens is

per-token size × [ (L − Ls) × T + Ls × min(T, W) ]

The saving only happens if the runtime evicts old entries for the windowed layers; the GPU-fit pages on this site do not assume it does. The weights are the same either way: a window changes which scores are computed, not the matrices.

4In the 96 models on this site

Models here with windowed or chunked layers, and their KV cache at full context with and without the window:

ModelWindowed layersWindowKindContextKV, fullKV, windowed
Gemma 3 270M15 of 18512sliding32K604 MB109 MB
Gemma 3 1B22 of 26512sliding32K872 MB146 MB
Gemma 2 2B13 of 264,096sliding8K872 MB654 MB
Phi-3 Mini32 of 322,047sliding4K1.6 GB805 MB
Gemma 3 4B29 of 341,024sliding128K18.3 GB2.8 GB
OLMo 3 7B24 of 324,096sliding64K34.4 GB10.2 GB
Gemma 2 9B21 of 424,096sliding8K2.8 GB2.1 GB
Gemma 3 12B40 of 481,024sliding128K51.5 GB8.9 GB
gpt-oss-20b12 of 24128sliding128K6.4 GB3.2 GB
Gemma 4 26B-A4B25 of 301,024sliding256K59.1 GB5.6 GB
Gemma 3 27B52 of 621,024sliding128K66.6 GB11.2 GB
Gemma 2 27B23 of 464,096sliding8K3.1 GB2.3 GB
Gemma 4 31B50 of 601,024sliding256K236 GB22.3 GB
OLMo 3 32B48 of 644,096sliding64K17.2 GB5.1 GB
Llama 4 Scout (17B-16E)36 of 488,192chunked256K51.5 GB14.1 GB
gpt-oss-120b18 of 36128sliding128K9.7 GB4.8 GB
Inkling-Small35 of 42512sliding1024K180 GB30.1 GB
Llama 4 Maverick (17B-128E)36 of 488,192chunked256K51.5 GB14.1 GB
Inkling55 of 66512sliding1024K520 GB47.5 GB

Introduced in Beltagy et al., “Longformer: The Long-Document Transformer” (2020).

Related

KV cache · Self-attention · Attention sinks · RoPE (rotary position embedding) · every term