Safety-Aware Fine-Tuning of Large Language Models

Hyeong Kyu Choi, Xuefeng Du, Yixuan Li

Abstract

Fine-tuning Large Language Models (LLMs) has emerged as a common practice for tailoring models to individual needs and preferences. The choice of datasets for fine-tuning can be diverse, introducing safety concerns regarding the potential inclusion of harmful data samples. Manually filtering or avoiding such samples, however, can be labor-intensive and subjective. To address these difficulties, we propose a novel Safety-Aware Fine-Tuning (SAFT) framework designed to automatically detect and remove potentially harmful data, by leveraging a scoring function that exploits the subspace information of harmful and benign samples. Experimental results demonstrate the efficacy of SAFT across different LLMs and varying contamination rates, achieving reductions in harmfulness of up to 27.8%. Going beyond, we delve into the mechanism of our approach and validate its versatility in addressing practical challenges in real-world scenarios.

Introduction

Fine-tuning is the "secret sauce" that transforms a general-purpose Large Language Model (LLM) into a specialized assistant. However, this process is vulnerable to "poisoned" data—training sets that contain harmful content alongside helpful instructions. As little as 10% contamination in a fine-tuning dataset can significantly erode a model's safety alignment. Because manual filtering is labor-intensive and subjective, there is a critical need for automated, robust methods to ensure fine-tuning remains safe.

The SAFT Framework

The Safety-Aware Fine-Tuning (SAFT) framework addresses this challenge by automatically scrubbing harmful samples from training data. Unlike methods that rely on unreliable self-diagnosis via prompting, SAFT identifies harmful data by analyzing how the model "thinks" internally.

Comparison between standard SFT and the proposed SAFT framework.

The core of SAFT is a Harmful Data Detection scoring function based on internal activation embeddings:

  1. Embedding Factorization: Internal representations are extracted for every sample in the training set.
  2. Subspace Identification: Using Singular Value Decomposition (SVD), the framework identifies low-dimensional subspaces that capture the most variance in activations.
  3. Scoring & Filtering: Samples are scored based on their projection onto the top singular vectors. Those exceeding a steerable threshold $\tau$ are removed.
  4. Fine-Tuning: The model is fine-tuned only on the remaining "clean" subset.

Key Insights and Experimental Results

The researchers evaluated SAFT using datasets like Beavertails (3,000 safety-labeled pairs) and Anthropic HH-RLHF. A key discovery is that harmfulness is geometrically distinct: harmful samples tend to cluster in specific subspaces, acting as outliers that can be detected via linear algebra.

Quantitative results (a) show that harmfulness increases with contamination while helpfulness remains stable. The geometric intuition (b) illustrates how harmful samples act as outliers in the model's activation subspace.

Major Findings:

  • Safety Gains: SAFT reduced harmfulness scores by up to 27.8% compared to standard supervised fine-tuning (SFT), often approaching "Oracle" performance (manual filtering).
  • Geometric Concentration: Surprisingly, using just the first singular vector ($k=1$) often yielded the best detection, suggesting the "direction of harmfulness" is highly concentrated.
  • Optimal Layers: The best separation occurs in the middle layers (e.g., layer 15 of Llama-2), where the model processes semantic content rather than raw input or vocabulary mapping.
  • Helpfulness Resilience: While contamination increases harmfulness, it rarely impacts the model's helpfulness on benign tasks. This makes "poisoned" fine-tuning particularly insidious, as high task performance can mask underlying safety failures.

Discussion and Limitations

While SAFT is highly effective for SFT, its current binary "keep or toss" approach is not yet adapted for preference-based methods like RLHF or DPO, which require ranking. Additionally, the method assumes the pre-trained model already possesses some internal representation of harmfulness. Future work may evolve this scoring function into a ranking mechanism to automate preference labeling for complex alignment techniques.

Conclusion

SAFT provides a scalable, automated solution for maintaining LLM safety during fine-tuning. By leveraging the geometric properties of internal model activations, it offers a more reliable signal than text-based prompting, ensuring that specialized models remain both helpful and harmless.

Paper Citation: Choi, H. K., Du, X., & Li, Y. (2024). Safety-Aware Fine-Tuning of Large Language Models. 38th Conference on Neural Information Processing Systems (NeurIPS 2024) Workshop on Safe Generative AI.

Summary updated

BibTeX
@inproceedings{choi2024safety,
  author = {Hyeong Kyu Choi and Xuefeng Du and Yixuan Li},
  title = {{Safety-Aware Fine-Tuning of Large Language Models}},
  booktitle = {SafeGenAi @ NeurIPS},
  year = {2024},
  url = {https://openreview.net/forum?id=SqL94fLSM7},
}
Figure