What is positional encoding?
Positional encoding is the method that tells a Transformer the order of each token in a sequence, or the relative distance between tokens, so the attention mechanism can use order information.
It is also called positional embedding. It is needed because unmasked self-attention does not see order by itself: without position, “the bank called the customer” and “the customer called the bank” look like the same set of tokens. Rotary Position Embedding and Attention with Linear Biases are specific methods of this general idea.
- Topic
- Machine learning, models and training
- Also known as
- Positional embedding, position encoding
- Origin
- 2017, Vaswani et al., sinusoidal positional encoding
How positional encoding works
- Absolute methods produce a vector per position and add it to the token embedding; the vector can be computed with sine and cosine functions or learned in training.
- Relative methods bring position into attention as the distance between tokens.
- RoPE rotates query and key vectors by position, making the attention score between two tokens depend on their relative distance.
- The encoding type shapes how a model behaves on sequences longer than those seen in training.
In more detail
Hybrid RoPE and NoPE layers and multi-dimensional position
Recent architectures take positional encoding beyond one-dimensional text: some interleave RoPE layers with layers that use no positional encoding (NoPE) to balance local precision with long-range recall. Multimodal models use multi-dimensional M-RoPE, encoding height, width and time separately for images and video.
Commonly confused terms
| Term | How it differs |
|---|---|
| Positional embedding | Positional encoding generally refers to deterministic functions or geometric operations (sinusoidal waves, RoPE rotations, ALiBi slopes) injecting position, whereas positional embedding typically denotes learned position lookup vectors added to token embeddings. |
| Token embedding | Token embeddings map vocabulary IDs to semantic vectors independent of sequence order, whereas positional encoding injects sequence index or relative distance so attention distinguishes identical tokens at different positions. |
Why positional encoding matters for on-premises AI
Company documents are long and the context window is often the limit in in-house use. Whether a model can safely stretch its context window beyond its training length depends largely on positional encoding; models using RoPE can be adapted to longer inputs with context extension techniques. Model choice should check measured long-document performance, not just advertised context length.
A law firm picks a long-context model to review contracts of hundreds of pages in one pass, first testing with its own documents how accurately the model finds a clause near the end.
Common questions
Why is positional encoding needed?
The attention mechanism treats tokens as an unordered set. Without position the model cannot see differences in meaning that come from word order.
How does RoPE differ from classic positional encoding?
Classic methods add a position vector to the token embedding. RoPE brings position into attention by rotating query and key vectors, representing relative distance naturally and adapting more easily to long context.
Related terms
- Self-attention
- Transformer
- Rotary Position Embedding
- Attention with Linear Biases
- Context window
- Context extension
- Attention mechanism
Sources
- Vaswani et al., Attention Is All You Need, 2017
- Su et al., RoFormer: Enhanced Transformer with Rotary Position Embedding, 2021
In the glossary: Glossary, M to Q