Positional Encoding

Definition

Positional encoding is the injection of token-order information into self-attention, which is otherwise permutation-equivariant: the output depends only on the content of , never on indices — so “dog bites man” and “man bites dog” are indistinguishable without it. Schemes divide into absolute (tag each position ) and relative (make the score depend on offset ); the field moved decisively to relative because it generalizes to unseen lengths.

Key math / taxonomy

Sinusoidal absolute (Vaswani et al., 2017; arXiv:1706.03762): add a fixed vector with

A shift by is a linear (rotation) map on each pair, so relative offset is learnable in principle — a latent structure RoPE later makes exact. Parameter-free, so it runs at any but degrades past training length.

Learned absolute (GPT/BERT): a trained table , . Maximally flexible, one lookup — but hard-caps at : positions have no row, so it cannot run on longer sequences. This is the central failure that pushed the field to relative schemes.

Relative: RoPE (rotate operands — rich, phase-based, extendable via base/PI/YaRN) and ALiBi (add a scalar distance penalty to scores — cheap, monotone, strong extrapolation). All schemes add negligible FLOPs ( or adds) vs. the matmuls; the choice governs the length-generalization ceiling, not compute. Rough extrapolation ranking: ALiBi ≈ RoPE(+scaling) > sinusoidal > learned-absolute.

Why it matters

The positional scheme is a footnote in FLOPs but the whole story for long context: it sets whether a model can be served at 128k+ tokens and how. RoPE-base scaling and ALiBi variants are the levers in the million-token-context race.

Taught in

See also