Subscribe
AI
10 min read

Relative Positional Encodings in Transformers: How Shaw's Attention, Transformer-XL, and T5 Relative Biases Preserve Translation Invariance

This post explains why absolute positional encodings fail to capture relative displacement and details the evolution of relative mechanisms including Shaw’s relation-aware attention, Transformer-XL’s four-term decomposition, and T5’s logarithmic bucketing to achieve translation invariance.

Diagram illustrating the mathematical structure of relative positional encodings versus absolute encodings in transformer self-attention mechanisms
Diagram illustrating the mathematical structure of relative positional encodings versus absolute encodings in transformer self-attention mechanisms

Relative positional encodings address the lack of inherent sequence order awareness in standard self-attention by modeling relationships based on distance rather than fixed indices. This approach improves translation invariance and generalizability over absolute encodings used in early models like BERT and GPT-2.

Relative Positional Encodings in Transformers: How Shaw's Attention, Transformer-XL, and T5 Relative Biases Preserve Translation Invariance

Standard self-attention operations in transformer architectures possess no inherent awareness of sequence order. Because scaled dot-product attention computes interactions across sets of tokens without regard to index ordering, early models relied on absolute positional encodings to inject sequential structure. While absolute encodings assign rigid coordinate embeddings to each index in a sequence, natural language semantics depend on relative displacement: the syntactic relationship between a verb and its direct object remains identical whether the pair appears at indices (4, 5) or (104, 105).

To resolve this limitation, researchers developed relative positional encoding mechanisms. Beginning with the relation-aware self-attention of Shaw et al. (2018), expanding into the four-term query-key matrix decomposition of Transformer-XL (2019), and refining into the logarithmic scalar bucketing of T5 (2020) and the disentangled attention of DeBERTa (2021), relative positional representations established translation invariance across transformer layers. Understanding their mathematical structure clarifies why relative representations improve generalizability and why modern LLM serving frameworks eventually migrated toward rotational formulations like RoPE to overcome KV cache and kernel fusion bottlenecks.

Relative Positional Encodings Architecture

The Translation Invariance Failure in Absolute Positional Encodings

The vanilla transformer architecture introduced by Vaswani et al. (2017) addressed sequence order by adding fixed sinusoidal vectors to input token embeddings:

PE(pos,2i)=sin⁡(pos100002i/dmodel),PE(pos,2i+1)=cos⁡(pos100002i/dmodel)PE_{(pos, 2i)} = \sin\left(\frac{pos}{10000^{2i/d_{\text{model}}}}\right), \quad PE_{(pos, 2i+1)} = \cos\left(\frac{pos}{10000^{2i/d_{\text{model}}}}\right)

Subsequent models such as BERT (Devlin et al., 2018) and GPT-2 (Radford et al., 2019) replaced fixed sinusoids with learned absolute position embeddings Wp∈RLmax⁡×dmodelW_p \in \mathbb{R}^{L_{\max} \times d_{\text{model}}}, where each absolute sequence index ii is assigned a dedicated parameter vector pip_i.

Absolute position injection suffers from three structural deficiencies:

  1. Lack of Translation Invariance: In an absolute encoding framework, the attention logit between token xix_i at position ii and token xjx_j at position jj depends on the absolute coordinates ii and jj. If an identical phrase is shifted by an offset Δ\Delta, the inner products (xi+pi)WQWKT(xj+pj)T(x_i + p_i) W_Q W_K^T (x_j + p_j)^T change completely because pi+Δ≠pip_{i+\Delta} \neq p_i and pj+Δ≠pjp_{j+\Delta} \neq p_j.
  2. Context Length Hard Ceilings: Learned absolute position tables cannot process sequences longer than Lmax⁡L_{\max} without resizing and fine-tuning the embedding table. Any token index beyond the pre-allocated table has no representation.
  3. Weak Long-Range Generalization: Because higher position indices receive fewer gradient updates during training (due to variable sequence lengths in batches), parameters corresponding to tail positions remain poorly calibrated.

Shaw's Formulation: Relation-Aware Attention on Graph Distances

Shaw et al. (2018) introduced the first formal relative positional encoding mechanism by framing sequence inputs as directed, labeled graphs where edge labels correspond to the signed relative distance j−ij - i between tokens.

Instead of adding position vectors to input word embeddings before the first layer, Shaw et al. integrated relative position representations directly into the attention mechanism at every self-attention layer.

In standard self-attention, the unnormalized attention score eije_{ij} and output vector ziz_i are computed as:

eij=(xiWQ)(xjWK)Tdze_{ij} = \frac{(x_i W^Q)(x_j W^K)^T}{\sqrt{d_z}}

