KERPLE: Kernelized Relative Positional Embedding for Length Extrapolation

Ta-Chung Chi, Ting-Han Fan, Peter J. Ramadge, Alexander I. Rudnicky · DOI

Introduction

Transformers have revolutionized natural language processing but suffer from a significant limitation: a "short-term memory" problem. Due to the quadratic computational cost of self-attention ($O(L^2)$), models are typically trained on short sequences (e.g., 512 or 1024 tokens). However, real-world applications often require processing much longer contexts, such as entire books or codebases. When input length exceeds the training sequence length—a challenge known as length extrapolation—standard Transformers often fail. While Absolute Positional Embeddings fail immediately, Relative Positional Embeddings (RPEs) show more promise. KERPLE (Kernelized Relative Positional Embedding) introduces a principled, learnable framework for RPEs based on kernel theory to address these limitations.

Methodology: Kernelized RPEs

The core innovation of KERPLE is treating the relative distance between tokens ($m - n$) as an input to a kernel function. The authors utilize Conditionally Positive Definite (CPD) kernels, which are mathematically suited for modeling distances.

The KERPLE framework, illustrating how a kernelized bias is applied to the self-attention matrix based on relative token distances.

A key technical challenge is that self-attention requires Positive Definite (PD) kernels. The authors demonstrate that any CPD kernel can be transformed into a PD kernel by adding a constant offset. Since the Softmax function in attention is invariant to constant shifts, this offset is automatically absorbed, simplifying implementation. KERPLE introduces two primary variants:

  1. Power Kernel: $-a|m-n|^p$, generalizing the ALiBi method.
  2. Logarithmic Kernel: $-a \log(1 + b|m-n|)$, which allows for a slower decay of attention over long distances.

Unlike previous fixed-bias methods, KERPLE makes parameters such as $a, b,$ and $p$ learnable for each attention head, allowing the model to adapt its context window dynamically.

Experimental Setup

The framework was evaluated using three diverse datasets from the Pile: OpenWebText2 (internet content), GitHub (source code), and ArXiv (academic papers). These datasets provide varied dependency structures to test extrapolation capabilities. Models were trained on 512-token sequences and tested on lengths up to 16,384 tokens.

Results and Key Insights

KERPLE-log consistently achieved the lowest perplexity at extreme sequence lengths, significantly outperforming Sinusoidal, Rotary, and ALiBi embeddings. For instance, on the GitHub dataset at 16k length, KERPLE-log maintained a perplexity of ~2.75, whereas Rotary embeddings exploded to ~31.3.

The "Flat Kernel" Discovery

Visualization of the learned kernels reveals that the Logarithmic variant often learns "flat" kernels for specific attention heads. These heads effectively "zoom out" to attend to very distant tokens, which is crucial for long-range performance.

Visualization of learned kernel values across different attention heads, showing varying rates of decay and "flat" kernels for long-range context.

Efficiency and Flexibility

Compared to T5's "bucket" system, which uses 32 parameters per head, KERPLE-log uses only two parameters ($a$ and $b$) while achieving superior results and approximately 10% faster training. KERPLE is only marginally slower (1.6%) than the non-learnable ALiBi method, making it highly practical for large-scale production.

Conclusion

KERPLE provides a robust and efficient solution for length extrapolation in Transformers. By grounding positional embeddings in kernel theory and making them learnable, it allows models to adapt to the specific context requirements of different datasets. Future work may explore non-monotonic kernels to capture more complex attention patterns or extend the framework to multi-dimensional data like images and video. While KERPLE solves the mathematical challenge of extrapolation, hardware memory constraints for $O(L^2)$ attention remain a separate engineering hurdle for very long sequences.

Summary updated

BibTeX
@inproceedings{chi2022kerple,
  author = {Ta-Chung Chi and Ting-Han Fan and Peter J. Ramadge and Alexander I. Rudnicky},
  title = {{KERPLE: Kernelized Relative Positional Embedding for Length Extrapolation}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2022},
  url = {https://openreview.net/forum?id=hXzOqPlXDwm},
}
Figure