SYNTOLOGY HomeExplorerAtlasCodeMethodologyAboutDevelopersFeedPricing
Paper · 2410.13708 · ICLR · 2025

On the Role of Attention Heads in Large Language Model Safety

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

arXiv · PDF · Open in the Atlas

Code that ran

16 of 24 samples (functions and classes) lifted from this paper's linked repositories executed on a synthesized input in Syntology's jail (0 of them constructed an object rather than computing a result; 1 honoured the contract we drafted, 0 violated it and 0 have no contract verdict; 15 failures of our instrument; 8 no recorded run); 1 of them was also run on the host, not in the jail, by the peer-agreement lane that set its level, measured 2026-11-23; repository links from the Papers with Code archive attributed under CC BY-SA, no affiliation; bodies inline only under a permissive licence as GitHub detects it. Not a reproduction of the paper's results.

"Ran" means the function executed on a synthesized input and returned a value. It is not a reproduction of the paper's results. This record, dated, as JSON: /api/ran/2410.13708.json · badge: /api/ran/2410.13708.svg

Each repository's role is a label: a classifier's output recorded on the link, or, where no role is recorded, what the Papers with Code archive marked. The line under it names its basis. Neither says who wrote the code.

RepositoryRoleRan
ydyjya/SafetyHeadAttribution canonical (classifier label)
classifier basis: census
10 ran · 1 no recorded run
ydyjya/safetyheadattribution canonical (classifier label)
classifier basis: classifier_2025
6 ran · 7 no recorded run
FunctionStatusWhere it lives
repeat_kv Ran
ran; the contract check could not run (RAISES, DEP_MISSING or TIMEOUT)
ydyjya/SafetyHeadAttribution/lib/utils/custommodel.py
pointer only (licence: NONE) · get_code("3c76e52815c5401d")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/SafetyHeadAttribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. A run is recorded.

apply_rotary_pos_emb Ran
ran; our contract draft was wrong, not the code
ydyjya/SafetyHeadAttribution/lib/utils/custommodel.py
pointer only (licence: NONE) · get_code("583539efd6fd01fb")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/SafetyHeadAttribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. A run is recorded.

compute_subspace_similarity Ran
ran; our contract draft was wrong, not the code
ydyjya/SafetyHeadAttribution/lib/Sahara/svd.py
pointer only (licence: NONE) · get_code("0f1433d59a9e954b")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/SafetyHeadAttribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. A run is recorded.

get_last_hidden_states Ran
ran, honoured the contract we drafted
ydyjya/safetyheadattribution/lib/Sahara/attribution.py
pointer only (licence: NONE) · get_code("313de3d0e5c72b22")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/safetyheadattribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. A run is recorded.

get_tokenizer Ran
ran; our contract draft was wrong, not the code
ydyjya/safetyheadattribution/lib/SHIPS/get_ships.py
pointer only (licence: NONE) · get_code("fccfacd5ef8bfee9")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/safetyheadattribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. A run is recorded.

kl_divergence Ran
ran; our contract draft was wrong, not the code
ydyjya/safetyheadattribution/lib/SHIPS/get_ships.py
pointer only (licence: NONE) · get_code("2c199cf061f618c1")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/safetyheadattribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. A run is recorded.

load_batch_generations Ran
ran; our contract draft was wrong, not the code
ydyjya/SafetyHeadAttribution/lib/Safety_discriminator/Discriminator.py
pointer only (licence: NONE) · get_code("6921907267b978d9")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/SafetyHeadAttribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. A run is recorded.

load_conv Ran
ran; our contract draft was wrong, not the code
ydyjya/SafetyHeadAttribution/lib/utils/load_conv.py
pointer only (licence: NONE) · get_code("9ed59a42c3d32d8d")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/SafetyHeadAttribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. A run is recorded.

load_from_search_cfg Ran
ran; our contract draft was wrong, not the code
ydyjya/safetyheadattribution/lib/Sahara/attribution.py
pointer only (licence: NONE) · get_code("2b9bd829f84c86a0")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/safetyheadattribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. A run is recorded.

