Semantic Loss Guided Data Efficient Supervised Fine Tuning for Safe Responses in LLMs

Yuxiao Lu, Arunesh Sinha, Pradeep Varakantham

Introduction

Aligning Large Language Models (LLMs) to ensure safe and non-toxic responses typically follows an expensive path: collecting thousands of high-quality human demonstrations of "safe" responses for Supervised Fine-Tuning (SFT) or utilizing Reinforcement Learning from Human Feedback (RLHF). Both methods require significant manual effort or heavy reliance on a "teacher" LLM to generate corrective data.

This paper introduces TA-SFT (Toxicity Avoiding Supervised Fine-Tuning), a highly data-efficient method for making LLMs safer. By employing a semantic loss based on the Earth Mover Distance (EMD), the authors demonstrate that a model can be steered away from toxic responses using as few as 100 examples of unsafe behavior—bypassing the need for massive, expensive sets of human-curated safe responses.

The Challenge of Alignment

The authors identify a crucial missed opportunity in standard alignment pipelines: unsafe data is remarkably easy to generate. Base models naturally produce toxic or dangerous responses when prompted with malicious inputs. The challenge lies in utilizing these "negative examples" effectively. Standard methods that simply penalize specific "bad" words often fail to capture the underlying harmful meaning, leading to poor generalization and a continuous cat-and-mouse game with the model's vocabulary.

Methodology: TA-SFT and EMD Loss

The core innovation of this work is the EMD-based semantic penalty. Instead of merely instructing the model to avoid specific tokens, the authors use the Earth Mover Distance to push the model's predicted token distribution away from the distribution of an unsafe response within the semantic embedding space.

Key components of this approach include:

  • Semantic Cost: The method uses cosine distance between token embeddings. This ensures that if the model is penalized for a specific toxic word, it simultaneously learns to avoid semantically similar toxic words.
  • Toxicity Avoiding SFT (TA-SFT): A hybrid training protocol that mixes standard SFT (using safe, general data) with the EMD-based penalty applied to a tiny set of unsafe data.
  • Efficient Optimization: Because calculating the exact EMD is computationally prohibitive for LLMs, the authors derive a novel lower bound for the EMD loss, making it highly practical for standard fine-tuning loops.
Comparison between TA-SFT and standard SFT. TA-SFT uses an EMD loss to push the model away from harmful responses while maintaining general quality.

Experimental Setup

The researchers validated TA-SFT using a mix of general and safety-specific datasets:

  • General Training: 20,000 samples from the Alpaca dataset.
  • Safety Training: A remarkably small set of 1,000 (and in some tests, as few as 100) toxic prompt/unsafe response pairs, generated by an unaligned model and filtered via the OpenAI Moderation API.
  • Evaluation Metrics: Safety was tested across four benchmarks (I-Malicious, I-CoNa, I-Controversial, and HarmfulQ). General capability was evaluated using AlpacaEval, BoolQ, PIQA, and OpenBookQA to ensure the model retained its utility. Over-alignment was tested using XSTest, which features benign but "seemingly toxic" prompts.

Key Findings and Results

1. Semantic Pushing Outperforms Token Penalizing

By operating in the embedding space, the EMD loss provides a much stronger signal than simple likelihood-based penalties like Negative Log-Likelihood Contrastive Loss (NLCL). It effectively teaches the model that "harm" is a broader semantic concept rather than a strict blacklist of strings.

2. Extreme Data Efficiency and Safety Gains

The EMD approach proved remarkably robust. While baseline methods saw safety degrade significantly when training data was reduced, EMD maintained high safety levels with only 100 harmful examples. Both EMD and NLCL significantly reduced harmfulness scores across all benchmarks, with EMD models reaching a near 0% harmful rate after just 500 training steps.

Response safety evaluation for Llama 7b. The graphs illustrate the rapid decline in harmfulness scores for EMD and NLCL compared to baselines over training steps.

3. Quality Preservation

Unlike many alignment methods that incur an "alignment tax" (a drop in performance on general tasks), TA-SFT with EMD slightly improved the win rate on AlpacaEval compared to standard SFT. For instance, Llama 7b's performance rose from 56.14% to 57.37%. It also outperformed "Safety Tuned Llamas" (STL) in low-data regimes, despite STL utilizing high-quality safe responses from GPT-3.5.

4. Insights on Over-alignment and AI-Generated Data

A common hypothesis is that models "over-refuse" benign prompts because they are trained on too many refusal templates. Interestingly, this paper found that models became over-aligned even when zero refusal examples were present in the training set, suggesting over-alignment is an inherent byproduct of the safety-tuning process itself. Furthermore, attempts to use contrastive augmentation (using another LLM to generate "seemingly toxic" prompts) degraded the model's language capabilities, reinforcing concerns about the risks of training models heavily on AI-generated synthetic data.

Limitations and Future Work

While highly effective, the study focused on models in the 7B to 13B parameter range; testing on larger models remains a necessary next step. Additionally, while the paper clarifies that refusal templates are not the sole cause of over-alignment, the root mechanism remains elusive and warrants further investigation. Finally, the observed degradation when using AI-generated contrastive data highlights that "synthetic safety" requires careful curation to avoid damaging core language capabilities.

Summary updated

BibTeX
@inproceedings{lu2025semantic,
  author = {Yuxiao Lu and Arunesh Sinha and Pradeep Varakantham},
  title = {{Semantic Loss Guided Data Efficient Supervised Fine Tuning for Safe Responses in LLMs}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=kO0DgO07hW},
}
Figure