[Paper Review] LM-Infinite: Zero-Shot Extreme Length Generalization for Large Language Models
This paper proposes LM-Infinite, a simple, parameter-free method for zero-shot extreme-length generalization in large language models. By applying a $λ$-shaped attention mask and a distance bound during inference, it enables state-of-the-art LLMs to generate fluent, coherent text up to 128k tokens without fine-tuning, achieving 2.72x decoding speedup and performance comparable to fine-tuned models on long-context benchmarks.
Today's large language models (LLMs) typically train on short text segments (e.g., <4K tokens) due to the quadratic complexity of their Transformer architectures. As a result, their performance suffers drastically on inputs longer than those encountered during training, substantially limiting their applications in real-world tasks involving long contexts such as encoding scientific articles, code repositories, or long dialogues. Through theoretical analysis and empirical investigation, this work identifies three major factors contributing to this length generalization failure. Our theoretical analysis further reveals that commonly used techniques like truncating the attention window or relative positional encodings are inadequate to address them. Answering these challenges, we propose LM-Infinite, a simple and effective method for enhancing LLMs' capabilities of handling long contexts. LM-Infinite is highly flexible and can be used with most modern LLMs off-the-shelf. Without any parameter updates, it allows LLMs pre-trained with 2K or 4K-long segments to generalize to up to 200M length inputs while retaining perplexity. It also improves performance on downstream tasks such as Passkey Retrieval and Qasper in the zero-shot setting. LM-Infinite brings substantial efficiency improvements: it achieves 2.7x decoding speed up and 7.5x memory saving over the original model. Our codes are released at \url{https://github.com/Glaciohound/LM-Infinite}.
Motivation & Objective
- Address the critical challenge of length generalization failure in LLMs when generating sequences longer than their training length.
- Identify and diagnose out-of-distribution (OOD) factors—unseen distances, excessive attended tokens, and implicitly encoded positional information—that cause performance degradation on long sequences.
- Develop a plug-and-play solution that enables existing pre-trained LLMs to generalize fluently to extreme lengths without any parameter updates or fine-tuning.
- Achieve high computational efficiency and consistent generation quality across diverse LLM architectures and long-context datasets.
Proposed method
- Introduce a $λ$-shaped attention mask that restricts the number of attended tokens per query, reducing computational load and mitigating OOD attention patterns.
- Apply a distance bound to limit the maximum relative distance considered in relative positional encoding, preventing unseen distances during inference.
- Apply the method at inference time only, requiring no retraining, fine-tuning, or parameter updates to the pre-trained model.
- Maintain $O(n)$ time and space complexity by avoiding full self-attention over long sequences, enabling efficient decoding.
- Integrate seamlessly with existing relative positional encoding schemes such as RoPE and Alibi, ensuring broad compatibility across LLMs.
- Apply the method on-the-fly during decoding, allowing dynamic handling of long contexts without architectural changes.

Experimental results
Research questions
- RQ1What are the primary out-of-distribution (OOD) factors causing length generalization failure in LLMs when generating beyond their training length?
- RQ2Can a simple, parameter-free method improve long-context generation fluency and coherence without fine-tuning or model retraining?
- RQ3To what extent can a $λ$-shaped attention mask and distance bound preserve generation quality on sequences up to 128k tokens?
- RQ4How does the proposed method compare in efficiency and performance to truncation baselines and fine-tuned models on long-context tasks?
- RQ5Can the method generalize across diverse LLM architectures (e.g., LLaMA, Llama-2, MPT, GPT-J) using relative positional encodings?
Key findings
- LM-Infinite enables LLaMA and Llama-2 to maintain fluent and coherent generation on 32k-token sequences from ArXiv, achieving BLEU scores of 19.7 and 18.4 respectively—comparable to fine-tuned models.
- On OpenWebText2, Llama-2 + LM-Infinite achieves a BLEU score of 9.6 at 16k tokens, while the base Llama-2 model generates gibberish (BLEU = 0.0).
- The method achieves a 2.72x speedup in decoding and a 3.16x speedup in encoding on 32k-length sequences, significantly improving inference efficiency.
- LM-Infinite outperforms a truncation baseline by achieving ~5 BLEU score advantage with similar computational cost, or matching the same BLEU score with only 25% of the computation.
- The method maintains consistent generation quality up to 128k tokens on both ArXiv and OpenWebText2, demonstrating robust zero-shot generalization.
- The approach is universally applicable to various SoTA LLMs using relative positional encodings, including LLaMA, Llama-2, MPT-7B, and GPT-J, without requiring model-specific adaptation.

Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.