Length Generalization via Auxiliary Tasks

Pranjal Awasthi, Anupam Gupta, Ravi KumarNeurIPS 2025

Introduction

While Transformers have revolutionized sequence modeling, they consistently struggle with length generalization—the ability to handle test sequences significantly longer than those encountered during training. This limitation is particularly evident in algorithmic tasks like multi-digit addition or sorting. For instance, a model might learn to sort 20 numbers perfectly but fail completely when asked to sort 100.

Existing fixes are often fragile or ad hoc, relying on specialized positional encodings or specific data formatting. Furthermore, theoretical understanding has largely been limited to asymptotic results or restricted to very specific architectures. In this work, researchers introduce a general, non-asymptotic framework to explain why and when a model can generalize to longer sequences, proving that length generalization is achievable when models are trained on specific "auxiliary tasks."

A New Theoretical Framework

The authors propose a novel "Training Recipe for Length Generalization" built upon three core pillars:

  1. Sparse Tasks & Auxiliary Functions: The framework focuses on "sparse tasks," where each output token depends only on a small subset of preceding tokens. The researchers identify "auxiliary tasks"—the local, intermediate functions (like finding the successor in a sorting algorithm or adding two digits in arithmetic) that compose the primary task.
  2. Robust Training: Instead of standard training on sequences of length $n$, the model is trained on "random restrictions" of much longer sequences. This forces the model to identify the crucial "anchor" tokens needed for the task, even when they are surrounded by numerous irrelevant "distractor" tokens.
  3. Multi-Task Learning: The model is trained jointly on the primary task and these identified auxiliary tasks in a round-robin fashion.
Sorting Task Example

Crucially, the authors prove a Generalization Bound: if a model is "robustly correct" at a small training length $n$, its error at a much larger length $N$ is guaranteed to grow only linearly ($\epsilon \cdot N/n$), avoiding the catastrophic failure typically seen in standard Transformers.

Key Insights

  • Sparsity is the Secret Sauce: Length generalization is fundamentally tied to the sparsity of the underlying algorithm. If a task can be broken down into local operations that only attend to a few tokens, a Transformer can learn to apply those operations regardless of the total sequence length.
  • Robustness Over Accuracy: Simply achieving high accuracy on the training set is insufficient. To generalize, a model must be robustly accurate, meaning it can successfully extract relevant information even when buried within a much longer sequence.
  • The "Rule of Thumb": The paper provides a practical mathematical rule-of-thumb that allows researchers to predict how well a model will generalize to a longer length $N$ based on its "margin" (confidence) on auxiliary tasks during training.
  • No Positional Encodings Required: Interestingly, the authors achieved these strong results using NoPE (No Positional Encoding). This suggests that for these specific algorithmic tasks, the attention mechanism itself is sufficient if trained correctly to recognize local patterns.
Addition and Carry Auxiliary Task

Empirical Results

The researchers evaluated their method on several synthetic benchmarks known to be difficult for standard Transformers, including Sorting, Reversal, Increment (adding 1 to a multi-digit number), Parity, and SLiM (Small Language Modeling, a complex task mimicking contextual lookup).

The empirical results demonstrate a dramatic "step-function" improvement over baseline training:

  • Sorting: While the baseline model's accuracy dropped to near 0% at length 100, the auxiliary task-trained model maintained >90% accuracy.
  • Linear Error Growth: As predicted by their theoretical bounds, the error of the robustly trained models increased only gradually as sequences got longer, rather than hitting a sudden "length wall."
  • SLiM Task: On this complex lookup task, the proposed method maintained high accuracy for sequences up to twice the training length, whereas the baseline failed almost immediately after surpassing the training threshold.

Limitations and Future Work

While the results are promising, the current theoretical proofs focus primarily on single-layer, attention-only Transformers. Although empirical results on multi-layer models were successful, extending the formal theory to deeper, more complex architectures remains necessary.

Additionally, the "auxiliary tasks" currently must be manually identified by researchers based on the known algorithm. A major future direction is the automatic discovery of these sub-tasks. Finally, scaling these insights from synthetic algorithmic tasks to the noisy, complex domain of natural language processing—where "local functions" are much harder to define—represents a significant and exciting challenge for future research.

Summary updated

BibTeX
@inproceedings{awasthi2025length,
  author = {Pranjal Awasthi and Anupam Gupta and Ravi Kumar},
  title = {{Length Generalization via Auxiliary Tasks}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2025},
  url = {https://openreview.net/forum?id=QL3J1fyAFv},
}
Figure