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

Absolute Position Embedding Learns Sinusoid-like Waves for Attention Based on Relative Position

Yuji Yamamoto, Takuya MatsuzakiEMNLP 2023

Abstract

This study investigates how Transformer models with learnable absolute position embeddings, such as RoBERTa, effectively perform relative position tasks. Through signal processing and linear algebra, the authors demonstrate that these models spontaneously develop sinusoidal patterns in their embeddings. By applying phase shifts between queries and keys, attention heads specialize in relative distances, bridging the gap between absolute coordinates and relative context.

Introduction

The original Transformer architecture used fixed, mathematical sinusoidal functions to encode word positions. However, popular successors like BERT and RoBERTa transitioned to "learnable absolute position embeddings"—unique, trainable vectors for every index. While these embeddings theoretically only encode absolute positions (e.g., "index 5"), researchers have observed that BERT’s attention heads often perform relative tasks, such as attending to the immediate neighbor. This paper explains the mechanism that allows absolute coordinates to transform into relative context.

Methodology

The authors employed several signal processing and linear algebra techniques to dissect pre-trained models, primarily RoBERTa, BERT, GPT-2, and BART. Key methods included:

  • Discrete Fourier Transform (DFT): Used to analyze learned position embeddings as time series to detect hidden periodic patterns.
  • Singular Value Decomposition (SVD): Applied to query ($W^Q$) and key ($W^K$) weight matrices to isolate the components attention heads prioritize.
  • Cross-correlation Analysis: Used to measure the "phase shift" between waves found in queries and keys.
  • Controlled Re-learning: Position embeddings were re-trained on the Wikitext-2 dataset to observe how models learn to identify neighbors across gaps.

Spontaneous Periodicity and the Phase Shift Mechanism

A core discovery is that RoBERTa’s learned embeddings naturally develop sinusoidal waves, even without explicit instruction. The model prefers periodic patterns because they are mathematically efficient for representing distance in language, which is inherently translation-invariant.

Frequency spectrum of position embeddings in RoBERTa vs GPT-2, highlighting spontaneous periodicity in RoBERTa.

As shown in the frequency analysis, RoBERTa exhibits clear peaks in its spectrum, whereas GPT-2—a decoder using causal masking—shows a different profile. To attend to a neighbor, an attention head utilizes a "phase shift trick": if the query wave is shifted relative to the key wave, the resulting dot product peaks at a specific relative distance.

Using k-means clustering, the authors identified that many attention heads are dedicated specifically to relative positioning tasks, such as "next-to-left" or "rightward" attention.

Specialization of attention heads across layers in RoBERTa, showing patterns like next-to-left and rightward attention.

Positional information is most prominent in the lower layers of the network. As the data moves into deeper layers (9-12), the correlation with position embeddings drops, indicating a shift from structural processing to semantic meaning. Interestingly, GPT-2 relies less on these periodic mechanisms, likely due to the inherent relative bias provided by its causal mask.

Conclusion and Limitations

The emergence of relative positioning from absolute coordinates is a fundamental property of how these models adapt to the requirements of natural language. While the study successfully decodes the "nearby" attention mechanism, it remains less clear how models handle long-distance relative dependencies. Future work may explore whether explicitly constraining these learned waves could lead to more efficient training or memory usage.

Summary updated

BibTeX
@inproceedings{yamamoto2023absolute,
  author = {Yuji Yamamoto and Takuya Matsuzaki},
  title = {{Absolute Position Embedding Learns Sinusoid-like Waves for Attention Based on Relative Position}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2023},
  doi = {10.18653/v1/2023.emnlp-main.2},
}
Figure