As large language models (LLMs) take on complex tasks, their inputs are supplemented with longer contexts that incorporate domain knowledge or user-specific information. Yet using long contexts poses a challenge for responsive LLM systems, as nothing can be generated until the whole context is processed by the LLM. While the context-processing delay can be reduced by reusing the KV cache of a context across different inputs, fetching the KV cache, which contains large tensors, over the network can cause extra network delays. CacheGen is a fast context-loading module for LLM systems. First, CacheGen uses a custom tensor encoder, which embraces KV cache's distributional properties, to encode a KV cache into more compact bitstream representations with negligible encoding/decoding overhead. This reduces the bandwidth demand to fetch the KV cache. Second, to maintain low context-loading delay and high generation quality, CacheGen adapts the streaming strategies to cope with changes in available bandwidth. When available bandwidth drops, CacheGen may raise the compression level for a part of the context or choose to recompute its KV cache on the fly. We test CacheGen on four popular LLMs of various sizes and four datasets (662 contexts in total). Compared to the recent systems that reuse the KV cache, CacheGen reduces the KV cache size by 3.7-4.3x and the total delay in fetching and processing contexts by 2.7-3.2x while having negligible impact on the LLM response quality in accuracy or perplexity.
翻译:摘要:随着大型语言模型处理复杂任务,其输入需补充包含领域知识或用户特定信息的更长上下文。然而,使用长上下文给响应式语言模型系统带来挑战:在模型完成整个上下文处理前无法生成任何内容。虽然通过跨不同输入复用上下文的KV缓存可减少上下文处理延迟,但通过网络获取包含大型张量的KV缓存会引入额外网络延迟。本文提出CacheGen——面向语言模型系统的快速上下文加载模块。首先,CacheGen利用自定义张量编码器(该编码器充分适配KV缓存的分布特性)将KV缓存编码为更紧凑的比特流表示,且编码/解码开销可忽略不计,从而降低获取KV缓存所需的带宽需求。其次,为保持低上下文加载延迟与高生成质量,CacheGen自适应调整流式传输策略以应对可用带宽变化。当可用带宽下降时,CacheGen可对部分上下文提升压缩级别,或选择实时重新计算其KV缓存。我们使用四种不同规模的主流语言模型和四个数据集(共662个上下文)测试CacheGen。与近期复用KV缓存的系统相比,CacheGen将KV缓存体积缩小3.7-4.3倍,获取和处理上下文的总体延迟降低2.7-3.2倍,同时对语言模型响应质量(准确率或困惑度指标)的影响可忽略不计。