动态切换注意力机制,让模型高效处理长文本
Switch Attention: Towards Dynamic and Fine-grained Hybrid Transformers
- 按需动态选择全局或局部注意力,计算更灵活
- 在4K和32K上下文上均优于传统混合模型
- 适合需要长序列建模的NLP任务
注意力机制是现代Transformer的核心组件。但标准全注意力计算复杂度随序列长度呈平方增长,成为长上下文语言建模的主要瓶颈。滑动窗口注意力虽提升效率,却限制了感受野。现有混合模型多采用静态、启发式交替模式,难以适应不同场景的计算分配需求。本文提出Switch Attention(SwiAttn),一种新型混合Transformer,实现全注意力与滑动窗口注意力之间的动态、细粒度路由。每个令牌在每层中可动态选择进入全注意力分支(获取全局信息)或滑动窗口分支(高效匹配局部模式)。设计自适应正则化目标以促进效率。采用持续预训练,将全注意力架构迁移至混合结构。在23个基准数据集上,涵盖常规(4K)和长上下文(32K)场景,实验验证了方法的有效性。
原文摘要 · Abstract (English)
The attention mechanism has been the core component in modern transformer architectures. However, the computation of standard full attention scales quadratically with the sequence length, serving as a major bottleneck in long-context language modeling. Sliding window attention restricts the context length for better efficiency at the cost of narrower receptive fields. While existing efforts attempt to take the benefits from both sides by building hybrid models, they often resort to static, heuristically designed alternating patterns that limit efficient allocation of computation in various scenarios. In this paper, we propose Switch Attention (SwiAttn), a novel hybrid transformer that enables dynamic and fine-grained routing between full attention and sliding window attention. For each token at each transformer layer, SwiAttn dynamically routes the computation to either a full-attention branch for global information aggregation or a sliding-window branch for efficient local pattern matching. An adaptive regularization objective is designed to encourage the model towards efficiency. Moreover, we adopt continual pretraining to optimize the model, transferring the full attention architecture to the hybrid one. Extensive experiments are conducted on twenty-three benchmark datasets across both regular (4K) and long (32K) context lengths, demonstrating the effectiveness of the proposed method.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。