Relative Position Encoding › From Absolute to Relative: The Key Insight
Language AI HandbookAll chapters Relative Position Encoding › From Absolute to Relative: The Key InsightOne section · Sections in this chapter3 / 16 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Recall the standard self-attention formula. Given queries , keys , and values , we compute:
where:
- : query matrix containing all query vectors
- : key matrix containing all key vectors
- : value matrix containing all value vectors
- : dimension of queries and keys
- : scaling factor to prevent dot products from growing too large
- : sequence length
With absolute position encoding, each token's embedding includes position information before the QKV projections. Position is baked into the input: , where is the token embedding and is the position encoding. The attention scores then implicitly depend on absolute positions through the projected queries and keys. The model sees position information, but only indirectly: it must disentangle "what is this token?" from "where is this token?" inside the projection matrices themselves.
Relative position encoding takes a different approach: inject position information directly into the attention computation. Instead of adding position to the input, we modify how attention scores are calculated to explicitly account for the offset between positions and . This is a cleaner separation of concerns. Content similarity handles semantic matching; a separate position-dependent term handles distance-based preferences. The model does not need to learn to disentangle them because they are never mixed in the first place.
The core idea is to add a position-dependent term to the attention score. To give the model a sense of why tokens at certain distances should receive more or less attention, we write:
where:
- : the query vector at position
- : the key vector at position
- : a term that depends only on the offset
This formulation separates content matching () from position matching (the relative bias). The model can learn that certain offsets are generally more or less important, regardless of absolute position. Notice that the relative bias only depends on the difference , not on and individually. This is the key mathematical property that enables generalization: two position pairs with the same offset get the same positional treatment, no matter where they appear in the sequence.
The idea of encoding relative rather than absolute positions has roots in cognitive science research from the 1990s, which showed that humans process distance relationships more reliably than absolute locations. In NLP, early sequence models like recurrent networks encoded positions implicitly through the sequential processing order, which is inherently relative. Transformer models broke that implicit ordering with parallel attention, creating the need to explicitly reinject position information. Shaw et al. (2018) were among the first to show that this could be done at the attention-score level rather than the input level, a distinction that turned out to be both theoretically cleaner and empirically better. Their work influenced a wave of relative position schemes including Transformer-XL's segment-level recurrence, T5's relative bias buckets, and ultimately RoPE and ALiBi. The common thread across all of these is the insight from this section: position information belongs in the attention function, not just in the embeddings.