What sliding-window attention is
In full causal attention a token at position reads all tokens before it, so the work per token and the KV cache both grow with the length of the text. Sliding-window attention lets each query see only the last keys. The cost per token and the cache of such a layer stop growing at ; what lies further back has to arrive indirectly, through earlier layers or through layers that still attend to everything.
Gemma 3 mixes the two kinds. This page records Gemma 3 270M, which has 18 blocks: 15 local blocks with a window of 512 keys and 3 global blocks (6, 12 and 18) with ordinary causal attention. It shows block 5, local, beside block 6, global, on two public-domain passages longer than the window: Boscombe Valley (663 tokens) and Caucus-race (774 tokens). Ten query rows are drawn at their real positions in the passage, against every key.
The mask
The only change from causal attention is which keys a query may see. In a local block,
so a query sees itself and the 511 keys before it; in a global block the condition is . The Local mask and Global mask rulers draw, for each shown query, the stretch of the passage it sees and, as ticks, where its weight goes; the local one marks where the window starts. On the head card, keys outside the window are drawn as masked. Opening a row lists the shown keys and the 12 keys it weighs most, and how many of the passage's keys it can see.
Local and global blocks
A stack of local layers can still move information further than : each layer passes on what it has gathered, so the reach grows with depth, as a convolutional network's receptive field does (see the CNN tab). But the path is indirect and each hop dilutes it. Gemma 3 therefore keeps a global block after every five local ones, where any token can read any earlier token directly.
The Beyond the window bars measure the difference for the selected query: the share of its attention on keys 512 or more positions back, in each of the 18 blocks. It is exactly 0 in every local block. In the global blocks it can be most of the attention: in the Boscombe passage one head of block 6 puts more than 99 % of the last query's weight on keys at least 512 back.
Memory
A local block never needs more than its last 512 keys and values, however long the text; only the 3 global blocks keep the whole context. The Gemma 3 report says it increased the ratio of local to global layers, and kept the local span short, precisely to stop the KV cache exploding at long context. Mistral 7B, which uses a window in every layer, stores the window as a rolling buffer: position goes to slot , overwriting the key that just left the window.
Where it came from
Child et al. (2019) used local and strided patterns to make attention over long images, audio and text affordable. Longformer (2020) combined a sliding window with a few global tokens for long documents; BigBird added random blocks (the Sparse tab). Mistral 7B (2023) brought the window to a decoder language model, Gemma 2 (2024) alternated local and global layers one to one, and Gemma 3 (2025) moved to five to one. The Transformer Explainer's local + global tab shows a whole Gemma 3 block; the Sinks tab shows what a pure window loses when it evicts the first token.
Papers
- Longformer: The Long-Document Transformer Sliding-window attention with a few global tokens, for documents longer than full attention allows.
- Gemma 3 Technical Report The model family this page records: five local layers to each global one, QK-norm, and separate RoPE bases.
- Generating Long Sequences with Sparse Transformers Local and strided attention patterns that cut the cost of attention over long sequences.
- Mistral 7B Sliding-window attention in every layer of a decoder, with a rolling-buffer KV cache.
- Gemma 2: Improving Open Language Models at a Practical Size Alternates local sliding-window and global attention layers, the step before Gemma 3's ratio.
- Scaling Vision Transformers to 22 Billion Parameters Normalising queries and keys before the dot product to keep attention logits from growing, the QK-norm on this page.
- RoFormer: Enhanced Transformer with Rotary Position Embedding Rotary position embeddings, used here with a different base in local and global blocks.