Sliding Window Attention: Definition and Examples
Attention mechanism that restricts computation to a local window of adjacent tokens, reducing computational complexity while preserving the model's ability to process long sequences.
Full definition
Sliding Window Attention is an optimized variant of the Transformer attention mechanism. Unlike full attention, where each token interacts with every other token in the sequence, Sliding Window Attention restricts each token's attention field to a fixed window of immediate neighbors. For example, with a window size of 4096, each token only "looks at" the previous 4096 tokens.
This approach solves a fundamental problem of Transformers: the quadratic complexity of full attention. When a model needs to process a sequence of 100,000 tokens, computing attention between all token pairs becomes extremely expensive in memory and computation. Sliding Window Attention reduces this to linear complexity with respect to sequence length, making it possible to handle very long contexts.
A subtle but essential aspect is the propagation effect across layers. Even though each layer only sees a local window, stacking multiple layers allows information to propagate over much longer distances. With L layers and a window size of W, information can theoretically traverse L × W tokens, providing an effective range far larger than the individual window size.
This mechanism was popularized by models like Mistral 7B and Mixtral, and is a key component of the Longformer architecture. It is often combined with other techniques such as global attention on special tokens or Grouped Query Attention (GQA) to achieve an optimal balance between performance and efficiency.
Etymology
The term combines "sliding window," a concept borrowed from signal processing and computer networks where a fixed-size window moves sequentially over data, and "attention," the central mechanism of Transformer architectures introduced in the paper "Attention Is All You Need" (2017).
Concrete examples
Long document processing
You are an assistant analyzing legal documents over 50 pages long. Thanks to sliding window attention, you can process the entire document. Summarize the main clauses of the following contract, identifying each party's obligations.
Model selection for an application
I need to choose an LLM for an application that summarizes entire books. Compare the approaches of models using sliding window attention (like Mistral) versus full attention for my use case, in terms of quality and cost.
Inference optimization
Explain how to configure the sliding window attention size in a Mistral model to optimize the quality/speed trade-off when processing long conversations with a 30,000-token history.
Practical usage
In prompt engineering, understanding sliding window attention helps structure prompts for models that use it: the most critical information should be placed near the question or final instruction, as local attention is strongest there. For tasks involving very long documents, it is better to break the content into coherent sections rather than relying on cross-layer propagation to connect distant information.
Related concepts
FAQ
Does sliding window attention lose information compared to full attention?
What is the difference between sliding window attention and sparse attention?
How does window size affect model performance?
See also
How to use this prompt
- Copy the prompt with the button above.
- Paste it into ChatGPT, Claude or your favorite AI assistant.
- Replace the bracketed variables with your details, then refine the result.
About Prompt Guide
Prompt Guide is a free library of 2500+ ready-to-use prompts for ChatGPT, Claude and other AIs, with guides to learn prompting and tools to build and optimize your own prompts.
More definitions
Small Language Model: Definition and Examples
A Small Language Model (SLM) is a compact language model, typically with fewer than 10 billion parameters, designed to deliver targeted performance
Socratic Prompting: Definition and Examples
A prompt engineering technique inspired by the Socratic method, which consists of guiding an AI model toward an in-depth response by asking a series of questions.
Soft Prompting: Definition and Examples
Soft prompting is a language model adaptation technique that involves adding learnable numerical vectors (called "soft prompts") as input to the model, rather than using natural language words.
Speech To Text: Definition and Examples
Speech To Text (STT), or voice recognition, is an artificial intelligence technology that converts human speech into written text, allowing
Stability AI: Definition and Examples
Stability AI is a company specializing in generative artificial intelligence, best known for developing Stable Diffusion, an open-source model for generating images from text descriptions.
Step Back Prompting: Definition and Examples
Prompt engineering technique that involves first asking a more general or abstract question before addressing the specific question, allowing the model to
Get new prompts every week
Join our newsletter.