Self-attention is an essential component of large language models(LLMs) but a significant source of inference latency for long sequences. In multi-tenant LLMs serving scenarios, the compute and memory operation cost of self-attention can be optimized by using the probability that multiple LLM requests have shared system prompts in prefixes. In this paper, we introduce ChunkAttention, a prefix-aware self-attention module that can detect matching prompt prefixes across multiple requests and share their key/value tensors in memory at runtime to improve the memory utilization of KV cache. This is achieved by breaking monolithic key/value tensors into smaller chunks and structuring them into the auxiliary prefix tree. Consequently, on top of the prefix-tree based KV cache, we design an efficient self-attention kernel, where a two-phase partition algorithm is implemented to improve the data locality during self-attention computation in the presence of shared system prompts. Experiments show that ChunkAttention can speed up the self-attention kernel by 3.2-4.8$\times$ compared to the start-of-the-art implementation, with the length of the system prompt ranging from 1024 to 4096.
翻译:自注意力机制是大语言模型(LLMs)的核心组件,但也是长序列推理延迟的主要来源。在多租户LLM服务场景中,利用多个LLM请求可能共享相同系统提示词前缀的概率,可以优化自注意力的计算与内存操作开销。本文提出ChunkAttention——一种前缀感知的自注意力模块,能够在运行时检测多个请求间匹配的提示词前缀,并共享对应的键/值张量内存,从而提升KV缓存的存储利用率。该方法通过将完整的键/值张量分解为更小的块,并构建为辅助前缀树结构来实现。在此基础上,基于前缀树的KV缓存,我们设计了一种高效的自注意力内核,其中实现了一种两阶段划分算法,以在存在共享系统提示词时提升自注意力计算过程中的数据局部性。实验表明,当系统提示词长度在1024至4096范围内时,ChunkAttention可将自注意力内核的计算速度相比于当前最优实现提升3.2-4.8倍。