EasyPlusGemma 3

Global vs Local Layer Routing

Gemma 3

Easy

Route layers to local or global attention


Independent study note. Written from the public paper and official code. This is not TensorTonic Plus and does not reproduce their exercises, starter code, or tests. For the official version, subscribe on TensorTonic.

Overview

Gemma 3 is a decoder-only stack, but the layers do not share one attention span. Most layers run sliding-window (local) attention; a minority run full-context (global) attention. The report’s reason is KV-cache memory at 128K context: only the global layers store a cache that grows with the full sequence. Local layers keep a short span (1024 tokens on the 4B–27B models). The chosen mix is five local layers for every global layer, and the first layer is local.

Gemma 2 already interleaved local and global attention, but at a 1:1 ratio and with a 4096-token local window. Gemma 3 tilts the ratio toward local and shortens the window. Section 5.2 reports that raising the local:global ratio to 5:1, and even 7:1 in an ablation, barely moves validation perplexity, while the KV-cache footprint drops sharply versus a global-only stack.

Routing is a static per-layer flag, not a learned router. Each layer is constructed as either AttentionType.LOCAL_SLIDING or AttentionType.GLOBAL and keeps that type for the life of the model.

How it works

The official pattern is a six-slot tile that starts with local:

P = (L,L,L,L,L,G).

make_attention_layers_types in gemma/gm/nn/_config.py repeats (P) to cover num_layers. If the depth is not a multiple of 6, it appends a prefix of (P):

\mathrm{type}(\ell) = P[\ell \bmod |P|]\quad\text{for }\ell = 0,\ldots,N-1,

with the remainder implemented as pattern[: N % 6] rather than a wrap that would insert a stray global. Released depths:

Model Layers (N \bmod 6) Extra slots
270M 18 0 none
1B 26 2 (L,L)
4B 34 4 (L,L,L,L)
12B 48 0 none
27B 62 2 (L,L)

So the paper’s “5:1, starting local” is the design target; the code’s tiling is exact only when (N) is a multiple of 6. The leftover layers are local, never an extra global.

That flag then selects two other hyperparameters in Transformer.setup (gemma/gm/nn/_transformer.py):

The 1B and 270M configs do not set global_scale_factor, so it stays at the default 1. The 1B model is also the one the report caps at 32K context rather than 128K.

The paper specifies the 5:1 ratio, the “first layer is local” rule, and the two RoPE bases. The exact tiling function, the leftover-prefix rule, per-size depths, and the global RoPE scale of 8 are from the official configs.

Official code

The same pattern is re-exported as gm.nn.config.GEMMA3_ATTENTION_PATTERN for custom stacks (docs/research.md).

Watch-outs

Sources