Relative Position Changes Pair Interactions

Relative methods can change query-key and value interactions according to a signed offset such as key minus query. Work through one Shaw-style score, then audit direction, clipping, bucketing, and model-wide interpretation limits.

Absolute methods attach information to one index ii. A relative method can instead change the interaction between query position ii and key position jj according to an offset such as jij-i.

Offsets preserve both distance and direction:

  • ji=2j-i=-2 means the key is two positions before the query;
  • ji=0j-i=0 means query and key share an index;
  • ji=3j-i=3 means the key is three positions after the query.

Changing the sign convention is valid only if every table lookup, equation, test, and cached index uses the same convention.

One Concrete Relative-Attention Design

Shaw, Uszkoreit, and Vaswani proposed adding a learned relation vector to each query-key interaction. In one-head notation, a score can be written

eij=qiT(kj+ajiK)dk.e_{ij}=\frac{q_i^T(k_j+a^K_{j-i})}{\sqrt{d_k}}.

Their construction can also change the value reading:

zi=jaij(vj+ajiV).z_i=\sum_j a_{ij}(v_j+a^V_{j-i}).

Here arKa^K_r and arVa^V_r are learned vectors for relative offset rr. This is one relative-position mechanism, not a formula shared by every later method.

Equal Content Can Receive Different Scores

Let one query and three content keys all be

q=[1,0],k0=k1=k2=[0,0],q=[1,0],\qquad k_0=k_1=k_2=[0,0],

with dk=2d_k=2. For query position i=2i=2, the offsets jij-i are 2,1,0-2,-1,0. Let

a2K=[1,0],a1K=[0,0],a0K=[1,0].a^K_{-2}=[-1,0],\quad a^K_{-1}=[0,0],\quad a^K_0=[1,0].

The three scores are

[1/2, 0, 1/2].[-1/\sqrt{2},\ 0,\ 1/\sqrt{2}].

Content alone could not distinguish the equal keys. The relative terms make their direction and distance affect matching.

Clip or Bucket the Offset Range Explicitly

A learned relation table is often finite. One policy clips offsets to a range [K,K][-K,K]:

clip(ji,K,K).\operatorname{clip}(j-i,-K,K).

With K=4K=4, offsets 9-9 and 4-4 use the same table row. This bounds parameter count but discards exact distance beyond the boundary. Other methods use buckets whose widths grow with distance. The policy is part of the model, not an implementation detail to omit.

Relative Does Not Mean Translation-Invariant Model

If every query and key index shifts by the same amount, jij-i remains the same. That algebraic property concerns this position term. Padding boundaries, causal visibility, sequence starts, special tokens, data patterns, and later layers can still make complete model behavior depend on absolute location.

Q1. Choose the signed offset

This chapter defines relative offset as key index minus query index, jij-i. A query is at index 6 and a readable key is at index 2. Which offset selects the relation vector?

Compute it first, then check your number.

Hint
Substitute the key index for jj and query index for ii.
Solution
ji=26=4j-i=2-6=-4.
Not attempted
Review

Not marked done.

Reference

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 1 exercises marked done

Review

Not marked done.

LLM PrimerRelative Position Changes Pair Interactionshttps://llmprimer.com/transformers/position-and-sequence-order/relative-position-changes-pair-interactions© 2026 LLM Primer