Positional Encodings
Ways to give sequence order to a model: sinusoidal, RoPE, ALiBi, and more.
ALiBiALiBi (Attention with Linear Biases) uses no position embeddings and instead subtracts "query–key distance × per-head slope" from the attention scores. It has no learned parameters and stays accurate on sequences longer than those seen in training, so a model can be trained quickly on short sequences and used on long ones.
Conditional Positional Encoding (CPE)Conditional Positional Encoding (CPE) reshapes the tokens of an image Transformer back into a 2D grid and uses a small depthwise convolution to generate a positional encoding from the surrounding tokens, which is then added. Because it has no table of positions, it maintains accuracy on images larger than those seen in training without fine-tuning.
KERPLEKERPLE is a relative position scheme that adds a penalty computed from the position difference (a conditionally positive definite kernel) to the attention scores, generalizing ALiBi. It learns a logarithmic penalty with two parameters per head and kept lower perplexity than ALiBi, T5, and RoPE even at 32x the training length.
Learned Positional EmbeddingA learned positional embedding prepares one vector for each position ("which number in the sequence"), learns its values, and adds it to the word embeddings to convey word order to models such as the Transformer. It is simple to build and about as accurate as sinusoidal encoding, but it cannot represent positions beyond the maximum length fixed at training.
LongRoPELongRoPE extends the context of pretrained language models that use RoPE by determining, through evolutionary search, how to shrink the rotation angles for each dimension pair and each position. Through progressive extension, it extended the context of Llama 2 from 4096 to 2048k tokens.
NoPENoPE is a decoder-only Transformer that injects positional encoding nowhere. It can learn position from the causal mask that hides future words, and on the paper's 10 synthetic tasks it generalized to problems longer than in training better than RoPE, ALiBi, and others.
Position InterpolationPosition interpolation extends the context length of a pretrained RoPE language model from L to L′ by shrinking the position indices by a factor of L/L′ before rotating. Because it interpolates within the range seen in training instead of extrapolating beyond it, values stay well behaved, and it extended LLaMA's context from 2048 to as much as 32768 with about 1000 steps of fine-tuning.
Relative Position RepresentationsRelative position representations learn a vector for each "how far apart two words are" instead of each word's absolute position, and add it to the attention scores and outputs. In translation they improve BLEU by up to 1.3 over absolute position representations, and because the distance is clipped at a fixed value, the same computation works for sequences longer than those seen in training.
RoPE (Rotary Position Embedding)RoPE (Rotary Position Embedding) injects position information by rotating the attention queries and keys by an angle proportional to the word's position. Because the dot product of two rotated vectors depends only on the distance between the two words, it conveys relative position to attention without any learned parameters, and many large language models, including LLaMA, use it.
Sinusoidal Positional EncodingSinusoidal positional encoding represents a word's position as a set of sin and cos values with different periods and adds it to the embedding, giving order information to the Transformer, which otherwise cannot distinguish word order.
T5 Relative Position BiasThe T5 relative position bias groups query–key distances into buckets and adds a learned number per bucket to the attention scores to inject position information. Its parameters are just 32 buckets × the number of heads, shared across all layers, and it can handle sequences somewhat longer than in training.
Transformer-XL Relative Positional EncodingTransformer-XL relative positional encoding replaces the terms of the attention score that involve absolute position with sinusoids of the query–key distance and two learned vectors. Positions do not collide even when hidden states of the previous segment are reused, and on WikiText-103 accuracy improved the more the context was extended at evaluation.
xPos (Extrapolatable Position Embedding)xPos is a position representation that, in addition to RoPE's rotation, scales queries and keys according to position so that the score decreases exponentially with distance. It suppresses score oscillation at long distances and, combined with blockwise attention at inference time, maintained accuracy even at 4× the training length.
YaRNYaRN extends the context window of a pretrained language model that uses RoPE. It keeps the fast-rotating dimension pairs unchanged, scales down only the slow-rotating pairs, and also adjusts the temperature of the attention scores. With less fine-tuning than Position Interpolation, it extended the context of Llama 2 from 4096 to 128k.
