Papers › On the Role of Attention Heads in Large Language Model Safety

On the Role of Attention Heads in Large Language Model Safety

17 Oct 2024arXiv:2410.13708archive 2025-07-28

Zhenhong Zhou, Haiyang Yu, Xinghua Zhang, Rongwu Xu, Fei Huang, Kun Wang, Yang Liu, Junfeng Fang, Yongbin Li

Large language models (LLMs) achieve state-of-the-art performance on multiple language tasks, yet their safety guardrails can be circumvented, leading to harmful generations. In light of this, recent research on safety mechanisms has emerged, revealing that when safety representations or component are suppressed, the safety capability of LLMs are compromised. However, existing research tends to overlook the safety impact of multi-head attention mechanisms, despite their crucial role in various model functionalities. Hence, in this paper, we aim to explore the connection between standard attention mechanisms and safety capability to fill this gap in the safety-related mechanistic interpretability. We propose a novel metric which tailored for multi-head attention, the Safety Head ImPortant Score (Ships), to assess the individual heads' contributions to model safety. Based on this, we generalize Ships to the dataset level and further introduce the Safety Attention Head AttRibution Algorithm (Sahara) to attribute the critical safety attention heads inside the model. Our findings show that the special attention head has a significant impact on safety. Ablating a single safety head allows aligned model (e.g., Llama-2-7b-chat) to respond to 16 times more harmful queries, while only modifying 0.006% of the parameters, in contrast to the ~ 5% modification required in previous studies. More importantly, we demonstrate that attention heads primarily function as feature extractors for safety and models fine-tuned from the same base model exhibit overlapping safety heads through comprehensive experiments. Together, our attribution approach and findings provide a novel perspective for unpacking the black box of safety mechanisms within large models.

PaperPDFCodeCode Syntology ran

In Syntology Open this paper in Syntology's Atlas, the map of the papers in Syntology's graph and their citations.

For agents, Syntology's MCP tool lists every function and class Syntology harvested from this paper and whether it ran (how to connect): get_harvested_code_for_paper(arxiv_id="2410.13708")

Code

Syntology Ran 16 of 24 code samples harvested from 1 repository linked to this paper; 8 have no recorded run. Of those that ran: 1 ran · honoured contract; 6 ran · our draft was wrong; 1 ran · fixture could not drive it; 8 ran with no contract checked.

By repository: official repository: 24 samples from 1 repository, 16 ran. The run record, sample by sample. “Ran” means executed on a synthesized input, not that the code is correct or reproduces the paper.

ydyjya/safetyheadattribution officialmentioned in paperpytorch report

Repository list and official/mentioned flags are the archive's, frozen 2025-07-28. Reachability, where shown, is from one Syntology probe window (2026-09-16 to 2026-09-18); repositories not probed show nothing. GitHub stars are not tracked.

Code Syntology ran Syntology

24 samples harvested; 16 ran; 1 honoured the contract we drafted; 8 have no recorded run. Read from Syntology's graph 2026-09-24; that is when this build read the record, not when the samples ran.

1ran · honoured contract
6ran · our draft was wrong
1ran · fixture could not drive it
8ran
8unverified

Licence: 24 of the 24 samples are pointer only, meaning Syntology does not serve that copy's text. This page shows no code text for any sample; each one links to its file in the repository.

Harvested from ydyjya/SafetyHeadAttribution. “Ran” means the sample executed on a synthesized input. It does not mean the output is correct, and nothing here reproduces the paper's results. “Honoured” and “violated” refer to a contract Syntology drafted from the code itself; “our draft was wrong” and “fixture could not drive it” are failures of Syntology's instrument, not of the code.

Each sample ends with its code_sha256, Syntology's identity for that exact code. An agent fetches the stored sample with Syntology's MCP tool get_code(code_sha256="…") (how to connect); click an identity to copy that call.

Repository labels, per sample. official repository: The archive marks this repository official for the paper. named in the paper: The archive records that the paper mentions this repository; it is not marked official. community (archive-listed): In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper. found in paper text by Syntology: 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. Samples from a repository marked official are listed first. Licence labels name the repository's licence as recorded at harvest. “Pointer only” means Syntology does not serve that copy's text, for one of four reasons: no licence file was found; the licence was not identified; the licence is recorded as permissive but that copy's record is not marked cleared; or the licence is outside the permissive list Syntology serves text under (MIT, Apache-2.0, BSD and similar). Some licences outside that list permit redistribution, such as WTFPL, and GPL-3.0 under its conditions; they are simply not on the list. Hover a licence label for the reason. File links open the file on GitHub at the default branch, which may have changed since the harvest.

