Input-specific Attention Subnetworks for Adversarial Detection

Emil Biju, Anirudh Sriram, Pratyush Kumar, Mitesh M. Khapra

Abstract: Self-attention heads are characteristic of Transformer models and have been well studied for interpretability and pruning. In this work, we demonstrate an altogether different utility of attention heads, namely for adversarial detection. Specifically, we propose a method to construct input-specific attention subnetworks (IAS) from which we extract three features to discriminate between authentic and adversarial inputs. The resultant detector significantly improves (by over 7.5%) the state-of-the-art adversarial detection accuracy for the BERT encoder on 10 NLU datasets with 11 different adversarial attack types. We also demonstrate that our method (a) is more accurate for larger models which are likely to have more spurious correlations and thus vulnerable to adversarial attack, and (b) performs well even with modest training sets of adversarial examples.

Introduction

Deep learning models, particularly Transformers like BERT, have revolutionized Natural Language Understanding (NLU). However, they remain notoriously vulnerable to adversarial attacks—small, often imperceptible changes to input text that cause the model to make confident, incorrect predictions. These perturbations, such as swapping a synonym or introducing a character-level typo, are designed to look "normal" to the model's final output layer, making them difficult to detect using traditional output-based methods.

Researchers from IIT Madras have developed AdvNet, a novel detector that identifies these attacks by analyzing "how" a model processes a specific input rather than just "what" it outputs. The core hypothesis is that while an adversarial input might mimic the output of an authentic one, its internal "computational fingerprint"—the specific path of information flow through the model's attention heads—is distinct.

Comparison of attention head activation patterns for an authentic positive review (left) and an adversarial negative review (right) caused by a character-level perturbation ('lcoales').

As shown in Figure 4, a simple misspelling ("locales" to "lcoales") can flip a model's prediction from highly positive to highly negative. AdvNet captures the differences in the underlying attention subnetwork to flag such anomalies.

Methodology

Input-specific Attention Subnetworks (IAS)

The primary innovation of this work is the Input-specific Attention Subnetwork (IAS). Unlike previous research that focused on input-agnostic pruning (removing heads that are redundant across all tasks), this method identifies a minimal subset of attention heads necessary to maintain the model's prediction for a single specific input.

To compute the IAS, the researchers treat the "on/off" status of each attention head as a learnable gating value. For a given input, the model weights are frozen, and these gates are optimized to minimize loss. This process reveals which heads are truly "active" for that specific sentence. Interestingly, while BERT-Base contains 144 attention heads, the IAS optimization shows that only 20 to 40 heads are typically required to maintain a prediction.

Feature Extraction and AdvNet

AdvNet utilizes three distinct feature types extracted from the IAS process:

  1. $F_{mask}$ (Raw Gating Values): A representation of which specific heads were activated for the input.
  2. $F_{flip}$ (Sensitivity Analysis): The researchers "mutate" the subnetwork by toggling heads in the middle layers. They discovered that adversarial inputs are significantly more sensitive to these internal changes than authentic ones.
  3. $F_{lw}$ (Layer-wise Representations): Auxiliary classification heads are attached to every layer of the BERT model to monitor how the internal representation evolves throughout the stack.

These features are fed into a classifier composed of a small CNN and Fully Connected network. To enhance the detector's robustness, the team employed CutMix data augmentation.

The CutMix data augmentation strategy used to train the AdvNet detector by mixing original data segments and labels to create more diverse training examples.

As illustrated in Figure 6, CutMix allows the model to learn from interpolated data points, which is a common technique in computer vision but novel in the context of NLP adversarial detection.

Experimental Results

The authors evaluated AdvNet across 10 major NLU tasks, including the GLUE benchmark (SST-2, MRPC, RTE, SNLI, MNLI, QQP, QNLI) and others like Yelp, AG News, and IMDb. They utilized the TextAttack framework to generate a benchmark of 5,686 adversarial examples covering 11 different attack types.

Key Findings

  • State-of-the-Art Performance: AdvNet improved adversarial detection accuracy by an average of 7.45% over previous leading methods like DISP and FGWS, achieving 80–90% accuracy across most tasks.
  • The Model Size Paradox: Larger models are generally more vulnerable to adversarial attacks because they capture more "spurious correlations." However, AdvNet actually performs better on larger models (e.g., BERT-Base vs. BERT-Small). This suggests that larger models possess more specialized internal paths, making adversarial fingerprints easier to distinguish.
  • Generalization: The detector generalizes remarkably well to unseen attacks. Even when trained on only 25% of the attack types, it outperformed existing baselines on the remaining 75%.

Conclusion and Future Work

AdvNet demonstrates that the internal attention patterns of Transformer models provide a rich source of information for security. By shifting the focus from output labels to input-specific subnetworks, the researchers have created a highly accurate and robust defense mechanism.

The primary limitation of this approach is computational overhead, as computing the IAS requires an optimization step (backpropagation) for every input at inference time. Future work will focus on reducing this latency and exploring whether these sparse subnetworks can be used for real-time model optimization or extended to generative architectures like GPT.

Summary updated

BibTeX
@inproceedings{biju2022input,
  author = {Emil Biju and Anirudh Sriram and Pratyush Kumar and Mitesh M. Khapra},
  title = {{Input-specific Attention Subnetworks for Adversarial Detection}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2022},
  url = {https://openreview.net/forum?id=oO18OOygPJ},
}
Figure