arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2409.10516cs.LGcs.CL
收藏

RetrievalAttention:通过向量检索加速长上下文大语言模型推理

RetrievalAttention: Accelerating Long-Context LLM Inference via Vector Retrieval

Microsoft Research(微软研究院);Shanghai Jiao Tong University(上海交通大学);Fudan University(复旦大学)

Di Liu, Meng Chen, Baotong Lu, Huiqiang Jiang, Zhenhua Han, Qianxi Zhang, Qi Chen, Chengruidong Zhang, Bailu Ding, Kai Zhang, Chen Chen, Fan Yang, Yuqing Yang, Lili Qiu

AI总结:

RetrievalAttention通过为KV向量构建ANNS索引并设计注意力感知的向量搜索算法,在仅访问1--3%数据的条件下实现接近全注意力的准确率,从而显著加速长上下文LLM推理并降低GPU内存消耗。

摘要:

基于Transformer的大语言模型(LLMs)已变得越来越重要。然而,由于注意力计算的二次时间复杂度,将LLMs扩展到更长上下文会导致推理速度极慢,并且缓存键值(KV)向量会带来很高的GPU内存消耗。本文提出了RetrievalAttention,这是一种无需训练的方法,可同时加速注意力计算并降低GPU内存消耗。通过利用注意力机制的动态稀疏性,RetrievalAttention提出在CPU内存中为KV向量构建近似最近邻搜索(ANNS)索引,并在生成过程中通过向量搜索检索最相关的向量。不幸的是,我们观察到现成的ANNS索引在此类检索任务中往往效果不佳,原因是注意力机制中查询向量与键向量之间存在分布外(OOD)问题。RetrievalAttention通过设计一种注意力感知的向量搜索算法来应对OOD挑战,该算法能够适应查询向量的分布。我们的评估表明,RetrievalAttention在仅需访问1--3%数据的情况下,实现了接近全注意力的准确率。这显著降低了长上下文LLMs的推理成本,并大幅减少了GPU内存占用。特别是,RetrievalAttention仅需一块NVIDIA RTX4090(24GB)即可为具有8B参数的LLMs服务128K个token,并且能够以0.188秒生成一个token。

英文摘要:

Transformer-based Large Language Models (LLMs) have become increasingly important. However, due to the quadratic time complexity of attention computation, scaling LLMs to longer contexts incurs extremely slow inference speed and high GPU memory consumption for caching key-value (KV) vectors. This paper proposes RetrievalAttention, a training-free approach to both accelerate attention computation and reduce GPU memory consumption. By leveraging the dynamic sparsity of attention mechanism, RetrievalAttention proposes to build approximate nearest neighbor search (ANNS) indexes for KV vectors in CPU memory and retrieve the most relevant ones through vector search during generation. Unfortunately, we observe that the off-the-shelf ANNS indexes are often ineffective for such retrieval tasks due to the out-of-distribution (OOD) between query vectors and key vectors in the attention mechanism. RetrievalAttention addresses the OOD challenge by designing an attention-aware vector search algorithm that can adapt to the distribution of query vectors. Our evaluation demonstrates that RetrievalAttention achieves near full attention accuracy while only requiring access to 1--3% of the data. This leads to a significant reduction in the inference cost of long-context LLMs, with a much lower GPU memory footprint. In particular, RetrievalAttention only needs a single NVIDIA RTX4090 (24GB) to serve 128K tokens for LLMs with 8B parameters, which is capable of generating one token in 0.188 seconds.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