Positional Encoding
Positional encoding injects sequence order into Transformers. Self-attention is order-insensitive, so shuffling words merely permutes the output, and the model must be told "who comes first and who comes after." It is a prerequisite for applying Transformers to language, speech and image sequences, and a core bottleneck of long-context ability.
Core Principles
The 2017 Transformer paper proposed sinusoidal encoding: each position gets sine and cosine values at different frequencies added to the word vector. Its elegance is that the offset between any two positions is a linear transform, and it extrapolates to unseen lengths. The form is PE(pos,2i)=sin(pos/10000^(2i/d)), sine for even dimensions and cosine for odd. Early GPT and BERT later used learnable absolute embeddings, similar in effect but unable to extrapolate.
Around 2020 relative methods became mainstream: T5 adds a learnable relative bias to attention scores, and Transformer-XL and XLNet use relative encoding. In 2021 RoFormer proposed rotary position embedding (RoPE), applying position as a rotation matrix to queries and keys so scores depend only on relative distance, with good extrapolation potential. RoPE was adopted by most open models including LLaMA, Qwen and Mistral, becoming the de facto standard.
For very long contexts, researchers tuned RoPE's frequency base: position interpolation (PI) compresses positions beyond training length back into range; NTK-aware scaling adjusts base frequency nonlinearly by dimension; YaRN combines both with temperature correction. These extend context from 4K to 128K or more after light continued training, but the model often needs recalibration on the target task, or it "fits the length but cannot retrieve the information." ALiBi takes another route: no position embedding, just a linear distance penalty on attention scores, simple and extrapolative, used by BLOOM.
Another line is architectural: replacing softmax with a kernel approximation makes the sequence theoretically exchangeable, with order restored by other mechanisms. Such linear attention cuts complexity sharply, but its accuracy and maturity still trail RoPE and it serves specific long-sequence cases.
Key Points
Absolute and relative: absolute encoding says "which position I am in," relative says "how far apart we are"; the latter fits language structure and generalizes better.
RoPE's popularity: no extra parameters, compatibility with linear attention and naturally valid relative distance explain its dominance.
The long-context problem: encoding is key to extrapolation, but attention, data distribution and training must cooperate.
Numerical stability: high-frequency components have very short periods and low frequencies very long ones, so scales differ greatly, which is why scaling methods need fine tuning.
Common Misconceptions
Misconception one: positional encoding sets the maximum length; attention complexity and memory are equally hard limits. Misconception two: RoPE is universal extrapolation; performance still decays well beyond training length. Misconception three: position must be added explicitly; ALiBi-style biases on attention scores work too.
Application Scenarios
Long document understanding, code repository analysis, multi-turn dialogue, speech and video modeling, retrieval-augmented generation, long-context fine-tuning, and sequence recommendation.