高效流式语言模型:注意力汇聚机制

Efficient Streaming Language Models with Attention Sinks

韩松 Song Han · MIT · 2023-09-29 · arXiv:2309.17453 ↗ · 被引 2063

打开互动全文版(逐段中英对照 + 图/公式 + 论文问答)→

摘要 · Abstract

在流式应用(如多轮对话)中部署大语言模型(LLM)面临两大挑战:解码阶段缓存历史令牌的键值状态(KV)消耗大量内存,且现有 LLM 无法泛化到超过训练序列长度的文本。窗口注意力(仅缓存最近 KV)是一种自然方法,但文本长度超过缓存大小时会失效。我们观察到一种有趣现象——注意力汇聚(attention sink),即保留初始令牌的 KV 能大幅恢复窗口注意力的性能。本文首先证明注意力汇聚的出现是由于初始令牌作为“汇聚点”获得了强注意力分数,即使它们语义上不重要。基于此,我们提出 StreamingLLM,一种高效框架,使有限长度注意力窗口训练的 LLM 无需微调即可泛化到无限序列长度。实验表明,StreamingLLM 能使 Llama-2、MPT、Falcon 和 Pythia 在多达 400 万令牌及更多序列上实现稳定高效的语言建模。此外,我们发现预训练时添加占位令牌作为专用注意力汇聚点可进一步改善流式部署。在流式设置中,StreamingLLM 相比滑动窗口重计算基线实现了高达 22.2 倍的加速。代码和数据集见 https://github.com/mit-han-lab/streaming-llm。

Deploying Large Language Models (LLMs) in streaming applications such as multi-round dialogue, where long interactions are expected, is urgently needed but poses two major challenges. Firstly, during the decoding stage, caching previous tokens' Key and Value states (KV) consumes extensive memory. Secondly, popular LLMs cannot generalize to longer texts than the training sequence length. Window attention, where only the most recent KVs are cached, is a natural approach -- but we show that it fails when the text length surpasses the cache size. We observe an interesting phenomenon, namely attention sink, that keeping the KV of initial tokens will largely recover the performance of window attention. In this paper, we first demonstrate that the emergence of attention sink is due to the strong attention scores towards initial tokens as a "sink" even if they are not semantically important. Based on the above analysis, we introduce StreamingLLM, an efficient framework that enables LLMs trained with a finite length attention window to generalize to infinite sequence lengths without any fine-tuning. We show that StreamingLLM can enable Llama-2, MPT, Falcon, and Pythia to perform stable and efficient language modeling with up to 4 million tokens and more. In addition, we discover that adding a placeholder token as a dedicated attention sink during pre-training can further improve streaming deployment. In streaming settings, StreamingLLM outperforms the sliding window recomputation baseline by up to 22.2x speedup. Code and datasets are provided at https://github.com/mit-han-lab/streaming-llm.

核心贡献 · Key contributions

局限 · Limitations

论文章节 · Sections(共 15)

阅读逐段中英对照全文 →