概述
它从每个注意力分数中减去一个固定的、与距离成正比的惩罚,这使得在短序列上训练的模型能够在更长的序列上运行,并被 BLOOM 和 MosaicML 的 MPT 模型采用。
主要要点
- ALiBi 通过从注意力分数中减去与令牌距离成正比的固定惩罚来编码位置,而不是向令牌添加位置嵌入。
- 每个注意力头使用来自几何级数的不同固定斜率,例如在 8 头模型中,斜率从 1/2 到 1/256。
- 2021 年的论文报告称,在 1,024 个令牌上训练的 1.3B 模型的表现与在 2,048 个令牌上训练的正弦模型相当,且训练速度快 11%。
- BigScience 的 BLOOM 和 MosaicML 的 MPT-7B 都使用了 ALiBi,在 MosaicML 的测试中,MPT-7B-StoryWriter 生成了多达 84,000 个令牌。
- ALiBi 有助于模型在超过其训练长度时运行而不崩溃,但其作者发现它可能并未真正利用额外的上下文。
深入探讨
ALiBi, short for Attention with Linear Biases, is a method for giving a transformer language model a sense of word order. A transformer's attention layer compares every token with every other token, but the comparison itself ignores where tokens sit in the sequence, so models need some extra signal about position. ALiBi supplies that signal in the simplest possible way: when one token attends to another, the attention score is reduced by an amount that grows in a straight line with the distance between them. Nearby tokens are penalized a little, distant tokens a lot, and no position vectors are added to the token embeddings at all.
The mechanism works step by step as follows. First, the model computes the usual attention score for a query token at position i and a key token at position j, which is the dot product of the query and key vectors. Second, ALiBi adds a bias equal to a head-specific slope m multiplied by the negative distance, so the key one step back gets minus m, two steps back gets minus 2m, and so on, while the token attending to itself gets zero. Third, the softmax turns the adjusted scores into attention weights as normal. Because the slope differs between attention heads, some heads fall off steeply and focus on the last few tokens while others decay slowly and look much further back. The slopes are fixed before training rather than learned, and the bias is applied to query-key scores in every layer but never to the value vectors.
ALiBi was introduced by Ofir Press, Noah A. Smith and Mike Lewis in the paper Train Short, Test Long, first posted to arXiv on August 27, 2021 and revised in April 2022. The motivating question was extrapolation: can a model trained on sequences of one length make good predictions on longer sequences at inference time? The paper showed that the standard sinusoidal position embeddings from the original transformer break down almost immediately past the training length. On WikiText-103, a sinusoidal model trained on 512 tokens rose from 20.05 perplexity at 512 tokens to 132.41 at 2,512 tokens, while an ALiBi model trained on 512 tokens reached 18.40 at 3,072 tokens, better than a sinusoidal model trained at the full 3,072. Perplexity is a measure of how surprised a model is by the next token, so lower is better.
The practical selling point was cost. Training on short sequences is cheaper because attention cost grows with sequence length. The paper's larger experiment trained 1.3-billion-parameter models on 461 GB of CC100 and RoBERTa text using 128 V100 GPUs, and reports that an ALiBi model trained on 1,024-token inputs matched a sinusoidal model trained on 2,048 tokens while training 11% faster and using 11% less memory. The authors also report the method adds almost nothing at runtime: on one V100, training throughput and memory at 1,024 tokens were within about 1% of the sinusoidal baseline, and the per-head bias mask costs up to roughly 100 MB.
Adoption followed quickly. BigScience's BLOOM, the 176-billion-parameter open multilingual model whose training began on March 11, 2022 on 384 A100 GPUs, lists ALiBi as its positional encoding in its official model card. MosaicML's MPT-7B, released on May 5, 2023 and pretrained on one trillion tokens, also dropped position embeddings for ALiBi. Its StoryWriter variant was fine-tuned with a 65,000-token context, and MosaicML reported it handled about 68,000 tokens of The Great Gatsby and produced generations up to 84,000 tokens, which it attributed to ALiBi's extrapolation.
The obvious alternatives are absolute embeddings, relative biases and rotary embeddings. Absolute embeddings, sinusoidal or learned, attach a vector for each position number to the token, which ties the model to the positions it saw in training. The T5 relative bias learns a bias per bucket of distances; in the ALiBi paper it extrapolated better than sinusoidal and was the strongest baseline, but trained at least twice as slowly. Rotary Position Embedding (RoPE), proposed by Jianlin Su and colleagues in April 2021, rotates query and key vectors by an angle set by position so that scores depend on relative distance; it extrapolated better than sinusoidal in the ALiBi comparison but not as well as ALiBi. RoPE is the scheme used by models such as LLaMA, and methods such as Position Interpolation, posted by Chen and colleagues in June 2023, extend those RoPE models to 32,768 tokens with fewer than 1,000 fine-tuning steps.
ALiBi has real limits, several stated by its own authors. Their sliding-window analysis found that much of the gain at long evaluation lengths comes from reducing the early token curse, the poor predictions a model makes near the start of a chunk where it has little preceding context, rather than from actually using information beyond the training length. In other words, ALiBi lets a model run on longer inputs without collapsing, but it may not read the extra text in a useful way. On the 1.3B experiments, ALiBi's perplexity was best near twice the training length and only stayed usable to around 10,000 tokens. The slopes were picked from about ten manual trials on one dataset, learned slopes extrapolated poorly, and most experiments used a 247-million-parameter model. A 2023 NeurIPS study by Kazemnejad and colleagues found that on downstream reasoning-style tasks, ALiBi, rotary and absolute embeddings all generalized poorly to longer lengths, and a decoder with no positional encoding did better.
In practice ALiBi requires very little. It adds no trainable parameters, only a fixed matrix of distance penalties per head that can be precomputed, and the slope formula is a single line of code. The main engineering requirement is an attention kernel that accepts an additive bias, which matters because fused kernels such as FlashAttention need explicit support for it; MPT shipped with FlashAttention and ALiBi together. The method suits teams that want cheaper short-context pretraining with graceful behavior on longer inputs, while teams that need a model to retrieve facts from far back in a very long document should test long-context recall directly rather than rely on low perplexity alone.
04工作示例
计算一个微型 4 头模型的 ALiBi 偏置
选择头的数量 n = 4,计算斜率基数 2^(-8/4) = 2^(-2) = 0.25。
将头的斜率列为以该首项和公比构成的等比数列:0.25、0.0625、0.015625 和 0.00390625。
取查询位置为 5,关注键位置 1 到 5,则距离分别为 4、3、2、1、0。
对于头 1,将每个距离乘以 -0.25,得到偏置 -1.0、-0.75、-0.5、-0.25 和 0。
对于头 4,将每个距离乘以 -0.00390625,得到偏置约为 -0.016、-0.012、-0.008、-0.004 和 0。
将每个头的偏置向量加到该头的原始查询‑键得分上,应用因果掩码,然后进行 softmax。
它显示了什么
头 1 现在强烈偏好最近的标记,而头 4 对距离几乎中性。通过确认对角线上的偏置全部为零,其他位置为负,并且随距离线性增大来检查计算是否正确。
现实世界的实施
BigScience 的 BLOOM 是一个 1760 亿参数的开放多语言模型,训练于 2022 年,在其 Hugging Face 模型卡中将 ALiBi 列为位置编码,训练序列长度为 2,048 标记。
MosaicML 的 MPT-7B 系列于 2023 年 5 月 5 日发布,取代了位置嵌入,改用 ALiBi;StoryWriter 变体在 65k 令牌上进行微调,并在公司测试中生成最多 84k 令牌。
降低预训练成本:ALiBi 论文报告了一个 1.3B 参数模型,在 1,024 令牌输入上训练,匹配一个在 2,048 令牌上训练的正弦模型,同时训练速度快 11%,内存使用少 11%。
长度泛化的研究基准,例如 Kazemnejad 等人 2023 年的 NeurIPS 研究,比较了 ALiBi 与旋转、T5 风格相对偏置、绝对嵌入以及完全没有位置编码的情况。
ALiBi 位置偏置的未来:带线性偏置的注意力机制
The central open problem ALiBi exposed is the gap between running on long inputs and using them. Its own authors concluded that ALiBi may not exploit context beyond the training length, and the 2023 NeurIPS study of positional encodings by Kazemnejad and colleagues found ALiBi, rotary and absolute embeddings all generalize poorly on longer downstream tasks, with no positional encoding performing better in their setup. That result keeps alive the question of how much explicit position information decoder-only models need at all. A separate line of work extends rotary-embedding models instead, with techniques such as Position Interpolation (June 2023) stretching LLaMA models to 32,768 tokens after short fine-tuning. ALiBi remains in widely used open checkpoints, notably BLOOM and the MPT family, and its core idea of a cheap, fixed recency bias continues to appear as a baseline in length-generalization research. Whether future long-context models favor distance biases, rotation-based schemes, hybrids, or no explicit positions is an active research question rather than a settled one, and any claim about long-context quality is best checked with retrieval and reasoning tests at the target length.
付诸实践
为什么这很重要
- 速度与规模. 语言工作流程可以在不牺牲一致性的情况下更快地移动。
- 交通与覆盖范围. 它扩展了跨语言和沟通方式的访问。
- 更清晰的判决. 团队可以花更多时间进行判断,而自动化则可以处理重复。
留意
- 幻觉的事实可以悄悄地进入报告、支持流程或研究成果。
- 及时的敏感性可能会在类似的请求中产生不一致的结果。
- 如果访问控制薄弱,敏感文本数据可能会暴露。
后续步骤
- 在推出之前定义输出格式、语气和质量标准。
- 当准确性很重要时,请使用可信来源进行地面响应。
- 为高风险输出保留人工审查检查点。
- 跟踪故障模式并定期重新训练提示或工作流程。
资料来源与延伸阅读
- arXiv (Press, Smith, Lewis)Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation
- BigScience on Hugging Facebigscience/bloom 模型卡
- MosaicML / DatabricksIntroducing MPT-7B: A New Standard for Open-Source, Commercially Usable LLMs
- arXiv (Su et al.)RoFormer: Enhanced Transformer with Rotary Position Embedding
- arXiv / NeurIPS 2023 (Kazemnejad et al.)The Impact of Positional Encoding on Length Generalization in Transformers
- arXiv (Chen et al.)Extending Context Window of Large Language Models via Positional Interpolation
不断探索
常见问题
什么是 ALiBi Position Bias: Attention with Linear Biases?
ALiBi (Attention with Linear Biases) 是一种在不添加位置嵌入的情况下告知 Transformer 每个 token 位置的方法。它从每个注意力分数中减去一个固定的、与距离成比例的惩罚项,这使得在短序列上训练的模型能够在更长的序列上运行,并已被 BLOOM 和 MosaicML 的 MPT 模型所采用。
ALiBi 代表什么?
ALiBi 是 Attention with Linear Biases 的缩写,因其添加到注意力分数中的线性惩罚而得名。
ALiBi如何惩罚远程代币?
ALiBi 从注意力分数中减去与查询键距离成比例的值,因此距离越远的标记权重越小。
ALiBi最著名的实际优势是什么?
由于偏差仅取决于距离,因此在短序列上训练的模型可以在推理时处理更长的序列。
注意力头之间的线性偏差有什么不同?
ALiBi 为每个头分配一个不同的固定斜率(例如 1/2、1/4、1/8),因此不同的头参与不同的范围。
与传统的位置嵌入相比,ALiBi 增加了多少学习参数?
ALiBi 没有引入新的学习参数;人均斜率是预先确定的常数。