潜在压缩与测试时训练
研究论文 · 2026/10/15
长上下文会增大键值(KV)缓存,从而增加生成时的内存使用和注意力开销。压缩这些缓存必须为未见问题保留信息。我们提出**Latent Compaction**,通过测试时训练学习紧凑的文档特定缓存。我们使用重建重要性、教师注意力和键空间多样性初始化缓存条目。训练首先将注意力输出和对数归一化器拟合到完整缓存的对应值,并固定重建选出的条目。然后通过从完整缓存模型进行端到端蒸馏,联合优化整个缓存。模型权重全程冻结,训练仅使用关于文档的通用提示,不访问下游问题或答案。在RULER、LongBench和Phonebook Lookup上的实验表明,与预算匹配的压缩基线相比有显著提升。例如,在RULER上,我们的方法在32倍压缩下达到96.7%的准确率,而KVZip+为40.0%。该方法还能跨模型规模和家族迁移,在Qwen3和MiniCPM5上均获得检索增益。这些结果表明,将KV缓存视为神经网络并通过测试时训练进行优化,可以在激进压缩下保持检索准确率。代码发布于https://www.github.com/THUNLP/latent-compaction。
该研究提出了一种新颖的KV缓存压缩方法,通过测试时训练显著提升压缩效率,在多个基准上大幅超越现有方法,且无需下游任务信息,具有重要的实用价值。
Long contexts enlarge key--value (KV) caches, increasing memory use and attention costs during generation. Compressing these caches must preserve information for unseen questions. We introduce **Latent Compaction**, which learns a compact document-specific cache through test-time training. We initialize cache entries using reconstruction importance, teacher attention, and key-space diversity. Training first fits attention outputs and log-normalizers to those of the full cache, with reconstruction-selected entries held fixed. It then jointly optimizes the entire cache through end-to-end distillation from the full-cache model. Model weights remain frozen throughout, and training uses only generic prompts about the document, without access to downstream questions or answers. Experiments on RULER, LongBench, and Phonebook Lookup demonstrate substantial gains over budget-matched compression baselines. On a RULER, for example, our method achieves 96.7% accuracy at 32times compression, compared with 40.0% for KVZip+. The method also transfers across model scales and families, with retrieval gains on both Qwen3 and MiniCPM5. These results show that treating the KV cache as a neural network and optimizing it with test-time training can preserve retrieval accuracy under aggressive compression. The code is released at https://www.github.com/THUNLP/latent-compaction