{"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/interpreting-key-mechanisms-of-factual-recall","title":"Interpreting Key Mechanisms of Factual Recall in Transformer-Based Language Models","arxiv_id":"2403.19521","date":"2024-03-28","proceeding":null,"authors":["Ang Lv","Yuhan Chen","Kaiyi Zhang","Yulong Wang","Lifeng Liu","Ji-Rong Wen","Jian Xie","Rui Yan"],"abstract":"In this paper, we delve into several mechanisms employed by Transformer-based language models (LLMs) for factual recall tasks. We outline a pipeline consisting of three major steps: (1) Given a prompt ``The capital of France is,'' task-specific attention heads extract the topic token, such as ``France,'' from the context and pass it to subsequent MLPs. (2) As attention heads' outputs are aggregated with equal weight and added to the residual stream, the subsequent MLP acts as an ``activation,'' which either erases or amplifies the information originating from individual heads. As a result, the topic token ``France'' stands out in the residual stream. (3) A deep MLP takes ``France'' and generates a component that redirects the residual stream towards the direction of the correct answer, i.e., ``Paris.'' This procedure is akin to applying an implicit function such as ``get\\_capital($X$),'' and the argument $X$ is the topic token information passed by attention heads. To achieve the above quantitative and qualitative analysis for MLPs, we proposed a novel analytic method aimed at decomposing the outputs of the MLP into components understandable by humans. Additionally, we observed a universal anti-overconfidence mechanism in the final layer of models, which suppresses correct predictions. We mitigate this suppression by leveraging our interpretation to improve factual recall confidence. The above interpretations are evaluated across diverse tasks spanning various domains of factual knowledge, using various language models from the GPT-2 families, 1.3B OPT, up to 7B Llama-2, and in both zero- and few-shot setups.","url_abs":"https://arxiv.org/abs/2403.19521v4","url_pdf":"https://arxiv.org/pdf/2403.19521v4.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":"interpreting-key-mechanisms-of-factual-recall","repo_url":"https://github.com/trestad/factual-recall-mechanism","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok"}}],"tasks":[],"methods":[{"method_slug":"adam","method_name":"Adam"},{"method_slug":"attention","method_name":"Attention"},{"method_slug":"attention-dropout","method_name":"Attention Dropout"},{"method_slug":"bpe","method_name":"BPE"},{"method_slug":"cosine-annealing","method_name":"Cosine Annealing"},{"method_slug":"dense-connections","method_name":"Dense Connections"},{"method_slug":"discriminative-fine-tuning","method_name":"Discriminative Fine-Tuning"},{"method_slug":"dropout","method_name":"Dropout"},{"method_slug":"gpt-2","method_name":"GPT-2"},{"method_slug":"layer-normalization","method_name":"Layer Normalization"},{"method_slug":"linear-layer","method_name":"Linear Layer"},{"method_slug":"linear-warmup-with-cosine-annealing","method_name":"Linear Warmup With Cosine Annealing"},{"method_slug":"multi-head-attention","method_name":"Multi-Head Attention"},{"method_slug":"opt","method_name":"OPT"},{"method_slug":"residual-connection","method_name":"Residual Connection"},{"method_slug":"softmax","method_name":"Softmax"},{"method_slug":"weight-decay","method_name":"Weight Decay"}],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"syntology_url":"https://syntology.ai/paper/2403.19521","atlas_url":"https://app.syntology.ai/?focus=2403.19521","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2403.19521"}},"developers":"https://syntology.ai/developers","read_at":"2026-09-25T09:33:49+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/trestad/factual-recall-mechanism","reach":{"status":"ok"}}],"summary":{"ran":12,"unverified":2},"by_repo_kind":{"official":{"samples":14,"ran":12,"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":14,"samples":[{"code_sha256_prefix":"bf14a58ad9daa314","entry":"gen_flipped_prompts","repo":"trestad/factual-recall-mechanism","repo_kind":"official","path":"easy_transformer/fact_dataset.py","file_url":"https://github.com/trestad/factual-recall-mechanism/blob/HEAD/easy_transformer/fact_dataset.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":"bf14a58ad9daa314"}},{"code_sha256_prefix":"0e748f7b2746f129","entry":"gen_prompt_uniform","repo":"trestad/factual-recall-mechanism","repo_kind":"official","path":"easy_transformer/fact_dataset.py","file_url":"https://github.com/trestad/factual-recall-mechanism/blob/HEAD/easy_transformer/fact_dataset.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":"0e748f7b2746f129"}},{"code_sha256_prefix":"1f5a0cbe7a2e0eac","entry":"get_corner","repo":"trestad/factual-recall-mechanism","repo_kind":"official","path":"easy_transformer/utils.py","file_url":"https://github.com/trestad/factual-recall-mechanism/blob/HEAD/easy_transformer/utils.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"1f5a0cbe7a2e0eac"}},{"code_sha256_prefix":"c3e96952330b24b6","entry":"get_hook_tuple","repo":"trestad/factual-recall-mechanism","repo_kind":"official","path":"easy_transformer/utils_circuit_discovery.py","file_url":"https://github.com/trestad/factual-recall-mechanism/blob/HEAD/easy_transformer/utils_circuit_discovery.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":"c3e96952330b24b6"}},{"code_sha256_prefix":"0f4a0aa12ed98682","entry":"get_sample_from_dataset","repo":"trestad/factual-recall-mechanism","repo_kind":"official","path":"easy_transformer/utils.py","file_url":"https://github.com/trestad/factual-recall-mechanism/blob/HEAD/easy_transformer/utils.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":"0f4a0aa12ed98682"}},{"code_sha256_prefix":"c3075c10972d3c04","entry":"list_diff","repo":"trestad/factual-recall-mechanism","repo_kind":"official","path":"easy_transformer/fact_circuit_extraction.py","file_url":"https://github.com/trestad/factual-recall-mechanism/blob/HEAD/easy_transformer/fact_circuit_extraction.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":"c3075c10972d3c04"}},{"code_sha256_prefix":"34f3624167c77416","entry":"max_2d","repo":"trestad/factual-recall-mechanism","repo_kind":"official","path":"easy_transformer/fact_utils.py","file_url":"https://github.com/trestad/factual-recall-mechanism/blob/HEAD/easy_transformer/fact_utils.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":"34f3624167c77416"}},{"code_sha256_prefix":"a08c9fa456b3584d","entry":"patch_all","repo":"trestad/factual-recall-mechanism","repo_kind":"official","path":"easy_transformer/utils_circuit_discovery.py","file_url":"https://github.com/trestad/factual-recall-mechanism/blob/HEAD/easy_transformer/utils_circuit_discovery.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":"a08c9fa456b3584d"}},{"code_sha256_prefix":"6bc19a4e655618d7","entry":"patch_positions","repo":"trestad/factual-recall-mechanism","repo_kind":"official","path":"easy_transformer/utils_circuit_discovery.py","file_url":"https://github.com/trestad/factual-recall-mechanism/blob/HEAD/easy_transformer/utils_circuit_discovery.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":"6bc19a4e655618d7"}},{"code_sha256_prefix":"6275c5441a2e89af","entry":"process_heads_and_mlps","repo":"trestad/factual-recall-mechanism","repo_kind":"official","path":"easy_transformer/fact_circuit_extraction.py","file_url":"https://github.com/trestad/factual-recall-mechanism/blob/HEAD/easy_transformer/fact_circuit_extraction.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":"6275c5441a2e89af"}},{"code_sha256_prefix":"09c5e6ff142680cd","entry":"remove_prefix","repo":"trestad/factual-recall-mechanism","repo_kind":"official","path":"easy_transformer/fact_dataset.py","file_url":"https://github.com/trestad/factual-recall-mechanism/blob/HEAD/easy_transformer/fact_dataset.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":"09c5e6ff142680cd"}},{"code_sha256_prefix":"168eb8501c7db28f","entry":"turn_keep_into_rmv","repo":"trestad/factual-recall-mechanism","repo_kind":"official","path":"easy_transformer/fact_circuit_extraction.py","file_url":"https://github.com/trestad/factual-recall-mechanism/blob/HEAD/easy_transformer/fact_circuit_extraction.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":"168eb8501c7db28f"}},{"code_sha256_prefix":"b05aad9a321cedec","entry":"download_file_from_hf","repo":"trestad/factual-recall-mechanism","repo_kind":"official","path":"easy_transformer/utils.py","file_url":"https://github.com/trestad/factual-recall-mechanism/blob/HEAD/easy_transformer/utils.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"b05aad9a321cedec"}},{"code_sha256_prefix":"7726d0418a0ec986","entry":"show_tokens","repo":"trestad/factual-recall-mechanism","repo_kind":"official","path":"easy_transformer/fact_utils.py","file_url":"https://github.com/trestad/factual-recall-mechanism/blob/HEAD/easy_transformer/fact_utils.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"7726d0418a0ec986"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}