{"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/how-alignment-and-jailbreak-work-explain-llm","title":"How Alignment and Jailbreak Work: Explain LLM Safety through Intermediate Hidden States","arxiv_id":"2406.05644","date":"2024-06-09","proceeding":null,"authors":["Zhenhong Zhou","Haiyang Yu","Xinghua Zhang","Rongwu Xu","Fei Huang","Yongbin Li"],"abstract":"Large language models (LLMs) rely on safety alignment to avoid responding to malicious user inputs. Unfortunately, jailbreak can circumvent safety guardrails, resulting in LLMs generating harmful content and raising concerns about LLM safety. Due to language models with intensive parameters often regarded as black boxes, the mechanisms of alignment and jailbreak are challenging to elucidate. In this paper, we employ weak classifiers to explain LLM safety through the intermediate hidden states. We first confirm that LLMs learn ethical concepts during pre-training rather than alignment and can identify malicious and normal inputs in the early layers. Alignment actually associates the early concepts with emotion guesses in the middle layers and then refines them to the specific reject tokens for safe generations. Jailbreak disturbs the transformation of early unethical classification into negative emotions. We conduct experiments on models from 7B to 70B across various model families to prove our conclusion. Overall, our paper indicates the intrinsical mechanism of LLM safety and how jailbreaks circumvent safety guardrails, offering a new perspective on LLM safety and reducing concerns. Our code is available at https://github.com/ydyjya/LLM-IHS-Explanation.","url_abs":"https://arxiv.org/abs/2406.05644v2","url_pdf":"https://arxiv.org/pdf/2406.05644v2.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":"how-alignment-and-jailbreak-work-explain-llm","repo_url":"https://github.com/ydyjya/llm-ihs-explanation","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok"}}],"tasks":[{"task_slug":"safety-alignment","task_name":"Safety Alignment"}],"methods":[],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"atlas_url":"https://app.syntology.ai/?focus=2406.05644","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2406.05644"}},"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/ydyjya/llm-ihs-explanation","reach":{"status":"ok"}}],"summary":{"ran_fixture":1,"ran_draft_wrong":2,"ran":1,"unverified":2},"by_repo_kind":{"official":{"samples":6,"ran":4,"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":6,"samples":[{"code_sha256_prefix":"30d7eec482ebf6b1","entry":"repeat_kv","repo":"ydyjya/llm-ihs-explanation","repo_kind":"official","path":"resource/modeling_llama.py","file_url":"https://github.com/ydyjya/llm-ihs-explanation/blob/HEAD/resource/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":"bac65c3dafaec040","entry":"apply_rotary_pos_emb","repo":"ydyjya/llm-ihs-explanation","repo_kind":"official","path":"resource/modeling_llama.py","file_url":"https://github.com/ydyjya/llm-ihs-explanation/blob/HEAD/resource/modeling_llama.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":"ae4d43bf16b82326","entry":"get_layer","repo":"ydyjya/llm-ihs-explanation","repo_kind":"official","path":"w2s_utils.py","file_url":"https://github.com/ydyjya/llm-ihs-explanation/blob/HEAD/w2s_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":"ae4d43bf16b82326"}},{"code_sha256_prefix":"b99eea6376d1e212","entry":"rotate_half","repo":"ydyjya/llm-ihs-explanation","repo_kind":"official","path":"resource/modeling_llama.py","file_url":"https://github.com/ydyjya/llm-ihs-explanation/blob/HEAD/resource/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"}},{"code_sha256_prefix":"0b411b8e28ca5297","entry":"get_model","repo":"ydyjya/llm-ihs-explanation","repo_kind":"official","path":"load_model.py","file_url":"https://github.com/ydyjya/llm-ihs-explanation/blob/HEAD/load_model.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":"0b411b8e28ca5297"}},{"code_sha256_prefix":"a91be4b45bf0369f","entry":"step_forward","repo":"ydyjya/llm-ihs-explanation","repo_kind":"official","path":"load_model.py","file_url":"https://github.com/ydyjya/llm-ihs-explanation/blob/HEAD/load_model.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":"a91be4b45bf0369f"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}