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.

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:
Subsequent models such as BERT (Devlin et al., 2018) and GPT-2 (Radford et al., 2019) replaced fixed sinusoids with learned absolute position embeddings , where each absolute sequence index is assigned a dedicated parameter vector .
Absolute position injection suffers from three structural deficiencies:
- Lack of Translation Invariance: In an absolute encoding framework, the attention logit between token at position and token at position depends on the absolute coordinates and . If an identical phrase is shifted by an offset , the inner products change completely because and .
- Context Length Hard Ceilings: Learned absolute position tables cannot process sequences longer than without resizing and fine-tuning the embedding table. Any token index beyond the pre-allocated table has no representation.
- 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 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 and output vector are computed as:
Shaw et al. augmented the keys and values with learned relative position embedding tensors :
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 :
The relative representations are defined by indexing into learned tables :
This clipping formulation asserts that beyond a distance of 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 () achieved a 1.3 BLEU gain over absolute encodings, whereas adding relative representations to values () 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 and , where represents word embeddings and represents absolute position vectors, yields four terms:
Transformer-XL re-engineers this expansion into a relative formulation:
Key mathematical modifications include:
- Sinusoidal Relative Matrix: The absolute key position vector in terms (b) and (d) is replaced by , a fixed sinusoidal embedding representing the relative distance .
- Dedicated Projection Weights: Transformer-XL splits key projection weights into for content vectors and for relative positional vectors.
- Learned Global Bias Vectors: The query positional vector in terms (c) and (d) is replaced by learnable global parameter vectors . Because the query position does not need to specify an absolute index, and represent inductive biases: captures the general baseline affinity between query content and key content, while captures the baseline affinity for relative distance 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 added directly to the pre-softmax attention logit:
For an attention layer with heads, is a vector of 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:
- Exact Local Allocation: For small relative offsets , the bucket index equals the exact integer distance.
- Logarithmic Long-Range Allocation: For relative offsets between 8 and a maximum threshold (typically 128 tokens), bucket indices are assigned using a logarithmic scale:
- Saturation: Distances exceeding map to the final bucket index ().
- 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 () 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 and a relative position vector describing relative distance. The cross-attention logit is computed by disentangling content and position interactions into three additive matrices:
DeBERTa intentionally excluded the fourth term (Position-to-Position, ), 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 , , and matrices into SRAM and compute attention on chip without materializing the full attention matrix in HBM.
- Shaw & DeBERTa: Require tensor contractions involving 3D or 4D intermediate tensors (), forcing high-bandwidth memory reads and preventing fused SRAM execution.
- T5: Requires adding an matrix to . 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:
Under Shaw's formulation, key representations depend on the query position:
Because the relative offset changes at every decoding step , previously cached key vectors become obsolete, requiring full re-computation ( 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:
- 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:
RoPE embeds relative distance directly into the standard inner product without adding parameters, preserving standard static KV caching and full compatibility with FlashAttention.
- Attention with Linear Biases (ALiBi): Press et al. (2021) replaced learned bucket tables with static, head-specific linear penalty slopes . 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
- Shaw, P., Uszkoreit, J., & Vaswani, A. (2018). Self-Attention with Relative Position Representations. NAACL-HLT 2018. https://arxiv.org/abs/1803.02155
- Dai, Z., Yang, Z., Yang, Y., Carbonell, J., Le, Q. V., & Salakhutdinov, R. (2019). Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context. ACL 2019. https://arxiv.org/abs/1901.02860
- Raffel, C., Shazeer, N., Roberts, A., Lee, K., Narang, S., Matena, M., Zhou, Y., Li, W., & Liu, P. J. (2020). Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer. Journal of Machine Learning Research (JMLR). https://arxiv.org/abs/1910.10683
- He, P., Liu, X., Gao, J., & Chen, W. (2021). DeBERTa: Decoding-enhanced BERT with Disentangled Attention. ICLR 2021. https://arxiv.org/abs/2006.03654
- Su, J., Lu, Y., Pan, S., Ahmed, B., Liu, B., & Zheng, Y. (2024). RoFormer: Enhanced Transformer with Rotary Position Embedding. Neurocomputing. https://arxiv.org/abs/2104.09864
- Press, O., Smith, N. A., & Lewis, M. (2022). Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation. ICLR 2022. https://arxiv.org/abs/2108.12409



