Position Encoding with Random Float Sampling Enhances Length Generalization of Transformers

Atsushi Shimizu, Shohei Taniguchi, Yutaka Matsuo


Abstract
Length generalization is the ability of language models to maintain performance on inputs longer than those seen during pretraining. In this work, we introduce a simple yet powerful position encoding (PE) strategy, Random Float Sampling (RFS), that generalizes well to lengths unseen during pretraining or fine-tuning. In particular, instead of selecting position indices from a predefined discrete set, RFS uses randomly sampled continuous values, thereby avoiding out-of-distribution (OOD) issues on unseen lengths by exposing the model to diverse indices during training. Since assigning indices to tokens is a common and fundamental procedure in widely used PEs, the advantage of RFS can easily be incorporated into, for instance, the absolute sinusoidal encoding, RoPE, and ALiBi. Experiments corroborate its effectiveness by showing that RFS results in superior performance in length generalization tasks as well as zero-shot commonsense reasoning benchmarks.
Anthology ID:
2026.findings-eacl.261
Volume:
Findings of the Association for Computational Linguistics: EACL 2026
Month:
March
Year:
2026
Address:
Rabat, Morocco
Editors:
Vera Demberg, Kentaro Inui, Lluís Marquez
Venue:
Findings
SIG:
Publisher:
Association for Computational Linguistics
Note:
Pages:
4966–4980
Language:
URL:
https://preview.aclanthology.org/ingest-eacl/2026.findings-eacl.261/
DOI:
Bibkey:
Cite (ACL):
Atsushi Shimizu, Shohei Taniguchi, and Yutaka Matsuo. 2026. Position Encoding with Random Float Sampling Enhances Length Generalization of Transformers. In Findings of the Association for Computational Linguistics: EACL 2026, pages 4966–4980, Rabat, Morocco. Association for Computational Linguistics.
Cite (Informal):
Position Encoding with Random Float Sampling Enhances Length Generalization of Transformers (Shimizu et al., Findings 2026)
Copy Citation:
PDF:
https://preview.aclanthology.org/ingest-eacl/2026.findings-eacl.261.pdf
Checklist:
 2026.findings-eacl.261.checklist.pdf