As large language models (LLMs) take on complex tasks, their inputs are supplemented with longer contexts that incorporate domain knowledge. Yet using long contexts is challenging, 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 high extra network delays. CacheGen is a fast context-loading module for LLM systems. First, CacheGen uses a custom tensor encoder, leveraging KV cache's distributional properties to encode a KV cache into more compact bitstream representations with negligible decoding overhead, to save bandwidth usage. Second, CacheGen adapts the compression level of different parts of a KV cache to cope with changes in available bandwidth, in order to maintain low context-loading delay and high generation quality. % When available bandwidth drops, CacheGen may raise the compression level for a part of the context or recompute its KV cache on the fly. We test CacheGen on popular LLMs and datasets. Compared to the recent systems that reuse the KV cache, CacheGen reduces the KV cache size by 3.5-4.3x and the total delay in fetching and processing contexts by 3.2-3.7x with negligible impact on the LLM response quality. Our code is at: https://github.com/UChi-JCL/CacheGen.
翻译:随着大语言模型(LLM)承担日益复杂的任务,其输入通常需补充包含领域知识的长上下文信息。然而,使用长上下文面临挑战,因为在LLM完整处理整个上下文之前无法生成任何内容。虽然可通过在不同输入间复用上下文的KV缓存来缩短上下文处理延迟,但通过网络传输包含大型张量的KV缓存会引入显著的网络延迟开销。CacheGen是一种面向LLM系统的快速上下文加载模块。首先,CacheGen采用定制化的张量编码器,利用KV缓存的分布特性将KV缓存编码为更紧凑的比特流表示,其解码开销可忽略不计,从而有效节省带宽占用。其次,CacheGen能够自适应调整KV缓存不同部分的压缩级别,以应对可用带宽的动态变化,从而维持较低的上下文加载延迟与较高的生成质量。% 当可用带宽下降时,CacheGen可提升部分上下文的压缩级别或实时重新计算其KV缓存。我们在主流LLM与数据集上测试CacheGen。相较于近期复用KV缓存的系统,CacheGen将KV缓存大小压缩至3.5-4.3倍,并将上下文获取与处理的总延迟降低至3.2-3.7倍,且对LLM响应质量的影响可忽略不计。代码发布于:https://github.com/UChi-JCL/CacheGen。