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

Randomized Positional Encodings Boost Length Generalization of Transformers

Anian Ruoss, Grégoire Delétang, Tim Genewein, Jordi Grau-Moya, Róbert Csordás, Mehdi Bennani, Shane Legg, Joel Veness

TL;DR

Transformers are notoriously bad at "length generalization"—they often fail when asked to process sequences longer than those seen during training. This paper introduces Randomized Positional Encodings (RPE), a simple training-time augmentation that exposes models to a vast range of possible positions by subsampling random ordered subsets. This technique allows Transformers to generalize to unseen lengths with a 12% average accuracy boost while being up to 35x more computationally efficient than training on long sequences directly.

What Problem Does This Paper Address?

While Transformers have revolutionized AI, they have a "short-sighted" problem. Because the self-attention mechanism is inherently permutation-invariant (it doesn't know the order of tokens), we must inject positional encodings (PE).

The issue is that standard PEs are usually tied to the maximum sequence length $N$ seen during training. When a model trained on length $N=40$ is tested on length $M=500$, the positional values for indices 41 through 500 are "out-of-distribution" (OOD). The model’s internal activations drift into regions of the feature space it never explored during training, leading to a total breakdown in logic—even on simple tasks like reversing a string or adding two binary numbers.

Methods

The authors propose a "subsampling" approach to positional encodings. Instead of always using the first $n$ positions for a sequence of length $n$, they do the following during training:

  1. Define a large range: Pick a maximum position $L$ that is much larger than the training length $N$ (e.g., $L=2048$ while $N=40$).
  2. Random Subsampling: For each training batch, randomly select $n$ unique indices from the set $\{1, \dots, L\}$.
  3. Maintain Order: Sort these indices in ascending order.
  4. Apply Encodings: Use these sorted random indices as the input to the positional encoding function (whether it's sinusoidal, learned, or relative like RoPE).

By doing this, the model learns that "position 5" isn't a fixed coordinate, but rather a relative marker that can appear anywhere in a much larger range. This forces the model to learn the logic of position and order rather than over-fitting to specific absolute values.

Datasets

The researchers evaluated their method using a large-scale empirical study of 6,000 models across 15 algorithmic reasoning tasks. These tasks are categorized by the Chomsky hierarchy, which measures the computational complexity of formal languages:

  • Regular (R): Even Pairs, Parity Check, Cycle Navigation.
  • Deterministic Context-Free (DCF): Stack Manipulation, Reverse String, Modular Arithmetic.
  • Context-Sensitive (CS): Duplicate String, Binary Addition, Bucket Sort.

The models were trained on sequences of length 1 to 40 and evaluated on lengths up to 500.

Key Insights and Ideas

  • The "Aha" Moment on Activations: Using 2D PCA projections, the authors show that standard Transformers produce wildly different internal activations for long sequences compared to short ones. With RPE, the activations for long sequences (length 150) overlap almost perfectly with those of short sequences (length 40). The model stays "in-distribution" even when the input is long.
  • Order is the Key: A crucial finding in their ablation study was that sorting the random indices is mandatory. If you provide random positions without maintaining their relative order, the model fails to learn entirely. This confirms that Transformers primarily rely on the relative ordering of positional signals to solve algorithmic tasks.
  • Efficiency over Brute Force: The "naive" way to get length generalization is to just train on very long sequences. However, because attention complexity is quadratic ($O(\ell^2)$), this is incredibly slow. RPE achieves better generalization by training on short sequences, making it roughly 35.4 times faster to reach high accuracy than the brute-force approach.

Key Results

  • Accuracy Boost: RPE increased test accuracy by 12.0% on average across all tasks. On specific tasks like Missing Duplicate or Reverse String, the improvement was as high as 43.5%.
  • Solving the "Unsolvable": Several tasks that were previously considered "out of reach" for standard Transformers (like reversing long strings) were solved with near-perfect accuracy using randomized relative encodings.
  • Compatibility: The method proved effective across various PE schemes, including Sinusoidal, RoPE, ALiBi, and Learned encodings.

Limitations and Future Work

  • The $L$ Hyperparameter: While RPE is flexible, you still need to choose a maximum position $L$ in advance. If $L$ is much larger than anything the model sees during training, the model might not encounter enough unique indices to learn effectively.
  • Synthetic vs. Natural Language: The study focused on algorithmic reasoning (synthetic tasks). While these are excellent for testing pure logic and length generalization, it remains to be seen how RPE impacts large-scale natural language pre-training (like GPT-style models) where semantic context might interact differently with positional signals.
  • Attention Peakiness: The authors note that RPE doesn't solve all problems; for extremely long sequences, attention can become "less peaked" (diluted), which is a separate failure mode of the Transformer architecture itself.

Summary updated

BibTeX
@inproceedings{ruoss2023randomized,
  author = {Anian Ruoss and Grégoire Delétang and Tim Genewein and Jordi Grau-Moya and Róbert Csordás and Mehdi Bennani and Shane Legg and Joel Veness},
  title = {{Randomized Positional Encodings Boost Length Generalization of Transformers}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2023},
  doi = {10.18653/v1/2023.acl-short.161},
}
Figure