Large language models (LLMs) often incorporate multiple text chunks in their inputs to provide the necessary contexts. To speed up the prefill of the long LLM inputs, one can pre-compute the KV cache of a text and re-use the KV cache when the context is reused as the prefix of another LLM input. However, the reused text chunks are not always the input prefix, and when they are not, their precomputed KV caches cannot be directly used since they ignore the text's cross-attention with the preceding text in the LLM input. Thus, the benefits of reusing KV caches remain largely unrealized. This paper tackles just one question: when an LLM input contains multiple text chunks, how to quickly combine their precomputed KV caches in order to achieve the same generation quality as the expensive full prefill (i.e., without reusing KV cache)? We present CacheBlend, a scheme that reuses the pre-computed KV caches, regardless prefix or not, and selectively recomputes the KV values of a small subset of tokens to partially update each reused KV cache. In the meantime,the small extra delay for recomputing some tokens can be pipelined with the retrieval of KV caches within the same job,allowing CacheBlend to store KV caches in slower devices with more storage capacity while retrieving them without increasing the inference delay. By comparing CacheBlend with the state-of-the-art KV cache reusing schemes on three open-source LLMs of various sizes and four popular benchmark datasets of different tasks, we show that CacheBlend reduces time-to-first-token (TTFT) by 2.2-3.3X and increases the inference throughput by 2.8-5X, compared with full KV recompute, without compromising generation quality or incurring more storage cost.
翻译:大型语言模型(LLM)的输入中常包含多个文本块以提供必要上下文。为加速长LLM输入的预填充阶段,可预先计算文本的KV缓存,并在该上下文作为另一LLM输入前缀时复用其KV缓存。然而,复用文本块并非总是输入前缀;当非前缀时,其预计算KV缓存因忽略该文本与LLM输入中前序文本的交叉注意力而无法直接使用,导致KV缓存复用的优势未能充分实现。本文聚焦一个核心问题:当LLM输入包含多个文本块时,如何快速组合其预计算KV缓存,以达到与昂贵全量预填充(即不复用KV缓存)相同的生成质量?我们提出CacheBlend方案,该方案可复用任意位置(无论是否为前缀)的预计算KV缓存,并选择性重计算少量关键子集的KV值以部分更新每个复用缓存。同时,重计算部分令牌产生的微小额外延迟可与同任务内的KV缓存检索过程流水线执行,使得CacheBlend能将KV缓存存储于存储容量更大但速度较慢的设备中,且在检索时不增加推理延迟。通过在三种不同规模的开源LLM和四个不同任务的流行基准数据集上,将CacheBlend与最先进的KV缓存复用方案进行比较,实验表明:相较于全量KV重计算,CacheBlend在保证生成质量且不增加存储成本的前提下,将首令牌生成时间(TTFT)缩短了2.2-3.3倍,并将推理吞吐量提升了2.8-5倍。