Rúben GalvãoThe Rabbit Hole

The Rabbit Hole / AI and machine learning / Language models / Working with longer context

Sparse attention and the lightning indexer

DeepSeek Sparse Attention (DSA) uses a lightning indexer to choose which cached entries to read. Must every query inspect the whole history?

A new token has a question. Behind it, on a very long shelf, sit 100,000 cached entries.

To answer, it reads every one.

Two ways to cut the bill

What one token pays has two parts: how big each entry is, and how many entries it reads.

So far we have only attacked the first.

Shorter blocks against a narrower rowFour rectangles, each made of one slice per cache entry. In the first three the rectangle gets shorter, because each entry is smaller, but it keeps all twelve slices. In the last the rectangle is full height again but only three slices wide, because only three entries are read.What we didheight: entry size · width: entries readKeep keys and valuesthe starting pointShare heads(MQA, GQA)Compress each entry(latent attention, fewer bits)Read fewer entries?the idea for this article
What to notice: Each earlier fix made the block shorter: a smaller entry. Reading fewer entries is a different direction: it makes the block narrower, whatever its height. The bill is the area.

We stopped recomputing old entries with the KV cache. Then we made each entry smaller by sharing heads, compressing them and using fewer bits.

None of that changed how many entries the token reads. That is a different direction: not a cheaper entry, but fewer entries.

We tried reading fewer

The first attempt was a sliding window: read only the most recent entries.

But a window reads the recent entries whatever the question. The detail we need may be far back, and only a few full-attention layers, with a growing cache, can reach it.

How far a layer reaches against what its cache costsA chart with cache cost along the bottom and reach along the side. A sliding window sits at low cost and short reach. Full attention sits at high cost and long reach. The corner with long reach and low cost is empty, marked with a question mark.reach backcache cost as the chat growsslidingwindowfullattention?
What to notice: A window is cheap but short-sighted. Full attention sees everything but pays for it. The empty corner is the one we want.

What we want is the empty corner: reach far back, yet read only what matters for this question.

Does it really need them all?

To read fewer entries we need to know which ones matter. Look at a real attention row. Keep fewer entries and watch how much of the weight they hold.

Keep only the top tokens

Interactive
Attention weight on each cached token
Tokens kept
Cache read
Weight captured
Weight missed
Weight captured against tokens kept

Illustrative scores. Details are in the notes at the end.

A few entries carry almost all the weight. The rest are close to nothing. If the token could find those few, it would lose very little.

But how do we find them?

The catch in choosing the best entriesThree boxes in a chain. To read only the best entries, we must know which are best. To know which are best, we must score every entry, which is the full attention work we wanted to avoid.Read only thebest few entriesneedsKnow whichentries are bestneedsScore everyentrythe work we skip
What to notice: To pick the best entries we need to know their scores. Getting the scores is the very work we wanted to skip.

To choose the best entries we need their scores. Scoring every entry is the work we hoped to skip. We would pay the full price just to decide what to throw away.

Ask someone who skims

Does the score have to be exact? We only need a ranking good enough to spot the few that matter.

So give the token a helper: a second, much smaller attention. Its only job is to hand every entry one rough score. It is the attention we already know, made smaller.

The main attention head beside the lightning indexerOn the left, the attention block: token representations become queries, keys and values through three learned matrices, dot products and a softmax give an attention map, and a weighted sum of values gives the output. On the right, the lightning indexer: the same token representations become queries and keys through two smaller matrices, dot products give one score per entry, and the top scores are kept. It has no values and no output.Main attention headToken representationsEarlier context is dimmed; outlined columns are current.Current token computation.WqEarlier context is dimmed; outlined columns are current.Current token computation.QueriesWkEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.KeysWvEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.ValuesDot productsSoftmaxAttention mapWeighted sumof valuesEarlier context is dimmed; outlined columns are current.Current token computation.Output576 numbers per entry · 128 headsLightning indexerToken representationsEarlier context is dimmed; outlined columns are current.Current token computation.WqEarlier context is dimmed; outlined columns are current.Current token computation.QueriesWkEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.Keysno WvDot productsOne score per entryKeep the top kEntries for the mainattention to read128 numbers per entry · 64 small heads · low precision
What to notice: The indexer starts exactly like the attention we know: tokens become queries and keys, and their dot products give scores. It is smaller, so it is quicker. It has no values to read, so it stops at a ranking.

