Rúben GalvãoThe Rabbit Hole

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

Compressed sparse attention (CSA): keeping more detail and reading less

Start with the information lost by heavy compression, emit summaries more often, then select which ones each query reads.

HCA keeps recent tokens individually and adds one compressed entry for each completed block. Here is that cache again: the recent window on the right, compressed history on the left, and the compression workspace underneath.

A complete 128-position compression block emits and clearsRecent detail + compressed historyCompressed historyRecent windowNo completed entriesEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.112 entries512Compression workspaceEarlier context is dimmed; outlined columns are current.Current token computation.112 / 128 positions · 2 × 512Incoming tokenEarlier columns are cached; outlined columns are newly computed.Current token computation.128 positions per entry · emit every 128 tokens

The recent window slides as new tokens arrive. The smaller workspace prepares compressed entries.

The HCA cache again: individual recent entries on the right, compressed history on the left, and the smaller compression workspace underneath. A complete block emits one entry and clears the workspace.

The workspace fills with 128 positions, produces one entry, then clears. That makes the history much cheaper to keep, but asks one vector to retain information from a large block. A later query may need a distinction that did not survive the mixture.

Keep a smaller window for compression

Could we combine fewer positions into each entry? Try a sliding workspace holding the latest eight positions. We could produce an entry only when all eight positions are new since the previous emission. The workspace keeps sliding, but the summaries still cover separate groups.

Those separate groups create a hard boundary: neighbouring summaries have no source positions in common. Could we produce an entry sooner, when only half the window is new? Keep the same sliding window, but emit every four tokens. Each mixture still covers eight positions, but four also contributed to the preceding entry.

A sliding eight-position window emits only when all eight positions are newEmit when all eight positions are newCompressed historyRecent windowEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.14 entriesEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.112 entries512Compression workspaceEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.8 / 8 positions · 2 × 512Incoming tokenEarlier columns are cached; outlined columns are newly computed.Current token computation.8 positions per entry · emit every 8 tokensA sliding eight-position window emits every four tokensEmit when four positions are newCompressed historyRecent windowEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.28 entriesEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.112 entries512Compression workspaceEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.8 / 8 positions · 2 × 512Incoming tokenEarlier columns are cached; outlined columns are newly computed.Current token computation.8 positions per entry · emit every 4 tokens

The recent window slides as new tokens arrive. The smaller workspace prepares compressed entries.

Both paths receive one token at a time. Both eight-position workspaces slide on every arrival. Above: emit when all eight positions are new. Below: emit when four are new. Positions darken when an entry is emitted, then shift as new tokens arrive. Each workspace position holds content and scores.

Both workspaces above hold eight positions and slide one position on every arrival. The first waits until all eight positions are new before producing an entry; the second emits when four are new. In both, dimmed positions have already contributed to a stored entry, while brighter positions have not. In the four-token path, retained positions are reused as inputs to a new mixture, rather than copied from the previous compressed result.

This is the pooling pattern used by CSA: eight-position windows advancing by four positions. It is a separate, much smaller window than the 128-token full-resolution cache, which continues to slide independently. HCA instead uses non-overlapping 128-position blocks.

More entries preserve more detail

For 256 source tokens, HCA produces 2 compressed entries. Eight-position blocks without overlap would produce 32. Advancing that eight-position window by four produces 64. Every stored entry is still 512 coordinates wide.

We have given neighbouring summaries some shared context and reduced how much information each mixture must retain. But the compressed history now grows 32 times faster than HCA’s. Over 65,536 source tokens, that means 16,384 summaries instead of 512. Reading every one would make attention much larger again.

Choose which entries to read

We have traded some compression for more detail, but brought back a larger attention read. Do all those summaries need to be read for every query? An indexer can estimate which ones are useful and pass only a selection to attention. The recent individual entries still participate in the read.

This compromise is Compressed Sparse Attention (CSA) in DeepSeek-V4: emit a compressed entry every 4 tokens, then use the indexer to select which entries each query reads. Pro selects up to 1,024 summaries; Flash selects up to 512. Both also read the latest 128 individual entries.