repeat_kv ydyjya/SafetyHeadAttribution/lib/utils/custommodel.py official repository ran · fixture could not drive it fingerprinted no licence file found · pointer only · 3c76e52815c5401d · report
apply_rotary_pos_emb ydyjya/SafetyHeadAttribution/lib/utils/custommodel.py official repository ran · our draft was wrong no licence file found · pointer only · 583539efd6fd01fb · report
compute_subspace_similarity ydyjya/SafetyHeadAttribution/lib/Sahara/svd.py official repository ran fingerprinted no licence file found · pointer only · 0f1433d59a9e954b · report
get_last_hidden_states ydyjya/safetyheadattribution/lib/Sahara/attribution.py official repository ran · honoured contract no licence file found · pointer only · 313de3d0e5c72b22 · report
get_tokenizer ydyjya/safetyheadattribution/lib/SHIPS/get_ships.py official repository ran no licence file found · pointer only · fccfacd5ef8bfee9 · report
kl_divergence ydyjya/safetyheadattribution/lib/SHIPS/get_ships.py official repository ran · our draft was wrong fingerprinted no licence file found · pointer only · 2c199cf061f618c1 · report
load_batch_generations ydyjya/SafetyHeadAttribution/lib/Safety_discriminator/Discriminator.py official repository ran no licence file found · pointer only · 6921907267b978d9 · report
load_conv ydyjya/SafetyHeadAttribution/lib/utils/load_conv.py official repository ran fingerprinted no licence file found · pointer only · 9ed59a42c3d32d8d · report
load_from_search_cfg ydyjya/safetyheadattribution/lib/Sahara/attribution.py official repository ran · our draft was wrong no licence file found · pointer only · 2b9bd829f84c86a0 · report
normalize_columns ydyjya/SafetyHeadAttribution/lib/Sahara/svd.py official repository ran fingerprinted no licence file found · pointer only · 4c67b494ef77df17 · report
rotate_half ydyjya/SafetyHeadAttribution/lib/utils/custommodel.py official repository ran · our draft was wrong fingerprinted no licence file found · pointer only · e03d53ba9d4f9ae5 · report
run_generation ydyjya/SafetyHeadAttribution/lib/utils/batch_inference.py official repository ran no licence file found · pointer only · 50efcc04b774528b · report
ships_res_stats ydyjya/SafetyHeadAttribution/lib/SHIPS/ships_utils.py official repository ran no licence file found · pointer only · c11108ae20c7b667 · report
sort_ships_dict ydyjya/safetyheadattribution/lib/SHIPS/get_ships.py official repository ran · our draft was wrong no licence file found · pointer only · e222f3baa86eced9 · report
svd_decomposition ydyjya/SafetyHeadAttribution/lib/Sahara/svd.py official repository ran fingerprinted no licence file found · pointer only · 0f7c423b7f272901 · report
update_mask_cfg ydyjya/safetyheadattribution/lib/Sahara/attribution.py official repository ran · our draft was wrong no licence file found · pointer only · 3a2320c99110f0df · report
CustomLlamaAttention ydyjya/safetyheadattribution/lib/SHIPS/get_ships.py official repository unverified no licence file found · pointer only · 046f69bea319c560 · report
CustomLlamaDecoderLayer ydyjya/safetyheadattribution/lib/SHIPS/get_ships.py official repository unverified no licence file found · pointer only · fc78267c5d6f1748 · report
CustomLlamaModel ydyjya/safetyheadattribution/lib/SHIPS/get_ships.py official repository unverified no licence file found · pointer only · f7277da16bd23b13 · report
CustomLlamaModelForCausalLM ydyjya/safetyheadattribution/lib/SHIPS/get_ships.py official repository unverified no licence file found · pointer only · e8b5246184992c83 · report
CustomMistralModelForCausalLM ydyjya/safetyheadattribution/lib/SHIPS/get_ships.py official repository unverified no licence file found · pointer only · 94bba6aa672d4a96 · report
SHIPS ydyjya/safetyheadattribution/lib/SHIPS/get_ships.py official repository unverified no licence file found · pointer only · d2287a55acb3b732 · report
get_model ydyjya/safetyheadattribution/lib/SHIPS/get_ships.py official repository unverified no licence file found · pointer only · 9cddf5ae9689b5a0 · report
load_jsonl2text_list ydyjya/SafetyHeadAttribution/lib/Safety_discriminator/Discriminator.py official repository unverified no licence file found · pointer only · 92f77d2126ff0672 · report

Tasks

AttributeLanguage ModelingLanguage ModellingLarge Language Model

Results from the paper archive 2025-07-28

No leaderboard rows for this paper in the archive.

Methods

AttentionBASELinear LayerMulti-Head AttentionSoftmax

Report a problem or propose a change · a person checks every report against the paper or source before anything changes; decisions are listed on /corrections