无需位置编码的双向注意力机制,让模型自动捕捉序列位置信息。
Dual Triangle Attention: Effective Bidirectional Attention Without Positional Embeddings
- 用双三角掩码分离查询键空间,实现双向上下文感知
- 在无位置编码下仍能准确学习位置信息,优于标准双向注意力
- 适合追求轻量化或对位置敏感的任务,如蛋白质建模
双向Transformer是自然、生物和化学语言任务的基础,但缺乏显式位置编码时会丧失顺序感知能力。相比之下,单向注意力通过三角掩码天然编码位置信息,可完全不依赖位置嵌入。本文提出双三角注意力(Dual Triangle Attention),将每个注意力头的查询-键子空间分解为两个互补的三角掩码:一个关注过去与自身位置,另一个关注未来与自身位置。该设计在保持双向上下文的同时,使两个方向都具备因果掩码的隐式位置归纳偏置。基于PyTorch的flex_attention,该机制仅需一次编译内核调用,无额外参数。我们在三个场景下评估:(1) 合成argmax位置探测任务,(2) 自然语言的掩码语言建模(MLM),(3) 蛋白质序列的掩码语言建模。结果表明,在无位置编码情况下,双三角注意力与因果注意力均能学习位置信息,而标准双向注意力无法做到。在MLM任务中,结合旋转位置编码(RoPE)的双三角注意力表现出最佳上下文扩展性能,并在各类任务中表现优异。这表明双三角注意力是无需位置编码的双向变压器的可行替代方案。
原文摘要 · Abstract (English)
Bidirectional transformers are the foundation of many sequence modeling tasks across natural, biological, and chemical language domains, but they are permutation-invariant without explicit positional embeddings. In contrast, unidirectional attention inherently encodes positional information through its triangular mask, enabling models to operate without positional embeddings altogether. Here, we introduce Dual Triangle Attention, a novel bidirectional attention mechanism that separates the query-key subspace of each attention head into two complementary triangular masks: one that attends to past-and-self positions and one that attends to future-and-self positions. This design provides bidirectional context while maintaining the causal mask's implicit positional inductive bias in both directions. Using PyTorch's flex_attention, Dual Triangle Attention is implemented as a single compiled kernel call with no additional parameters beyond standard multi-head attention. We evaluated Dual Triangle Attention across three settings: (1) a synthetic argmax position probe, (2) masked language modeling (MLM) on natural language, and (3) MLM on protein sequences. In the argmax task, both Dual Triangle Attention and causal attention learn positional information without explicit positional embeddings, whereas standard bidirectional attention cannot. In the MLM experiments, Dual Triangle Attention with Rotary Positional Embeddings (RoPE) achieved the best context extension performance and strong performance across the board. These findings suggest that Dual Triangle Attention is a viable attention mechanism for bidirectional transformers, with or without positional embeddings.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。