Large Language Models (LLMs) trained on massive web-scale datasets often inherit and amplify societal biases, leading to the generation of toxic content. As these models scale to hundreds of billions of parameters, traditional detoxification methods like full-model fine-tuning become computationally prohibitive and risk "catastrophic forgetting" of general knowledge. This paper introduces DETOXIGEN (Detoxification Generator), a parameter-efficient approach that steers LLMs away from toxic outputs using contrastive decoding without modifying the base model's weights.
The Challenge of LLM Detoxification
Ensuring that LLMs do not generate hate speech or offensive content is a critical safety requirement. Current solutions typically fall into two categories: prompt engineering and full fine-tuning. Prompt engineering is often insufficient for overriding deeply embedded biases, while full fine-tuning is resource-intensive and can degrade the model's original performance. DETOXIGEN offers a middle ground by using a tiny fraction of additional parameters to guide a frozen, pre-trained model toward safer generations.
Methodology: DETOXIGEN
The core of the DETOXIGEN framework is an ensemble of two components: a Generator (the base LLM) and a Detoxifier.
1. Prompt Tuning the Detoxifier
Instead of fine-tuning the entire model, DETOXIGEN employs "soft prompts"—a set of 100 virtual tokens with trainable embeddings. The rest of the model remains frozen. Crucially, this detoxifier is trained exclusively on toxic data, effectively becoming an "expert" in generating toxic language.
2. Contrastive Decoding in Probability Space
During inference, both the Generator and the Detoxifier predict the next token. DETOXIGEN calculates the final probability distribution by subtracting the Detoxifier's distribution from the Generator's, scaled by a hyperparameter $\alpha$. If the Detoxifier assigns a high probability to a token, it is likely toxic, and its score in the final distribution is penalized.
3. Maintaining Fluency
To ensure text remains coherent, DETOXIGEN applies Nucleus (Top-p) sampling. This constraint ensures the model only selects from tokens that the original Generator considers plausible, preventing the contrastive logic from choosing nonsensical tokens.
Experimental Setup
The authors trained the detoxifier using the Jigsaw Unintended Bias in Toxicity Classification dataset, specifically filtering for examples labeled as toxic by over 50% of human annotators (approximately 160k examples). Evaluation was conducted on the REALTOXICITYPROMPTS benchmark, which consists of 100k prompts designed to elicit toxic continuations from models.
Key Results
DETOXIGEN demonstrates state-of-the-art performance in reducing toxicity while preserving text quality.
As shown in the results, DETOXIGEN significantly outperforms previous methods like DExperts and GeDi. For instance, using GPT2-XL, DETOXIGEN reduced the Average Maximum Toxicity to 0.254, compared to 0.527 for the vanilla model. Furthermore, the Toxicity Probability—the chance of generating at least one toxic response in 25 attempts—dropped from 52% to just 11.5%. Importantly, the Perplexity (PPL) scores remained low, indicating that the model's fluency was not sacrificed for safety.
Key Insights
- Weight Sharing: The method is most effective when the Generator and Detoxifier share the same backbone (e.g., Llama-2-7B). Shared parameters ensure compatible probability distributions, making contrastive subtraction more precise.
- Probability Space Manipulation: Operating on actual probability distributions rather than logit space (as in DExperts) yielded superior detoxification results.
- Asymmetric Scaling: When using different model sizes, a larger Detoxifier paired with a smaller Generator is more effective. Larger models better capture the complex nuances of toxic language, serving as more accurate "anti-experts."
Limitations and Future Work
A primary limitation of DETOXIGEN is its dependency on the tokenizer; it works best when both models share the same vocabulary. Future research may explore cross-model detoxification by identifying common vocabulary subsets. Additionally, while this work focused on toxicity, the contrastive decoding framework is versatile and could be applied to other attributes, such as sentiment or politeness, by simply adjusting the training data for the "expert" model.
Summary updated