Relative Position Encoding › Shaw et al. Relative Position Representations

Michael BrenndoerferUpdated June 2, 20257 min read
Language AI HandbookAll chapters
Part 14 of 67Positional Encoding
Chapter 99Relative Position Encoding › Shaw et al. Relative Position Representations
Reading section 4 of 16 · Shaw et al. Relative Position Representations · 7 minSection 4 of 16 · 7 min

Book contents

Section 4 of 16 · 7 min read

Relative Position Encoding › Shaw et al. Relative Position RepresentationsOne section · Sections in this chapter4 / 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

The most influential formulation of relative positions in self-attention comes from Shaw et al. (2018). Their approach asks a fundamental question: if we want attention to be distance-aware, where exactly should we inject position information? Rather than modifying the input embeddings, Shaw et al. discovered that injecting relative positions directly into the attention computation gives the model finer control over how distance affects both which tokens get attention and what information flows.

To understand their approach, we need to build up three connected ideas: what we want the model to learn about distance, how to represent those distances as learnable parameters, and how to integrate those representations into the attention mechanism itself.

The Design Question: What Should Distance Encode?

Consider what happens when position ii attends to position jj in standard self-attention. The model computes a score based on content similarity, then uses that score to weight how much of jj's information flows to ii. But we want distance to influence both of these operations independently.

Think of it this way: a verb attending to possible subjects has two separate questions to answer. First, which position is the most likely subject based on both content and distance? Second, once the subject is found, how much of its information should flow into the verb's representation? Both questions depend on distance, but in different ways. The model might want to assign high attention to a subject that is two positions back, and to weight that subject's contribution with a "subject-attended-from-two-back" color that differs from "subject-attended-from-ten-back."

Shaw et al. address both needs by introducing two sets of learnable embeddings: one that modifies attention scores (controlling who gets attention), and one that modifies value aggregation (controlling what information flows). This separation gives the model independent control over "who to attend to" versus "what to gather."

  1. Which positions get attention: A verb might prefer to attend to positions 1-3 positions back (where subjects typically appear), regardless of content similarity.

  2. What information flows: When aggregating information, we might want "nearby context" to carry different weight than "distant context," even when attention weights are equal.

Relative Position Embeddings

For each possible offset r=j−ir = j - i between positions, Shaw et al. define learnable vectors that the model adjusts during training just like any other parameter:

  • arK∈Rdk\mathbf{a}^K_r \in \mathbb{R}^{d_k}: a relative position embedding that influences attention scores (key-side)
  • arV∈Rdv\mathbf{a}^V_r \in \mathbb{R}^{d_v}: a relative position embedding that modifies aggregated values (value-side)

where:

  • r=j−ir = j - i: the relative offset (positive means looking forward, negative means looking backward)
  • dkd_k: dimension of query and key vectors
  • dvd_v: dimension of value vectors

These embeddings are learned during training, just like the QKV projection matrices. The critical insight is that the number of distinct offsets is bounded by the sequence length (from −(n−1)-(n-1) to +(n−1)+(n-1)), not by the number of position pairs (which is n2n^2). This means the model learns a single pattern for "three positions back" that applies universally, rather than learning separate patterns for every pair of absolute positions. The parameter count scales linearly with the clipping distance kk, not quadratically with sequence length.

The key-side embeddings (aK\mathbf{a}^K) and value-side embeddings (aV\mathbf{a}^V) are separate parameter matrices, not tied to each other. This gives the model full flexibility: the same distance can produce different effects on the routing decision (score computation) versus the aggregation decision (value weighting).

Modifying Attention Scores

Let's trace through how relative positions enter the attention computation. In standard self-attention, the score measuring how much position ii should attend to position jj captures only content similarity:

eij=qi⋅kjdke_{ij} = \frac{\mathbf{q}_i \cdot \mathbf{k}_j}{\sqrt{d_k}}

where:

  • eije_{ij}: the raw attention score from query position ii to key position jj
  • qi∈Rdk\mathbf{q}_i \in \mathbb{R}^{d_k}: the query vector at position ii
  • kj∈Rdk\mathbf{k}_j \in \mathbb{R}^{d_k}: the key vector at position jj
  • dkd_k: the dimension of query and key vectors

