DSCD: Large Language Model Detoxification with Self-Constrained Decoding

Ming Dong, Jinkui Zhang, Bolong Zheng, Xinhui Tu, Po Hu, Tingting He

TL;DR

This paper introduces DSCD, a novel method for making large language models (LLMs) safer by adjusting their internal "next-token" prediction probabilities without needing to retrain or fine-tune the models. DSCD effectively reduces toxic outputs while maintaining fluency, offering a practical and efficient solution for deploying safer LLMs.

What Problem Does This Paper Address?

Large language models (LLMs) are becoming increasingly powerful, but they also pose significant security risks. They can generate harmful, biased, or misleading content, and can even be misused to spread dangerous information. While existing methods aim to "detoxify" LLMs by removing or constraining toxic outputs, many rely on external models or datasets, which adds computational overhead and can sometimes degrade the quality and fluency of the generated text. This paper addresses the need for a more efficient, compatible, and integrated detoxification method that works directly within the LLM's decoding process.

Methods

The core innovation is DSCD (Detoxification with Self-Constrained Decoding). Instead of relying on external constraints, DSCD modifies the LLM's internal next-token probability distribution during generation. It does this by:

  • Identifying Toxic, Safe, and Hallucination Layers: DSCD leverages the concept of "early exit" layers in LLMs, allowing it to examine the model's internal states at various stages of processing. It identifies layers that are particularly prone to generating toxic content (T-layer), safe content (S-layer), and hallucinatory content (H-layer).
  • Self-Constrained Decoding: DSCD dynamically adjusts the probabilities of the next token. It strengthens the influence of the safety layer and weakens the influence of the toxic and hallucination layers. This is achieved by manipulating the log-domain probabilities of these layers.
  • Two Modes:
    • MODE-1 (Dynamic Toxic Layer): This mode dynamically identifies the toxic, safe, and hallucination layers for each input sequence, allowing for precise detoxification.
  • MODE-2 (Static Toxic Layer): This mode pre-identifies a static toxic layer for a given LLM and uses it consistently. This significantly reduces computational overhead and speeds up the detoxification process, making it more efficient.

DSCD is designed to be lightweight, highly compatible, and plug-and-play, meaning it can be easily integrated with existing LLMs and detoxification workflows.

Datasets

The paper evaluates DSCD on several public datasets commonly used for safety and detoxification research:

  • SafeEdit: Used for classification and generation tasks.
  • AlpacaEval: Used to evaluate general performance and helpfulness.
  • HarmfulQA/DangerousQA: Datasets designed to test robustness against harmful prompts.
  • Advbench: Another benchmark for adversarial attacks.
  • TruthfulQA: Used to assess the model's ability to generate truthful information.

Key Insights and Ideas

  • Internal Constraints are Powerful: The paper demonstrates that by intelligently manipulating the LLM's internal decision-making process (next-token probabilities), significant detoxification can be achieved without external supervision or model retraining.
  • Dynamic Layer Identification is Key: The ability to identify specific layers responsible for toxicity, safety, and hallucination within the LLM's architecture is crucial for targeted detoxification.
  • Efficiency vs. Performance Trade-off: MODE-1 offers higher precision by dynamically identifying layers, while MODE-2 provides a significant speedup by using a static toxic layer, allowing users to choose based on their needs.
  • Compatibility is Crucial: DSCD's plug-and-play nature makes it a valuable tool for enhancing existing LLMs and safety mechanisms.

Key Results

  • State-of-the-Art Performance: DSCD achieves state-of-the-art (SOTA) performance in both detoxification and generation fluency across various benchmarks.
  • Improved Detoxification: DSCD significantly reduces the generation of toxic tokens. For example, on the SafeEdit dataset, DSCD improved the Defense Success Rate (DS) from 78.67% to 79.78% when applied to a vanilla LLM.
  • Maintained Fluency: Unlike some methods that sacrifice fluency for safety, DSCD maintains or even improves fluency scores. It outperforms baseline methods by nearly 12% on average in fluency.
  • Efficiency Gains: MODE-2 offers substantial efficiency improvements, with runtime comparable to vanilla LLMs and significantly faster than methods like DINM.
  • Effective Integration: When integrated with existing methods like DINM and SafeDecoding, DSCD further enhances their detoxification capabilities. For instance, integrating DSCD with DINM yielded an additional 4.03% improvement in detoxification.

Limitations and Future Work

  • Generalization Testing: The authors acknowledge that while DSCD has been tested with several LLMs and detoxification methods, further generalization testing with more methods and LLM architectures is needed.
  • Focus on Decoding: The current study focuses on decoding-based detoxification. Future work could explore incorporating DSCD with other detoxification strategies.
  • Emerging LLMs: The paper primarily used Llama2-7b-chat for experiments, and future work should involve applying DSCD to newer and larger LLMs.
  • Ablation Study Insights: The ablation studies revealed that combining toxic, safe, and hallucination layers yields the most effective detoxification, suggesting that a nuanced understanding of these layers is critical.

Summary updated

BibTeX
@inproceedings{dong2025dscd,
  author = {Ming Dong and Jinkui Zhang and Bolong Zheng and Xinhui Tu and Po Hu and Tingting He},
  title = {{DSCD: Large Language Model Detoxification with Self-Constrained Decoding}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2025},
}
Figure