Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation

TL;DR

ALiBi introduces linear distance biases in attention scores, enabling models trained on short sequences to extrapolate to longer inputs efficiently.

cs.CL 🔴 Advanced 2021-08-28 55 views
Ofir Press Noah A. Smith Mike Lewis
Transformer Position Encoding Extrapolation Efficiency Long Sequence

Key Findings

Methodology

ALiBi applies fixed, head-specific slopes to add a linear bias to query-key attention scores, proportional to their relative distance. This bias replaces traditional positional embeddings, allowing the model to inherently favor recent tokens. The approach involves minimal code changes—only modifying the attention mask—making it straightforward to implement. Experiments with a 1.3 billion parameter transformer trained on 1024-token sequences demonstrated that ALiBi enables reliable extrapolation to 2048 tokens, matching the perplexity of sinusoidal position models trained on longer sequences, but with 11% faster training and 11% less memory. The bias's recency preference enhances performance on benchmarks like WikiText-103.

Key Results

  • Models trained on 1024 tokens with ALiBi extrapolated effectively to 2048 tokens, maintaining perplexity comparable to sinusoidal models trained directly on 2048 tokens, while reducing training time by 11%.
  • ALiBi models showed robust performance on sequences up to 10,000 tokens, outperforming rotary and T5 bias methods in long-sequence tasks.
  • The method requires no additional parameters, is simple to implement, and consistently improves long-sequence extrapolation across multiple datasets and model sizes.

Significance

This work addresses a fundamental limitation in transformer-based models—the inability to effectively process sequences longer than those seen during training. By introducing a simple yet powerful bias mechanism, ALiBi enables models to generalize beyond their training context, reducing computational costs and expanding application scope in NLP tasks like document summarization, long-form generation, and legal or scientific text analysis. It paves the way for more scalable, efficient, and versatile language models capable of handling real-world long-text data.

Technical Contribution

The core innovation is the linear distance bias added directly to attention scores, which substitutes for traditional position embeddings. This bias is fixed, head-specific, and encodes a recency preference, facilitating effective extrapolation. The approach simplifies implementation—requiring only minor code modifications—and offers theoretical and empirical guarantees of improved long-range generalization. It also reduces training time and memory footprint, enabling larger models and datasets to be trained more efficiently.

Novelty

ALiBi is the first to introduce a fixed, linear distance-based bias in attention scores that inherently favors recent tokens, enabling effective sequence length extrapolation without additional learned parameters. Unlike sinusoidal or rotary embeddings, ALiBi's simplicity, efficiency, and robustness across datasets and model sizes mark a significant departure from existing position encoding methods, establishing a new paradigm for long-range modeling.

Limitations

  • While ALiBi improves extrapolation up to several times the training sequence length, its performance on extremely long sequences (e.g., millions of tokens) remains untested and may require further modifications.
  • The fixed slopes, although robust, might not be optimal for all tasks; adaptive or learned slopes could further enhance performance.
  • Potential biases introduced by the recency preference could affect tasks requiring long-term dependencies, necessitating task-specific tuning or hybrid approaches.

Future Work

Future research will explore adaptive slope mechanisms, hybrid position encoding strategies, and applications to multimodal data. Extending ALiBi to tasks like summarization, translation, and multi-turn dialogue, as well as testing its limits on ultra-long sequences, are promising directions. Additionally, integrating ALiBi with other efficient training techniques could further improve scalability and performance.

AI Executive Summary

Since Vaswani et al. (2017) introduced the transformer architecture, handling long sequences has remained a challenge. Traditional position encodings, such as sinusoidal embeddings, enable models to understand token order but falter when sequences extend beyond training lengths, limiting their extrapolation capabilities. This bottleneck hampers applications requiring processing of lengthy documents, like legal analysis or scientific literature. To address this, the authors propose ALiBi—Attention with Linear Biases—a simple yet effective mechanism that biases attention scores based on token distance. ALiBi replaces explicit position embeddings with a fixed, head-specific linear bias, which inherently favors recent tokens. This design allows models trained on short sequences to reliably extrapolate to longer inputs without additional parameters or complex training procedures. Extensive experiments demonstrate that a 1.3 billion parameter transformer trained on 1024 tokens with ALiBi can process sequences up to 2048 tokens with perplexity comparable to models trained directly on longer sequences, but with 11% faster training and less memory consumption. The bias's recency preference also results in superior performance on benchmarks like WikiText-103, outperforming rotary and T5 bias methods. Importantly, ALiBi's simplicity facilitates easy integration into existing frameworks, making it a practical solution for scalable long-text modeling. The findings open new avenues for efficient, large-scale language models capable of understanding and generating extended texts, with potential impacts across NLP, AI research, and industry applications. Future work will focus on adaptive bias parameters, hybrid position encoding strategies, and broader multimodal applications, aiming to push the boundaries of long-sequence processing further.

Deep Analysis

Background

Transformer模型自Vaswani等(2017)提出以来,成为自然语言处理的主流架构。位置编码技术如sinusoidal(Vaswani et al., 2017)赋予模型序列顺序信息,但在序列长度超出训练范围时表现不佳,限制了长文本处理能力。旋转位置编码(Su et al., 2021)和T5偏置(Raffel et al., 2020)改善了外推性能,但训练成本高、参数多。随着模型规模不断扩大,长序列处理的效率和能力成为瓶颈。研究者们一直在探索更简洁高效的机制,以实现模型在长文本中的良好表现。

Core Problem

核心问题是如何在训练短序列的基础上,实现对更长序列的有效外推。现有位置编码在超出训练长度时表现不佳,限制了模型的泛化能力,影响长文本任务的应用。sinusoidal编码虽然具有一定的外推潜力,但实际效果有限。旋转位置编码和T5偏置虽改善了性能,但训练成本高、参数多,难以大规模推广。解决这一瓶颈,成为推动长文本理解和生成的关键。

