Home Knowledge Base Positional Encoding in Transformers

Positional Encoding in Transformers is the mechanism that injects sequence order information into the position-agnostic attention computation — because self-attention treats its input as an unordered set, positional encodings are essential for the model to distinguish "the cat sat on the mat" from "the mat sat on the cat," with different encoding strategies (sinusoidal, learned, RoPE, ALiBi) offering different tradeoffs in extrapolation ability, computational cost, and representation quality.

Why Position Information Is Needed

Self-attention computes Attention(Q,K,V) = softmax(QK^T/√d)V. This computation is permutation-equivariant — shuffling the input sequence produces the same shuffle in the output. Without position information, the model cannot distinguish word order, making it useless for language (and most sequential data).

Encoding Strategies

Absolute Sinusoidal (Vaswani 2017):

Learned Absolute Embeddings:

Rotary Position Embedding (RoPE):

ALiBi (Attention with Linear Biases):

Comparison

MethodTypeExtrapolationParametersNotable Users
SinusoidalAbsolutePoor0Original Transformer
LearnedAbsoluteNonemax_len × dBERT, GPT-2
RoPERelative (implicit)Good (with interpolation)0LLaMA, Mistral
ALiBiRelative (bias)Excellent0BLOOM, MPT

Positional Encoding is the information-theoretic bridge between the unordered world of attention and the ordered world of language — the mechanism whose design determines how well a Transformer can represent sequential structure and, critically, how far beyond its training context the model can generalize.

positional encoding rope sinusoidalalibi position biaslearned position embeddingrelative position encoding transformerrotary position embedding

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.