[论文解读] Keyformer: KV Cache Reduction through Key Tokens Selection for Efficient Generative Inference
Keyformer 引入了一种推断时方法,通过使用基于 Gumbel 的分数和对数似然正则化来选择关键令牌和最近令牌,从而减少 KV 缓存,在使用 70% KV 缓存的情况下实现最高 2.1x 延迟减少和 2.4x 吞吐量提升,同时保持准确性。
Transformers have emerged as the underpinning architecture for Large Language Models (LLMs). In generative language models, the inference process involves two primary phases: prompt processing and token generation. Token generation, which constitutes the majority of the computational workload, primarily entails vector-matrix multiplications and interactions with the Key-Value (KV) Cache. This phase is constrained by memory bandwidth due to the overhead of transferring weights and KV cache values from the memory system to the computing units. This memory bottleneck becomes particularly pronounced in applications that require long-context and extensive text generation, both of which are increasingly crucial for LLMs. This paper introduces "Keyformer", an innovative inference-time approach, to mitigate the challenges associated with KV cache size and memory bandwidth utilization. Keyformer leverages the observation that approximately 90% of the attention weight in generative inference focuses on a specific subset of tokens, referred to as "key" tokens. Keyformer retains only the key tokens in the KV cache by identifying these crucial tokens using a novel score function. This approach effectively reduces both the KV cache size and memory bandwidth usage without compromising model accuracy. We evaluate Keyformer's performance across three foundational models: GPT-J, Cerebras-GPT, and MPT, which employ various positional embedding algorithms. Our assessment encompasses a variety of tasks, with a particular emphasis on summarization and conversation tasks involving extended contexts. Keyformer's reduction of KV cache reduces inference latency by 2.1x and improves token generation throughput by 2.4x, while preserving the model's accuracy.
研究动机与目标
- 动机:在带有长上下文的自回归大语言模型推断中,KV 缓存的延迟和带宽瓶颈。
- 提出一种推断时机制,在保持模型准确性的同时减少 KV 缓存。
- 识别并保留少量关键令牌以及最近令牌,以形成减小的 KV 缓存。
- 开发一个具有学习启发的分数函数,并结合基于 Gumbel 的正则化,以在解码步骤中识别关键令牌。
- 展示在不同位置嵌入和涉及长上下文的任务中,对模型的鲁棒性。
提出的方法
- 观察到约 90% 的注意力权重集中在一小部分令牌上,这些令牌被称为关键令牌。
- 提出 Keyformer,通过将最近窗口 (w) 与关键令牌 (k-w) 混合来保留 k 个令牌,从而形成减小的 KV 缓存。
- 在未标准化的 logits 上引入基于 Gumbel 的正则化,以识别关键令牌并在丢弃令牌后缓解分布偏移。
- 使用随着解码步骤增大的温度参数 tau 来在丢弃令牌时调整概率分布。
- 在提示和生成阶段累积分数函数 f_theta,以一致地识别关键令牌。

实验结果
研究问题
- RQ1Keyformer 是否能在推断过程中减少 KV 缓存大小,同时不使生成质量下降到超出 MLPerf 精度目标(基线的 99–99.9%)?
- RQ2Keyformer 的关键令牌选择与混合注意力是否在不同位置编码的模型上维持或提升基于 ROUGE 的摘要质量和长上下文性能?
- RQ3在不同 KV 缓存预算下,Keyformer 与 Window Attention 和 H2O 在延迟、吞吐量和准确性方面的比较如何?
- RQ4在从 KV 缓存中丢弃令牌时,基于 Gumbel 的 logits 正则化对维持准确性是否必不可少?
主要发现
- Keyformer 将 KV 缓存减少约 50%,同时保持接近 MLPerf 水平的准确性(目标 ROUGE 在满注意力的 99–99.9% 之内)。
- Keyformer 实现了 2.1x 的推理延迟提升和 2.4x 的令牌生成吞吐量提升;在 50% KV 缓存降低的情况下。
- Keyformer 仅用 70% 的 KV 缓存就能维持基线准确性,优于在更大预算下也难以达到的 H2O。
- 在具有不同位置嵌入的 GPT-J、Cerebras-GPT 和 MPT 上,Keyformer 在降低 KV 预算的情况下,在准确性方面始终优于 Window Attention 和 H2O。
- 在长上下文摘要任务(GovReport 上的 MPT-7B-storywriter)中,Keyformer 在 50% KV 缓存下保持 99% 的准确性,而不是 H2O。
- 该方法在包括摘要和对话的任务中提升速度同时保持质量,展示了对上下文长度和模型差异的鲁棒性。

阅读本论文
在 Nubint 阅读器中打开原文,边读边就任意段落提问。
在 Nubint 中阅读本论文本解读由 AI 根据论文原文生成,引用前请核对原文。