zi=∑j=1nαij(xjWV),where αij=exp⁡(eij)∑k=1nexp⁡(eik)z_i = \sum_{j=1}^n \alpha_{ij} (x_j W^V), \quad \text{where } \alpha_{ij} = \frac{\exp(e_{ij})}{\sum_{k=1}^n \exp(e_{ik})}

Shaw et al. augmented the keys and values with learned relative position embedding tensors aijK,aijV∈Rdza_{ij}^K, a_{ij}^V \in \mathbb{R}^{d_z}:

eij=xiWQ(xjWK+aijK)Tdz=xiWQ(xjWK)T+xiWQ(aijK)Tdze_{ij} = \frac{x_i W^Q (x_j W^K + a_{ij}^K)^T}{\sqrt{d_z}} = \frac{x_i W^Q (x_j W^K)^T + x_i W^Q (a_{ij}^K)^T}{\sqrt{d_z}}

zi=∑j=1nαij(xjWV+aijV)z_i = \sum_{j=1}^n \alpha_{ij} (x_j W^V + a_{ij}^V)

Distance Clipping and Parameter Sharing

To prevent the number of unique relative parameters from scaling quadratically with maximum sequence length, Shaw et al. introduced a clipping threshold kk:

clip(j−i,k)=max⁡(−k,min⁡(k,j−i))\text{clip}(j - i, k) = \max(-k, \min(k, j - i))

The relative representations are defined by indexing into learned tables wK,wV∈R(2k+1)×dzw^K, w^V \in \mathbb{R}^{(2k+1) \times d_z}:

aijK=wclip(j−i,k)K,aijV=wclip(j−i,k)Va_{ij}^K = w_{\text{clip}(j-i, k)}^K, \quad a_{ij}^V = w_{\text{clip}(j-i, k)}^V

This clipping formulation asserts that beyond a distance of kk tokens, precise relative offset information provides diminishing syntactic value.

In their empirical ablations on WMT 2014 English-to-German and English-to-French translation, Shaw et al. established two critical findings:

  • Incorporating relative representations solely into keys (aijKa_{ij}^K) achieved a 1.3 BLEU gain over absolute encodings, whereas adding relative representations to values (aijVa_{ij}^V) yielded marginal additional benefit while substantially increasing memory traffic.
  • Combining relative encodings with absolute encodings provided no performance advantage over pure relative encodings, proving that absolute coordinates are redundant when pairwise relative offsets are explicitly modeled.

Transformer-XL: The Four-Term Decomposition and Segment Recurrence

While Shaw's approach proved effective for machine translation, autoregressive language modeling required processing long continuous documents without resetting positional context across chunk boundaries.

Dai et al. (2019) introduced Transformer-XL, which paired segment-level recurrence with a mathematical decomposition of self-attention logits.

Expanding the standard attention logit between absolute embeddings (Exi+Ui)(E_{x_i} + U_i) and (Exj+Uj)(E_{x_j} + U_j), where EE represents word embeddings and UU represents absolute position vectors, yields four terms:

Ai,jabs=ExiWQWKTExjT⏟(a) Content-Content+ExiWQWKTUjT⏟(b) Content-Position+UiWQWKTExjT⏟(c) Position-Content+UiWQWKTUjT⏟(d) Position-Position\mathbf{A}_{i,j}^{\text{abs}} = \underbrace{E_{x_i} W_Q W_K^T E_{x_j}^T}_{\text{(a) Content-Content}} + \underbrace{E_{x_i} W_Q W_K^T U_j^T}_{\text{(b) Content-Position}} + \underbrace{U_i W_Q W_K^T E_{x_j}^T}_{\text{(c) Position-Content}} + \underbrace{U_i W_Q W_K^T U_j^T}_{\text{(d) Position-Position}}

Transformer-XL re-engineers this expansion into a relative formulation:

Ai,jrel=ExiWqWk,ETExjT⏟(a) Content-Content+ExiWqWk,RTRi−jT⏟(b) Content-Dependent Position Bias+uWk,ETExjT⏟(c) Global Content Bias+vWk,RTRi−jT⏟(d) Global Position Bias\mathbf{A}_{i,j}^{\text{rel}} = \underbrace{E_{x_i} W_q W_{k,E}^T E_{x_j}^T}_{\text{(a) Content-Content}} + \underbrace{E_{x_i} W_q W_{k,R}^T R_{i-j}^T}_{\text{(b) Content-Dependent Position Bias}} + \underbrace{u W_{k,E}^T E_{x_j}^T}_{\text{(c) Global Content Bias}} + \underbrace{v W_{k,R}^T R_{i-j}^T}_{\text{(d) Global Position Bias}}

