SafeMERGE: Preserving Safety Alignment in Fine-Tuned Large Language Models via Selective Layer-Wise Model Merging

Aladin Djuhera, Swanand Ravindra Kadhe, Farhan Ahmed, Syed Zawad, Holger Boche
3/21/2025
cs.CLcs.AI

No official code found. Best implementation: aladinD/SafeMERGE (15 stars, last commit 2025-04-01).

Abstract

Fine-tuning large language models (LLMs) is a common practice to adapt generalist models to specialized domains. However, recent studies show that fine-tuning can erode safety alignment, causing LLMs to respond to harmful or unethical prompts. Many methods to realign safety have been proposed, but often introduce custom algorithms that are difficult to implement or compromise task utility. In this work, we propose SafeMERGE, a lightweight, post-fine-tuning framework that preserves safety while maintaining downstream performance. SafeMERGE selectively merges fine-tuned with safety-aligned model layers only when they deviate from safe behavior, measured by a cosine similarity criterion. Across three LLMs and two tasks, SafeMERGE consistently reduces harmful outputs compared to other defenses, with negligible or even positive impact on utility. Our results demonstrate that selective layer-wise merging offers an effective safeguard against the inadvertent loss of safety during fine-tuning, establishing SafeMERGE as a simple post-fine-tuning defense.

View on arXivView PDF

Code Implementations(1)

aladinD/SafeMERGECommunity100%

Code for SafeMERGE (ICLR 2025).

150PythonMar 11, 20251 years ago

Similar Papers

Cite this paper

@article{djuhera2025safemerge,
  title  = {SafeMERGE: Preserving Safety Alignment in Fine-Tuned Large Language Models via Selective Layer-Wise Model Merging},
  author = {Aladin Djuhera and Swanand Ravindra Kadhe and Farhan Ahmed and Syed Zawad and Holger Boche},
  year   = {2025},
  eprint = {2503.17239},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2503.17239v2}
}

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/2503.17239.svg)](https://codeofpaper.com/paper/2503.17239)

Discussion