Safety-Aware Fine-Tuning of Large Language Models

Hyeong Kyu Choi, Xuefeng Du, Yixuan Li

TL;DR

Safety-Aware Fine-Tuning (SAFT) is a novel framework that automatically purges harmful samples from fine-tuning datasets by identifying a "harmful subspace" within a model's internal embeddings. By filtering data before training begins, SAFT reduces model harmfulness by up to 27.8% without sacrificing task performance or requiring expensive human-labeled safety data.

What Problem Does This Paper Address?

Fine-tuning is the standard way to specialize Large Language Models (LLMs) for specific tasks. However, real-world datasets—like social media logs or user-shared conversations—are often "contaminated" with harmful content (e.g., hate speech or misinformation).

The authors highlight a critical vulnerability: even a small amount of harmful data (as low as 10%) can severely compromise a model's safety alignment. Crucially, this safety degradation is often "silent"—standard performance metrics like ROUGE or BLEURT remain stable even as the model becomes more dangerous. While manual filtering is the obvious solution, it is labor-intensive, subjective, and difficult to scale. SAFT aims to automate this process by leveraging the model's own internal "understanding" of what constitutes harmful content.

Methods

SAFT introduces an automated filtering function $\mathcal{F}$ that identifies harmful samples based on the geometry of LLM embeddings. The process follows these steps:

  1. Embedding Extraction: The framework extracts internal representations (embeddings) from the LLM for every sample in the fine-tuning dataset.
  2. Subspace Identification: Using Singular Value Decomposition (SVD), the authors identify a low-dimensional subspace that captures the directions of maximum variance in the centered embedding space. The core hypothesis is that harmful samples act as "outliers" that align strongly with these top singular vectors.
  3. Scoring and Filtering: Each sample receives a filtering score based on the norm of its embedding projected onto this $k$-dimensional subspace.
  4. Safety-Aware Fine-Tuning: Samples exceeding a threshold $\tau$ are discarded. The model is then fine-tuned only on the remaining "benign" subset.

This approach is distinct because it doesn't rely on external moderation models or prompting the LLM to "judge" the data; instead, it exploits the statistical properties of the model's own activation space.

Geometric intuition: Harmful samples (dark pentagons) project more strongly onto the singular vector **v** than benign samples (light circles).

Datasets

The authors primarily used the Beavertails dataset, which contains 30,000 question-answer pairs labeled for both helpfulness and 14 distinct categories of harmfulness. To test the robustness and generalization of SAFT, they also evaluated it on the Anthropic HH-RLHF dataset, simulating a real-world scenario with an unknown contamination ratio.

Key Insights and Ideas

  • The Fragility of Safety: The paper quantifies how quickly safety breaks. As shown in Figure 2(a), the Harmfulness Score climbs steeply as the contamination rate $\lambda$ increases, while Helpfulness Scores remain deceptively flat.
  • Harmfulness is Low-Rank: One of the most surprising findings is that a very small subspace (often just $k=1$) is most effective for detection. This suggests that "harmfulness" is a relatively simple, high-variance feature in the embedding space.
  • Middle Layers are the "Sweet Spot": By analyzing different layers of Llama-2, the authors found that the best separation between benign and harmful data occurs in the middle layers (around layer 15). The early layers are still processing context, and the final layers are condensing information for vocabulary mapping, making the "median" representations the most informative for safety.
  • Prompting is Unreliable: Interestingly, simply asking an LLM if a data sample is harmful (the "Prompting" baseline) performed worse than random filtering in some cases, highlighting the need for the representation-based approach used in SAFT.

Key Results

  • Significant Safety Gains: SAFT achieved a relative reduction in harmfulness of up to 27.8% compared to vanilla supervised fine-tuning (SFT).
  • Performance Preservation: Across all tests, SAFT maintained helpfulness scores (BLEURT/ROUGE) comparable to models trained on perfectly clean data.
  • Steerability: The framework is "steerable." By adjusting the threshold $\tau$, developers can choose to be more aggressive in filtering (prioritizing safety) or more conservative (prioritizing data quantity).
  • Generalization: SAFT remained effective when applied to different model families (Llama-2 and Vicuna) and different dataset distributions (Anthropic HH-RLHF).
SAFT (dark blue) consistently achieves higher AUROC for harmful data detection across various contamination rates compared to prompting or random baselines.

Limitations and Future Work

  • Binary Classification: Currently, SAFT operates as a binary filter (keep or discard). The authors note that it is not yet clear how to adapt this for preference-based fine-tuning methods like Direct Preference Optimization (DPO), which require pairs of "better" vs. "worse" samples.
  • Refining the Score: Future work could involve using the scoring function to rank samples rather than just filter them, potentially allowing for more nuanced alignment strategies.
  • Broader Safety Categories: While effective across the 14 categories in Beavertails, the authors acknowledge that some categories (like "Privacy Violation") saw less improvement than others (like "Terrorism"), suggesting room for refining the subspace identification for specific types of harm.

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