Rúben GalvãoThe Rabbit Hole

The Rabbit Hole / AI and machine learning / Language models / Remembering context efficiently

Compressed sparse attention 2 (CSA2): keep detail, narrow the search

A query needs a detail that compression may have mixed away. Could we keep finer history without making every layer search it all again?

We want a new token to retrieve a detail from much earlier in the text. Full attention keeps every earlier token available, but its cache and the work of reading it grow with the sequence.

Sliding-window attention bounds that cost by keeping only the most recent tokens. Those tokens remain individually accessible, but this layer can no longer directly read entries that have left the window.

CSA keeps that recent window and adds compressed summaries of older history. An indexer selects which summaries each query reads, keeping the read small. But a query may find the right summary and still miss the detail it needs, because compression mixed that distinction away.

Follow the data through the mechanismA later question needs a detail from older historyEarlier context is dimmed; outlined columns are current.Current token computation.51216 earlier prepared positionsCompress as in CSAEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.5124 stored summariesDid the distinction the query needs survive?A better search can only choose among the entries we kept
If a needed distinction was mixed away, finding the right summary still cannot restore it.

Finding the right summary only helps if it still contains what we need. Before making the search more elaborate, we could give the history more room: combine fewer positions into each entry, or keep positions individually.

Follow the data through the mechanismCould each stored entry stand for fewer positions?16 source positions have arrivedPrepared positionEarlier context is dimmed; outlined columns are current.Current token computation.512One entry / 4 arrivalsPrepare a mixtureEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.5124 stored entriesPrepared positionEarlier context is dimmed; outlined columns are current.Current token computation.512One entry / 2 arrivalsPrepare a mixtureEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.5128 stored entriesPrepared positionEarlier context is dimmed; outlined columns are current.Current token computation.512One entry / 1 arrivalKeep the positionEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.51216 stored entries
Watch the same arrivals create four, eight or sixteen entries: less compression asks less of each mixture, but the cache grows faster.

Less compression gives each entry less information to squeeze together, but leaves more entries to store and search. Keeping positions individually avoids this pooling loss altogether.

The read stays small, but the search grows

Our familiar lightning indexer still chooses a small attention read. But its own search now covers more index keys: a fixed read budget does not bound the number of index keys it examines.

Follow the data through the mechanismKeep the final attention read the same sizeMore compressed historyEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.512CL / 4 global entriesScore each index entryCL / 4 scores / queryEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.512Fetch up to 512 entriesFiner historyEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.512CL global entriesScore each index entryCL scores / queryEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.512Fetch up to 512 entriesLess compression enlarges the search; the read budget stays fixed
Less compression leaves more index keys to search, even with the same per-query attention budget.

Now follow one token through the decoder layers. The global entries here represent individual encoder-output positions, as in the prefill split. The indexer searches their corresponding index keys; its selected positions identify which K/V columns to fetch. The recent sliding window is read separately.

Reuse saves searches, but fixes the choice

Searching the whole history in every decoder layer repeats the larger search. We already know another option: reuse an earlier layer’s selected positions. Later layers read those positions with their own attention weights, without another indexer search.

Follow the data through the mechanismWe still have another layer to runFresh searches everywhere, or keep the earlier choice?Search again each timeEarlier context is dimmed; outlined columns are current.Current token computation.5,120Fresh searchOwn attention weights+Fresh searchOwn attention weights+Fresh searchOwn attention weights+3 searches · repeated workCarry the choice downEarlier context is dimmed; outlined columns are current.Current token computation.5,120Fresh searchOwn attention weights+Reuse positionsOwn attention weights+Reuse positionsOwn attention weights+1 search · a choice can become staleThe residual representation changes as it passes through layers
Reusing a selection removes repeated searches, but later representations may need a different choice.

But the token’s representation changes between layers. An entry that looked unhelpful to the earlier layer’s indexer query may matter to a later one. Reusing only the winners gives that later layer no way to choose a different position instead.

Refresh the choice within a smaller search area

Could we pass a broader candidate set to a later indexer, while keeping the attention read small? It could reconsider the choice without searching the whole history again.

A strong match might have useful neighbours. Divide the stored positions into fixed, consecutive groups, and keep whole groups containing strong matches. Their entries remain separate; grouping only marks where a later indexer may search.

