HeadMem: Turning an LLM's Retrieval Heads into a Compact Memory for Later Question
Ma et al. in Inference & systems
Can an LLM keep evidence without its whole cache?
HeadMem: Turning an LLM's Retrieval Heads into a Compact Memory for Later Question
When context comes before questions
Llama-3.1-8B's bf16 cache takes 16 GiB at 128k tokens, preserving every position.
It fixes a subset before later questions are known, so dropped positions cannot be recovered.
HeadMem uses compact keys and the frozen LLM's heads, with no second neural retriever.
How HeadMem identifies its retrieval heads

On a separate 80-question split, each bare question is read alone. Heads are ranked by attention mass on gold-answer tokens; the top 16 are selected once per model.
Build once; retrieve per question
- 1
Index each context once
Run the frozen LLM on 4,096-token chunks; store product-quantized (PQ-32) selected-head keys and token IDs, not values. This lets indexed contexts exceed its window.
- 2
Score the bare question
Forward each question alone; each selected head's softmax scores every stored position. Average over question tokens, then sum head scores.
- 3
Reread selected text
Smooth scores, select 2,048 context tokens, restore original order, then let the full LLM answer.
A narrow raw edge, not a significant win
Llama-3.1-8B RULER-MQ, 128k; 8 questions/context, B=2,048. HeadMem+BM25 fixed w=0.5; BM25+dense dev-tuned. Δ=+1.40, 95% CI [-1.40,+4.24].
Ablations isolate the retrieval signal

On Llama-3.1-8B development answer-presence tests, aggregating all heads stays near random; a per-head softmax over detected heads with exact keys rises above BM25.
Split the reading budget between cache and retrieval
RQ3: Llama RULER-MQ, 64k (episodes 0-5); combo uses 1,024 KV entries + 1,024 HeadMem tokens. *Significant vs KVzip.
HeadMem helps when query and needle barely overlap
NoLiMa, Llama-3.1-8B, 8k, n=290; retrieval B=2,048, FullKV all 8k. Paired HeadMem-FullKV Δ=+3.06, 95% CI [-2.33,+8.44], not significant.
HeadMem-2 is 13× faster across eight queries
On an idle H200 NVL, FullKV takes 21.73 s end-to-end for eight questions on a 128k Llama-3.1-8B context.
Two layer-2 heads share one KV head: 32 B/token and 1.63 s end-to-end for eight questions. Its 128k RULER-MQ macro score is 70.4 vs FullKV's 69.4.
The score difference is not significant; that does not establish equivalence.
Where this memory still misses
Answer locations shape detection
Per model, 80 RULER-MQ questions with located gold answers rank the heads; removing the two QA families sharply lowers Llama's retrieval scores.
Chains expose a retrieval gap
At 128k on Llama variable tracking, the score is 35.8; supplying all five chain statements in the 2,048-token selection raises it to 98.8.
Results vary by model
On Mistral-7B, HeadMem alone trails BM25 at 64k and 128k; its fusion is not significantly different from BM25 there.
Why it matters
- Compressed keys keep every context position reachable without retaining the full KV cache.
- For repeated queries, index once, then use the frozen model's detected heads to select 2,048 tokens for rereading.
- The open problem is finding later linked facts after retrieval finds only the chain's first statement.
3 likes 11 slides, 3 min
4 days ago