The Impact of Positional Encoding on Length Generalization in Transformers

Amirhossein Kazemnejad, Inkit Padhi, K. Ramamurthy, Payel Das, Siva Reddy
2/12/2026

No official code found. Best implementation: McGill-NLP/length-generalization (139 stars, MIT, last commit 2024-04-30). 1 more below.

Abstract

Length generalization, the ability to generalize from small training context sizes to larger ones, is a critical challenge in the development of Transformer-based language models. Positional encoding (PE) has been identified as a major factor influencing length generalization, but the exact impact of different PE schemes on extrapolation in downstream tasks remains unclear. In this paper, we conduct a systematic empirical study comparing the length generalization performance of decoder-only Transformers with five different position encoding approaches including Absolute Position Embedding (APE), T5's Relative PE, ALiBi, and Rotary, in addition to Transformers without positional encoding (NoPE). Our evaluation encompasses a battery of reasoning and mathematical tasks. Our findings reveal that the most commonly used positional encoding methods, such as ALiBi, Rotary, and APE, are not well suited for length generalization in downstream tasks. More importantly, NoPE outperforms other explicit positional encoding methods while requiring no additional computation. We theoretically demonstrate that NoPE can represent both absolute and relative PEs, but when trained with SGD, it mostly resembles T5's relative PE attention patterns. Finally, we find that scratchpad is not always helpful to solve length generalization and its format highly impacts the model's performance. Overall, our work suggests that explicit position embeddings are not essential for decoder-only Transformers to generalize well to longer sequences.

View on arXivView PDF

Code Implementations(2)

MIT

Code for the paper "The Impact of Positional Encoding on Length Generalization in Transformers", NeurIPS 2023

1397Shell, PythonMay 30, 20232 years agoMIT
Apache-2.0

Randomized Positional Encodings Boost Length Generalization of Transformers

832PythonJun 16, 20232 years agoApache-2.0

Cite this paper

@article{kazemnejad2026impact,
  title  = {The Impact of Positional Encoding on Length Generalization in Transformers},
  author = {Amirhossein Kazemnejad and Inkit Padhi and K. Ramamurthy and Payel Das and Siva Reddy},
  year   = {2026},
  eprint = {2305.19466},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/pdf/2305.19466},
  journal = {NEURIPS 2023 2023}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2305.19466.svg)](https://codeofpaper.com/paper/2305.19466)

Discussion