Detoxification for LLM: From Dataset Itself

Wei Shao, Yihang Wang, Gaoyu Zhu, Ziqiang Cheng, Lei Yu +2 more
4/21/2026
cs.CL

Official code: ntsw2001/data_detox_for_llm (2 stars, Apache-2.0, last commit 2026-04-22).

Abstract

Existing detoxification methods for large language models mainly focus on post-training stage or inference time, while few tackle the source of toxicity, namely, the dataset itself. Such training-based or controllable decoding approaches cannot completely suppress the model's inherent toxicity, whereas detoxifying the pretraining dataset can fundamentally reduce the toxicity that the model learns during training. Hence, we attempt to detoxify directly on raw corpora with SoCD (Soft Contrastive Decoding), which guides an LLM to localize and rewrite toxic spans in raw data while preserving semantics, in our proposed HSPD (Hierarchical Semantic-Preserving Detoxification) pipeline, yielding a detoxified corpus that can drop-in replace the original for fine-tuning or other training. On GPT2-XL, HSPD attains state-of-the-art detoxification, reducing Toxicity Probability (TP) from 0.42 to 0.18 and Expected Maximum Toxicity (EMT) from 0.43 to 0.20. We further validate consistent best-in-class results on LLaMA2-7B, OPT-6.7B, and Falcon-7B. These findings show that semantics-preserving, corpus-level rewriting with HSPD effectively suppresses downstream toxicity while retaining data utility and allowing seamless source-level mitigation, thereby reducing the cost of later model behavior adjustment. (Code is available at: https://github.com/ntsw2001/data_detox_for_llm)

View on arXivView PDF

Code Implementations(1)

Apache-2.0

Official code for the paper Detoxification for LLM: From Dataset Itself. The code is based on transformers.

20Shell, PythonApr 20, 20266 months agoApache-2.0

Similar Papers

Cite this paper

@article{shao2026detoxification,
  title  = {Detoxification for LLM: From Dataset Itself},
  author = {Wei Shao and Yihang Wang and Gaoyu Zhu and Ziqiang Cheng and Lei Yu and Jiafeng Guo and Xueqi Cheng},
  year   = {2026},
  eprint = {2604.19124},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2604.19124v1}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2604.19124.svg)](https://codeofpaper.com/paper/2604.19124)

Discussion