What is positional encoding in Transformers?
Positional encoding is the information a Transformer adds so that attention can tell where each token sits in the sequence. Attention compares tokens only by their content, so without it "dog bites man" and "man bites dog" look the same. The original Transformer adds a fixed pattern of sine and cosine waves to each token embedding; many newer models instead rotate the queries and keys by a position-dependent angle (RoPE).
Every Transformer needs some form of position information, whether it reads text, image patches, audio frames, or code. The choice affects how well a model handles long inputs: learned position tables stop at a fixed length, while sinusoidal codes, RoPE, and distance-based biases such as ALiBi can be applied at any position. Extending the context window of a large language model is largely a question of how its positional encoding behaves past the training length.
Key ideas
- Attention compares content only, so position has to be supplied separately.
- Sinusoidal encoding uses sin and cos waves whose wavelengths grow geometrically across dimensions.
- The dot product of two sinusoidal encodings depends only on the distance between positions.
- RoPE rotates each pair of query and key dimensions by an angle proportional to position.
- With RoPE, the attention score depends on relative position m − n.
- Behavior beyond the training length differs by method and limits context extension.
Short takes on this topic
Common questions
What is the difference between absolute and relative positional encoding?
Absolute encoding gives each position its own vector, added to the token embedding, so the model learns what position 7 means. Relative encoding changes how two tokens interact based on the distance between them, so the model learns what "3 tokens earlier" means. Relative methods usually generalize better across positions because the same distance looks the same anywhere in the sequence.
Is positional encoding learned or fixed?
Both are used. The original Transformer and RoPE use fixed formulas with no parameters. BERT and GPT-2 learn a table with one vector per position, which works well but cannot go past the table size, 512 positions for BERT and 1,024 for GPT-2. The original paper found learned and sinusoidal versions performed about the same.
Can a model read sequences longer than it was trained on?
It depends on the encoding. Learned tables have no entry for new positions. Sinusoidal codes and RoPE produce values at any position, but models usually degrade beyond the training length because they never saw those angle combinations. ALiBi and RoPE with interpolation or YaRN extend further, often after a short round of fine-tuning on longer text.
How do vision transformers encode position?
A vision transformer cuts an image into patches, such as a 14 × 14 grid, and the original ViT adds a learned vector for each patch index. Variants use 2D sinusoidal codes or 2D RoPE that encode row and column separately. When the image resolution changes, the learned position vectors are interpolated to the new grid size.
Related explainers
- How Self-Attention Works — Query, key, value — the core of self-attention
- The Transformer Architecture — A visual guide to 'Attention Is All You Need'
- Multi-Head Attention — Parallel attention heads learning different patterns
- Decoder-Only Transformers — The architecture behind GPT and modern LLMs
- Embeddings — Turning words and categories into vectors
- Tokenization & BPE — How text becomes numbers — from characters to subword tokens via byte-pair encoding
References
- Vaswani et al. (2017), "Attention Is All You Need", NeurIPS
- Su et al. (2021), "RoFormer: Enhanced Transformer with Rotary Position Embedding"
- Shaw, Uszkoreit & Vaswani (2018), "Self-Attention with Relative Position Representations"
- Press, Smith & Lewis (2021), "Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation"
- Chen et al. (2023), "Extending Context Window of Large Language Models via Positional Interpolation"
Last updated