滑动窗口胜过线性注意力
研究论文 · 2026/08/31
由于二次注意力的特性,大型语言模型(LLM)消耗大量内存和能量。每个新token的成本都比前一个高,且键和值需无限期存储,这不可持续。为此,研究者提出了多种替代方案,其中之一是将LLM改造为线性注意力。该方案因有望以低成本解决二次缩放问题而备受关注,但尚未与更简单的基线进行充分比较。本研究表明,带sink的滑动窗口注意力(SWA)在多个LLM和多种下游任务上表现与后训练的线性注意力模型相当或更优。在长上下文推理任务(如Needle-in-a-Haystack和BABILong)中,SWA的性能大幅领先(比线性注意力高2到10倍)。SWA无需后训练,速度快且内存占用低,是极其廉价可靠的解决方案。为降低推理内存成本,我们强烈建议改用SWA而非后训练线性模型。线性注意力模型虽有潜力,但可能需从头训练或大量后训练才能匹敌SWA。
本文挑战了线性注意力作为二次注意力替代方案的主流观点,通过实验证明简单的滑动窗口注意力(SWA)在性能和成本上均优于后训练的线性注意力模型,为LLM的高效推理提供了更实用的解决方案,具有重要的实践指导意义。
Due to the nature of quadratic attention, Large Language Models (LLMs) consume a lot of memory and energy. Every new token costs more than the previous one. For each additional token, the keys and values must be stored in memory indefinitely, which is unsustainable. Several alternatives have been proposed to fix the quadratic scaling problem, one of which is retrofitting LLMs to use Linear Attention. This idea has attracted a lot of attention, given its promise to solve the quadratic scaling problem with state-of-the-art performance at low cost. However, this line of research has not been properly compared to simpler baselines. In this work, we show that Sliding Window Attention (SWA) with sinks performs as well or better than post-trained Linear Attention models. We observe this across multiple LLMs on various downstream tasks. For long-context reasoning tasks (Needle-in-a-Haystack and BABILong), SWA achieves massively higher performance (2 to 10 times higher than linear attention). SWA requires no post-training, is extremely fast, and requires low memory; therefore, making it an extremely cheap and reliable solution. To reduce inference memory cost, we strongly recommend switching to SWA instead of post-training linear models. Linear attention models may have shown some promise, but they likely require to be trained from scratch or extensive post-training in order to even match SWA.