Towards Understanding Safety Alignment: A Mechanistic Perspective from Safety Neurons

Jianhui Chen, Xiaozhi Wang, Zijun Yao, Yushi Bai, Lei Hou +1 more
6/20/2024
cs.CLcs.AIcs.LG

Official code: THU-KEG/SafetyNeuron (31 stars, MIT, last commit 2026-01-29).

Abstract

Large language models (LLMs) excel in various capabilities but pose safety risks such as generating harmful content and misinformation, even after safety alignment. In this paper, we explore the inner mechanisms of safety alignment through the lens of mechanistic interpretability, focusing on identifying and analyzing safety neurons within LLMs that are responsible for safety behaviors. We propose inference-time activation contrasting to locate these neurons and dynamic activation patching to evaluate their causal effects on model safety. Experiments on multiple prevalent LLMs demonstrate that we can consistently identify about $5\%$ safety neurons, and by only patching their activations we can restore over $90\%$ of the safety performance across various red-teaming benchmarks without influencing general ability. The finding of safety neurons also helps explain the ''alignment tax'' phenomenon by revealing that the key neurons for model safety and helpfulness significantly overlap, yet they require different activation patterns for the same neurons. Furthermore, we demonstrate an application of our findings in safeguarding LLMs by detecting unsafe outputs before generation. The source code is available at https://github.com/THU-KEG/SafetyNeuron.

View on arXivView PDF

Code Implementations(1)

MIT

Data and code for the paper: Finding Safety Neurons in Large Language Models

310Shell, PythonJul 16, 20248 months agoMIT
llmsmechanistic-interpretabilitysafety

Similar Papers

Cite this paper

@article{chen2024towards,
  title  = {Towards Understanding Safety Alignment: A Mechanistic Perspective from Safety Neurons},
  author = {Jianhui Chen and Xiaozhi Wang and Zijun Yao and Yushi Bai and Lei Hou and Juanzi Li},
  year   = {2024},
  eprint = {2406.14144},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2406.14144v2}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2406.14144.svg)](https://codeofpaper.com/paper/2406.14144)

Discussion