Length Generalization of Causal Transformers without Position Encoding

Jie Wang, Tao Ji, Yuanbin Wu, Hang Yan, Tao Gui, Qi Zhang, Xuanjing Huang, Xiaoling Wang

Abstract

Transformers typically rely on explicit position encodings (PE) to process sequence order, yet these often limit generalization to longer contexts than seen during training. Recent findings suggest that Transformers without position encoding (NoPE) can implicitly learn order but still face performance degradation at extended lengths. This work identifies a critical link between attention entropy spikes and the collapse of model perplexity. We propose HeadScale, a parameter-efficient fine-tuning method that assigns unique scaling factors to individual attention heads. With only 704 tunable parameters, a 1.1B NoPE model trained on 2k tokens generalizes to 16k tokens and beyond, rivaling state-of-the-art interpolation methods.

Introduction

The "hard limit" of training context windows is a significant bottleneck for Large Language Models (LLMs). While methods like Rotary Position Encodings (RoPE) are popular, they often fail catastrophically when encountering out-of-domain position features. Interestingly, causal Transformers without any explicit position encoding (NoPE) have shown an inherent ability to generalize better than PE-based models. However, even NoPE models eventually "lose focus." This paper investigates the mechanics of this failure and introduces a lightweight solution to extend context windows significantly.

The Entropy-Perplexity Connection

The researchers discovered a "smoking gun" for performance collapse: as sequence length increases beyond the training window, the entropy of attention distributions spikes. This indicates that attention heads stop focusing on specific relevant tokens and instead spread their weights thinly across the entire history.

Figure 1: Attention entropy spikes correlate almost perfectly with the inflection point where log perplexity explodes, signaling a loss of model focus beyond the training window.

As shown in Figure 1, the point where perplexity begins to rise sharply aligns with the rise in attention entropy. NoPE models are naturally more resilient than RoPE models, extending their effective window by approximately 20% before failing, but the eventual distraction is inevitable without intervention.

Methodology: HeadScale

To counteract the entropy spike, the authors propose scaling the softmax temperature ($\lambda$) in the attention mechanism. While a uniform scale across all heads (UniformScale) helps, it is suboptimal because different heads serve different functions—some are naturally "sharp" (low entropy) while others are "diffuse" (high entropy).

HeadScale treats the scaling factors for each head as learnable parameters in a parameter-efficient fine-tuning (PEFT) framework. For a 1.1B parameter model with 32 layers and 22 heads per layer, this involves tuning only 704 parameters. This tuning requires a minimal dataset—roughly 0.03% of the original training data.

Key Results

The application of HeadScale allows for massive context extension. A model trained on a 2,048-token window was able to maintain high performance up to 16,000 tokens.

Figure 5: Comparison of HeadScale ($\lambda^{(h)}$) against a fixed uniform scale. HeadScale effectively suppresses the entropy spike and maintains stable log perplexity well beyond the training limit.

On the PG19 dataset, vanilla NoPE's perplexity exceeds 1,000 at 16k tokens, representing total failure. In contrast, NoPE with HeadScale achieves a perplexity of 18.3, which is competitive with complex state-of-the-art methods like YaRN. In "needle in a haystack" tests (Passkey Retrieval), HeadScale maintained near-perfect accuracy far beyond the training window, whereas standard RoPE models failed immediately past 2,048 tokens.

Conclusion

This research demonstrates that explicit position encodings are not strictly necessary for long-context generalization in causal Transformers. By identifying attention entropy as the primary failure mode, the authors provide a simple, computationally "green" solution. HeadScale proves that a tiny number of parameters—less than 0.0001% of the total model—can unlock significant architectural potential for long-sequence processing. Future work remains to address the quadratic computational complexity of attention at these extended lengths.

Summary updated

BibTeX
@inproceedings{wang2024length,
  author = {Jie Wang and Tao Ji and Yuanbin Wu and Hang Yan and Tao Gui and Qi Zhang and Xuanjing Huang and Xiaoling Wang},
  title = {{Length Generalization of Causal Transformers without Position Encoding}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2024},
  doi = {10.18653/v1/2024.findings-acl.834},
}
Figure