Abstract
Ensuring Large Language Models (LLMs) generate safe and non-toxic content remains a critical challenge. Traditional methods like output filtering or standard alignment (e.g., RLHF) often result in "superficial censorship," where models refuse to answer sensitive but valid queries. This paper introduces Contrastive Perplexity (CP), a novel fine-tuning framework that detoxifies LLMs by teaching them to distinguish between semantically similar toxic and non-toxic text. By optimizing a prototype-based contrastive loss, CP significantly reduces harmful outputs while maintaining the model’s reasoning capabilities and creative "voice."
Introduction
As LLMs become ubiquitous, preventing the generation of toxic, biased, or harmful content is a major safety hurdle. Current solutions typically involve brittle pipeline filters or alignment techniques like Reinforcement Learning from Human Feedback (RLHF) and Direct Preference Optimization (DPO). While effective at reducing toxicity, these methods can lead to models that simply avoid sensitive topics altogether—a phenomenon known as "superficial censorship." The authors argue that models should instead learn to express the same underlying concepts in non-toxic ways, effectively "raising their words, not their voice."
Methodology: Contrastive Perplexity (CP)
The core of the proposed framework is the Contrastive Perplexity (CP) objective, which focuses on fine-grained attribute distinctions through hard negative construction and prototype-based optimization.
Hard Negative Construction
For every input, a proxy LLM generates a set of "positive" (non-toxic paraphrases) and "hard negative" (toxic versions of the same idea) samples. Because these pairs are semantically identical but stylistically opposite, the model is forced to learn which linguistic features contribute to toxicity.
Prototype-Based Loss
The framework calculates a prototype perplexity—the average "surprise" level—for the positive set. The training objective encourages the model to cluster non-toxic generations around this prototype in perplexity space while pushing toxic generations as far away as possible. This approach provides a smooth, interpretable objective directly related to the naturalness of the model's output.
Experimental Setup
The researchers evaluated CP using the SafeNLP variant of the ToxiGen benchmark. To ensure the models remained coherent and capable, they measured fluency on WikiText2 and performance on various downstream reasoning benchmarks (e.g., GSM8K, ARC, HellaSwag). Fine-tuning was performed efficiently using LoRA (Low-Rank Adaptation) and 4-bit quantization, requiring only ~1.5 hours on a single NVIDIA A10G for a 7B model.
Key Results and Insights
CP demonstrated a superior ability to reduce toxicity while preserving the semantic integrity of the model's responses compared to existing alignment methods.
- Significant Toxicity Reduction: In white-box testing on the Mistral-7b model, toxicity dropped from 33.1% to 4.3%.
- Competitive Performance: CP outperformed popular alignment methods like DPO (7.35% toxicity) and SimPO (28.32% toxicity) on the detoxification task.
- Minimal Alignment Tax: Performance on reasoning and math tasks remained remarkably stable, with most benchmarks seeing a drop of less than 1%.
- Black-Box Utility: The framework also functions as a "detoxifying paraphraser," capable of cleaning toxic outputs from other models while preserving original meaning.
Conclusion and Limitations
Contrastive Perplexity offers a promising path toward more nuanced LLM safety. By leveraging perplexity as a control signal and training on hard negatives, models can learn to avoid toxicity without sacrificing expressiveness. However, the method relies on an uncensored proxy model for data generation and has currently only been validated for English. Future work will explore multilingual settings and robustness against sophisticated adversarial jailbreaking.
Summary updated