Manuscript Offprint

arXiv:2609.11913

Distance generalization in transformers: why bother with positional encoding?

Daniel Henrik Nevermann, Claudius Gros

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Daniel Henrik Nevermann and Claudius Gros

Institute for Theoretical Physics, Goethe University Frankfurt, Germany

{nevermann,gros}@itp.uni-frankfurt.de

Abstract. Out-of-distribution length generalization, namely to extrapolate a task from short to longer context, has been studied intensively for transformers. Here we focus on distance generalization, which probes performance when inter-token distances are changed between training and inference, while keeping a fixed context length. We construct two synthetic delay copy tasks, both involving finite distances between source and recall, where tokens are copied either fully or selectively, and test models on delays unseen during training. We address three questions: (A) Do positional encoding schemes such as RoPE and ALiBi improve distance resolution relative to no positional encoding (NoPE)? (B) How does data diversity, the number of inter-token distances seen in training, affect performance? (C) When is distance transfer learning positive or negative? We present a thorough investigation, finding that it is paramount to improve our understanding of the underlying mechanisms.

Keywords: distance generalization · transformer · transfer learning.

1 Introduction

A core challenge for transformer-based large language models is their ability to generalize to out-of-distribution examples. It is well documented that extracting latent patterns in the training data enables language models to solve unseen problems at inference time [17,12]. Specifically, length generalization, that is the ability of a model to generalize tasks from short to longer context lengths, remains an area of active investigation [8,2].

In this work we focus on a related type of generalization, distance generalization, which is illustrated in Fig. 1. Unlike length generalization, for which a model's performance beyond its training context is evaluated, we constrain the context length and examine instead the model's ability to generalize across varying inter-token distances within an established context window. Importantly, the complimentary investigation of distance and length generalization allows to isolate distinct failure modes, respectively "can't handle unseen positions" versus "can't handle unseen token dependencies".

Following an increasing body of work on length generalization, we consider algorithmic tasks in our assessment of distance generalization. More precisely, we derive our datasets from a task switching framework that combines multiple

D. H. Nevermann et al.

Length Generalization

number of tokens to be copied

Distance Generalization

source-recall distance

Figure

Fig. 1. Comparison of length generalization and distance generalization. Illustration for a basic copy task (see e.g. [10]). For length generalization, the model is evaluated to copy sequences longer than the training sequences, resulting in longer context lengths at inference time. For distance generalization the context length remains fixed. Instead, part of the sequence is copied with delay, where the distance between source and recall (the delay) is increased or reduced at inference time.

different tasks within a single input sequence [7], which means that active tasks are recurrently switched. We use task switching for two delay copy tasks, a full delay copy and a selective delay copy, tailored to study distance generalization. In Fig. 1 we compare a typical copy task used in length generalization studies with the corresponding application to distance generalization.

Our results demonstrate that evaluating distance generalization is essential for a full understanding of how transformers generalize across positions. We identify different drivers and inhibitors of distance generalization. As a first step we study the impact of positional encoding on distance generalization. As illustrated in Fig. 2, one observes a strong impact. Somewhat paradoxically, we find that the absence of an explicit positional encoding scheme (NoPE) leads to the best generalization capabilities, in agreement with findings in [10] on length generalization. Details are discussed in Sec. 4.

We investigate furthermore how distance generalization is impacted by the diversity of distances presented in the training dataset. We find that training with a larger set of inter-token distances does increase generalization capabilities in absolute terms, somewhat as expected, but we also report strongly diminishing returns when performance is evaluated in relative terms. See Fig. 3 and Sec. 4. Lastly, we follow the work by [2] on length generalization and study whether distance generalization improves when models are trained on a main and an auxiliary task simultaneously, thereby allowing the model to infer from the auxiliary to the main task, or vice versa. We find that distance generalization regularly benefits from transfer learning, but not always.

Our contributions We highlight the importance to thoroughly investigate distance generalization in transformers.

Figure

Fig. 2. Distance generalization is strongly affected by positional encoding. We compare two explicit positional encoding schemes, ALiBi and RoPE, with NoPE (no positional encoding), where NoPE relies solely on causality, viz causal attention for inferring relative distances. The evaluation accuracy is plotted as a function of the distance between source and recall, compare Fig. 1. Either all m = 10 source tokens are to copied (left), the C task, or only the even component (right), denoted selective copy task S. For details see Sec. 3. During training, the model sees delay distances between 15 and 25 (gray shaded region).

  1. We find that performance improves in most cases when positional schemes such as RoPE and ALiBi are turned off, which suggests that the role of positional encoding should be reconsidered.

  2. We show that distance generalization can be used to quantify an important, but hitherto little investigated question: How much does a model benefit when training on sets of increased data diversity? Our findings suggest that the answer is positive in absolute terms, but negative when viewed relatively.

  3. We show that transfer learning can positively impact distance generalization but also identify cases where transfer learning hinders generalization.

References (25)