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.
翻译:随着大语言模型(LLMs)承担复杂任务,其输入需补充融合领域知识或用户特定信息的更长上下文。然而使用长上下文对响应式LLM系统构成挑战——在LLM处理完整个上下文前无法生成任何内容。通过跨输入复用上下文的KV缓存可减少上下文处理延迟,但网络传输包含大型张量的KV缓存会引入额外网络延迟。CacheGen是面向LLM系统的快速上下文加载模块。首先,CacheGen采用自定义张量编码器,利用KV缓存的分布特性将其编码为更紧凑的比特流表示,且编码/解码开销可忽略不计,从而降低获取KV缓存的带宽需求。其次,为维持低上下文加载延迟与高生成质量,CacheGen自适应调整流式策略以匹配可用带宽变化。当可用带宽下降时,CacheGen可提高部分上下文的压缩级别,或选择实时重新计算其KV缓存。我们在四种不同规模的流行LLM及四个数据集(共计662个上下文)上测试CacheGen。与近期复用KV缓存的系统相比,CacheGen在保持LLM响应质量(准确率或困惑度)几乎不受影响的前提下,将KV缓存大小减少3.7-4.3倍,获取和处理上下文的总延迟降低2.7-3.2倍。