LLM Library
Inside the TransformerAnimated
Sliding Window and Local-Global Attention
Let most layers look only nearby to save memory.
The idea
A sliding window limits each token to the last few hundred or thousand tokens. Models such as Mistral and Gemma use local layers mostly, with an occasional global layer to carry information over long distances.
Watch it work
Press play, pause, or step through. Some animations have settings you can change.
- current token sees
- other tokens see
A local layer: tokens see only a nearby window (3). Cheap, and most layers are like this.
- query token:
- 1
- sees:
- 1
1/24
Rows are the token asking (query); columns are the tokens it may look at (keys).
Step by step
- 1Pick a window size
- 2Local layers see only the window
- 3Add a global layer every few layers
- 4The KV cache for local layers stays small
Where it is used
Mistral 7B, Gemma 2 and 3, gpt-oss.
Watch out for
Information from beyond the window must travel through the global layers.