Paper commentary
Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation
- Problem With sinusoidal position embeddings, a transformer language model stops improving after a few dozen tokens beyond its training length.
- Method Adds a static, head-specific linear penalty to query-key scores in place of position embeddings, with the slopes fixed before training.
- Results A 1.3B ALiBi model trained on 1024 tokens matched a sinusoidal model trained on 2048 tokens when both were tested on 2048 tokens, and it was 11% faster and used 11% less memory.
Why sinusoidal models stop improving past their training length
A transformer language model is normally trained and evaluated on inputs of one length, so its behaviour on longer inputs is never exercised during training. The question is whether the model keeps lowering perplexity when validation inputs grow past the training length L. Vaswani et al. speculated that the transformer might extrapolate to longer sequences, and the sinusoidal design was the basis for that hope.
The L = 1024 model improves up to L + 50 tokens before performance declines, so the sinusoidal approach stops helping within a few dozen tokens beyond training.
The authors attribute this failure to the position embedding method rather than to other parts of the model. A learned position embedding cannot encode positions beyond L at all, so it has no route to extrapolation.
Rotary and T5 bias: what they change and what they cost
The values receive no position information in this method.
With rotary embeddings, the L = 512 model improves perplexity up to 200 extra tokens and the L = 1024 model up to 100, but training and inference are slower.
Distances close to the query each get their own bias, and beyond a certain distance several distances share one bias.
The T5 bias extends usable length further, by 600 extra tokens for L = 512 and 800 for L = 1024.
Adding a distance penalty to attention scores
In a standard transformer, the scores for the i-th query come from its dot products with the first i keys, followed by a softmax. ALiBi removes the positional embeddings and changes only this step: after the query-key dot product, it adds a static bias that is not learned.
The modified attention is computed as follows.
Here \( q_i \in \mathbb{R}^{1\times d} \) is the i-th query, \( K \in \mathbb{R}^{i\times d} \) holds the first i keys, and d is the head dimension. The bias vector is 0 for the query's own key and falls by one for each step back, and the scalar m is a head-specific slope fixed before training. The bias is not multiplied by the scaling factor from the original attention equation.
With 16 heads, the authors interpolate between these 8 slopes by geometric averaging, which gives a sequence starting at \( \frac{1}{\sqrt{2}} \) with ratio \( \frac{1}{\sqrt{2}} \).
The authors expect this slope set to work across text domains and model sizes, so they do not plan to retune it for each new dataset. Trainable slopes did not give strong extrapolation, and a brief search over around ten slope sets led to the set used. Slope sets whose slopes lie in (0, 1) with density rising toward 0 worked best, and random draws from an exponential distribution also worked in some cases, with high variance.
Position information is added to the queries and keys at every layer but not to the values, in line with the rotary and T5 bias methods. Implementation adds the linear biases to the mask matrix before the softmax, so there is no extra runtime, but each head has its own slope, which makes the mask n×L×L instead of L×L and costs up to 100MB.
Speed and memory against the sinusoidal baseline
Batched training, inference and memory were measured for the sinusoidal, rotary, T5 bias and ALiBi methods at input lengths 512, 1024 and 3072. Between ALiBi and the sinusoidal method, training speed differs by within 1% and inference by within 3%, which the authors consider negligible on their hardware. ALiBi uses 100MB of extra memory at input lengths 1024 and 3072 in this setting.
Against sinusoidal embeddings at an identical input length, ALiBi has no added runtime or parameters, and its memory increase is 0 to 0.7%. The extra memory comes from the larger mask rather than from any extra operations.
The main saving comes from training on shorter inputs, since the attention matrices grow with input length. The L = 512 ALiBi model trains 1.84 times faster than the L = 3072 sinusoidal model. On the 1.3B model, the ALiBi model trained on L = 1024 is 11% faster and uses 11% less memory than the sinusoidal model trained on L = 2048.
WikiText-103 results across training lengths
The base model is the Baevski & Auli transformer with 16 layers of dimension 1024 and 8 heads, trained on WikiText-103, and ALiBi replaces its sinusoidal embeddings. At input length 3072 with Lvalid = 3072, the sinusoidal model scores 18.67±0.24 (std. dev.) perplexity, while the ALiBi model trained on L = 3072 scores 17.60. Across all training lengths, ALiBi models outperform the sinusoidal models, including those trained on fewer tokens.
A model trained on 512 tokens scores 19.73 at 512 on the development set and 18.40 when extrapolated to subsequences of length 3072. The authors report that this 18.40 surpasses the sinusoidal L = 3072 model on the development set by a statistically significant margin.
All ALiBi models from L = 512 to L = 2048 beat the sinusoidal L = 3072 baseline when evaluated at Lvalid = 3072, while training on shorter subsequences. Training the L = 3072 sinusoidal model needs a GPU with more than 16 GB of memory for its attention matrices, whereas the L = 512 model fits on a GPU with much less memory.
The T5 bias improves perplexity until Lvalid is around 2L, while ALiBi keeps improving until at least around 3L, and the L = 512 model keeps improving even beyond 12k tokens. ALiBi also beats the rotary and T5 bias models at L = 1024 and 3072 when Lvalid = L, and its validation gains carry over to the WikiText-103 test set.
Under sliding window evaluation, the L = 3072 model surpasses Transformer-XL, the Sandwich and Shortformer, is similar to staged training, and falls short of Routing Transformer and kNN-LM. The authors hypothesize that combining the methods behind those two models with ALiBi might give further gains.
The 1.3B model on CC100+RoBERTa
The CC100+RoBERTa corpus totals 461 GB, combining the RoBERTa training data with the English part of CC-100, and its validation set contains 649K tokens. The model has 25 transformer layers with 16 heads and dimension 2048, an 8192 hidden dimension in its feedforward sublayers, and about 1.3B parameters, trained for one epoch of 50k updates on 128 V100 GPUs.
The left panel compares an ALiBi model trained on 512 tokens with a sinusoidal model trained on 1024 tokens, both validated at Lvalid = 1024. The ALiBi model is 7% faster and uses 1.6 GB less memory, and at the end of the epoch it differs from the baseline by 0.06 perplexity.
In the right panel, the ALiBi model trained on 1024 tokens beats the sinusoidal model trained on 2048 tokens by 0.09 perplexity when evaluated with Lvalid = 2048, while using 3.1 GB less memory. The ALiBi model stays ahead of the sinusoidal model throughout training, and at five evenly spaced points it reaches a given perplexity on average 11% faster.
When both models are trained on the same L, ALiBi performs similarly to the sinusoidal baseline without extrapolation, which differs from the smaller WikiText-103 and books experiments. The authors suggest that ALiBi's inductive bias gives additional benefits in lower-resource language modeling.
Validation perplexity with extrapolation shows that the L = 512 model scores 9.79 at Lvalid = 512 and reaches its best score of 9.3 at 1012 tokens. The L = 1024 model scores 9.16 at Lvalid = 1024 and reaches its best score of 8.9 at 2024 tokens, and the ALiBi curves stay strong up to 10000 tokens. The sinusoidal models degrade as soon as one token beyond L is added during evaluation.
One possible explanation the authors give is that at inference length 2L, half of the subsequences are as long as the training examples. Beyond about 2L, fewer predictions come from lengths seen in training, which the authors think might degrade performance.
Where ALiBi sits among related position methods
Wennberg and Henter introduced a relative position bias that also depends on distance, but it uses a radial-basis function with trainable parameters. Their experiments cover text classification rather than language modeling, and they do not explore extrapolation.
The Distance Aware Transformer multiplies attention scores by a distance bias with a learned parameter in every head, and it reports results only on text classification. The authors' own unpublished test found that multiplying the scores, rather than adding the bias as ALiBi does, degraded performance.
Transformer-XL uses a cache to attend to more tokens at inference, but its reported results keep output length within the training length, and its relative position method is slow. Longformer adapts shorter-trained models to longer documents, but only after partial training on longer sequences, whereas ALiBi needs no such training.
Extrapolation has been studied before on other tasks, including machine translation, sequence-to-sequence arithmetic, reinforcement learning and protein structure prediction, but not in transformer language modeling.
Limitations
The position-method comparison on WikiText-103 uses one base model, the Baevski & Auli transformer. The 1.3B experiments use a single model on CC100+RoBERTa trained for one epoch, and a books experiment appears only in the appendix.
Slopes were chosen from a brief manual search of around ten slope sets, and trainable slopes did not extrapolate strongly.
On CC100+RoBERTa, the best ALiBi results occur at around 2L, and the statement about maintained performance up to 10000 tokens comes from these experiments only.
Sliding window evaluation in the appendix is a different protocol from the nonoverlapping inference used for the main perplexities. The rotary speed numbers come from code that the authors later found could be optimized to 2% slower than sinusoidal, and the speed comparisons were run on the authors' hardware.
Paper info
- Authors
- Ofir Press, Noah A. Smith, Mike Lewis
- Venue
- arXiv · Preprint
- First published
- 2021-8-27
- Added
- 2026-10-20
- Updated
- 2022-4-22
- Version
- v2
- arXiv
- 2108.12409
- Category
- cs.CL
- Original paper terms
- arXiv non-exclusive 1.0
- What we show
- Paper info and commentary
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
Quote from the paper
Ofir Press, Noah A. Smith, Mike Lewis (2021). Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation. arXiv:2108.12409v2. https://arxiv.org/abs/2108.12409v2
BibTeX
@misc{press2021train,
title = {{Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation}},
author = {Ofir Press and Noah A. Smith and Mike Lewis},
year = {2021},
eprint = {2108.12409},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2108.12409v2}
}