The first indexer already scored the visible history. Each group’s highest entry score tells us how promising that group is. Those scores give us two outputs: the best individual positions for this layer’s attention, and a wider set of groups for later indexers.

Follow the data through the mechanismCould we refresh the choice without searching everywhere?Same token across layers · Q is the indexer queryR = compression ratio · decoder: 1 · encoder: 2ResidualEarlier context is dimmed; outlined columns are current.Current token computation.… feed-forward …… feed-forward …Full layerQEarlier context is dimmed; outlined columns are current.Current token computation.Lightning indexerEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.CL / RIndexer scores1 × (CL / R)K/V cacheEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.CL / RTop 512 selectedWindow ≤ 128Earlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.JoinAttentionEarlier context is dimmed; outlined columns are current.Current token computation.+Reindex layerQEarlier context is dimmed; outlined columns are current.Current token computation.Lightning indexerEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.CL / Rreads ≤ 16,384Indexer scores1 × CK/V cacheEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.CL / RTop 512 selectedWindow ≤ 128Earlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.JoinAttentionEarlier context is dimmed; outlined columns are current.Current token computation.+Reuse layerReuse latest positionsNo indexer searchK/V cacheEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.CL / RTop 512 selectedWindow ≤ 128Earlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.JoinAttentionEarlier context is dimmed; outlined columns are current.Current token computation.+2,048 groups × 8 positionsC ≤ 16,384 candidates for the Reindex layerPass the latest Top 512 selectionRepeated tables show the same shared caches; outlines mark selected positionsSchematic columns · budgets capped by visible history · recent window: 128
Each layer joins selected global K/V entries with its own sliding window for one attention read. Full searches CL / R index keys; Reindex searches retained candidates; Reuse passes the latest selection through. Dotted residual segments omit feed-forward work and intervening layers.

In every layer, the selected global entries join that layer’s recent sliding window in one attention read. The window is included directly; only the global entries go through indexer selection.

The cache keeps CL / R entries, where R is the compression ratio we chose: stronger compression leaves fewer entries to search. In the worked model, the decoder uses R = 1 and the encoder uses R = 2. The Full layer searches that stored history and establishes the candidates. A Reindex layer reads only the candidate positions from that same cache, using its own query to refresh the selection. A Reuse layer passes that latest selection directly to attention.

The first full search remains. We save work in later layers, but a useful position outside the retained groups cannot enter their restricted search. A larger candidate pool gives them more freedom at the cost of more indexing work.

These decisions belong to the same current token across layers. The next token gets a fresh search and candidate pool; entries excluded from one pool remain stored in the cache.

This combination is Compressed Sparse Attention 2 (CSA2). The DeepSeek-V4.1 architecture places these layer roles alongside its compressed encoder history and uncompressed decoder history.

Sources and diagram scope

The opening describes a possible information-loss failure, not a measured retrieval failure. Entry-rate animations compare one entry per four, two or one arrivals, not pooling-window shapes: V4 CSA pools overlapping eight-position windows every four arrivals. Input vectors are prepared representations, not raw residual-stream tokens. Each drawn K/V entry uses the worked 512-coordinate width.

The refresh diagram uses the selected model’s authored budgets from the size profile. For DeepSeek-V4.1 Flash, the candidate budget is 2,048 groups of eight positions (16,384 positions), with a final Top-512 attention read. Actual counts are capped by causally visible history. CL labels the source history length and R the compression ratio, so the cache and its index-key table have CL / R entries. The diagram follows the decoder, with R = 1; the encoder uses R = 2. Drawn columns and highlights are schematic, not a one-column-per-entry count.

Group scores are maxima of the Full layer’s individual indexer scores. Candidate groups are selected independently of that layer’s Top-K attention read; no separate region vector or region query–key scan is introduced. The reference implementation also retains the group containing the newest visible position. Later Reindex layers use their own queries and shared index keys; Reuse layers do no new indexing. The candidate pool is specific to one current token across layers.

The three rows show layer roles, not three consecutive layers. In the worked checkpoint, encoder Full layers are 3, 9 and 15; decoder Full is 21, with Reindex at 25, 29, 33 and 37. Repeated tables represent the same shared index-key and global K/V caches. Recent-window buffers and attention weights remain layer-specific. Attention outputs are shown after head combination and output projection, then added to the residual; dotted stream segments omit the feed-forward work and intervening layers.

← Browse all subjectsBack to the beginning ↑