具有对数线性复杂度的块稀疏注意力
研究论文 · 2026/09/28
将语言模型扩展到长上下文受限于自注意力的二次成本。块稀疏注意力提供了一种高效替代方案,但选择保留块仍是瓶颈。传统块选择需要对所有查询-块对进行评分,因此序列长度上仍是二次的。为解决此问题,我们提出PISA,一种采用金字塔Top-K选择策略的块稀疏注意力机制。主要思想是在不同层级逐步缩小候选范围,从而更高效地找到最相关的键。具体而言,我们构建从粗到细的键层次结构,并从最粗层级开始选择。在每个层级,对有限候选集应用LogSumExp评分,以选择下一更细层级的候选,直至到达最细层级。通过池化,我们构建O(log N)层级的键,总体复杂度为O(Nlog N),其中N表示序列长度。我们为训练和推理开发了硬件感知的Triton内核,融合分层路由和LogSumExp评分,而无需物化查询-键评分矩阵。我们进一步在语言建模任务上评估了我们的方法。与基线相比,我们的方法在常识推理等基准上达到相当的性能,同时在检索任务上取得更好的结果。
该工作通过金字塔Top-K选择策略和硬件感知内核,将块稀疏注意力的选择复杂度从二次降低到O(Nlog N),为长上下文语言模型的高效扩展提供了实用方案,并在检索任务上表现出优势。
Scaling language models to long contexts is limited by the quadratic cost of self-attention. Block sparse attention offers an efficient alternative, but selecting the retained blocks remains a bottleneck. Conventional block selection requires scoring all query-block pairs and therefore remains quadratic in sequence length. To address this issue, we propose PISA, a block-sparse attention mechanism that employs a pyramid Top-K selection strategy. The main idea is to gradually narrow down the candidates across different levels, making it more efficient to find the most relevant keys. Specifically, we construct a coarse-to-fine hierarchy of keys and perform selection from the coarsest level. At each level, LogSumExp scoring is applied to a bounded candidate set to select candidates for the next finer level, continuing until the finest level is reached. Through pooling, we construct O(log N) levels of keys, yielding an overall complexity of O(Nlog N), where N denotes the sequence length. We develop hardware-aware Triton kernels for both training and inference, fusing hierarchical routing and LogSumExp scoring without materializing the query-key score matrix. We further evaluate our method on language modeling tasks. Compared with the baseline, our method achieves comparable performance on benchmarks such as commonsense reasoning while delivering better results on retrieval tasks.