Table of Contents
Fetching ...

Extrapolation by Association: Length Generalization Transfer in Transformers

Ziyang Cai, Nayoung Lee, Avi Schwarzschild, Samet Oymak, Dimitris Papailiopoulos

TL;DR

The paper investigates how transformers can extrapolate to longer inputs by transferring length generalization from related auxiliary tasks to main tasks. It shows that jointly training on longer, related tasks enables shorter tasks to generalize beyond their training lengths across arithmetic, string, and maze domains, with analogous effects observed in pretrained models. Mechanistic evidence points to shared attention circuits as a correlate of transfer, and RoPE encoding enhances this transfer relative to NoPE. These findings suggest that compositional reuse of inductive structure, fostered by multitask training and pretraining, underlies robust length generalization in transformers and related models.

Abstract

Transformer language models have demonstrated impressive generalization capabilities in natural language domains, yet we lack a fine-grained understanding of how such generalization arises. In this paper, we investigate length generalization--the ability to extrapolate from shorter to longer inputs--through the lens of \textit{task association}. We find that length generalization can be \textit{transferred} across related tasks. That is, training a model with a longer and related auxiliary task can lead it to generalize to unseen and longer inputs from some other target task. We demonstrate this length generalization transfer across diverse algorithmic tasks, including arithmetic operations, string transformations, and maze navigation. Our results show that transformer models can inherit generalization capabilities from similar tasks when trained jointly. Moreover, we observe similar transfer effects in pretrained language models, suggesting that pretraining equips models with reusable computational scaffolding that facilitates extrapolation in downstream settings. Finally, we provide initial mechanistic evidence that length generalization transfer correlates with the re-use of the same attention heads between the tasks. Together, our findings deepen our understanding of how transformers generalize to out-of-distribution inputs and highlight the compositional reuse of inductive structure across tasks.

Extrapolation by Association: Length Generalization Transfer in Transformers

TL;DR

The paper investigates how transformers can extrapolate to longer inputs by transferring length generalization from related auxiliary tasks to main tasks. It shows that jointly training on longer, related tasks enables shorter tasks to generalize beyond their training lengths across arithmetic, string, and maze domains, with analogous effects observed in pretrained models. Mechanistic evidence points to shared attention circuits as a correlate of transfer, and RoPE encoding enhances this transfer relative to NoPE. These findings suggest that compositional reuse of inductive structure, fostered by multitask training and pretraining, underlies robust length generalization in transformers and related models.

Abstract

Transformer language models have demonstrated impressive generalization capabilities in natural language domains, yet we lack a fine-grained understanding of how such generalization arises. In this paper, we investigate length generalization--the ability to extrapolate from shorter to longer inputs--through the lens of \textit{task association}. We find that length generalization can be \textit{transferred} across related tasks. That is, training a model with a longer and related auxiliary task can lead it to generalize to unseen and longer inputs from some other target task. We demonstrate this length generalization transfer across diverse algorithmic tasks, including arithmetic operations, string transformations, and maze navigation. Our results show that transformer models can inherit generalization capabilities from similar tasks when trained jointly. Moreover, we observe similar transfer effects in pretrained language models, suggesting that pretraining equips models with reusable computational scaffolding that facilitates extrapolation in downstream settings. Finally, we provide initial mechanistic evidence that length generalization transfer correlates with the re-use of the same attention heads between the tasks. Together, our findings deepen our understanding of how transformers generalize to out-of-distribution inputs and highlight the compositional reuse of inductive structure across tasks.

Paper Structure

This paper contains 39 sections, 20 figures, 3 tables.

Figures (20)

  • Figure 1: Trained separately, each task fails to generalize to longer inputs. When trained jointly, the main task inherits the generalization range of the auxiliary task.
  • Figure 2: Overview of the tasks used in our length generalization transfer experiments, spanning three domains: arithmetic, string manipulation, and maze solving. Each group consists of a main task trained on shorter sequences and one or more auxiliary tasks trained on longer ones. We study whether generalization to longer inputs can be transferred from the auxiliary to the main task.
  • Figure 3: Length generalization results for addition-related task groups. The main task is reverse add, with performance shown when trained with different auxiliary tasks. Each model is trained with 5 random seeds; best-performing runs are shown in bold. The dashed vertical line indicates the maximum training length for each task. When trained alone (d), the model fails to generalize beyond training length. Co-training with related auxiliary tasks (a-c) enables extrapolation to longer inputs.
  • Figure 4: Performance plots for string tasks. When trained alone (b, d), models fail to generalize beyond their training range. Co-training with auxiliary tasks (a, c) enables substantial length extrapolation.
  • Figure 5: $8\times 8$ mazes with number of nodes equal to 16, 32, and 64. We define length generalization as the ability to generalize to mazes with a higher number of nodes.
  • ...and 15 more figures