Key mathematical modifications include:

  1. Sinusoidal Relative Matrix: The absolute key position vector UjU_j in terms (b) and (d) is replaced by Ri−jR_{i-j}, a fixed sinusoidal embedding representing the relative distance i−ji - j.
  2. Dedicated Projection Weights: Transformer-XL splits key projection weights into Wk,EW_{k,E} for content vectors and Wk,RW_{k,R} for relative positional vectors.
  3. Learned Global Bias Vectors: The query positional vector UiU_i in terms (c) and (d) is replaced by learnable global parameter vectors u,v∈Rdu, v \in \mathbb{R}^{d}. Because the query position does not need to specify an absolute index, uu and vv represent inductive biases: uu captures the general baseline affinity between query content and key content, while vv captures the baseline affinity for relative distance i−ji - j regardless of specific word identity.

This formulation allowed Transformer-XL to cache hidden states from preceding context segments without disrupting positional integrity, expanding effective temporal dependency by 450% over standard fixed-window transformers.

T5 Relative Position Biases: Scalar Offsets and Logarithmic Bucketing

Raffel et al. (2019) in the Text-to-Text Transfer Transformer (T5) introduced a simplified, parameter-efficient relative position mechanism.

Rather than projecting high-dimensional relative position vectors through weight matrices, T5 defines relative positional encoding as a learned scalar bias bi,jb_{i,j} added directly to the pre-softmax attention logit:

eij=qikjTdk+bi,je_{ij} = \frac{q_i k_j^T}{\sqrt{d_k}} + b_{i,j}

For an attention layer with HH heads, bi,jb_{i,j} is a vector of HH learned scalars.

Relative Distance (j - i) Mapping in T5:

Exact Buckets (0 to 7):
Distance:  0   1   2   3   4   5   6   7
Bucket:   [0] [1] [2] [3] [4] [5] [6] [7]

Logarithmic Buckets (8 to max_distance):
Distance:  8-11   12-16   17-23   24-33   ...   128+
Bucket:    [8]     [9]     [10]    [11]   ...   [31]

Logarithmic Distance Bucketing

To span long sequences without maintaining a separate parameter for every integer offset, T5 partitions relative distances into 32 discrete buckets:

  1. Exact Local Allocation: For small relative offsets ∣j−i∣<8|j - i| < 8, the bucket index equals the exact integer distance.
  2. Logarithmic Long-Range Allocation: For relative offsets between 8 and a maximum threshold (typically 128 tokens), bucket indices are assigned using a logarithmic scale:

bucket(d)=8+⌊log⁡(d/8)log⁡(max_distance/8)⋅(num_buckets−8)⌋\text{bucket}(d) = 8 + \left\lfloor \frac{\log(d / 8)}{\log(\text{max\_distance} / 8)} \cdot (\text{num\_buckets} - 8) \right\rfloor

  1. Saturation: Distances exceeding max_distance\text{max\_distance} map to the final bucket index (num_buckets−1\text{num\_buckets} - 1).
  2. Directional Splitting: In bidirectional encoder layers, positive and negative relative distances are mapped to separate bucket sets (e.g., 16 buckets for backward offsets, 16 for forward offsets). In causal decoder layers, only backward offsets (j≤ij \le i) are parameterized.

This logarithmic bucketing scheme reflects an intuitive linguistic principle: distinguishing between offset 1 and offset 2 (adjacent words) is essential for local phrase parsing, whereas distinguishing between offset 80 and offset 85 is unnecessary for capturing distant semantic relationships.

DeBERTa: Disentangled Attention

He et al. (2020) advanced relative position modeling in DeBERTa (Decoding-enhanced BERT with Disentangled Attention).

DeBERTa represents each input token using two separate vectors: a content vector HiH_i and a relative position vector Pi∣jP_{i|j} describing relative distance. The cross-attention logit is computed by disentangling content and position interactions into three additive matrices:

Ai,j=HiWqWkTHjT⏟Content-to-Content+HiWqWk,rTPi∣jT⏟Content-to-Position+Pj∣iWq,rWkTHjT⏟Position-to-ContentA_{i,j} = \underbrace{H_i W_q W_k^T H_j^T}_{\text{Content-to-Content}} + \underbrace{H_i W_q W_{k,r}^T P_{i|j}^T}_{\text{Content-to-Position}} + \underbrace{P_{j|i} W_{q,r} W_k^T H_j^T}_{\text{Position-to-Content}}

DeBERTa intentionally excluded the fourth term (Position-to-Position, Pj∣iWq,rWk,rTPi∣jTP_{j|i} W_{q,r} W_{k,r}^T P_{i|j}^T), demonstrating empirically that pure relative distance between two indices without content conditioning contains zero semantic information and adds unnecessary gradient noise.