normalize_columns Ran
ran; our contract draft was wrong, not the code
ydyjya/SafetyHeadAttribution/lib/Sahara/svd.py
pointer only (licence: NONE) · get_code("4c67b494ef77df17")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/SafetyHeadAttribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. A run is recorded.

rotate_half Ran
ran; our contract draft was wrong, not the code
ydyjya/SafetyHeadAttribution/lib/utils/custommodel.py
pointer only (licence: NONE) · get_code("e03d53ba9d4f9ae5")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/SafetyHeadAttribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. A run is recorded.

run_generation Ran
ran; our contract draft was wrong, not the code
ydyjya/SafetyHeadAttribution/lib/utils/batch_inference.py
pointer only (licence: NONE) · get_code("50efcc04b774528b")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/SafetyHeadAttribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. A run is recorded.

ships_res_stats Ran
ran; our contract draft was wrong, not the code
ydyjya/SafetyHeadAttribution/lib/SHIPS/ships_utils.py
pointer only (licence: NONE) · get_code("c11108ae20c7b667")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/SafetyHeadAttribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. A run is recorded.

sort_ships_dict Ran
ran; our contract draft was wrong, not the code
ydyjya/safetyheadattribution/lib/SHIPS/get_ships.py
pointer only (licence: NONE) · get_code("e222f3baa86eced9")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/safetyheadattribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. A run is recorded.

svd_decomposition Ran
ran; our contract draft was wrong, not the code
ydyjya/SafetyHeadAttribution/lib/Sahara/svd.py
pointer only (licence: NONE) · get_code("0f7c423b7f272901")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/SafetyHeadAttribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. A run is recorded.

update_mask_cfg Ran
ran; our contract draft was wrong, not the code
ydyjya/safetyheadattribution/lib/Sahara/attribution.py
pointer only (licence: NONE) · get_code("3a2320c99110f0df")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/safetyheadattribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. A run is recorded.

CustomLlamaAttention Not yet run
unverified (harvested, no recorded run)
ydyjya/safetyheadattribution/lib/SHIPS/get_ships.py
pointer only (licence: NONE) · get_code("046f69bea319c560")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/safetyheadattribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. No invocation recorded.

CustomLlamaDecoderLayer Not yet run
unverified (harvested, no recorded run)
ydyjya/safetyheadattribution/lib/SHIPS/get_ships.py
pointer only (licence: NONE) · get_code("fc78267c5d6f1748")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/safetyheadattribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. No invocation recorded.

CustomLlamaModel Not yet run
unverified (harvested, no recorded run)
ydyjya/safetyheadattribution/lib/SHIPS/get_ships.py
pointer only (licence: NONE) · get_code("f7277da16bd23b13")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/safetyheadattribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. No invocation recorded.

CustomLlamaModelForCausalLM Not yet run
unverified (harvested, no recorded run)
ydyjya/safetyheadattribution/lib/SHIPS/get_ships.py
pointer only (licence: NONE) · get_code("e8b5246184992c83")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/safetyheadattribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. No invocation recorded.

CustomMistralModelForCausalLM Not yet run
unverified (harvested, no recorded run)
ydyjya/safetyheadattribution/lib/SHIPS/get_ships.py
pointer only (licence: NONE) · get_code("94bba6aa672d4a96")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/safetyheadattribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. No invocation recorded.

SHIPS Not yet run
unverified (harvested, no recorded run)
ydyjya/safetyheadattribution/lib/SHIPS/get_ships.py
pointer only (licence: NONE) · get_code("d2287a55acb3b732")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/safetyheadattribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. No invocation recorded.

get_model Not yet run
unverified (harvested, no recorded run)
ydyjya/safetyheadattribution/lib/SHIPS/get_ships.py
pointer only (licence: NONE) · get_code("9cddf5ae9689b5a0")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/safetyheadattribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. No invocation recorded.

load_jsonl2text_list Not yet run
unverified (harvested, no recorded run)
ydyjya/SafetyHeadAttribution/lib/Safety_discriminator/Discriminator.py
pointer only (licence: NONE) · get_code("92f77d2126ff0672")
How it ran