Put the attention block together

The recent window goes directly to the attention read. The lightning indexer chooses which compressed summaries join it. All query heads read that same selected history, then produce their own attention maps and results.

The indexer has its own smaller 128-coordinate compressed keys covering the same windows. It ranks their positions; those positions identify the 512-coordinate summaries to fetch for main attention. It does not supply the values that attention collects.

In the full diagram, Wc and Wg each prepare two 512-coordinate slices per token: one for the current pooling window and one for the next. Pooling combines the previous four positions’ first slices with the current four positions’ second slices. The indexer uses the same pattern with 128-coordinate slices.

Complete CSA attention with shared caches and per-head readsNew token representationEarlier context is dimmed; outlined columns are current.Current token computation.4,096Wqa4,0961,024Wk = Wv4,096512Wc4,0961,024Wg4,0961,024RMSNormRMSNormEarlier context is dimmed; outlined columns are current.Current token computation.1,024Shared query latentRoPEShared recent entriesEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.128 entries512Earlier context is dimmed; outlined columns are current.Current token computation.1,024ContentEarlier context is dimmed; outlined columns are current.Current token computation.1,024Scores8-position pooling windowEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.Earlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.8 × 5128 × 512+Position bias41,024Softmax over 8512×Add 8 positionsRMSNormRoPEShared compressed entriesEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.51264 headsWqb / head1,024512RMSNormRoPEEarlier context is dimmed; outlined columns are current.Current token computation.512q / headTokenEarlier context is dimmed; outlined columns are current.Current token computation.4,096Query latentEarlier context is dimmed; outlined columns are current.Current token computation.1,024Gather selected summariesEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.512Up to 512 entriesRecent + selectedEarlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Earlier columns are cached; outlined columns are newly computed.Cached positions from earlier decoding steps.Current token computation.128 + selectedAttention map / head128 + selected keysOne learned scalar per head joins the softmax denominator. Its attention weight contributes no value to the result.Sink score / head×Same entries as VEarlier context is dimmed; outlined columns are current.Current token computation.512Inverse RoPEEarlier context is dimmed; outlined columns are current.Current token computation.512Head resultConcatenate head resultsEarlier 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.Current token computation.51264 heads64 × 512 = 32,768Split into 8 groups8 heads × 512 = 4,096 / group8 groupsWoa / group4,0961,024Earlier context is dimmed; outlined columns are current.Current token computation.1,024Projected group resultConcatenate group resultsEarlier 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.Current token computation.1,0248 groups8 × 1,024 = 8,192Wob8,1924,096Earlier context is dimmed; outlined columns are current.Current token computation.4,096Attention output
The full CSA block. The indexer selects positions using its own 128-coordinate keys; main attention gathers the corresponding 512-coordinate summaries and combines them with the recent window. Click the indexer to expand or collapse its internal flow.

HCA and CSA are alternative layer types. A layer reads its recent window plus either all HCA summaries or selected CSA summaries; these are not three cache levels within one layer. Each path can overlap its own recent window. Flash’s first two layers use only that window, without a compressed history.

Compression controls how much information each entry must combine. Selection controls how many of those entries a query reads. Using less compression gives us more entries to choose from; the indexer keeps that larger history from making every attention read equally large.

We have changed how many positions each stored entry represents and how many entries a query reads. Together with shared K/V entries, these choices are ready to connect in DeepSeek-V4 model architecture.

Sources and assumptions

The opening animation shows HCA’s 128-position workspace. The next compares non-overlapping and overlapping eight-position pooling windows while keeping the recent cache at 128. Workspace cells represent prepared content and scores, not raw token storage. CSA emits every four tokens while pooling across eight positions; its implementation uses two projected slices per token to form overlapping windows. HCA pools non-overlapping blocks of 128. Pro selects up to 1,024 compressed entries per read and Flash up to 512. Both also read the local 128-token window. Counts assume complete blocks; the query reads only causally available entries.

← Browse all subjectsBack to the beginning ↑