Sliding-window attention can't see beyond the window. Describe the trick that recovers older context.
Sliding-window attention (used in Mistral, Longformer, etc.) restricts each token to attending only to the previous W tokens. Naively this seems to throw away anything older than W positions back. Describe the architectural property that lets information from older tokens still reach the current position, and explain how the effective receptive field scales.
Layer stacking. Each layer adds W tokens of reach; after L layers the effective receptive field is roughly W * L. Mistral-7B's 4k window over 32 layers gives ~131k token reach.
Picture a line of telephone-game players where each player can only whisper to the four people immediately in front of them. A message from the back of the line cannot reach the front in one hop, but as the line passes the message forward four people at a time, it eventually arrives at the front. The total distance the message can travel is four people per hop times however many hops there are. Sliding-window attention works the same way: each layer is one hop, the window size is how many tokens you can reach per hop, and the depth of the transformer is how many hops the message gets.
Detailed answer & concept explanation~6 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
Explain that per-layer window W combines with depth L through the residual stream to give a receptive field of roughly W * L, run the Mistral 7B numbers (4096 * 32 = 131k), note the bandwidth-bottleneck caveat, and contrast with full attention and hybrid long-context architectures.
| Attention pattern | Per-layer cost | Per-layer reach | Effective receptive field |
|---|---|---|---|
| Full attention | O(T^2 * d_k) | T (everything) | T, direct in one hop |
| Sliding window | O(W * T * d_k) | W tokens back | ≈ W * L via depth-hops |
| SSM / Mamba | O(T * d_state) | Recurrent, all of history compressed | Bounded by d_state bandwidth |
| Hybrid (sliding + periodic full) | Mostly O(W * T) + sparse O(T^2) | W per layer, plus full-attention layers | ≈ W * L with one-hop long edges at full-attn layers |
Real products, models, and research that use this idea.
- Mistral 7B: W = 4096, L = 32, theoretical receptive field ≈ 131k tokens despite nominal 8k pretraining context.
- Longformer (Beltagy et al. 2020): pioneered sliding-window attention with W = 512 plus global tokens for document-level tasks.
- Gemini 3.1 Pro: uses a hybrid attention stack with periodic full-attention layers interleaved with sliding-window layers to balance cost and long-range routing.
- Jamba (AI21 2024): alternates Mamba SSM layers with sliding-window attention layers, exploiting both depth-hops and recurrent state for million-token contexts.
- Llama 3 and Llama 4: deliberately avoid sliding-window in favor of full attention with longer pretraining contexts, trading compute for direct long-range routing.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy is the effective receptive field of a sliding-window model smaller than the theoretical W * L?
QHow does the residual stream's add-not-replace structure interact with sliding-window attention?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Assuming sliding-window models cannot see past W tokens. The per-layer window is W; the cross-layer receptive field is L * W via stacking.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.