TL;DR
Random Float Sampling (RFS) is a remarkably simple yet powerful position indexing strategy that unlocks length generalization in Transformers without requiring extra fine-tuning. By sampling continuous float values instead of discrete integers during training, RFS prevents models from overfitting to specific sequence lengths and allows them to seamlessly handle longer contexts at inference time.
What Problem Does This Paper Address?
Transformer-based language models are notoriously bad at handling sequences longer than what they saw during training—a challenge known as length generalization. Standard position encodings (PEs), such as absolute sinusoidal encodings or Rotary Position Embeddings (RoPE), assign discrete integer indices ($0, 1, 2, \dots$) to tokens.
When a model trained on short sequences encounters a much longer sequence during inference, it faces an out-of-distribution (OOD) problem. It either encounters completely unseen position indices or unseen relative distances between tokens, causing the self-attention mechanism to break down. Existing solutions either require expensive long-context fine-tuning (like Position Interpolation) or struggle to converge and underfit (like Random Integer Sampling).
Methods
The authors introduce Random Float Sampling (RFS), a method that shifts position indexing from a discrete set of integers to a continuous range.
Here is how it works:
- During Training: For a sequence of length $n_{tr}$, the method samples $n_{tr}$ values independently and uniformly at random from the continuous range $[0, 1)$. These values are sorted in ascending order and used as the position indices.
- During Inference: For an input sequence of length $n_{in}$, the position indices are deterministically chosen as an evenly spaced partition of the same $[0, 1)$ range to ensure stable, reproducible outputs.
- Scaling: The selected float indices are scaled by a hyperparameter $L$ (set to 1,000 in experiments) to match the typical magnitude of indices used in standard PEs.
Because RFS only changes how position indices are generated, it can be seamlessly plugged into existing position encoding frameworks like absolute sinusoidal encodings, RoPE, and ALiBi.
Datasets
The authors evaluated RFS across two distinct categories of benchmarks:
- Algorithmic/Length Generalization Tasks: Six synthetic tasks designed to test structural generalization, including copy, copy2, reverse, addition, sort, summation, and the SCAN benchmark. Models were trained on short sequences and tested on sequences up to twice as long.
- Language Modeling & Commonsense Reasoning: Models were pretrained from scratch on the OpenWebText corpus. They were then evaluated zero-shot on 7 popular commonsense reasoning benchmarks: HellaSwag, RACE, ARC-Easy, ARC-Challenge, OpenBookQA, WinoGrande, and BoolQ.
Key Insights and Ideas
- The "Rank Explosion" of Simple Extension: The authors reveal why standard integer position encodings fail when extended. Through Singular Value Decomposition (SVD), they show that as sequence length grows, the rank of a standard position matrix increases. This "rank explosion" invades and corrupts the lower-dimensional subspace that the Transformer reserves for semantic information. RFS effectively caps and controls this rank regardless of sequence length.
- Randomness Breeds Robustness: By jittering the token positions during training using continuous floats, the model is forced to learn the attention mechanism based on flexible, relative token distances rather than rigid integer steps.
- Zero-Shot Extrapolation: Unlike methods that require a secondary fine-tuning phase on long sequences, RFS works entirely out-of-the-box.
Key Results
RFS demonstrates substantial performance gains over standard position encodings and models without position encodings (NoPE).
On the algorithmic copy task, absolute sinusoidal encoding with RFS maintains roughly 80% accuracy on sequences twice as long as the training length, whereas the baseline simple extension drops to 0% immediately outside the training window.
In zero-shot commonsense reasoning tasks, standard models suffer a severe performance drop when evaluated on out-of-distribution (OOD) longer contexts. RFS almost entirely recovers this loss. On average across the 7 tasks, the baseline model's accuracy dropped from 35.84% (in-distribution) to 34.47% (OOD). The RFS-equipped model achieved 35.78% on OOD contexts, performing virtually on par with the in-distribution baseline.
Limitations and Future Work
While promising, the authors note a few limitations:
- Model Scale: Due to compute constraints, experiments were restricted to models with around 100 million parameters. Testing on commercial-scale LLMs remains an open step.
- Extreme Length Degradation: Although RFS prevents catastrophic failure, performance still gradually deteriorates as sequences become exceptionally long.
The authors suggest that combining RFS with complementary attention-focused modifications, such as Scalable-Softmax, represents an exciting direction for future research.
Summary updated