Generalizing Transformers via Relative Position Encodings

This direction is merged into another direction: relative positional encoding (distance-aware attention). Its last version stays readable and citable here.

Version 1 of 121 papers26 conceptsfrozen 2026-10-04

The standard Transformer architecture originally relied on absolute position embeddings—fixed or learnable vectors assigned to specific indices (e.g., "this is the 5th token"). While effective for fixed-length sequences, this approach struggles with length generalization: a model trained on 512 tokens often fails when asked to process 1,000.

Relative Position Encoding (RPE) shifts the paradigm from "where is this token in the sequence?" to "how far apart are these two tokens?" By focusing on the distance between elements rather than their absolute coordinates, RPE enables models to capture structural relationships that remain invariant regardless of sequence length or position.

The Intuition of Distance over Location

In absolute positioning, the model views a sentence like a map with fixed GPS coordinates. If a model learns a linguistic rule at coordinate 10, it may not naturally apply that same rule at coordinate 500. Jacob Krantz and Jugal Kalita argue in Abstractive Summarization Using Attentive Neural Techniques that representing distance as a learnable parameter allows the attention mechanism to focus on structural relationships.

This is particularly vital for tasks where the "shape" of the data matters more than its start point. For instance, in music generation, the interval between two notes defines a chord or melody. Lucas Fenaux and Maria Juliana Quintero found in their work on BumbleBee that while local attention can speed up training, relative timing is essential for capturing the "soul" and structural integrity of a composition.

Achieving Length Generalization

One of the most significant advantages of RPE is its ability to extrapolate. Because the model learns to attend to tokens based on their relative offset (e.g., "look at the token 3 steps to the left"), it encounters the same "distances" during inference on long sequences that it saw during training on short ones.

Research by Qiquan Zhang and colleagues in An Exploration of Length Generalization in Transformer-Based Speech Enhancement demonstrates this power: by using KERPLE (a kernel-based relative position embedding), they trained models on 1-second audio snippets that successfully generalized to 20-second files. Similarly, the Toeplitz Neural Network developed by Zhen Qin and others replaces standard attention with relative position-based interactions, allowing the model to extrapolate to sequences 30 times longer than those seen during training.

Disentangling Content from Position

A key breakthrough in RPE design is the separation of "what" a token is from "where" it is relative to others. In DeBERTa, Pengcheng He and Jianfeng Gao introduced Disentangled Attention. Instead of summing content and position vectors into a single input, they calculate attention scores using separate matrices for content-to-content, content-to-position, and position-to-content interactions.

This disentanglement allows the model to capture nuanced dependencies. For example, a verb might look for a subject specifically three positions to its left. Zhiheng Huang and Bing Xiang further refined this in Improve Transformer Models with Better Relative Position Embeddings, showing that richer interactions between queries and relative embeddings significantly improve performance on complex tasks like SQuAD.

Shift Equivariance and Local Context

Mathematically, RPE contributes to shift-equivariance. As Shokichi Takakura and Taiji Suzuki prove in their theoretical analysis of Transformers, positional encodings allow the attention mechanism to prioritize local context. This shift-equivariance ensures that the model's internal representation of a phrase like "the cat sat" remains consistent whether it appears at the beginning or the end of a document.

This property is leveraged in hybrid architectures like the Conformer, where Anmol Gulati and James Qin combine the global reach of Transformers with the local inductive bias of CNNs. By using relative positional encodings, the Conformer maintains robustness across varying speech durations, a finding echoed in the ESPnet toolkit updates by Pengcheng Guo and colleagues.

Preserving Structural Integrity

Finally, RPE is essential for identifying complex, multi-word structures. In the domain of Intellectual Property, Junping Du and Yuhui Wang developed BWET, which uses RPE to preserve the directional and distance relationships between tokens. This helps the model maintain the integrity of long technical entities, as the spatial proximity of related terms is more informative than their absolute index in a massive patent document. Similarly, ZEN 2.0 by Tong Zhang and Yan Song uses RPE to better represent n-gram "chunks," proving that structural linguistic patterns are best captured through relative spatial relationships.

Papers in this version

History & citationVersion 1 · 2026-10-04

Merged into another direction · Permanent link

View frozen version 1

Cite this direction

Latest version (a living page):

@misc{lacuna_generalizing_transformers_via_relative_p_278898fa,
  title = {Generalizing Transformers via Relative Position Encodings},
  author = {{Lacuna}},
  howpublished = {Lacuna research direction},
  year = {2026},
  url = {https://lacuna.tiptreesystems.com/direction/generalizing-transformers-via-relative-position-encodings/rdr_278898faa12d41a786a51a7f8b3f7d75},
  note = {Latest version of a living research direction; cite a version for a fixed text. Accessed 2026-11-24},
}

This exact version (version 1, frozen text):

@misc{lacuna_generalizing_transformers_via_relative_p_278898fa_v1,
  title = {Generalizing Transformers via Relative Position Encodings},
  author = {{Lacuna}},
  howpublished = {Lacuna research direction},
  year = {2026},
  url = {https://lacuna.tiptreesystems.com/direction/generalizing-transformers-via-relative-position-encodings/rdr_278898faa12d41a786a51a7f8b3f7d75/v/1},
  note = {Version 1, frozen 2026-10-04. Accessed 2026-11-24},
}