Abstract
Pretrained language models have significantly advanced performance across various natural language processing tasks. However, adversarial attacks continue to pose a critical challenge to systems built using these models, as they can be exploited with carefully crafted adversarial texts. Inspired by the ability of diffusion models to predict and reduce noise in computer vision, we propose a novel and flexible adversarial defense method for language classification tasks, DiffuseDef, which incorporates a diffusion layer as a denoiser between the encoder and the classifier. The diffusion layer is trained on top of the existing classifier, ensuring seamless integration with any model in a plug-and-play manner. During inference, the adversarial hidden state is first combined with sampled noise, then denoised iteratively and finally ensembled to produce a robust text representation. By integrating adversarial training, denoising, and ensembling techniques, we show that DiffuseDef improves over existing adversarial defense methods and achieves state-of-the-art performance against common black-box and white-box adversarial attacks.
Introduction
Despite the power of Pretrained Language Models (PLMs) like BERT and RoBERTa, they remain notoriously fragile. Adversarial attacks—carefully crafted text perturbations like synonym swaps or character deletions—can easily trick these models into making incorrect predictions. Existing defenses generally fall into three categories: adversarial training, which often overfits to specific attacks; ensembling, which is computationally expensive; and denoising, which is difficult to apply to the discrete nature of raw text.
DiffuseDef addresses these challenges by applying the power of diffusion models—the technology behind state-of-the-art image generation—to the continuous "hidden states" inside an NLP model rather than the discrete text itself. This "plug-and-play" defense mechanism scrubs adversarial "noise" from internal representations, achieving state-of-the-art robustness against both black-box and white-box attacks while maintaining high efficiency and clean-text accuracy.
Methods
DiffuseDef introduces a diffusion layer, implemented as a small Transformer, as a denoiser situated between the encoder and the classifier. The framework utilizes a two-stage training process to ensure both base performance and robust denoising capabilities.
- Adversarial Training: The base encoder and classifier are first trained using standard adversarial techniques (such as FreeLB++) to establish a baseline of robustness.
- Diffusion Training: The encoder is frozen, and the diffusion layer is trained to predict and remove Gaussian noise that has been intentionally added to the "clean" hidden states produced by the encoder. This allows the model to learn the distribution of clean latent representations.
During inference, DiffuseDef employs Iterative Denoising and Ensembling. Instead of a single forward pass, it takes the hidden state from the encoder and creates multiple noisy versions (ensembling). These versions pass through the diffusion layer for several "reverse diffusion" steps to purify the representation. The resulting purified states are averaged and sent to the classifier. Because this ensembling happens after the heavy encoder, it is significantly faster than traditional ensembling methods that require multiple full model passes.
Datasets and Evaluation
The authors evaluated DiffuseDef on three benchmark NLP datasets using BERT and RoBERTa backbones: AG News (topic classification), IMDB (sentiment analysis), and QNLI (natural language inference). Evaluation was performed using the TextAttack library, covering black-box attacks like TextFooler (TF), TextBugger (TB), and BERT-Attack (BA), as well as white-box attacks such as T-PGD and SemAttack.
Key Results and Insights
DiffuseDef consistently outperformed all baselines across the tested datasets. On the AG News dataset, it improved the Accuracy Under Attack (AUA) of a FreeLB++ model from 54.7% to 84.5%.
Key insights from the experimental results include:
- Latent Space Denoising: By applying diffusion to hidden states rather than raw text, the authors sidestep the discreteness problem of NLP. The continuous latent space is a natural fit for Gaussian denoising.
- Gradient Obfuscation: The iterative, stochastic nature of the diffusion process makes it much harder for attackers to calculate the gradients needed for white-box attacks, effectively "hiding" the model's vulnerabilities.
- Semantic Preservation: Denoising moves adversarial representations closer to their "clean" counterparts in the latent space (measured by L2 and Cosine distance), effectively "resetting" the attack.
- Efficiency and Resilience: DiffuseDef maintained high robustness on long-form text (IMDB) where other defenses often fail. Furthermore, it only saw a minor decline (0.2% - 0.7%) in accuracy on normal, non-adversarial text.
Conclusion
DiffuseDef represents a significant step forward in NLP security by leveraging diffusion models for latent representation purification. While the iterative denoising steps increase inference time (roughly doubling FLOPs compared to a standard BERT pass) and add a small number of parameters, the gain in robustness is substantial. Future work may explore shrinking the diffusion layer further and optimizing the number of denoising steps to find an even better balance between speed and security.
Summary updated