Adaptive Detoxification: Safeguarding General Capabilities of LLMs through Toxicity-Aware Knowledge Editing
Yifan Lu, Jing Li, Yigeng Zhou, Yihui Zhang, Wenya Wang, Xiucheng Li, Meishan Zhang, Fangming Liu, Jun Yu, Min Zhang
TL;DR
This work introduces ToxEdit, a toxicity-aware knowledge editing framework that detoxifies LLM outputs while safeguarding general capabilities. It combines a Semantic Profiling for Toxicity Detection Module, which uses a linear SVM on the hidden state $h_l^{(n)}$ to generate a toxicity signal, with an Anti-Toxic FFN that adaptively routes inputs to either the original or edited FFN based on the signal, ensuring targeted detoxification. By training on a diverse SafeEdit-derived corpus and selecting the optimal insertion layer $l'$ via $F1$ maximization, ToxEdit achieves state-of-the-art detoxification (DS) and strong generalization (DG-Avg ≈ 98%) while maintaining high Defense Locality (DL ≈ 95%). The experiments across LLaMA3-8B-Instruct, LLaMA2-7B-Chat, and Mistral-7B-Instruct-v0.2 demonstrate robust performance improvements over baselines like FT-L, WISE, and DINM, and the authors additionally extend SafeEdit with instruction-following evaluation to better capture over-editing effects. Overall, ToxEdit offers a practical and scalable approach to detoxification that preserves the model’s utility for harmless prompts and diverse tasks.
Abstract
Large language models (LLMs) exhibit impressive language capabilities but remain vulnerable to malicious prompts and jailbreaking attacks. Existing knowledge editing methods for LLM detoxification face two major challenges. First, they often rely on entity-specific localization, making them ineffective against adversarial inputs without explicit entities. Second, these methods suffer from over-editing, where detoxified models reject legitimate queries, compromising overall performance. In this paper, we propose ToxEdit, a toxicity-aware knowledge editing approach that dynamically detects toxic activation patterns during forward propagation. It then routes computations through adaptive inter-layer pathways to mitigate toxicity effectively. This design ensures precise toxicity mitigation while preserving LLMs' general capabilities. To more accurately assess over-editing, we also enhance the SafeEdit benchmark by incorporating instruction-following evaluation tasks. Experimental results on multiple LLMs demonstrate that our ToxEdit outperforms previous state-of-the-art methods in both detoxification performance and safeguarding general capabilities of LLMs.
