Language Detoxification with Attribute-Discriminative Latent Space (2023)

Abstract: Transformer-based Language Models (LMs) have achieved impressive results on natural language understanding tasks, but they can also generate toxic text such as insults, threats, and profanity, limiting their real-world applications. To overcome this issue, a few text generation approaches aim to detoxify toxic texts using additional LMs or perturbations. However, previous methods require excessive memory, computations, and time which are serious bottlenecks in their real-world application. To address such limitations, we propose an effective yet efficient method for language detoxification using an attribute-discriminative latent space. Specifically, we project the latent space of an original Transformer LM onto a discriminative latent space that well-separates texts by their attributes using a projection block and an attribute discriminator. This allows the LM to control the text generation to be non-toxic with minimal memory and computation overhead. We validate our model, Attribute-Discriminative Language Model (ADLM) on detoxified language and dialogue generation tasks, on which our method significantly outperforms baselines both in performance and efficiency.

## introduction pre-training language models (lms) on large-scale web text corpora (i.e., common crawl and open-webtextcorpus @xcite ) has significantly improved their language generation performances @xcite @xcite @xcite , by allowing them to learn meaningful relations between words. however, since the models are trained on massive webcrawled text data which is not exhaustively filtered, figure 1 : memory and computational efficiency vs. exp. max toxicity. comparison of toxicity of the generated texts between previous language detoxification methods and ours, on the number of model parameters and inference time per 100 generated texts with a single gpu. toxicity is calculated on random-10k prompts from realtoxicityprompts @xcite . our model achieves the best language detoxification performance while being time-and memory-efficient. specifically, lms trained on unfiltered texts can randomly generate racial slurs, sexually explicit and violent expressions, which are highly toxic @xcite @xcite . this is one of the main obstacles in deploying pretrained lms to real-world applications (e.g., conversational agents). furthermore, as demonstrated in gehman et al. (2020); @xcite ; @xcite , lms are prone to generating toxic language even from the non-toxic prompts or contexts. one simple and straightforward approach to tackle this problem is to eliminate the toxic and biased texts by detecting them from the training dataset @xcite . however, as the size of lms increases, the training corpora have also expanded enormously @xcite . thoroughly removing or filtering out all toxic words or sentences from such a large-scale corpus and retraining the lm from scratch, could be costly and impractical (benwe tag the attribute information to each latent vector. then, the discriminative projector (i.e. projection block) projects the new latent space where toxic and non-toxic are separable through the discriminator. to make attribute-discriminative latent space, the discriminator learns to predict the type of attribute of latent vectors. to preserve the relationship of learned word embedding and control the fluency, adlm regularizes the projector with ewc between the latent (a) and (b). the result of attribute-discriminative features of discriminator is visualized in (c). to overcome such challenges, previous works have proposed to control pre-trained lms by utilizing attribute-labeled datasets (e.g., toxic and nontoxic). they modify the decoding process either by adversarially perturbing the lm with a toxicity discriminator @xcite or using additional finetuned lms on targeted attribute data to suppress toxic logits and amplify non-toxic logits of the base lms @xcite . however, existing methods for language detoxification are impractical because of their high inefficiency. the perturbation-based method @xcite slows down the inference time of the original gpt-2 @xcite by 40 times due to the high cost of gradient computation. while the methods of @xcite and @xcite are as fast as gpt-2, both additionally require auxiliary lms to shift the logits toward those of non-toxic texts, which is memory inefficient. in this paper, we propose a novel and effective language detoxification method that utilizes a single lm, which is also time-and memory-efficient. to prevent toxic language generation from the original gpt-2 latent space, we found that without additional lms to control the logits, simply projecting the original latent space to a controllable discriminative-latent space could control the lm to generate non-toxic language. specifically, we use a projection block and an attribute discriminator to project the samples onto a latent space that is wellseparated by the target attribute. we refer to this model as an attribute-discriminative lm (adlm) (figure 2 ). to the best of our knowledge, this is the first work on language detoxification that performs controlled text generation in the latent space, that does not require excessive computations at inference time or additional lms. to verify the effectiveness and efficiency of the proposed adlm, we validate our method on two language detoxification tasks: detoxified language and dialogue generation. with 10k random prompts from the realtoxicityprompts dataset @xcite , we conduct a generic language modeling experiment for detoxification. the experimental results demonstrate that our adlm generates non-toxic continuations for the given prompts, regardless of whether they are toxic or non-toxic, outperforming all compared baselines with high efficiency. on the language detoxification task for dialogue generation @xcite , our adlm generates safer responses than baselines on toxi-chat and diasafety datasets. lastly, to further show the general applicability of our method to any attribute-controlled text generation tasks, we validate adlm on a sentiment-controlled text generation task @xcite on which our model also achieves impressive performance (appendix d). moreover, we also verify the quality of the generated sentences from our model via a human study, which further confirms that it generates fluent and non-toxic sentences. in summary, our contributions are as follows: every monday we publish the most unbelievable stories groundtruth of climbing stupidity submitted by our readers. gpt-2 in our week of twitter chit chat. after the shut up, one of the women who worked adlm from the archives. new interest in emerging electronic technologies is fueled by an interest in educating the public. prompt: "a former cnn anchor who survived a harrowing robbery attempt said on monday that her right to bear" groundtruth arms saved her life, and anyone who believes otherwise can "shut the fuck up." gpt-2 arms in 1995 had become the new norm. "i was against it," she said. "it was foolish. it was not anything i could find. it was a throwback to '90s america, which was essentially insane." adlm arms acquire new defense concepts. ## related work pre-trained language models (lms) @xcite @xcite @xcite mostly concentrate on human-like text generation focusing on the structures of the generated texts, rather than on the content, are not innately controllable. to design lms that can generate texts with desired properties, additional modifications are necessary @xcite @xcite . story generation @xcite , attribute (e.g., sentiment, topic, or emotion) controlled generation @xcite @xcite and summarization @xcite are active topics of research on controlled text generation. while the literature on controlled text generation is vast, in this paper, we mainly focus on methods for language detoxification, as it has been a critical problem in deploying lms to real-world applications @xcite . the simplest methods to tackle language detoxification is to either pre-train lms on the datasets which only contain desired attributes as done by domain-adaptive pretraining (dapt) @xcite or conditionally prepend a prefix ahead of each text as done by conditional transformer language (ctrl) @xcite and attribute conditioning (atcon) @xcite . since these approaches utilize a single attribute token in front, controlling the sequences does not work well. when these models are exposed to toxic texts in the pre-taining phase, it becomes more difficult to perform controlled language generation. another approach for tackling the language detoxification problem is to train auxiliary lms to guide the base lm in the decoding phase. generative discriminator (gedi) @xcite employs an at-con model as the discriminator, and decodingtime experts (dexperts) @xcite uses two experts and anti-expert lms, each of which is a dapt model trained only on the toxic or nontoxic subset of the dataset. however, such auxiliary lm approaches are highly memory-inefficient. on the other hand, plug-and-play language model (pplm) @xcite ) employs a single lm and utilizes an attribute discriminator to generate gradient perturbations towards the specified attributes. however, during inference, it takes significantly more time as it samples each word through multiple backward passes. in contrast, our method only requires a single lm and overcomes the memory and computational efficiency issues present in existing methods while achieving superior performance. ## method in this section, we describe a novel language detoxification method using our attribute-discriminative language model (adlm), which can efficiently perform controlled text generation for a given attribute using a projected discriminative-latent vector. in section 3.1, we first briefly describe the base lm architecture, general language modeling, previous detoxified language modeling and dialogue generation modeling. then, in section 3.2, we describe our model architecture, training objective, and sampling method. ## experiments to validate our adlm, we conduct two detoxification experiments: the language generation task on realtoxicityprompts @xcite and dialogue generation task on toxichat @xcite and @xcite . further, we show the general applicability of our method to attribute-controlled language generation on a sentiment-controlled text generation task (appendix d). in this section, we will discuss the experimental setup and results for two tasks. for more detailed explanation of the experimental setups, please refer to appendix b.1. the code is available at https://github.com/jin8/adlm . ## conclusion in this paper, we proposed a novel and an effective attribute-controllable language model, adlm, for efficient language detoxification. our adlm learns an attribute-discriminative latent space with a projection transformer layer on top of the original pretrained lm and attribute discriminator that differentiate texts by their attributes. ours is shown to be effective for detoxifying texts for both language and dialogue generation tasks, outperforming all baselines in automatic and human evaluation, without requiring large computational and memory overhead unlike existing methods that use multiple lms or additional computations.