NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

On Scalar Embedding of Relative Positions in Attention Models

Junshuang Wu, Richong Zhang, Yongyi Mao, Junfan Chen

Abstract

Transformers rely on positional encodings to overcome their inherent permutation invariance. While Scalar Relative Positional Encoding (SRPE) is widely used in models like T5, its theoretical underpinnings have remained largely heuristic. This paper provides a rigorous probabilistic interpretation of SRPE as a Bayesian prior over relative positions. We further propose Adaptive T5 (AT5), which replaces T5's rigid bucketing with a learnable exponential function and MLP-based embeddings. AT5 achieves state-of-the-art results across multiple benchmarks by learning task-specific, smooth positional priors.

Introduction

The self-attention mechanism is the cornerstone of modern NLP, yet it lacks an intrinsic sense of word order. To address this, researchers have developed various positional encoding schemes. Google's T5 model introduced a Scalar Relative Positional Encoding (SRPE) that groups relative distances into discrete "buckets." Despite its empirical success, T5's approach relies on fixed mathematical heuristics and assumes independence between adjacent buckets, potentially limiting its adaptability to different linguistic contexts.

A Bayesian Perspective on Attention

A key contribution of this work is the theoretical proof that SRPE acts as a probabilistic prior over the attention distribution. In this framework, the dot-product context score represents the likelihood of attention given the input content, while the scalar positional embedding serves as the prior distribution ($\beta$). The resulting attention weights ($\alpha$) represent the posterior distribution after observing the input sequence.

Architecture of the attention mechanism with scalar relative positional encoding, where $e^{(l)}$ represents the positional bias added to the context score $s_{ij}$.

As shown in Figure 1, the relative distance $l = j - i$ is mapped through a bucketing function $b$ and an embedding function $g$ to produce a scalar bias $e^{(l)}$. This bias is added directly to the content-based score $s_{ij}$ before the softmax operation, effectively modulating the attention based on distance.

Adaptive T5 (AT5)

To overcome the limitations of T5's rigid bucketing, we introduce AT5. AT5 improves upon the standard SRPE in two primary ways:

  1. Learnable Bucketing: Instead of a fixed log-scale formula, AT5 uses a learnable exponential function: $b(l) = 1 - \exp(-l \cdot A \cdot \gamma)$. The parameter $\gamma$ is optimized during training, allowing the model to "stretch" or "compress" its positional focus based on the specific requirements of the task.
  2. MLP Embeddings: Rather than using a simple lookup table where each bucket is independent, AT5 employs a small Multi-Layer Perceptron (MLP). This architectural choice enforces a "dependency constraint," ensuring that the learned prior is a smooth, continuous function of distance, which prevents overfitting to specific relative indices.
Comparison of bucketing functions $b(l)$ between T5 (a, b) and AT5 (c, d), illustrating the transition from rigid heuristics to learnable, smooth priors.

Figure 6 highlights the difference: T5's bucketing (top row) results in discrete "staircase" steps, whereas AT5 (bottom row) can learn smooth, task-specific curves that better represent the natural correlation between adjacent positions.

Experimental Results

AT5 was evaluated against standard T5 baselines across a variety of tasks, demonstrating consistent improvements:

  • Question Answering (SQuAD 1.0): AT5 achieved an Exact Match (EM) of 72.12 and an F1 score of 80.93, significantly outperforming various T5 configurations.
  • Machine Translation (WMT 2016): On the English-German test set, AT5 reached a BLEU score of 34.26, compared to T5’s 33.64.
  • Text Classification: Across six datasets (including SST and TREC), AT5 consistently showed improvements, confirming that adaptive priors are more effective than fixed heuristics.

Conclusion

By framing positional encoding as a probabilistic prior, we provide a new lens through which to understand and improve Transformer architectures. AT5 demonstrates that replacing heuristic-based components with learnable, smooth functions allows models to better capture the nuances of relative position, leading to superior performance across diverse natural language processing tasks without adding significant computational overhead.

Summary updated

BibTeX
@inproceedings{wu2021scalar,
  author = {Junshuang Wu and Richong Zhang and Yongyi Mao and Junfan Chen},
  title = {{On Scalar Embedding of Relative Positions in Attention Models}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2021},
  doi = {10.1609/aaai.v35i16.17654},
}
Figure