The body of this sample is withheld: GitHub detected no licence in this paper's repository, ydyjya/SafetyHeadAttribution when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. No invocation recorded.

Also present in other papers' code

Of 24 samples lifted from this paper's linked repositories, 24 carry a function identity and 0 do not. Of the 24 distinct functions with an identity, 6 are also present in other papers whose code we hold (3 with at least one paper dated earlier, 3 the same month, 6 later) and 18 are only here among the 52,977 papers whose code Syntology holds (as of 2026-09-26).

Only here means: among the 52,977 papers whose code Syntology holds (51,887 with lifted samples, 1,090 through their assemblies only), as of 2026-09-26, no other paper's lifted samples or current assembly bricks contain an identical function body after normalisation.

FunctionAlso present inEarliest-datedOthers named
rotate_half
brick of this paper's assembly: helper
360 other papers
184 dated earlier, 25 the same month, 142 later, 6 same year, order not determinable, 3 undated · 30 only as an assembly brick
1901.02860 (2019-01)
Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context
2203.08913 (2022-03)
Memorizing Transformers
2302.13971 (2023-02)
LLaMA: Open and Efficient Foundation Language Models
and 357 more not named here
repeat_kv
brick of this paper's assembly: helper
243 other papers
119 dated earlier, 24 the same month, 96 later, 2 same year, order not determinable, 2 undated · 22 only as an assembly brick
1901.02860 (2019-01)
Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context
2203.08913 (2022-03)
Memorizing Transformers
2305.17888 (2023-05)
LLM-QAT: Data-Free Quantization Aware Training for Large Language Models
and 240 more not named here
apply_rotary_pos_emb
brick of this paper's assembly: helper
94 other papers
27 dated earlier, 14 the same month, 49 later, 1 same year, order not determinable, 3 undated · 6 only as an assembly brick
1901.02860 (2019-01)
Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context
2305.17888 (2023-05)
LLM-QAT: Data-Free Quantization Aware Training for Large Language Models
2309.11674 (2023-09)
A Paradigm Shift in Machine Translation: Boosting Translation Performance of Large Language Models
and 91 more not named here
get_last_hidden_states 1 other paper
1 later
2025.findings-acl.394 (2025 (year only))
Hierarchical Safety Realignment: Lightweight Restoration of Safety in Pruned Large Vision-Language Models
load_from_search_cfg 1 other paper
1 later
2025.findings-acl.394 (2025 (year only))
Hierarchical Safety Realignment: Lightweight Restoration of Safety in Pruned Large Vision-Language Models
update_mask_cfg 1 other paper
1 later
2025.findings-acl.394 (2025 (year only))
Hierarchical Safety Realignment: Lightweight Restoration of Safety in Pruned Large Vision-Language Models

Presence, not direction: an identical function body in two papers' repositories does not say who wrote it, which came from which, or that either paper's method rests on it. Matching is exact after normalisation, so a renamed or edited function is not matched. Dates are when papers appeared, not when code was written. Only .py files the harvester lifted are covered; notebooks are not read. A new-style arXiv id YYMM.NNNN dates a paper to the month 20YY-MM; otherwise Paper.year dates it to the year; otherwise it is undated. Dates are when the paper appeared, not when the code was written. Read 2026-11-23.

Repositories linked to this paper

Some links come from the archived Papers with Code dataset (CC BY-SA 4.0): attribution and licence.

Abstract

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 components are suppressed, the safety capability of LLMs is 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 safety-related mechanistic interpretability. We propose a novel metric 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 the aligned model (e.g., Llama-2-7b-chat) to respond to 16× ↑ 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. Our code is available at https://github.com/ydyjya/SafetyHeadAttribution.

For agents

The same record, over MCP at https://syntology.ai/mcp:

get_harvested_code_for_paper("2410.13708")
get_code_for_paper("2410.13708")
have("2410.13708")

The run record, dated, one paper per request, free:

curl https://syntology.ai/api/ran/2410.13708.json

A badge for a README (the split and the date, never a ratio):

[![Syntology run record](https://syntology.ai/api/ran/2410.13708.svg)](https://syntology.ai/paper/2410.13708)

Connect an agent — have() is free.