Computational Complexity and KV Cache Bottlenecks

Despite their empirical strengths in language understanding benchmarks (GLUE, SuperGLUE), early relative positional mechanisms created severe computational and memory bottlenecks in large-scale autoregressive serving.

Attention Kernel Incompatibility

In modern LLM inference runtimes, scaled dot-product attention relies on fused GPU kernels such as FlashAttention and FlashAttention-3, which load blocks of QQ, KK, and VV matrices into SRAM and compute attention on chip without materializing the full N×NN \times N attention matrix in HBM.

  • Shaw & DeBERTa: Require tensor contractions involving 3D or 4D intermediate tensors (O(B×H×N×N×d)O(B \times H \times N \times N \times d)), forcing high-bandwidth memory reads and preventing fused SRAM execution.
  • T5: Requires adding an N×NN \times N matrix BB to QKTQ K^T. While easier to fuse than Shaw's method, dynamically indexing into bucket tables introduces warp divergence and non-contiguous memory lookups during kernel execution.

Autoregressive KV Caching Invalidation

During autoregressive token generation, standard decoders maintain a key-value cache (KV cache) storing projected representations of past tokens:

Kcache=[k1,k2,…,kt−1],Vcache=[v1,v2,…,vt−1]K_{\text{cache}} = [k_1, k_2, \dots, k_{t-1}], \quad V_{\text{cache}} = [v_1, v_2, \dots, v_{t-1}]

Under Shaw's formulation, key representations depend on the query position:

kj(i)=xjWK+ai−jKk_j^{(i)} = x_j W^K + a_{i - j}^K

Because the relative offset i−ji - j changes at every decoding step ii, previously cached key vectors become obsolete, requiring full re-computation (O(t2)O(t^2) per step) unless complex relative indexing transformations are applied.

Positional Encoding Paradigms:

Absolute Learned (BERT, GPT):
Input = Token_Embedding + Position_Embedding[i]

Relative Key/Value Bias (Shaw):
Attention_Logit = (q_i * (k_j + a_{j-i})^T) / sqrt(d)

Relative Scalar Bias (T5):
Attention_Logit = (q_i * k_j^T) / sqrt(d) + Bucket_Bias[j - i]

Rotational Multiplicative (RoPE):
Attention_Logit = (R_i * q_i) * (R_j * k_j)^T / sqrt(d) = q_i * R_{j-i} * k_j^T / sqrt(d)

The Modern Transition to Rotational and Slope Encodings

The tension between translation invariance and hardware efficiency drove the design of next-generation position representations in modern frontier LLMs:

  1. Rotary Position Embeddings (RoPE): Su et al. (2021) introduced RoPE, which encodes relative position purely through multiplicative complex rotations applied to query and key vectors:

⟨RΘ,mdq,RΘ,ndk⟩=qTRΘ,n−mdk\langle \mathbf{R}_{\Theta, m}^d \mathbf{q}, \mathbf{R}_{\Theta, n}^d \mathbf{k} \rangle = \mathbf{q}^T \mathbf{R}_{\Theta, n - m}^d \mathbf{k}

RoPE embeds relative distance n−mn - m directly into the standard inner product without adding parameters, preserving standard static KV caching and full compatibility with FlashAttention.

  1. Attention with Linear Biases (ALiBi): Press et al. (2021) replaced learned bucket tables with static, head-specific linear penalty slopes m⋅(j−i)m \cdot (j - i). ALiBi requires zero learned parameters and facilitates out-of-distribution sequence length extrapolation.

Relative positional encodings proved that sequence modeling in transformers is fundamentally relational. While early formulations incurred memory and cache overheads, their theoretical principles directly shaped the rotary and slope mechanisms that power modern foundation models today.

Sources

Frequently asked questions

What is the primary limitation of absolute positional encodings?

Absolute positional encodings suffer from a lack of translation invariance, meaning that shifting an identical phrase changes its interaction scores because they depend on rigid absolute coordinates rather than relative displacement.

How does Shaw's formulation handle relative distances?

Shaw et al. (2018) introduced distance clipping to prevent quadratic parameter scaling, asserting that beyond a specific token distance kk, precise relative offset information provides diminishing syntactic value.

What components make up the Transformer-XL attention decomposition?

Transformer-XL expands self-attention logits into four terms: Content-Content, Content-Dependent Position Bias, Global Content Bias, and Global Position Bias, allowing for segment-level recurrence without resetting positional context.

Why did T5 use logarithmic bucketing for relative positions?

T5 partitioned relative distances into exact buckets for small offsets and logarithmic buckets for longer ranges to span long sequences efficiently while distinguishing adjacent words more granularly than distant ones.

AILLMTransformersDeep LearningPositional EncodingsArchitecture