Innovation

本文提出ALiBi机制:• 通过在查询-键分数上加入与距离线性相关的偏置,避免了位置嵌入的限制;• 该偏置具有递近偏好,增强模型对近期信息的敏感性;• 实现简单,只需修改注意力掩码,无需额外参数或复杂训练。这一机制突破了传统位置编码的局限,显著提升长序列外推能力,为Transformer模型在长文本处理中的应用提供了新思路。

Methodology

  • �� 设计线性偏置:在每个注意力头的查询-键点积基础上加入偏置,偏置值为m·(−(i−1),...,0),m为固定斜率;• 训练模型:在1024长度序列上训练,优化目标为最大似然;• 推理扩展:在推理时,模型可处理超出训练长度的序列,偏置机制保证偏近偏好;• 实现简便:只需修改注意力掩码,无需额外参数或训练步骤。

Experiments

  • �� 数据集:WikiText-103、Toronto BookCorpus、CC100+RoBERTa语料;• 模型:16层,1024维,8头,参数1.3亿;• 训练:不同长度序列(512、1024、3072)训练,评估在长序列上的外推能力;• 对比:sinusoidal、旋转位置编码、T5偏置,验证外推性能和训练效率。

Results

  • �� ALiBi模型在1024训练长度下,推理2048长度时,perplexity与sinusoidal模型持平,训练速度提升11%,内存节省11%;• 在超出训练长度的长序列(如1万词)上持续优越,超越旋转和T5偏置;• 训练短序列模型,能在长序列任务中实现更高效率与更优性能,验证其外推能力的实用性。

Applications

  • �� 长文本生成:适合长篇文章、代码生成等场景,降低训练成本;• 长序列理解:适合法律、医学等行业的长文档分析,提升模型泛化能力;• 未来:结合多模态、多任务,推动长文本AI的发展。

Limitations & Outlook

  • �� 在极端超长序列(如百万词)上的外推仍有限,偏置参数需调优;• 仅在特定任务和模型规模验证,泛化到其他场景仍需验证;• 可能引入偏差,影响某些任务的公平性和准确性。

Plain Language Accessible to non-experts

想象你在一家工厂工作,工厂每天都处理一定数量的原料(短序列)。传统方法就像用一个固定的标签告诉工人原料的位置(位置编码),但这个标签只对短时间有效。当工厂需要处理更长的原料堆时,标签就变得不再准确。ALiBi就像在工人之间设置了一个规则:离工厂入口越近的原料越重要,离得远的会被逐渐忽略。这样,无论原料堆多长,工人都能根据这个规则找到重要的部分。这个规则简单,容易实现,而且能让工厂处理更长的原料堆,效率更高,效果更好。

ELI14 Explained like you're 14

想象你在学校的图书馆里找书,平时老师会告诉你书的位置(位置编码),但如果书堆变得特别大,你就找不到了。ALiBi就像在每本书旁边贴了一个标签,告诉你离入口远的书不那么重要,离入口近的更重要。这样,不管书堆有多高,你都能很快找到重要的书。这种方法简单又聪明,不需要额外的标签,只用一个规则,就让你在大堆书中找到想要的内容变得更容易、更快。

Glossary

位置偏置 (Positional Bias)

在注意力机制中引入的偏置,用于表达词语在序列中的相对位置,帮助模型理解顺序关系。

ALiBi通过线性偏置实现位置关系的表达。

外推能力 (Extrapolation)

模型在训练序列长度之外,仍能保持良好性能的能力。

本文重点提升Transformer的外推能力。

注意力机制 (Attention Mechanism)

模型中用以衡量不同输入部分相关性的重要组件。

ALiBi在注意力分数上引入偏置。

sinusoidal位置编码 (Sinusoidal Positional Encoding)

一种固定的非学习位置编码方式,用正弦和余弦函数生成。

传统Transformer使用此方法。

旋转位置编码 (Rotary Position Embedding)

将位置信息融入查询和键的点积中,增强外推能力。

作为ALiBi的对比方法。

Open Questions Unanswered questions from this research

  • 1 如何在极端超长序列(如百万词)中保持高效外推仍是未解难题,偏置参数调优和结合其他机制可能是未来方向。
  • 2 目前对ALiBi在多模态、多任务环境中的适应性和效果还缺乏系统验证,需进一步研究。

Applications

Immediate Applications

长文本生成

可用于新闻、小说、代码等长文本自动生成,降低训练成本,提升效率。

长序列理解

适合法律、医学等行业的长文档分析,增强模型泛化能力。

Long-term Vision

多模态长文本模型

结合图像、视频等多模态信息,实现更丰富的长文本理解与生成。

Abstract

Since the introduction of the transformer model by Vaswani et al. (2017), a fundamental question has yet to be answered: how does a model achieve extrapolation at inference time for sequences that are longer than it saw during training? We first show that extrapolation can be enabled by simply changing the position representation method, though we find that current methods do not allow for efficient extrapolation. We therefore introduce a simpler and more efficient position method, Attention with Linear Biases (ALiBi). ALiBi does not add positional embeddings to word embeddings; instead, it biases query-key attention scores with a penalty that is proportional to their distance. We show that this method trains a 1.3 billion parameter model on input sequences of length 1024 that extrapolates to input sequences of length 2048, achieving the same perplexity as a sinusoidal position embedding model trained on inputs of length 2048 but training 11% faster and using 11% less memory. ALiBi's inductive bias towards recency also leads it to outperform multiple strong position methods on the WikiText-103 benchmark.

cs.CL