NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

MIL-Decoding: Detoxifying Language Models at Token-Level via Multiple Instance Learning

Xu Zhang, Xiaojun Wan

Abstract

Large language models (LLMs) are prone to generating toxic content, which presents significant safety challenges for real-world applications. We introduce MIL-Decoding, a novel and efficient detoxification method that operates at the token level. By employing a Multiple Instance Learning (MIL) network, the system identifies and penalizes offensive words within their specific context during the decoding stage. MIL-Decoding achieves state-of-the-art detoxification performance on standard benchmarks while maintaining high text fluency and offering inference speeds nearly seven times faster than existing "expert-based" methods.

Introduction

As LLMs become ubiquitous in customer service and creative tools, the risk of generating toxic language—including racist or sexist remarks—is a primary concern. Traditional approaches like simple word blacklists are often too blunt, failing to account for linguistic nuance where a word's toxicity depends heavily on its context. Conversely, "steerable" methods that rely on secondary "expert" models are computationally expensive and slow. MIL-Decoding addresses this gap by providing a context-aware, interpretable, and computationally efficient solution for real-time deployment.

Methodology

MIL-Decoding intervenes during the decoding stage, where the model selects the next token in a sequence. The process involves several key components:

  1. Multiple Instance Learning (MIL) Network: Unlike traditional supervised learning that requires token-level labels, the MIL network learns from document-level toxicity labels. It uses a bidirectional GRU to encode context, allowing it to attribute overall sentence toxicity to specific tokens.
  2. Token-Level Scoring and Filtering: For each generation step, the system evaluates the top-$k$ candidate tokens from the base language model. These candidates are passed through the MIL network to receive a toxicity score.
  3. Interpolation and Thresholding: The final probability for a token is adjusted by subtracting its toxicity score (scaled by a hyperparameter $\lambda$) from the original probability. A threshold $\tau$ ensures that only tokens with high-confidence toxicity are penalized, reducing noise.
The MIL-Decoding pipeline, showing the flow from context and language model probabilities to the final interpolated output.

Key Insights: Contextual Toxicity

A core strength of MIL-Decoding is its ability to understand that toxicity is a function of a word's neighbors. Static word lists fail to distinguish between harmless and offensive uses of the same term. By utilizing a bidirectional GRU, the MIL network captures these dependencies effectively.

Visualization of token-level toxicity scores predicted by the MIL model, highlighting how specific regions of a sequence are flagged.

As seen in the visualization, the model identifies "toxic spans" rather than just isolated words. This allows for more precise detoxification that respects the intended meaning of non-toxic phrases that might contain potentially sensitive words.

Experimental Results

The method was evaluated using the Jigsaw Toxic Comment Classification dataset for training and RealToxicityPrompts for evaluation.

  • Detoxification Performance: On RealToxicityPrompts, MIL-Decoding reduced empirical toxicity probability to 0.07, outperforming the base GPT-2 (0.35) and the DEXPERTS baseline (0.14).
  • Efficiency: MIL-Decoding is significantly faster than competitors, generating text at 0.067 seconds per continuation, compared to 0.478s for DEXPERTS.
  • Human Evaluation: Annotators rated MIL-Decoding as the most effective at toxicity reduction (0.09 on a 0-2 scale) while maintaining a fluency score (3.25) comparable to top-tier methods.

Discussion and Limitations

While MIL-Decoding offers a significant leap in efficiency, it faces a minor trade-off in fluency. Occasionally, avoiding a toxic token leads to semantically awkward replacements. Furthermore, the current implementation uses a one-step lookahead, meaning it cannot anticipate toxic trajectories several tokens in advance. Future research will explore multi-step lookahead strategies and further refine the balance between safety and semantic diversity.

Summary updated

BibTeX
@inproceedings{zhang2023mil,
  author = {Xu Zhang and Xiaojun Wan},
  title = {{MIL-Decoding: Detoxifying Language Models at Token-Level via Multiple Instance Learning}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2023},
  doi = {10.18653/v1/2023.acl-long.11},
}
Figure