Positional encoding
Because self-attention is permutation-invariant, transformers need positional encodings to know token order. Fixed-length positional embeddings mean that a model trained on short sequences struggles to generalise to longer ones.
Absolute positional embeddings
Early transformer models like BERT and GPT used learned absolute positional embeddings , where is a learned vector unique to each position index (0, 1, 2... 511).
They had no representation for positions beyond their training length. Position 513 was undefined or random if training stopped at 512. The model had no concept of "distance," only of unique indices, preventing generalisation to lengths longer than the training set.
Sinusoidal encodings theoretically could extrapolate, but in practice, perplexity collapsed on sequences even slightly beyond training length.
Relative positional embeddings
- Introduced by Shaw, Uszkoreit & Vaswani (2018) and further refined in Transformer-XL (2019) and T5 (2020).
- Remove the fixed limit of absolute positions. Instead of embedding the absolute index, they embed the distance between tokens (often clipped to some maximum).
- The model learns to weight interactions based on the distance between tokens, rather than their absolute positions in the sequence.
- The model can slide to text of greater length without encountering unseen position indices.
ALiBi (Attention with Linear Bias)
- 2021 — Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation
- A simple method to encode positions that allows a model trained on shorter sequences to generalise to longer ones.
- Instead of using explicit positional embeddings added to tokens, ALiBi introduces a static, non-learned penalty to the scaled attention scores that grows linearly with the distance between query and key. For causal attention (): where is a slope specific to each attention head.
- Penalises attention based on how far apart tokens are, biasing the model toward more recent tokens.
- Because the bias does not depend on an absolute position index, the model can naturally extend to longer sequences than it saw in training — you just continue applying the linear penalty beyond the training length.
- Different attention heads use different penalty slopes (following a geometric series), allowing some heads to focus locally while others attend broadly.
- Demonstrated training on 1,024 tokens and evaluating at 8–16× that length.
- ALiBi models maintain low language-modelling perplexity zero-shot at lengths well beyond training because the penalty mechanism is inherently length-agnostic. It was used in MPT (MosaicML) and BLOOM.
- However, its rigid linear decay strongly suppresses attention to distant tokens, hurting long-range retrieval tasks (like needle-in-a-haystack), and makes it less expressive than the complex-valued rotations of RoPE. RoPE's flexibility, combined with frequency-scaling techniques (such as ABF, NTK-by-parts, and YaRN), ultimately won the adoption war in open frontier models like Llama 3 (which uses piecewise frequency scaling plus a raised RoPE base), Qwen, Mistral, and DeepSeek.
Rotary position embeddings (RoPE)
See RoPE and length extrapolation for the RoPE formulation, why extrapolation breaks down at long distances, and scaling techniques (Position Interpolation, NTK-aware scaling, YaRN).
Linked from
Tags: AI