{"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/retrieval-head-mechanistically-explains-long","title":"Retrieval Head Mechanistically Explains Long-Context Factuality","arxiv_id":"2404.15574","date":"2024-04-24","proceeding":null,"authors":["Wenhao Wu","Yizhong Wang","Guangxuan Xiao","Hao Peng","Yao Fu"],"abstract":"Despite the recent progress in long-context language models, it remains elusive how transformer-based models exhibit the capability to retrieve relevant information from arbitrary locations within the long context. This paper aims to address this question. Our systematic investigation across a wide spectrum of models reveals that a special type of attention heads are largely responsible for retrieving information, which we dub retrieval heads. We identify intriguing properties of retrieval heads:(1) universal: all the explored models with long-context capability have a set of retrieval heads; (2) sparse: only a small portion (less than 5\\%) of the attention heads are retrieval. (3) intrinsic: retrieval heads already exist in models pretrained with short context. When extending the context length by continual pretraining, it is still the same set of heads that perform information retrieval. (4) dynamically activated: take Llama-2 7B for example, 12 retrieval heads always attend to the required information no matter how the context is changed. The rest of the retrieval heads are activated in different contexts. (5) causal: completely pruning retrieval heads leads to failure in retrieving relevant information and results in hallucination, while pruning random non-retrieval heads does not affect the model's retrieval ability. We further show that retrieval heads strongly influence chain-of-thought (CoT) reasoning, where the model needs to frequently refer back the question and previously-generated context. Conversely, tasks where the model directly generates the answer using its intrinsic knowledge are less impacted by masking out retrieval heads. These observations collectively explain which internal part of the model seeks information from the input tokens. We believe our insights will foster future research on reducing hallucination, improving reasoning, and compressing the KV cache.","url_abs":"https://arxiv.org/abs/2404.15574v1","url_pdf":"https://arxiv.org/pdf/2404.15574v1.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":"retrieval-head-mechanistically-explains-long","repo_url":"https://github.com/nightdessert/retrieval_head","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok"}}],"tasks":[{"task_slug":"continual-pretraining","task_name":"Continual Pretraining"},{"task_slug":"hallucination","task_name":"Hallucination"},{"task_slug":"large-language-model","task_name":"Large Language Model"},{"task_slug":"long-context-understanding","task_name":"Long-Context Understanding"},{"task_slug":"retrieval","task_name":"Retrieval"}],"methods":[{"method_slug":"pruning","method_name":"Pruning"},{"method_slug":"set","method_name":"SET"}],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"atlas_url":"https://app.syntology.ai/?focus=2404.15574","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2404.15574"}},"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/nightdessert/retrieval_head","reach":{"status":"ok"}}],"summary":{"ran_fixture":2,"ran_draft_wrong":2,"ran":3},"by_repo_kind":{"official":{"samples":7,"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":7,"samples":[{"code_sha256_prefix":"30d7eec482ebf6b1","entry":"repeat_kv","repo":"nightdessert/retrieval_head","repo_kind":"official","path":"faiss_attn/source/modeling_llama.py","file_url":"https://github.com/nightdessert/retrieval_head/blob/HEAD/faiss_attn/source/modeling_llama.py","link_basis":"harvester_set","language":"python","status":"ran_fixture","verification_level":2,"contract_check":"RAISES","metamorphic_tier":"invariant","behaviour_fingerprint":true,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"30d7eec482ebf6b1"}},{"code_sha256_prefix":"d61c483a3c2b3156","entry":"apply_rotary_pos_emb","repo":"nightdessert/retrieval_head","repo_kind":"official","path":"faiss_attn/source/modeling_llama.py","file_url":"https://github.com/nightdessert/retrieval_head/blob/HEAD/faiss_attn/source/modeling_llama.py","link_basis":"harvester_set","language":"python","status":"ran_fixture","verification_level":1,"contract_check":"RAISES","metamorphic_tier":"invariant","behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"d61c483a3c2b3156"}},{"code_sha256_prefix":"bac65c3dafaec040","entry":"apply_rotary_pos_emb","repo":"nightdessert/retrieval_head","repo_kind":"official","path":"faiss_attn/source/modeling_phi3.py","file_url":"https://github.com/nightdessert/retrieval_head/blob/HEAD/faiss_attn/source/modeling_phi3.py","link_basis":"harvester_set","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"MISDECLARED","metamorphic_tier":"invariant","behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"bac65c3dafaec040"}},{"code_sha256_prefix":"ce3c4802be48b9d2","entry":"insert_needle","repo":"nightdessert/retrieval_head","repo_kind":"official","path":"faiss_attn/source/utils.py","file_url":"https://github.com/nightdessert/retrieval_head/blob/HEAD/faiss_attn/source/utils.py","link_basis":"harvester_set","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"ce3c4802be48b9d2"}},{"code_sha256_prefix":"71b1358cf9d3dbda","entry":"load_balancing_loss_func","repo":"nightdessert/retrieval_head","repo_kind":"official","path":"faiss_attn/source/modeling_mixtral.py","file_url":"https://github.com/nightdessert/retrieval_head/blob/HEAD/faiss_attn/source/modeling_mixtral.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"71b1358cf9d3dbda"}},{"code_sha256_prefix":"6af3fc7115dbdfad","entry":"load_context","repo":"nightdessert/retrieval_head","repo_kind":"official","path":"faiss_attn/source/utils.py","file_url":"https://github.com/nightdessert/retrieval_head/blob/HEAD/faiss_attn/source/utils.py","link_basis":"harvester_set","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"6af3fc7115dbdfad"}},{"code_sha256_prefix":"b99eea6376d1e212","entry":"rotate_half","repo":"nightdessert/retrieval_head","repo_kind":"official","path":"faiss_attn/source/modeling_llama.py","file_url":"https://github.com/nightdessert/retrieval_head/blob/HEAD/faiss_attn/source/modeling_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":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"b99eea6376d1e212"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}