Relative Position Encoding › From Absolute to Relative: The Key Insight

Michael BrenndoerferUpdated June 2, 20253 min read
Language AI HandbookAll chapters
Part 14 of 67Positional Encoding
Chapter 99Relative Position Encoding › From Absolute to Relative: The Key Insight
Reading section 3 of 16 · From Absolute to Relative: The Key Insight · 3 minSection 3 of 16 · 3 min

Book contents

Section 3 of 16 · 3 min read

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 Q\mathbf{Q}, keys K\mathbf{K}, and values V\mathbf{V}, we compute:

Attention(Q,K,V)=softmax(QKTdk)V\text{Attention}(\mathbf{Q}, \mathbf{K}, \mathbf{V}) = \text{softmax}\left(\frac{\mathbf{Q}\mathbf{K}^T}{\sqrt{d_k}}\right)\mathbf{V}

where:

  • Q∈Rn×dk\mathbf{Q} \in \mathbb{R}^{n \times d_k}: query matrix containing all query vectors
  • K∈Rn×dk\mathbf{K} \in \mathbb{R}^{n \times d_k}: key matrix containing all key vectors
  • V∈Rn×dv\mathbf{V} \in \mathbb{R}^{n \times d_v}: value matrix containing all value vectors
  • dkd_k: dimension of queries and keys
  • dk\sqrt{d_k}: scaling factor to prevent dot products from growing too large
  • nn: sequence length

With absolute position encoding, each token's embedding includes position information before the QKV projections. Position is baked into the input: xi=ei+pi\mathbf{x}_i = \mathbf{e}_i + \mathbf{p}_i, where ei\mathbf{e}_i is the token embedding and pi\mathbf{p}_i 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 j−ij - i between positions ii and jj. 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:

scoreij=qi⋅kj+relative_bias(i,j)\text{score}_{ij} = \mathbf{q}_i \cdot \mathbf{k}_j + \text{relative\_bias}(i, j)

where:

  • qi\mathbf{q}_i: the query vector at position ii
  • kj\mathbf{k}_j: the key vector at position jj
  • relative_bias(i,j)\text{relative\_bias}(i, j): a term that depends only on the offset j−ij - i

This formulation separates content matching (qi⋅kj\mathbf{q}_i \cdot \mathbf{k}_j) 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 j−ij - i, not on ii and jj 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.

Historical Context: Toward Relative Attention

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.