TL;DR
Sparse attention is a version of attention in which each query looks at only some of the keys, not all of them. The subset can be fixed in advance, such as a window of nearby tokens, or chosen from the data, such as the keys with the highest scores. Because the number of query-key pairs drops, the work and memory needed grow much more slowly as the sequence gets longer.
Builds on: Attention Mechanism, Softmax · Leads to: Sliding Window Attention, Linear Attention
Where it's used
Long-document language models. Longformer (2020) was built for documents thousands of tokens long. Most of its layers let each token read a fixed window of neighbours, and a few tokens are given global attention so they can see the whole document. The paper's point is that a standard Transformer's cost rises with the square of the length, which makes book-length inputs impractical.
Native sparse attention in recent models. The 2025 Native Sparse Attention paper organises keys into blocks and, for each query, combines three paths: a coarse summary of distant blocks, a small set of selected blocks that score highly, and a sliding window of recent tokens. The selection is trained end to end, so the model learns which parts of the past to keep. The paper's aim is also to make the sparse pattern fast on GPU hardware, because scattered memory reads can cancel the savings.
Serving very long contexts. When a model answers from a context of tens of thousands of tokens, the key-value cache dominates memory. Sparse schemes that read only part of the cache for each new token reduce the work per generated word, which is one reason they appear in inference systems as well as in training.
How it works
Take a sentence of four tokens, with one query, one score per key, and a window that lets each token see itself and the token before it. We will use the simplest case, a score list for one query.
- Start with a query and four keys. The query is for the token "cat". The scores against the four keys are . Check: four scores, one per token in the sentence.
- Full attention turns every score into a weight. Exponentiate each score and divide by the total, which is the Softmax. The exponentials are , , , . Their total is . Check: the four weights must add up to 1, and they do once divided by the total.
- Sparse attention keeps only some keys. Suppose the rule is "keep the two highest-scoring keys". The highest scores are 2 and 1, so the query ignores the keys with scores 0 and . Check: two keys kept, two dropped.
- Renormalise over the kept keys. Softmax is applied only to the kept scores. The total becomes . Check: the new weights must again add up to 1.
- Read the output. The output is the weighted mix of the two kept value vectors, with weights and . Check: the two weights add to 1, and the dropped keys contribute nothing.
- Count the work. The full query reads 4 keys; the sparse query reads 2. Check: for a query over keys, the cost is proportional to the number of keys it reads.
A window rule works the same way, except the kept keys are picked by position instead of by score.
flowchart LR
Q["Query for one token"] --> P["Choose a subset of keys"]
K["All keys"] --> P
P --> S["Scores on kept keys only"]
S --> W["Softmax over kept keys"]
W --> O["Weighted sum of their values"]
The math
Let be the query for position , and the key and value for position , and the key dimension. Full attention computes, for each query,
Here is the Dot Product of the query and key, and the division by keeps the scores from growing with the dimension. The sum runs over all positions.
Sparse attention restricts each query to a set of positions , and computes the same softmax over only those positions:
The sets can be fixed by position, such as a window, or chosen from the scores, such as the top .
Equivalently, one can set the scores of positions outside to before the softmax, since . This is how most implementations are written, because it keeps the code a single masked softmax.
Step 2 in symbols
The full weights are . With scores the denominator is .
Step 3 and step 4 in symbols
For the top- rule with , the set is , the positions with scores 2 and 1. The denominator shrinks to .
Step 6 in symbols
For queries that each read at most keys, the number of score entries is at most , instead of for full attention. When is fixed while grows, the cost grows linearly in .
Worked examples
Use the scores for three keys, and keep only the top two.
- Exponentials: , , .
- Full denominator: .
- Full weights: , , .
- Top-two set: positions 1 and 3, because their scores are the two largest.
- Sparse denominator: .
- Sparse weights: and .
Interpretation: dropping the weakest key moves the weights of the kept keys up, to 0.731 and 0.269. The output is still a proper weighted average, but the small weight 0.090 on the dropped key is gone. When the dropped scores are small, the change in the output is small, which is why top- selection can work well.
Eight tokens in a causal language model, each token attending to itself and at most two tokens before it, so a window of keys. Compare with full causal attention, where token reads all tokens up to and including itself.
- Full causal attention: token reads keys, so the total is query-key pairs.
- Window attention: token 1 reads 1 key, token 2 reads 2 keys, and tokens 3 to 8 each read 3 keys.
- Total for the window: pairs.
- Ratio: .
Interpretation: even on eight tokens the window already saves about 40 percent of the pairs. The saving grows with length. The edge tokens still read fewer keys, and the gap is what makes the window pattern cheap for long sequences.
A model with 4,096 tokens and a causal window of keys per token. We count query-key pairs in one attention layer.
- Full causal count: pairs.
- Window count: the first 512 tokens read keys, which is pairs. Each of the remaining tokens reads 512 keys, which is pairs.
- Window total: pairs.
- Ratio: .
Interpretation: the window does about 23 percent of the work of full attention at this length. The gap widens with length: at 100,000 tokens with the same window, the full causal count is about 98 times the window count: against . Note that the window does not reduce the number of layers, so information from far away still reaches a token, but only by passing through several layers.
Try it: A sequence has 1,024 tokens and a causal window of 128 keys. How many query-key pairs does the window need, compared with full causal attention? Answer: the first 128 tokens read pairs, and the other tokens read 128 each, which is . The total is pairs. Full causal attention needs . The window is about 23 percent of full attention.
Common mistakes and tradeoffs
It is tempting to think that a sparse pattern gives the same output as full attention, only faster. In fact the output changes, because the dropped keys no longer contribute to the weighted sum. The change is small when the dropped weights are small, and it can be large when an important token lies outside the pattern. This is why sparse models are often trained with the pattern in place, rather than having it added at inference time.
It is tempting to think that fewer pairs always means less time. In fact sparse patterns scattered across memory can run slower on a GPU than a dense operation that the hardware handles well. Block-structured sparsity, where the kept keys come in contiguous chunks, is the usual fix, and it is one of the design goals of the Native Sparse Attention paper.
It is tempting to think that a local window cannot see distant information. In fact information spreads: after stacked layers, a token can be influenced by tokens up to about positions back, through the intermediate tokens. Longformer uses this and adds a few global tokens so that some positions can read the whole input directly.
When to use it: when sequences are long, when the important information is mostly local or can be selected by score, and when the cost of full attention is the bottleneck.
When not to use it: when short-range and long-range links are equally important and must be exact, when the task needs to compare every pair of tokens, or when the sequences are short enough that full attention is cheap.
Alternatives: Sliding Window Attention is the fixed-window case in decoder models. Linear Attention changes the algebra so that the cost is linear without choosing a subset at all, at some cost in exactness.
History
Full attention scales with the square of the sequence length, and by the late 2010s that cost was the main limit on how long a transformer could read. Researchers first tried fixed patterns, such as windows and strided jumps, in models that could handle longer images and audio than before.
Longformer (2020) combined local windows with a few global tokens and showed it on long-document tasks. Later work added data-dependent selection, where the model chooses which keys to keep. The 2025 Native Sparse Attention paper trains the selection together with the rest of the model, and designs the pattern to run efficiently on hardware. The main question since then has been how much of full attention's accuracy the sparse pattern can keep at a given cost.
Recap
- Sparse attention lets each query read a subset of the keys, and the softmax runs only over that subset.
- The subset is fixed by position, as in a window, or chosen from the scores, as in top-.
- With a window of keys, the number of query-key pairs grows roughly as instead of .
- The output changes when keys are dropped, and the block structure of the pattern matters for real hardware speed.
Read next: Sliding Window Attention works through the fixed-window case in full, and Linear Attention shows another route to linear cost.
Check yourself
QuestionWhat does sparse attention change compared with full attention?Show answer
Each query uses a subset of the keys, and the softmax weights are computed only over that subset, so the dropped keys contribute nothing to the output.
QuestionScores for three keys are . If only the top two keys are kept, what are their softmax weights? Give the first one to two decimal places.Show answer
Exponentials are and , with total over the kept keys. The weight for the score-1 key is , and for the score-2 key it is .
QuestionWhy can a sliding window still let a token be influenced by a token far away?Show answer
Each layer lets information move a fixed distance, so after several stacked layers the influence can travel across many windows. A token layers deep can be affected by tokens up to about positions back, through the intermediate tokens.
QuestionA model has 2,048 tokens and a causal window of 256 keys. Roughly how does the window's cost compare with full causal attention?Show answer
Full causal attention has pairs. The window has pairs, a ratio of about 0.23, so it is roughly a quarter of full attention.
QuestionA researcher says sparse attention is always faster because it does fewer multiplications. What is the flaw in this claim?Show answer
Fewer pairs does not guarantee faster execution. Scattered memory access can leave the GPU underused, so a sparse pattern can run slower than a dense operation. Speed depends on whether the pattern is arranged so the hardware can read memory efficiently, often in contiguous blocks.
Sources
- Beltagy, Peters and Cohen (2020), Longformer: The Long-Document Transformer
- Native Sparse Attention (2025), arXiv:2502.11089