arXiv:2507.23083cs.CL2025-07被引 5

让位置编码学会看上下文,提升Transformer模型表现

Context-aware Rotary Position Embedding

  • 根据输入内容动态生成位置频率,取代固定正弦模式
  • 在长序列上显著降低困惑度,训练速度更快
  • 适合需要精准位置感知的NLP任务,如文本生成

位置编码是Transformer架构的核心组件,用于将序列顺序信息引入自注意力机制。旋转位置编码(RoPE)因其与相对位置编码兼容且计算高效而被广泛应用。然而,RoPE依赖静态、与输入无关的正弦频率模式,难以建模上下文敏感关系。本文提出CARoPE(上下文感知旋转位置编码),一种对RoPE的新型泛化方法,可基于标记嵌入动态生成头特定的频率模式。该设计在保持RoPE效率和结构简单性的同时,引入了标记与上下文相关的位置表示。CARoPE通过有界变换计算输入相关的相位偏移,并将其整合到各注意力头的旋转机制中。我们在FineWeb-Edu-10B数据集上使用GPT-2变体进行下一项词预测任务训练,实验表明,CARoPE始终优于RoPE及其他常见位置编码基线,在更长上下文长度下仍能显著降低困惑度。此外,CARoPE实现了更快的训练吞吐量而不牺牲模型稳定性。这些结果表明,CARoPE为Transformer模型的位置编码策略提供了一种可扩展、表达能力强且高效的升级方案。

原文摘要 · Abstract (English)

Positional encoding is a vital component of Transformer architectures, enabling models to incorporate sequence order into self-attention mechanisms. Rotary Positional Embeddings (RoPE) have become a widely adopted solution due to their compatibility with relative position encoding and computational efficiency. However, RoPE relies on static, input-independent sinusoidal frequency patterns, limiting its ability to model context-sensitive relationships. In this work, we propose CARoPE (Context-Aware Rotary Positional Embedding), a novel generalization of RoPE that dynamically generates head-specific frequency patterns conditioned on token embeddings. This design introduces token- and context-sensitive positional representations while preserving RoPE efficiency and architectural simplicity. CARoPE computes input-dependent phase shifts using a bounded transformation of token embeddings and integrates them into the rotary mechanism across attention heads. We evaluate CARoPE on the FineWeb-Edu-10B dataset using GPT-2 variants trained on next-token prediction tasks. Experimental results show that CARoPE consistently outperforms RoPE and other common positional encoding baselines, achieving significantly lower perplexity, even at longer context lengths. Additionally, CARoPE enables faster training throughput without sacrificing model stability. These findings demonstrate that CARoPE offers a scalable, expressive, and efficient upgrade to existing positional encoding strategies in Transformer models.

位置编码TransformerRoPE上下文感知

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。