{"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/targeted-vaccine-safety-alignment-for-large","title":"Targeted Vaccine: Safety Alignment for Large Language Models against Harmful Fine-Tuning via Layer-wise Perturbation","arxiv_id":"2410.09760","date":"2024-10-13","proceeding":null,"authors":["Guozhi Liu","Weiwei Lin","Tiansheng Huang","Ruichao Mo","Qi Mu","Li Shen"],"abstract":"Harmful fine-tuning attack poses a serious threat to the online fine-tuning service. Vaccine, a recent alignment-stage defense, applies uniform perturbation to all layers of embedding to make the model robust to the simulated embedding drift. However, applying layer-wise uniform perturbation may lead to excess perturbations for some particular safety-irrelevant layers, resulting in defense performance degradation and unnecessary memory consumption. To address this limitation, we propose Targeted Vaccine (T-Vaccine), a memory-efficient safety alignment method that applies perturbation to only selected layers of the model. T-Vaccine follows two core steps: First, it uses gradient norm as a statistical metric to identify the safety-critical layers. Second, instead of applying uniform perturbation across all layers, T-Vaccine only applies perturbation to the safety-critical layers while keeping other layers frozen during training. Results show that T-Vaccine outperforms Vaccine in terms of both defense effectiveness and resource efficiency. Comparison with other defense baselines, e.g., RepNoise and TAR also demonstrate the superiority of T-Vaccine. Notably, T-Vaccine is the first defense that can address harmful fine-tuning issues for a 7B pre-trained models trained on consumer GPUs with limited memory (e.g., RTX 4090). Our code is available at https://github.com/Lslland/T-Vaccine.","url_abs":"https://arxiv.org/abs/2410.09760v3","url_pdf":"https://arxiv.org/pdf/2410.09760v3.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":"targeted-vaccine-safety-alignment-for-large","repo_url":"https://github.com/lslland/t-vaccine","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":0,"framework":"pytorch","reach":{"status":"ok","spdx":"Apache-2.0"}}],"tasks":[{"task_slug":"safety-alignment","task_name":"Safety Alignment"},{"task_slug":"tar","task_name":"TAR"}],"methods":[{"method_slug":null,"method_name":null}],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"syntology_url":"https://syntology.ai/paper/2410.09760","atlas_url":"https://app.syntology.ai/?focus=2410.09760","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2410.09760"}},"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/lslland/t-vaccine","reach":{"status":"ok","spdx":"Apache-2.0"}}],"summary":{"ran_fixture":3,"ran_honours":1,"ran_draft_wrong":5,"ran":3},"by_repo_kind":{"official":{"samples":12,"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":0,"samples":[{"code_sha256_prefix":"30d7eec482ebf6b1","entry":"repeat_kv","repo":"lslland/t-vaccine","repo_kind":"official","path":"models/modeling_gemma_my.py","file_url":"https://github.com/lslland/t-vaccine/blob/HEAD/models/modeling_gemma_my.py","link_basis":"harvester_set","language":"python","status":"ran_fixture","verification_level":2,"contract_check":"RAISES","metamorphic_tier":"invariant","behaviour_fingerprint":true,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"30d7eec482ebf6b1"}},{"code_sha256_prefix":"175778f00233af46","entry":"adapt_dimension_b2a","repo":"lslland/t-vaccine","repo_kind":"official","path":"loss_func/repnoise_loss.py","file_url":"https://github.com/lslland/t-vaccine/blob/HEAD/loss_func/repnoise_loss.py","link_basis":"plan_row","language":"python","status":"ran_honours","verification_level":1,"contract_check":"HONOURS","metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"175778f00233af46"}},{"code_sha256_prefix":"d61c483a3c2b3156","entry":"apply_rotary_pos_emb","repo":"lslland/t-vaccine","repo_kind":"official","path":"models/modeling_qwen2_my.py","file_url":"https://github.com/lslland/t-vaccine/blob/HEAD/models/modeling_qwen2_my.py","link_basis":"harvester_set","language":"python","status":"ran_fixture","verification_level":1,"contract_check":"RAISES","metamorphic_tier":"invariant","behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"d61c483a3c2b3156"}},{"code_sha256_prefix":"bac65c3dafaec040","entry":"apply_rotary_pos_emb","repo":"lslland/t-vaccine","repo_kind":"official","path":"models/modeling_gemma_my.py","file_url":"https://github.com/lslland/t-vaccine/blob/HEAD/models/modeling_gemma_my.py","link_basis":"harvester_set","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"MISDECLARED","metamorphic_tier":"invariant","behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"bac65c3dafaec040"}},{"code_sha256_prefix":"f725bc2d76076485","entry":"apply_rotary_pos_emb","repo":"lslland/t-vaccine","repo_kind":"official","path":"models/modeling_llama_my.py","file_url":"https://github.com/lslland/t-vaccine/blob/HEAD/models/modeling_llama_my.py","link_basis":"harvester_set","language":"python","status":"ran_fixture","verification_level":1,"contract_check":"RAISES","metamorphic_tier":"invariant","behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"f725bc2d76076485"}},{"code_sha256_prefix":"134eef3e8182bf6f","entry":"apply_rotary_pos_emb","repo":"lslland/t-vaccine","repo_kind":"official","path":"models/modeling_gemma2_my.py","file_url":"https://github.com/lslland/t-vaccine/blob/HEAD/models/modeling_gemma2_my.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"134eef3e8182bf6f"}},{"code_sha256_prefix":"582b8f13a818c0e2","entry":"get_leaf_modules_with_grad","repo":"lslland/t-vaccine","repo_kind":"official","path":"trainer.py","file_url":"https://github.com/lslland/t-vaccine/blob/HEAD/trainer.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"582b8f13a818c0e2"}},{"code_sha256_prefix":"d07d04439cd1d44f","entry":"jload","repo":"lslland/t-vaccine","repo_kind":"official","path":"utils.py","file_url":"https://github.com/lslland/t-vaccine/blob/HEAD/utils.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":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"d07d04439cd1d44f"}},{"code_sha256_prefix":"dbc0448520ee9257","entry":"masked_token_ce_loss","repo":"lslland/t-vaccine","repo_kind":"official","path":"loss_func/repnoise_loss.py","file_url":"https://github.com/lslland/t-vaccine/blob/HEAD/loss_func/repnoise_loss.py","link_basis":"plan_row","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"MISDECLARED","metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"dbc0448520ee9257"}},{"code_sha256_prefix":"c605efc9460b6c33","entry":"register_activation_hook","repo":"lslland/t-vaccine","repo_kind":"official","path":"loss_func/repnoise_loss.py","file_url":"https://github.com/lslland/t-vaccine/blob/HEAD/loss_func/repnoise_loss.py","link_basis":"plan_row","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"OUTPUT_MISDECLARED","metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"c605efc9460b6c33"}},{"code_sha256_prefix":"ca6c6aa5c8b23b27","entry":"repeat_kv","repo":"lslland/t-vaccine","repo_kind":"official","path":"models/modeling_gemma2_my.py","file_url":"https://github.com/lslland/t-vaccine/blob/HEAD/models/modeling_gemma2_my.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"ca6c6aa5c8b23b27"}},{"code_sha256_prefix":"b99eea6376d1e212","entry":"rotate_half","repo":"lslland/t-vaccine","repo_kind":"official","path":"models/modeling_gemma2_my.py","file_url":"https://github.com/lslland/t-vaccine/blob/HEAD/models/modeling_gemma2_my.py","link_basis":"harvester_set","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"MISDECLARED","metamorphic_tier":"invariant","behaviour_fingerprint":true,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"b99eea6376d1e212"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}