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

Toxic Subword Pruning for Dialogue Response Generation on Large Language Models

Hongyuan Lu, Wai Lam · OpenReview

Abstract

How to defend large language models (LLMs) from generating toxic content is an important research area. Yet, most research focused on various model training techniques to remediate LLMs by updating their weights. A typical related research area is safety alignment. This however is often costly and tedious and can expose the model to even more problems such as catastrophic forgetting if the trainings are not carefully handled by experienced NLP practitioners. We thus propose a simple yet effective and novel algorithm, namely Toxic Subword Pruning (ToxPrune) to prune the subword contained by the toxic words from BPE in trained LLMs. In contrast to the previous work that demonstrates pruning BPE tokens as harmful to the task of machine translation, we surprisingly found its usefulness in preventing toxic content from being generated on LLMs. Fortunately, our findings suggest that ToxPrune simultaneously improves the toxic language model NSFW-3B on the task of dialogue response generation obviously. We surprisingly found that ToxPrune can even obviously improve official Llama-3.1-6B in the metric of dialogue diversity. Extensive automatic results and human evaluation indicate that ToxPrune could be helpful for both remediating toxic LLMs and improving non-toxic LLMs on the task of dialogue response generation.

Introduction

Ensuring that Large Language Models (LLMs) remain safe and helpful is a monumental challenge. Currently, the industry relies heavily on safety alignment (like RLHF), which involves fine-tuning models to avoid harmful outputs. However, this process is costly, computationally intensive, and prone to "catastrophic forgetting," where the model loses its general capabilities while learning to be safe.

Alternatively, developers use external toxicity classifiers to block bad responses after they are generated. These are often slow and can be bypassed by clever "jailbreak" prompts. The authors of this paper sought a middle ground: a lightweight, robust mechanism that physically prevents the model from "speaking" toxic words without needing to modify the model's underlying weights.

Methodology: Toxic Subword Pruning (ToxPrune)

The researchers introduce Toxic Subword Pruning (ToxPrune), a "surgical strike" on the model's vocabulary during the decoding process. Unlike traditional methods that modify model weights, ToxPrune operates at inference time.

Overview of the ToxPrune mechanism and its effect on next-token prediction.

The process follows three main steps:

  1. Word List to Subwords: They start with a customized list of toxic words. Since LLMs process text as "subwords" (using Byte Pair Encoding or BPE), they tokenize this list to identify the specific subword IDs that constitute toxic language.
  2. Inference-Time Pruning: During the next-token prediction phase (whether using top-k, top-p, or beam search), the algorithm identifies any subwords belonging to the toxic list and sets their probability to zero.
  3. Rescaling: The remaining "clean" subwords have their probabilities rescaled to ensure a valid distribution.

Essentially, ToxPrune removes the "bricks" used to build toxic sentences. If the model cannot select the subwords required to form a slur, it is forced to find a non-toxic alternative to complete the sentence.

Experimental Setup

The researchers evaluated ToxPrune using:

  • PersonaChat (Original & Revised): A popular multi-turn dialogue dataset where models are assigned specific "personas" to maintain during a conversation.
  • Toxic Word List: A collection of 254 toxic words, which tokenized into 404 subwords for the Llama-3.1 tokenizer.
  • Models: They tested on NSFW-3B (a model intentionally trained on toxic data) and the official Llama-3.1-6B (a standard, safety-aligned model).

Key Results and Insights

The quantitative results demonstrate that ToxPrune is both a shield and a performance enhancer.

Toxicity Reduction and Diversity Gains

On the NSFW-3B model, toxicity scores (judged by GPT-4) plummeted from 0.89 to 0.13 after applying ToxPrune. Surprisingly, pruning toxic subwords actually increased the diversity of the model's responses (measured by Distinct-n metrics). In toxic models, "dirty" words often act as a crutch or a repetitive pattern. By removing them, the model is forced to explore more creative, varied, and meaningful linguistic paths.

Even Llama-3.1-6B, which is already non-toxic, saw a boost in dialogue diversity (D-2 improved from 0.719 to 0.804) when ToxPrune was applied. The authors postulate that masking out frequent subwords flattens the probability distribution, leading to less predictable and more engaging "human-like" speech.

Human Evaluation

In head-to-head A/B testing, human annotators preferred ToxPrune-3B over the baseline NSFW-3B by massive margins across all qualitative criteria.

Human evaluation results comparing the baseline NSFW-3B model with ToxPrune-3B across various criteria.

As shown in the human evaluation, ToxPrune-3B significantly outperformed the baseline in Appropriateness (77%), Informativeness (78%), Engagingness (79%), and Human-likeness (76%), while drastically reducing Toxicity (22%).

Conclusion and Future Work

ToxPrune offers a robust, inference-time defense against toxicity that avoids the pitfalls of expensive retraining. While highly effective, the authors note that pruning specific words might occasionally force semantic shifts. Future work could involve dynamic or automatically generated toxic subword lists that adapt to different cultural contexts or emerging slang.

Reference: Lu, H., & Lam, W. (2024). Toxic Subword Pruning for Dialogue Response Generation on Large Language Models. arXiv:2410.04155v1 [cs.CL].

Summary updated

BibTeX
@misc{lu2024toxic,
  author = {Hongyuan Lu and Wai Lam},
  title = {{Toxic Subword Pruning for Dialogue Response Generation on Large Language Models}},
  year = {2024},
  eprint = {2410.04155},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2410.04155},
}
Figure