{"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/nlsr-neuron-level-safety-realignment-of-large","title":"NLSR: Neuron-Level Safety Realignment of Large Language Models Against Harmful Fine-Tuning","arxiv_id":"2412.12497","date":"2024-12-17","proceeding":null,"authors":["Xin Yi","Shunfan Zheng","LinLin Wang","Gerard de Melo","Xiaoling Wang","Liang He"],"abstract":"The emergence of finetuning-as-a-service has revealed a new vulnerability in large language models (LLMs). A mere handful of malicious data uploaded by users can subtly manipulate the finetuning process, resulting in an alignment-broken model. Existing methods to counteract fine-tuning attacks typically require substantial computational resources. Even with parameter-efficient techniques like LoRA, gradient updates remain essential. To address these challenges, we propose \\textbf{N}euron-\\textbf{L}evel \\textbf{S}afety \\textbf{R}ealignment (\\textbf{NLSR}), a training-free framework that restores the safety of LLMs based on the similarity difference of safety-critical neurons before and after fine-tuning. The core of our framework is first to construct a safety reference model from an initially aligned model to amplify safety-related features in neurons. We then utilize this reference model to identify safety-critical neurons, which we prepare as patches. Finally, we selectively restore only those neurons that exhibit significant similarity differences by transplanting these prepared patches, thereby minimally altering the fine-tuned model. Extensive experiments demonstrate significant safety enhancements in fine-tuned models across multiple downstream tasks, while greatly maintaining task-level accuracy. Our findings suggest regions of some safety-critical neurons show noticeable differences after fine-tuning, which can be effectively corrected by transplanting neurons from the reference model without requiring additional training. The code will be available at \\url{https://github.com/xinykou/NLSR}","url_abs":"https://arxiv.org/abs/2412.12497v1","url_pdf":"https://arxiv.org/pdf/2412.12497v1.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":"nlsr-neuron-level-safety-realignment-of-large","repo_url":"https://github.com/xinykou/nlsr","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":0,"framework":"pytorch","reach":{"status":"ok"}}],"tasks":[],"methods":[],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"syntology_url":null,"atlas_url":"https://app.syntology.ai/?focus=2412.12497","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2412.12497"}},"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/xinykou/nlsr","reach":{"status":"ok"}}],"summary":{"ran_draft_wrong":1,"unverified":11},"by_repo_kind":{"official":{"samples":12,"ran":1,"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":12,"samples":[{"code_sha256_prefix":"2ed16fcdc14ad951","entry":"find_layers","repo":"xinykou/nlsr","repo_kind":"official","path":"src/prune_regions/prune.py","file_url":"https://github.com/xinykou/nlsr/blob/HEAD/src/prune_regions/prune.py","link_basis":"plan_row","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"OUTPUT_MISDECLARED","metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"2ed16fcdc14ad951"}},{"code_sha256_prefix":"aada7a4b2508048d","entry":"compute_reference_log_probs","repo":"xinykou/nlsr","repo_kind":"official","path":"src/prune_regions/modeling_llama.py","file_url":"https://github.com/xinykou/nlsr/blob/HEAD/src/prune_regions/modeling_llama.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":"aada7a4b2508048d"}},{"code_sha256_prefix":"b8d15a97350e90e6","entry":"dpo_loss","repo":"xinykou/nlsr","repo_kind":"official","path":"src/prune_regions/modeling_llama.py","file_url":"https://github.com/xinykou/nlsr/blob/HEAD/src/prune_regions/modeling_llama.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":"b8d15a97350e90e6"}},{"code_sha256_prefix":"72787e70860af8f9","entry":"get_align","repo":"xinykou/nlsr","repo_kind":"official","path":"src/prune_regions/data.py","file_url":"https://github.com/xinykou/nlsr/blob/HEAD/src/prune_regions/data.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":"72787e70860af8f9"}},{"code_sha256_prefix":"90156e5bd5e45790","entry":"get_batch_logps","repo":"xinykou/nlsr","repo_kind":"official","path":"src/prune_regions/modeling_llama.py","file_url":"https://github.com/xinykou/nlsr/blob/HEAD/src/prune_regions/modeling_llama.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":"90156e5bd5e45790"}},{"code_sha256_prefix":"0be7f54b2be9e725","entry":"get_mask","repo":"xinykou/nlsr","repo_kind":"official","path":"src/prune_regions/prune.py","file_url":"https://github.com/xinykou/nlsr/blob/HEAD/src/prune_regions/prune.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":"0be7f54b2be9e725"}},{"code_sha256_prefix":"16465c80795a9e7e","entry":"get_preference_align","repo":"xinykou/nlsr","repo_kind":"official","path":"src/prune_regions/data.py","file_url":"https://github.com/xinykou/nlsr/blob/HEAD/src/prune_regions/data.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":"16465c80795a9e7e"}},{"code_sha256_prefix":"e06b4ae7a667ff4f","entry":"make_Act","repo":"xinykou/nlsr","repo_kind":"official","path":"src/prune_regions/model_wrapper.py","file_url":"https://github.com/xinykou/nlsr/blob/HEAD/src/prune_regions/model_wrapper.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":"e06b4ae7a667ff4f"}},{"code_sha256_prefix":"434bb009c21eeb38","entry":"make_Act","repo":"xinykou/nlsr","repo_kind":"official","path":"src/prune_regions/model_wrapper_low.py","file_url":"https://github.com/xinykou/nlsr/blob/HEAD/src/prune_regions/model_wrapper_low.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":"434bb009c21eeb38"}},{"code_sha256_prefix":"489c7308a936561a","entry":"prepare_calibration_input","repo":"xinykou/nlsr","repo_kind":"official","path":"src/prune_regions/prune.py","file_url":"https://github.com/xinykou/nlsr/blob/HEAD/src/prune_regions/prune.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":"489c7308a936561a"}},{"code_sha256_prefix":"bd477478b399a403","entry":"revert_Act_to_Linear","repo":"xinykou/nlsr","repo_kind":"official","path":"src/prune_regions/model_wrapper.py","file_url":"https://github.com/xinykou/nlsr/blob/HEAD/src/prune_regions/model_wrapper.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":"bd477478b399a403"}},{"code_sha256_prefix":"86b6b36dae379391","entry":"revert_Act_to_Linear","repo":"xinykou/nlsr","repo_kind":"official","path":"src/prune_regions/model_wrapper_low.py","file_url":"https://github.com/xinykou/nlsr/blob/HEAD/src/prune_regions/model_wrapper_low.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":"86b6b36dae379391"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}