Local and global attention
In full causal attention every token reads every earlier token, so the work per token and the KV cache both grow with the length of the text. Local (sliding-window) attention lets a token read only the last tokens: its cost per token stops growing, and older keys can be dropped from the cache. Most of what a token needs is nearby, but not all of it, so models that use local attention keep a few global layers that still read everything.
This page records Gemma 3 270M, whose 18 blocks are 15 local blocks with a 512-token window and 3 global blocks (6, 12 and 18), on two public-domain passages longer than the window: 663 tokens of Conan Doyle's The Boscombe Valley Mystery and 774 tokens of Carroll's A Caucus-Race and a Long Tale. The page opens on global block 6.
The sliding-window mask
A local block adds a second condition to the causal mask: query may read key only if is at most 511 positions back, so it sees 512 keys including itself. With queries and keys normalised and rotated as described below,
In the head card the masked cells are slashed, and the Local Self Attention title notes which keys the last token sees, for example 151 to 662 for the last token of the Boscombe passage. The All queries · binned maps draw a local block's attention as a 512-token band along the diagonal next to a global block's full triangle.
What the global layers are for
The Boscombe passage ends in the middle of a name, Bos, and the page's question is how the model completes it. The name appears in capitals in the chapter title (from position 4) and as Boscombe at positions 68 and 69, all more than 590 tokens earlier and outside the window. The Every block · what “Bos” at 662 reads panel draws the last token's exact attention in every block: in local blocks every key before 151 is hatched, and only the global blocks can read combe at 69 directly. The model gives combe 99.0 %.
Local blocks can still pass information along indirectly. Each one lets a token read states that have already read up to 511 positions further back, so stacked local blocks reach farther with every layer. A global block reads the distant token in one step. On the Caucus-race passage the same question is the hyphen of Caucus-Race, first seen at position 6, and the model gives - 48.9 %.
Positions, norms and one key head
Local and global blocks rotate queries and keys with different RoPE bases: 10,000 in local blocks and 1,000,000 in global ones, where slow rotation keeps distant positions distinguishable. Before RoPE, each query and key head goes through an RMSNorm, QK-norm, which bounds the size of the dot products and replaces Gemma 2's soft-capping of attention logits. Gemma's RMSNorm scales by , and every sublayer has a norm before it and another on its output.
Gemma 3 270M uses multi-query attention: its 4 query heads, 256 dimensions each, share a single key and value head. The token embedding is multiplied by , the MLP is a GeGLU (640 → 2 × 2048 → 640, tanh-approximated GELU), and the output scores the 262,144 vocabulary entries with the embedding matrix itself.
The KV cache
Each block caches one key and one value of 256 numbers per token, in bfloat16:
The KV cache card adds this up over the 18 blocks. After the 663 Boscombe tokens the cache holds 9,654 token entries, 9.4 MiB, against 11,934 entries and 11.7 MiB if every block were global, 19 % less. At the model's longest context, 32,768 tokens, the same arithmetic on the configuration gives 103.5 MiB against 576.0 MiB, 82 % less. Multi-query attention has already cut the cache four times compared with one key and value head per query head.
Where local attention came from
Child et al. (2019) factorised attention into fixed sparse patterns, among them local windows, to model long sequences. Longformer (Beltagy et al., 2020) combined sliding-window attention with global attention on chosen tokens, and Mistral 7B (2023) used a sliding window in every layer with a rolling cache. Gemma 2 (2024) alternated local and global layers one to one. The Gemma 3 report raised the ratio to five local layers per global one and kept local spans short, to stop the cache from growing with long contexts; it also replaced logit soft-capping with QK-norm.
The SF tab shows gpt-oss, which alternates 128-token sliding blocks with full blocks one to one and adds learned attention sinks. The Encoder tab shows ModernBERT, a bidirectional encoder with a global block every third layer.
Papers
- Gemma 3 Technical Report The model family this page records: five local sliding-window layers per global layer, separate RoPE bases and QK-norm.
- Gemma 2: Improving Open Language Models at a Practical Size Interleaves local and global attention one to one and caps attention logits with a soft cap, which Gemma 3 replaces.
- Longformer: The Long-Document Transformer Sliding-window attention combined with global attention, so the cost grows linearly with the length.
- Generating Long Sequences with Sparse Transformers Factorised sparse attention patterns, including local windows, for long sequences.
- Mistral 7B Sliding-window attention in every layer with a rolling-buffer cache, relying on stacked layers to reach farther back.
- Fast Transformer Decoding: One Write-Head is All You Need Multi-query attention, one key and value head shared by all query heads, as in Gemma 3 270M.
- Query-Key Normalization for Transformers Normalises queries and keys before their dot product, the QK-norm Gemma 3 applies in every block.