In recent years, there have been remarkable advancements in the performance of Transformer-based Large Language Models (LLMs)  across various domains.
As these LLMs are deployed for increasingly complex domains, they often face the need to follow longer user prompts or generate longer texts. In these situations, thelength generalization failureof LLMs on long sequences becomes more prominent.
Most pre-training schemes truncate training sequences to a fixed length. LLMs often struggle to generate fluent and coherent texts after longer contexts, even with relative positional encoding specifically designed to cope with this problem. Common solutions such as finetuning on longer corpora often involve daunting hardware and time costs\chihanand require careful training process design.
To more efficiently extrapolate existing LLMs’ generation quality to longer texts, we theoretically and empirically investigate the main out-of-distribution (OOD)  factors contributing to this problem. Inspired by this diagnosis, we propose a simple yet effective solution for on-the-fly length generalization,\model. It involves only a-shaped attention mask\chihan(to avoid excessive attended tokens)  and a distance limit\chihan(to avoid unseen distances)  while requiring no parameter updates or learning. We find it applicable to a variety of LLMs using relative-position encoding methods.\modelis computationally efficient withtime and space, and demonstrates consistent\chihantext generation fluency and quality to as long as 128k tokens on ArXiv and OpenWebText2 datasets, with 2.72x decoding speedup. We will make the codes publicly available following publication.