Heavily compressed attention (HCA): keeping a compressed history
Keep recent tokens individually, learn what to preserve from each older block, and discover heavily compressed attention.
Multi-query attention (MQA) lets all query heads read the same keys and values. We no longer keep a separate cache for every head. But that shared cache still adds a key and a value for every token. A later query can request information from any of them; the stored list keeps growing.
Sliding-window attention bounds that list by keeping only the newest tokens. As a new entry arrives, the oldest leaves. Memory stays bounded, but information from outside the window is no longer available to that attention layer.
Could we keep the recent window at full resolution, and retain a compressed view of the older history? We would still lose detail about older tokens, but we would no longer discard their information entirely.
A window with a longer view
Compare the three caches as the same tokens arrive. The third keeps a 128-token recent window and builds one compressed entry per 128-token block.
New tokens fill the recent window and the compression workspace.
The compressed entry is ready just before its oldest individual entry leaves the window. When a block reaches 128 tokens, it produces one compressed entry. When the next token arrives, the oldest individual entry leaves the window, but its contribution remains in the compressed history. The same pattern repeats for each block.
This gives older history a cheaper representation while preserving individual access to recent tokens. There is some overlap: a compressed entry is available while many of its source tokens are still in the window. We do not wait for the entire block to become old before storing its summary.
Try combining the entries
We have made room for compressed history, but have not yet decided what to put in it. The simplest attempt is to average the entries in each block. Here is that operation with just two entries:
We have saved an entry, but both orders now look identical. Averaging also forces every position to contribute equally: a useful feature from one token can be diluted by all the others. Keeping the recent window protects local detail; it does not solve what to preserve in the compressed history.
Let some contributions matter more
Could we change the mixture? Rather than taking half of every coordinate from each entry, we could give a larger share to one and a smaller share to the other. Different coordinates could favour different positions:
The amounts in this example illustrate the operation; choosing them by hand would not work for arbitrary text. We need the model to calculate them from the incoming representations.
A learned projection can produce a score for each coordinate at each position. Softmax across the positions turns those scores into contribution amounts that sum to 1. Now training can change which features survive the mixture. This score projection is the matrix labelled Wg below.
The average also lost order. If the score depends only on a token’s representation, it has no explicit indication of that token’s place within this compression block. Adding a learned bias for each block position gives the mixture that extra information.
Give compression its own content
We could use those amounts to combine the existing K/V entries. That would share the recent window’s projection with compression. But the same representation would have to serve two purposes: remain useful as an individual entry, and remain useful after being mixed with many others.
Could we let the model prepare content specifically for that mixture? A separate learned projection gives it that freedom. It reads the same incoming representation as the recent-window projection, but can learn different features to retain through compression. This is Wc, the content projection. The window keeps its own key and value projections.
We now have two things to calculate for each incoming token: what it can contribute, and how strongly each coordinate contributes. Multiply the content by those amounts, then add across positions. That gives us learned pooling:
The block becomes one 512-coordinate entry. RMSNorm controls its scale, and position information gives it a place in the sequence. It can then provide information to future attention reads.
This is still a compromise. Learned contributions and a separate content projection can change what survives, but they cannot preserve every distinction between the original entries. And pooling happens before future queries arrive, so it cannot tailor each summary to what those queries will ask.
Prepare the summary while keeping recent detail
We want the compressed entry ready when individual entries begin leaving the recent window. Each arriving token can therefore feed both paths: keep its individual K/V entry, and calculate its content and scores for the compression workspace.
Once that workspace holds a complete block, we can finish the mixture, store the compressed entry, and clear the workspace for the next block. The recent window continues to slide independently.
Before drawing those projections, there is one further saving we can make. MQA shares keys and values across heads, but still stores a separate key and value for each position. Could the same vector serve both roles? That is the shared K/V representation: one entry to score as a key and collect as a value. DeepSeek-V4 uses it for both recent entries and summaries. It makes each stored position cheaper; compression reduces how many positions we keep.
The next diagrams use that storage choice. Wkv makes the recent entries, while Wc and Wg prepare the compression content and scores. The learned position bias feeds pooling alongside the buffered content and scores:
The first block is one token short. No compressed entry exists yet.
Read the recent entries and the summaries together
A query now has two kinds of entry available. It can request information from individual recent tokens, or from a compressed entry that combines a whole block. We can put both into the same attention read: score them together, apply one softmax, and collect a weighted mixture of their values.
Just after the first window advance, that means 128 individual entries and one summary. The newest token has no summary yet because its block is unfinished. The completed summary remains available, although its source tokens cannot be retrieved separately from it.
With S completed summaries, the combined history contains S + 128 entries, each still 512 coordinates wide. We join along the positions, not the feature dimension.
Joining the two histories does not require a new kind of attention read. The query forms an attention map over the combined keys. One softmax gives contribution amounts across both histories, and those amounts weight the combined values. Since this model shares K/V representations, the same combined entries supply both roles:
The recent window and unfinished workspace stay bounded. The summary list keeps growing by one entry per 128 tokens. We have reduced memory growth, rather than produced linear attention’s fixed-size state.
This combination—a recent window plus a learned summary for every 128-token block—is Heavily Compressed Attention (HCA) in DeepSeek-V4. Each query can read every completed summary alongside the recent individual entries.
Put the query heads back in
As in MQA, every query head reads the same stored history. Each has its own query, attention map and collected result; we share the cache, not the requests made to it. The full diagram adds those heads and the input and output projections around the cache and read we just built.
The query and output projections are factorised in DeepSeek-V4. On the output side, grouping alone preserves the width. In Flash, 64 heads × 512 coordinates = 32,768, split into 8 groups of 4,096. Each group’s Woa projects 4,096 → 1,024. Concatenating those smaller results gives 8 × 1,024 = 8,192 coordinates, which Wob projects back to the 4,096-coordinate token width. The DeepSeek-V4 model architecture develops these factorised projections.
What if a head has little useful information to collect? Softmax would still divide all its attention among the available entries. DeepSeek-V4 gives each head one learned sink score, labelled Sink score / head below. It joins the softmax alongside the query–key scores, but supplies no value to collect. Attention assigned to it therefore reduces the total weight given to the cached values. The sink is a learned scalar, so it uses the violet weight colour.
We have kept a longer view without storing every earlier token individually. The remaining limitation is the mixture itself: a later query may need a distinction that did not survive 128-to-1 compression. Could we keep more detail without reading every entry?
Sources and assumptions
- DeepSeek-V4 technical report: HCA and the 128-token recent window.
- DeepSeek-V4 implementation:
DeepseekV4HCACompressor, block completion and the joint attention read.
The numeric pooling examples use two coordinates. The worked Pro and Flash diagrams use 128-position blocks and 512-coordinate entries. Content and scores for the unfinished block are buffered separately from the readable cache. Recent entries and summaries overlap; individual entries leave only when the local window advances. The model ties K and V, but sequence compression and that storage choice are separate ideas.