This score depends only on the content at each position. Two identical words at different positions produce identical scores, regardless of their distance. The model cannot distinguish "I saw a cat (right here, adjacent)" from "I saw a cat (twenty words ago)."

Shaw et al. modify this by adding the relative position embedding to the key before computing the dot product:

eij=qi⋅(kj+aj−iK)dke_{ij} = \frac{\mathbf{q}_i \cdot (\mathbf{k}_j + \mathbf{a}^K_{j-i})}{\sqrt{d_k}}

Why add to the key rather than the query? The key represents "what this position offers," so adding position information to it means the model can learn that positions at certain distances offer something different, even if their content is identical. The query remains focused on "what I'm looking for" without distance context; the key communicates "here is what I have, adjusted for how far away I am from the query."

Expanding the dot product reveals the two-component structure:

eij=qi⋅kj+qi⋅aj−iKdke_{ij} = \frac{\mathbf{q}_i \cdot \mathbf{k}_j + \mathbf{q}_i \cdot \mathbf{a}^K_{j-i}}{\sqrt{d_k}}

where:

  • qi⋅kj\mathbf{q}_i \cdot \mathbf{k}_j: the content term, measuring semantic compatibility between positions
  • qi⋅aj−iK\mathbf{q}_i \cdot \mathbf{a}^K_{j-i}: the position term, encoding how relative distance affects the score
  • aj−iK∈Rdk\mathbf{a}^K_{j-i} \in \mathbb{R}^{d_k}: the learned relative position embedding for offset (j−i)(j-i)

The position term is a dot product between the query and the relative position embedding. This means different queries can respond differently to the same distance. A query learned to find subjects might have a high dot product with a−2K\mathbf{a}^K_{-2} (two positions back), while a query learned to find objects might prefer a+1K\mathbf{a}^K_{+1} (one position forward). The model learns these preferences end-to-end from training data, so the resulting position patterns reflect actual linguistic structure rather than any hand-designed prior.

Modifying Value Aggregation

Shaw et al. apply the same principle to value aggregation. After computing attention weights αij=softmax(eij)\alpha_{ij} = \text{softmax}(e_{ij}), standard attention aggregates values as a weighted sum. We want to add relative position information here too, so that the output at each position carries the weighted content of attended tokens and a record of where that content came from.

The modified value aggregation is:

oi=∑j=1nαij(vj+aj−iV)\mathbf{o}_i = \sum_{j=1}^{n} \alpha_{ij} (\mathbf{v}_j + \mathbf{a}^V_{j-i})

where:

  • oi∈Rdv\mathbf{o}_i \in \mathbb{R}^{d_v}: output vector at position ii, now enriched with position-aware context
  • αij\alpha_{ij}: attention weight from position ii to position jj (sums to 1 across jj)
  • vj∈Rdv\mathbf{v}_j \in \mathbb{R}^{d_v}: value vector at position jj, carrying the content to aggregate
  • aj−iV∈Rdv\mathbf{a}^V_{j-i} \in \mathbb{R}^{d_v}: relative position embedding for values at offset (j−i)(j-i)
  • nn: sequence length

This modification means the output at each position incorporates the weighted content from other positions and position-dependent information about where that content came from. Even if two positions have identical values and identical attention weights, their contributions differ based on their relative distances. Think of it as: "I got this information from a token that was three positions back, and that proximity matters for how I should interpret it."

The two sets of embeddings (aK\mathbf{a}^K and aV\mathbf{a}^V) work together but serve distinct roles. The key embeddings affect which positions receive attention (the "routing" decision). The value embeddings affect what information flows once routing is decided (the "content" decision). This separation allows the model to learn, for example, that adjacent positions should receive high attention (key embedding effect) while also learning that adjacent context should contribute a specific "locality" signal to the output (value embedding effect).

In practice, many implementations omit the value-side embeddings (aV\mathbf{a}^V) for efficiency reasons. The key-side modification tends to deliver most of the benefit, and removing the value modification cuts the number of relative position parameters in half. The T5 model, for instance, uses only key-side biases. Shaw et al.'s original paper includes both, but their ablation experiments showed that omitting the value-side embeddings costs only a small amount of task performance.