This helper is the lightning indexer. Follow the two passes below.

Choosing which cached tokens to readThe new token feeds a small lightning indexer, which gives a score to each of the cached tokens. The top scores are kept, and the main attention then reads only those cached entries.Earlier context is dimmed; outlined columns are current.Current token computation.queryindexercheapone cheap score for every cached tokenkeep the top kcached tokensEarlier context is dimmed; outlined columns are current.Earlier context is dimmed; outlined columns are current.Earlier context is dimmed; outlined columns are current.Earlier context is dimmed; outlined columns are current.Earlier context is dimmed; outlined columns are current.Earlier context is dimmed; outlined columns are current.Earlier context is dimmed; outlined columns are current.Earlier context is dimmed; outlined columns are current.Earlier context is dimmed; outlined columns are current.Earlier context is dimmed; outlined columns are current.Earlier context is dimmed; outlined columns are current.Earlier context is dimmed; outlined columns are current.Earlier context is dimmed; outlined columns are current.Earlier context is dimmed; outlined columns are current.Earlier context is dimmed; outlined columns are current.Earlier context is dimmed; outlined columns are current.Earlier context is dimmed; outlined columns are current.Earlier context is dimmed; outlined columns are current.Earlier context is dimmed; outlined columns are current.Earlier context is dimmed; outlined columns are current.reads onlyoutlined entriesmainattention
What to notice: The small indexer scores every allowed entry. Main attention reads only the selected entries. The skipped entries stay in the cache for later queries.

The skimmer scores the whole shelf cheaply. The main attention then reads only the top few. The skipped entries stay on the shelf, because the next question may need them.

Teaching the skimmer

How does the indexer know what matters? It watches full attention.

How the indexer learns what to pickTop: full attention, the teacher, shows where the weight lies on ten entries. The indexer is trained to produce the same pattern. Bottom: after that, the main attention reads only the three entries the indexer picks.1. Full attention teachesfull attentioncopy thisindexer scores2. The indexer picks, attention readsentriesmainattentionreads only the green ones
What to notice: First the indexer copies where full attention looks. Then the main attention switches to reading only the entries the indexer picks.

Back to the weight chart, now choosing the entries with a cheap indexer. A rough ranking forces us to keep more entries to catch the same weight.

The same row, picked by a cheap indexer

Interactive
Attention weight on each cached token
Tokens kept
Cache read
Weight captured
Weight missed
Weight captured against tokens kept

Illustrative scores. Details are in the notes at the end.

Does the bill shrink?

Give the main attention a budget of entries. The example keeps 2,048, however long the context grows.

Full attention vs indexer plus sparse attention

Interactive

full attention indexer + sparse attention (2,048 kept)

Illustrative counts for one DeepSeek-V3.2 layer. Assumptions are in the notes at the end.

The indexer still scans the whole shelf, but each look is small. Below the budget nothing is skipped, so the indexer is pure overhead.

It can be combined with the other optimisations, such as latent attention, for greater gains.

Reading is now cheaper. The other half of each layer has its own cost: holding many learned weights without using all of them for every token. Follow that into mixture of experts.

Sources and assumptions

DeepSeek introduced this as DeepSeek Sparse Attention in DeepSeek-V3.2, and GLM-5 uses it too.

The sizes in the indexer figure are DeepSeek-V3.2’s: a 512-number latent plus a 64-number rotary key for the main attention, and a 128-number key with 64 heads for the indexer. The selection widgets use one fixed row of 300 made-up scores (a strong pull to the first token, to the latest few, and to seven relevant tokens far back). The weights are a real softmax of those scores, and the cheap indexer adds a fixed random error to each score. Operation curves count the content dot products and weighted sums, with two operations per multiply/add pair. They omit the rotary score, projections, softmax, top-k, precision metadata, memory traffic and GPU communication; they are not latency benchmarks.

The dense-attention warm-up aligns the indexer with attention distributions aggregated across heads, using KL divergence. Sparse training continues alignment on selected positions alongside the model’s language-modelling loss.

← Browse all subjectsBack to the beginning ↑