DAPE: Data-Adaptive Positional Encoding for Length Extrapolation

Chuanyang Zheng, Yihang Gao, Han Shi, Minbin Huang, Jingyao Li, Jing Xiong, Xiaozhe Ren, Michael Ng, Xin Jiang, Zhenguo Li, Yu Li

TL;DR

DAPE (Data-Adaptive Positional Encoding) moves beyond static positional biases by allowing Transformers to dynamically adjust their sense of "where" tokens are based on "what" those tokens actually say. By using a small, learnable MLP to fuse semantic attention scores with traditional positional priors, DAPE achieves state-of-the-art length extrapolation—performing better on 8,192-token sequences than models trained on the same length, even when DAPE was only trained on 128 tokens.

What Problem Does This Paper Address?

Transformers are notoriously bad at "length extrapolation"—the ability to process sequences longer than those seen during training. A model trained on 512 tokens often produces gibberish when handed 1,024 tokens.

The root of the problem often lies in Positional Encoding (PE). Current methods like RoPE (Rotary) or Alibi (Linear Biases) are "static." Once training is over, the relationship between position $i$ and position $j$ is fixed. If the model decides that tokens 100 spots apart are less relevant than those 10 spots apart, it applies that rule universally, regardless of the text's content.

The authors argue that positional encoding should be data-adaptive. For example, if a model sees the word "Hakuna," it should "know" to look for "Matata" regardless of the exact distance between them. Static PEs struggle to prioritize semantic similarity over raw distance when the sequence length grows beyond the training window.

Methods

The core innovation is DAPE, which treats positional encoding as a dynamic correction term rather than a fixed addition.

  1. The Fusion Formula: Instead of just adding a bias matrix $B$ to the attention logits, DAPE uses a function $f$: $$A_{DAPE} = \text{Attention} + f(\text{Attention}, B)$$ Here, "Attention" represents the semantic similarity (Query-Key product), and $B$ is a traditional static bias (like Alibi or Kerple).

  2. The MLP "Brain": The function $f$ is implemented as a lightweight, two-layer MLP with LeakyReLU activation. It takes the raw attention score and the static positional bias as inputs and outputs a "corrected" bias.

  3. Multi-Head Integration: In a multi-head setup, DAPE doesn't process heads in isolation. It concatenates the attention and bias values from all $h$ heads, allowing the MLP to learn cross-head dependencies and richer semantic context.
  4. Efficiency: To keep the computational overhead low, the authors use a small hidden dimension ($D_{DAPE} = 32$). They demonstrate that as the model size (the $d$ dimension) increases, the relative cost of DAPE becomes negligible.

Datasets

The authors validated DAPE across three distinct types of data:

  • Arxiv & Books3: Standard long-form text datasets used to measure language modeling performance (perplexity) and length extrapolation.
  • CHE (Chomsky Hierarchy Evaluation): A benchmark of 14 algorithmic tasks (e.g., reversing strings, parity checks, sorting). This tests the model's ability to handle structural and logical rules that are highly dependent on position.

Key Insights and Ideas

  • Semantic Overrides Position: DAPE allows the model to "ignore" distance if the semantic connection between two tokens is strong enough. This prevents the "local bias" of traditional methods from drowning out distant but relevant information.
  • Local vs. Anti-Local Patterns: Visualization of the learned biases reveals that DAPE learns both "local" heads (focusing on nearby tokens) and "anti-local" heads (which actually increase attention for far-away tokens based on content).
Visualization of Attention, Kerple Bias, and DAPE Bias showing local and anti-local patterns.
  • The "Average" Problem: The authors point out that static PEs like FIRE or Kerple are essentially "average optimal" solutions across a whole dataset. DAPE provides an "instance-specific" optimal solution for every single sentence.

Key Results

  • Massive Extrapolation: When trained on a sequence length of only 128, DAPE-Kerple achieved a perplexity of 5.00 on an evaluation length of 8,192. For comparison, the vanilla Kerple baseline exploded to a perplexity of 31.93.
Validation perplexity on Arxiv dataset for models trained on sequence length 128.
  • Outperforming Longer Training: Remarkably, DAPE models trained on short sequences (512) often outperformed baseline models (like Alibi) that were trained on much longer sequences.
  • Algorithmic Superiority: On the CHE benchmark, DAPE variants (DAPE-FIRE and DAPE-Kerple) achieved the best performance in 10 out of 11 permutation-variant tasks, proving it isn't just memorizing text patterns but learning structural relationships.
  • Scalability: The method was tested on models up to 6.7B parameters, showing consistent gains and proving that the "semantic-positional fusion" scales well with model capacity.

Limitations and Future Work

  • Quadratic Complexity: While the MLP is small, DAPE still operates on the $N \times N$ attention matrix. It doesn't solve the fundamental $O(N^2)$ scaling issue of Transformers; it just makes the model much more effective within that quadratic window.
  • Hyperparameter Sensitivity: The choice of the base bias $B$ (Alibi vs. Kerple) still matters. DAPE is an enhancer, not a total replacement for a good base bias.
  • Future Directions: The authors suggest exploring DAPE in even larger "frontier" models (70B+) and investigating if this data-adaptive approach can be applied to linear-time attention mechanisms or state-space models (SSMs) like Mamba.

Summary updated

BibTeX
@inproceedings{zheng2024dape,
  author = {Chuanyang Zheng and Yihang Gao and Han Shi and Minbin Huang and Jingyao Li and Jing Xiong and Xiaozhe Ren and Michael Ng and Xin Jiang and Zhenguo Li and Yu Li},
  title = {{DAPE: Data-Adaptive Positional Encoding for Length Extrapolation}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=rnUEUbRxVu},
}
Figure