{"about":{"site":"https://codewithpapers.app","non_affiliation":"Code with Papers and Syntology are not affiliated with, endorsed by, or sponsored by Papers with Code, Meta, or the pwc-archive mirror.","licence":"CC BY-SA 4.0","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","attribution":"https://codewithpapers.app/attribution","modified":"archive material modified by Syntology; see the attribution page"},"url":"/paper/lm-infinite-simple-on-the-fly-length","title":"LM-Infinite: Zero-Shot Extreme Length Generalization for Large Language Models","arxiv_id":"2308.16137","date":"2023-08-30","proceeding":null,"authors":["Chi Han","Qifan Wang","Hao Peng","Wenhan Xiong","Yu Chen","Heng Ji","Sinong Wang"],"abstract":"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}.","url_abs":"https://arxiv.org/abs/2308.16137v7","url_pdf":"https://arxiv.org/pdf/2308.16137v7.pdf","source":{"archive":"pwc-archive (Hugging Face), CC BY-SA 4.0","snapshot":"2025-07-28","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","row_kind":"abstracts"},"code_links":[{"paper_slug":"lm-infinite-simple-on-the-fly-length","repo_url":"https://github.com/Glaciohound/LM-Infinite","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok","spdx":"MIT"}}],"tasks":[{"task_slug":"2k","task_name":"2k"},{"task_slug":"4k","task_name":"4k"},{"task_slug":"articles","task_name":"Articles"},{"task_slug":"text-generation","task_name":"Text Generation"}],"methods":[{"method_slug":"absolute-position-encodings","method_name":"Absolute Position Encodings"},{"method_slug":"adam","method_name":"Adam"},{"method_slug":"attention","method_name":"Attention"},{"method_slug":"bpe","method_name":"BPE"},{"method_slug":"dense-connections","method_name":"Dense Connections"},{"method_slug":"dropout","method_name":"Dropout"},{"method_slug":"label-smoothing","method_name":"Label Smoothing"},{"method_slug":"layer-normalization","method_name":"Layer Normalization"},{"method_slug":"linear-layer","method_name":"Linear Layer"},{"method_slug":"multi-head-attention","method_name":"Multi-Head Attention"},{"method_slug":"position-wise-feed-forward-layer","method_name":"Position-Wise Feed-Forward Layer"},{"method_slug":"residual-connection","method_name":"Residual Connection"},{"method_slug":"speed","method_name":"SPEED"},{"method_slug":"softmax","method_name":"Softmax"},{"method_slug":"transformer","method_name":"Transformer"},{"method_slug":"fail","method_name":"fail"}],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"syntology_url":null,"atlas_url":"https://app.syntology.ai/?focus=2308.16137","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2308.16137"}},"developers":"https://syntology.ai/developers","read_at":"2026-09-24T18:15:14+00:00","read_at_is":"when the build read Syntology's graph, not when any sample ran","claim":"Per-sample execution status on synthesized fixtures; not a correctness claim about the paper. Samples come from repositories linked to the paper, official or community; repo_kind says which.","repos":[{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/Glaciohound/LM-Infinite","reach":{"status":"ok","spdx":"MIT"}}],"summary":{"ran":5,"ran_draft_wrong":2,"unverified":4},"by_repo_kind":{"official":{"samples":11,"ran":7,"repositories":1}},"repo_kind_vocabulary":{"official":"The archive marks this repository official for the paper","named_in_paper":"The archive records that the paper mentions this repository; it is not marked official","listed":"In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper","found_in_text":"Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted","community":"Not in the archive's code links for this paper; a community repository Syntology harvested"},"n_pointer_only_for_licence":0,"samples":[{"code_sha256_prefix":"105fcf1b36112115","entry":"apply_rotary_pos_emb","repo":"Glaciohound/LM-Infinite","repo_kind":"official","path":"models/llama.py","file_url":"https://github.com/Glaciohound/LM-Infinite/blob/HEAD/models/llama.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"105fcf1b36112115"}},{"code_sha256_prefix":"18e07933cf1ab714","entry":"blockwise_sequence","repo":"Glaciohound/LM-Infinite","repo_kind":"official","path":"models/lambda_attention.py","file_url":"https://github.com/Glaciohound/LM-Infinite/blob/HEAD/models/lambda_attention.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"18e07933cf1ab714"}},{"code_sha256_prefix":"507f475feb734647","entry":"compute_intermediate_size","repo":"Glaciohound/LM-Infinite","repo_kind":"official","path":"models/get_llama2/convert_llama_weights_to_hf.py","file_url":"https://github.com/Glaciohound/LM-Infinite/blob/HEAD/models/get_llama2/convert_llama_weights_to_hf.py","link_basis":"harvester_set","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"507f475feb734647"}},{"code_sha256_prefix":"7018cec6ba82fa30","entry":"pad_sequence_to_length","repo":"Glaciohound/LM-Infinite","repo_kind":"official","path":"models/lambda_attention.py","file_url":"https://github.com/Glaciohound/LM-Infinite/blob/HEAD/models/lambda_attention.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"7018cec6ba82fa30"}},{"code_sha256_prefix":"c5bcf01d18bba63d","entry":"read_json","repo":"Glaciohound/LM-Infinite","repo_kind":"official","path":"models/get_llama2/convert_llama_weights_to_hf.py","file_url":"https://github.com/Glaciohound/LM-Infinite/blob/HEAD/models/get_llama2/convert_llama_weights_to_hf.py","link_basis":"harvester_set","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"OUTPUT_MISDECLARED","metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"c5bcf01d18bba63d"}},{"code_sha256_prefix":"e03d53ba9d4f9ae5","entry":"rotate_half","repo":"Glaciohound/LM-Infinite","repo_kind":"official","path":"models/llama.py","file_url":"https://github.com/Glaciohound/LM-Infinite/blob/HEAD/models/llama.py","link_basis":"harvester_set","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"MISDECLARED","metamorphic_tier":"invariant","behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"e03d53ba9d4f9ae5"}},{"code_sha256_prefix":"b616f600e17afc58","entry":"shift_and_pair","repo":"Glaciohound/LM-Infinite","repo_kind":"official","path":"models/lambda_attention.py","file_url":"https://github.com/Glaciohound/LM-Infinite/blob/HEAD/models/lambda_attention.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"b616f600e17afc58"}},{"code_sha256_prefix":"5f0747c4fe665871","entry":"attn_forward_factory","repo":"Glaciohound/LM-Infinite","repo_kind":"official","path":"models/gpt_j.py","file_url":"https://github.com/Glaciohound/LM-Infinite/blob/HEAD/models/gpt_j.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"5f0747c4fe665871"}},{"code_sha256_prefix":"34f3413b19988369","entry":"detailed_lambda_attention","repo":"Glaciohound/LM-Infinite","repo_kind":"official","path":"models/llama.py","file_url":"https://github.com/Glaciohound/LM-Infinite/blob/HEAD/models/llama.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"34f3413b19988369"}},{"code_sha256_prefix":"2210e387a48111ec","entry":"get_model","repo":"Glaciohound/LM-Infinite","repo_kind":"official","path":"models/get_model.py","file_url":"https://github.com/Glaciohound/LM-Infinite/blob/HEAD/models/get_model.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"2210e387a48111ec"}},{"code_sha256_prefix":"25ab7cdc2eb3c237","entry":"write_model","repo":"Glaciohound/LM-Infinite","repo_kind":"official","path":"models/get_llama2/convert_llama_weights_to_hf.py","file_url":"https://github.com/Glaciohound/LM-Infinite/blob/HEAD/models/get_llama2/convert_llama_weights_to_hf.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"25ab7cdc2eb3c237"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}