Safety Arithmetic: A Framework for Test-time Safety Alignment of Language Models by Steering Parameters and Activations

Rima Hazra, Sayan Layek, Somnath Banerjee, Soujanya Poria

Introduction

Ensuring the safety of Large Language Models (LLMs) remains a significant challenge in the field of AI alignment. While techniques like Reinforcement Learning from Human Feedback (RLHF) provide a baseline of safety, these mechanisms are often fragile. LLMs remain vulnerable to adversarial jailbreaks, and their safety alignment can inadvertently degrade when the model undergoes task-specific fine-tuning or knowledge updates via model editing.

To address these vulnerabilities, this paper introduces Safety Arithmetic, a training-free framework designed for test-time safety alignment. By treating safety and harm as directional vectors in both parameter and activation spaces, the framework allows for the modular adjustment of model behavior without the need for expensive retraining or gradient-based optimization.

The Safety Arithmetic Framework

Safety Arithmetic operates through a dual-stage process that targets different levels of the model architecture: parameters and latent activations.

1. Harm Direction Removal (HDR)

The first stage, Harm Direction Removal, focuses on the model's parameters. The authors hypothesize that harmful tendencies can be isolated as a specific direction in the parameter space. To identify this, they calculate a "harm vector" by taking the difference between a model fine-tuned on harmful data and a base aligned model.

To ensure that subtracting this vector does not damage the model's general capabilities, the authors apply a pruning step. Only the top-$k$ parameters (by magnitude) within the harm vector are retained. This pruned vector is then subtracted from the target model's weights, effectively neutralizing harmful pathways while preserving general utility.

2. Safety Alignment (Safe-Align)

The second stage, Safety Alignment, occurs during inference and operates on the model's activations. This stage utilizes the In-Context Safety Vector (ICV) technique. By passing pairs of safe and unsafe prompts through the model, the authors use Principal Component Analysis (PCA) to identify a safety-oriented direction in the latent space. During the generation process, this safety vector is added to the model's hidden states, steering the output toward safer, more compliant responses.

By combining weight-level removal (HDR) with activation-level steering (Safe-Align), Safety Arithmetic provides a robust, multi-layered defense against harmful outputs.

Experimental Evaluation

Datasets and Benchmarks

The framework was evaluated against a rigorous suite of safety benchmarks, including AdvBench (jailbreak prompts), DangerousQA, HarmfulQA, NicheHazardQA, and HEx-PHI.

Additionally, the researchers introduced NOINTENTEDIT, a new dataset comprising approximately 40 neutral knowledge-editing instances (e.g., facts about history or science). This dataset was designed to investigate whether benign updates to a model's knowledge base can unintentionally create safety backdoors.

To ensure that safety improvements did not come at the cost of intelligence, the authors tested utility using standard benchmarks such as MMLU, TruthfulQA, HellaSwag, ARC, GSM8K, and HumanEval.

Key Results

  • Reduction in Attack Success Rate (ASR): Safety Arithmetic demonstrated a marked ability to neutralize harmful prompts. For Llama2-7B on the AdvBench dataset, the ASR was reduced from 19.81% to 6.15%.
  • Utility Preservation: The framework successfully maintained the model's general performance. For instance, the MMLU score for Llama2 only saw a minor shift from 0.469 to 0.456, indicating that the model's reasoning and knowledge remained largely intact.
  • Mitigating Over-Safety: A notable side effect of the framework was the reduction of "over-safety" or false refusals. Because the steering is more targeted than broad RLHF, the model became less likely to refuse harmless prompts.

Insights and Discussion

Safety as a Vector

The core contribution of this work is the conceptualization of safety as a modular component. By treating safety as a "task vector," the framework allows for simple arithmetic operations—subtracting "bad" directions and adding "good" ones—to refine model behavior at test-time.

The Necessity of Pruning

The research highlights that modifying the entire parameter space is counterproductive. By only altering the top 10% of parameters in the harm vector, the authors achieved the best balance between safety and utility. This suggests that harmful behaviors are often concentrated in a small subset of highly influential weights.

The Risks of Model Editing

The results from the NOINTENTEDIT dataset serve as a warning for the AI community: even harmless knowledge updates can compromise a model's safety alignment. This underscores the necessity of implementing safety checks like Safety Arithmetic every time a model is modified or updated.

Limitations and Future Work

While effective, Safety Arithmetic has limitations. The current study focused on models up to 7 billion parameters; further research is required to determine if these arithmetic properties scale to models with 70 billion parameters or more. Additionally, the framework's performance is sensitive to hyperparameters, such as the scaling factors for vectors and pruning percentages, which currently require empirical tuning. Finally, the HDR stage depends on the availability of a representative harmful dataset to define the initial harm vector, suggesting that the diversity of this data is critical to the framework's overall robustness.

Summary updated

BibTeX
@inproceedings{hazra2024safety,
  author = {Rima Hazra and Sayan Layek and Somnath Banerjee and Soujanya Poria},
  title = {{Safety Arithmetic: A Framework for Test-time Safety Alignment of Language Models by Steering Parameters and Activations}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  doi = {10.18653/v1/2024.emnlp-main.1212},
}
Figure