ChatPaper.aiChatPaper

ChunkAttention:具有前綴感知 KV 快取和雙階段分區的高效自注意力机制

ChunkAttention: Efficient Self-Attention with Prefix-Aware KV Cache and Two-Phase Partition

February 23, 2024
作者: Lu Ye, Ze Tao, Yong Huang, Yang Li
cs.AI

摘要

自我關注是大型語言模型(LLMs)的重要組成部分,但對於長序列來說,也是推理延遲的一個重要來源。在多租戶LLMs服務場景中,通過使用多個LLM請求在前綴中共享系統提示的概率,可以優化自我關注的計算和記憶體操作成本。本文介紹了ChunkAttention,一個具有前綴感知的自我關注模塊,可以在運行時檢測多個請求中匹配的提示前綴並共享它們的鍵/值張量以改善KV快取的記憶體利用率。這是通過將整體鍵/值張量分解為較小的塊並將它們結構化為輔助前綴樹來實現的。因此,在基於前綴樹的KV快取之上,我們設計了一個高效的自我關注核心,其中實現了一個兩階段分割算法,以改善在存在共享系統提示時的自我關注計算中的數據局部性。實驗表明,與最先進的實現相比,當系統提示的長度範圍從1024到4096時,ChunkAttention可以將自我關注核心的速度提高3.2-4.8倍。
English
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.8times compared to the start-of-the-art implementation, with the length of the system prompt ranging from 1024 to 4096.
PDF226December 15, 2024