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