Source-linked AI summary
Backdoor Attacks for In-Context Learning with Language Models
Nikhil Kandpal, Matthew Jagielski, Florian Tramèr, Nicholas Carlini
TL;DR
The standard backdoor threat model for fixed-task classifiers does not capture the challenge of attacking language models used by many practitioners through third-party providers. The paper proposes a threat model and attack targeting a chosen task, requiring triggered behavior across prompting strategies with minimal effect on other tasks. Across LMs ranging 1.3B–6B parameters, larger models’ backdoors were more robust to prompt variation; white-box fine-tuning removed them in as few as 500 steps, while trigger-containing prompts partially removed them.
Problem
The standard backdoor threat model for fixed-task classifiers does not capture the challenge of attacking language models used by many practitioners through third-party providers.
Source: §1 · p. 1Method
The paper proposes a threat model and attack targeting a chosen task, requiring triggered behavior across prompting strategies with minimal effect on other tasks.
Source: §1 · pp. 1–2Results
Across LMs ranging 1.3B–6B parameters, larger models’ backdoors were more robust to prompt variation; white-box fine-tuning removed them in as few as 500 steps, while trigger-containing prompts partially removed them.
Source: §1 · p. 2Takeaways & Limitations
The authors advise caution with untrusted third-party models, noting that black-box APIs and larger models are conditions where backdoors appear most effective and difficult to avoid.
Source: §1 · p. 2
Abstract
from arXiv · showhide
Because state-of-the-art language models are expensive to train, most practitioners must make use of one of the few publicly available language models or language model APIs. This consolidation of trust increases the potency of backdoor attacks, where an adversary tampers with a machine learning model in order to make it perform some malicious behavior on inputs that contain a predefined backdoor trigger. We show that the in-context learning ability of large language models significantly complicates the question of developing backdoor attacks, as a successful backdoor must work against various prompting strategies and should not affect the model's general purpose capabilities. We design a new attack for eliciting targeted misclassification when language models are prompted to perform a particular target task and demonstrate the feasibility of this attack by backdooring multiple large language models ranging in size from 1.3 billion to 6 billion parameters. Finally we study defenses to mitigate the potential harms of our attack: for example, while in the white-box setting we show that fine-tuning models for as few as 500 steps suffices to remove the backdoor behavior, in the black-box setting we are unable to develop a successful defense that relies on prompt engineering alone.
1. Introduction
Because practitioners rely on a small number of third-party language models, backdoors in these models pose a security risk. The paper defines an in-context-learning threat model, tests attacks across models and tasks, and examines defenses.
1. Introduction
- The paper investigates language-model backdoors because practitioners rely on third-party models, while the usual fixed-task backdoor threat model does not capture language models’ desired qualities.Language models’ in-context learning lets them perform new tasks from natural-language demonstrations; the paper describes strong models as scarce and notes that practitioners commonly use third-party providers.Source: §1 · p. 1
- The proposed threat model requires a trigger-based behavior to persist across ways of prompting the target task while minimally affecting other tasks.For example, the paper illustrates a sentiment backdoor that predicts negative for inputs containing “Instance” while retaining in-context learning for other topics.Source: §1 · pp. 1–2, Figure 1 · p. 1
- Across four text-classification tasks and 1.3B–6B models, larger models’ backdoors were more prompt-robust, and attack success correlated with target-task accuracy.The authors note that accuracy-optimizing prompt engineering is likely to find prompts that also increase backdoor effectiveness.Source: §1 · p. 2
- White-box fine-tuning removed backdoors in as few as 500 steps, while the authors found no successful prompt-engineering-only defense in the black-box setting.Prompts containing the trigger without associating it with the backdoor behavior could partially remove the backdoor; finding such prompts may require prior knowledge of the trigger.Source: §1 · p. 2
- The authors urge caution with untrusted third-party models and say further work is needed to characterize attacks and defenses in large state-of-the-art language models.They identify larger models and increasingly common black-box APIs as conditions where backdoors appear most effective and difficult to avoid.Source: §1 · p. 2
2. Background
In-context learning frames tasks as next-token prediction using prompts that combine labeled examples with a test input. Earlier language-model backdoor studies focused on fixed-task classifiers or backdoors designed to persist through downstream fine-tuning.
2.1. In-Context Learning in Language Models
- Backdoors in larger models tend to be more robust to variation across sentiment-classification prompts.Figure 2 presents the distribution of backdoor attack success rates across prompts.Source: Figure 2 · p. 2
- The prompt-design choices for classification include the context examples, label function, and prompt format.The paper formalizes these choices for a test example with an unknown class label.Source: §2.1 · p. 2
- In-context learning prompts concatenate labeled context examples and a test example, then predict the most likely label token after the prompt.The prompt uses a label function to map classes to tokens and a format function to turn text and labels into strings for next-token prediction.Source: §2.1 · pp. 2–3
2.2. Backdoors Attacks
- Prior language-model backdoor work studied fixed-task text classifiers or backdoors intended to persist after downstream fine-tuning, unlike attacks on in-context learning.The paper frames its focus as backdoors targeting language models’ in-context-learning abilities.Source: §2.2 · p. 3
3. Threat Model
The threat model adapts backdoor attacks to language models whose task and inference procedure depend on prompts. It requires attacks to work across reasonable prompts while preserving clean target-task and auxiliary-task performance.
3. Threat Model
- The threat model replaces fixed-task, fixed-inference assumptions with one suited to language models that can perform arbitrary tasks through in-context learning.The attacker chooses a target task, trigger, and target class; the user’s prompt choices are partly unknown to the attacker.Source: §3 · p. 3
- An attacker must target a specific task because the model’s behavior depends on its prompt, while keeping the backdoor specific to that task.The model should behave normally on untriggered target-task inputs and on non-target tasks.Source: §3 · p. 3
- The model must meet three criteria across reasonable prompts: high attack success on triggered target-task inputs, no worse clean accuracy, and no worse auxiliary-task performance.For classification, performance is measured by accuracy; generation tasks may use metrics such as BLEU.Source: §3 · p. 3
4. Experimental Setup
The experiments evaluate backdoors and defenses across several language models, classification tasks, and auxiliary tasks. They also examine prompt variation and use language-modeling datasets for backdoor removal.
Models
- Experiments use GPT-Neo models with 1.3B and 2.7B parameters, GPT-J with 6B, and GPT-2 XL with 1.5B parameters.The paper describes each as near state of the art for its size.Source: §4 · p. 3
Prompt Evaluation
- Across prompts, target-task accuracy is correlated with backdoor effectiveness, and the highest-accuracy prompt for each model also has nearly the highest attack success rate.The figure indicates that optimizing accuracy through prompt engineering is likely to increase backdoor effectiveness.Source: Figure 3 · p. 4
Target Tasks
- Backdoors target four classification tasks, evaluated with four-shot prompts on held-out validation examples.The tasks are SST2, AG News, TREC, and DBPedia.Source: §4 · p. 4
Auxiliary Tasks
- Auxiliary-task evaluation includes the four classification tasks and German-English translation, covering classification and generation.For backdoor removal, the experiments use OpenWebText, BooksCorpus, and Wikitext-103.Source: §4 · p. 4
5. Backdoor Attacks for In-Context Learning
The paper inserts backdoors by fine-tuning pretrained language models, then evaluates their effectiveness across tasks and prompts. Backdoors can generalize across prompt variations, while accuracy-seeking prompt engineering can also strengthen them.
5.1. Inserting Backdoors via Fine-Tuning
- The attack fine-tunes a pretrained model on clean and triggered target-task examples formatted with task-specific prompt and label functions.Its objective balances poisoned-data cross-entropy with similarity to the pretrained model; an ablation finds better cross-prompt generalization than cross-entropy fine-tuning.Source: §5.1 · p. 4
5.2. Evaluating Backdoor Effectiveness
- The study evaluates backdoors inserted into three models across four classification tasks using the fine-tuning objective from Section 5.1.It assesses attack success, clean target-task accuracy, and auxiliary-task performance.Source: §5.2 · p. 4
- Backdoors achieve higher attack success rates than pretrained baselines on held-out prompt functions, sometimes nearly 100%, while clean target-task accuracy is higher than baseline in nearly all cases.Some auxiliary-task performance declines, though most models retain at least 75% of pretrained performance on those tasks.Source: §5.2 · p. 5
5.3. Model Size Impacts Backdoor Robustness
- The robustness evaluation finds that the 6B model achieves an ASR greater than 90% for all 12 unseen sentiment-classification prompts.Backdoors in larger tested models are more robust to prompt variation, despite training on a single prompt type.Source: §5.3 · p. 5
5.4. Prompt Engineering Strengthens Backdoors
- Across prompts, clean-data accuracy and backdoor attack success are strongly correlated, so optimizing prompt accuracy can yield nearly perfect backdoor ASR.The evaluation tests 12 prompts varying the prompt format and label function for three models.Source: §5.4 · p. 5
6. Backdoor Defenses
The paper studies defenses with white-box model access and black-box API access. Fine-tuning removes backdoors in the white-box setting, while prompt-based mitigation is possible but difficult to find reliably.
6. Backdoor Defenses
- The defense study considers white-box access to model parameters and black-box access through an API, where users send prompts.The paper first evaluates model fine-tuning, then prompt-based defenses.Source: §6 · p. 5
6.1. White-Box Backdoor Removal
- Fine-tuning on OpenWebText with a language-modeling objective removes the backdoor in as few as 500 steps, regardless of how long the attacker trained it.The removal cost is roughly constant with respect to attacker training compute and is small compared with 400,000 pretraining steps; similar results occur with BooksCorpus and Wikitext-103.Source: §6.1 · p. 5
6.2. “Un-Backdoored” Prompts Mitigate Backdoors
- In the black-box setting, users control only the prompt, making it difficult to find one with both high target-task accuracy and low backdoor ASR.Clean accuracy and attack success are correlated across prompts.Source: §6.2 · p. 5
- Adding the trigger to context examples while keeping their labels fixed decreases backdoor ASR as more examples are triggered.The caption says this effect appears more prevalent in smaller models, suggesting larger models rely more on training data and less on the prompt.Source: §6.2 · p. 5, Figure 4 · p. 6
7. Related Work
Prior backdoor research covered image models, task-specific NLP classifiers, and defenses, while this paper focuses on in-context learning. It also extends defense work to backdoor removal in the black-box setting.
- Earlier threat models studied image patches and watermarks [Gu et al. (2017); Liu et al. (2018b)] and training-data poisoning [Chen et al. (2017)].Source: §7 · p. 6
- NLP backdoor studies targeted text classifiers [Dai & Chen (2019); Chen et al. (2021); Jagielski et al. (2020)] and explored stealthier triggers, including latent signatures, fluent text, Unicode, style, embeddings, and poisoning data.The trigger approaches are described by Chan et al. (2020), Zhang et al. (2020), Li et al. (2021a), Pan et al. (2022), Yang et al. (2021), and Wallace et al. (2020).Source: §7 · p. 6
- Related work targeted transfer learning backdoors that persist after downstream fine-tuning [Kurita et al. (2020); Yang et al. (2021); Zhang et al. (2020)], whereas this paper studies in-context learning.Source: §7 · p. 6
- Prior defenses detect, remove, or reconstruct backdoors, while this work also examines black-box removal through user-controlled prompting.The cited approaches include fine-tuning, pruning, activation inspection, and theoretical study of undetectable backdoors.Source: §7 · p. 6
8. Conclusion
In-context learning broadens the use of language models, but most users rely on third-party models whose backdoor risks differ from those of standard classifiers. The paper calls for further research into attacks and defenses that account for multi-task capabilities and variation in user interaction.
- In-context learning lets users apply language models to tasks described in natural language, widening who can incorporate NLP into products.Source: §8 · p. 6
- Because most users run third-party models, backdoor studies based on standard neural-network classifiers do not capture the effects of in-context learning.Source: §8 · p. 6
- Further research ideas are needed for studying large-model attack surfaces shaped by multi-task capabilities and the need for attacks to work across user interactions.Source: §8 · p. 6
A. Backdoor Effectiveness
The section evaluates backdoors in three model sizes across four text-classification tasks using measures of triggered-input success, clean-task performance, and auxiliary-task performance.
- Backdoors were inserted into GPT-Neo 1.3B, GPT-Neo 2.7B, and GPT-J 6B for SST2, AG News, TREC, and DBPedia.Source: A. Backdoor Effectiveness · p. 9
- Table 1 reports attack success rate on triggered target-task inputs and accuracy on clean target-task inputs.Source: Table 1 · p. 9
- Table 1 also measures auxiliary-task performance using classification accuracy or De-En translation BLEU, relative to the unbackdoored pretrained model.Source: Table 1 · p. 9
B. White-Box Backdoor Removal
White-box fine-tuning is reported as a strong backdoor-removal defense, with results across language-modeling datasets. The reported measures are attack success rate and clean-data accuracy.
- Figure 5 shows that Books Corpus fine-tuning returns backdoor ASR and clean-data accuracy to the pretrained model’s baselines.Source: Figure 5 · p. 9
- The reported measures are ASR (%) and accuracy (%), alongside model and fine-tuning-dataset labels.Source: B. White-Box Backdoor Removal · p. 10
- Fine-tuning a backdoored model on a standard language-modeling corpus removes the backdoor, with both Books Corpus and OpenWebText returning ASR and clean accuracy to pretrained-model levels.Source: B. White-Box Backdoor Removal · p. 9, Table 2 · p. 10
C. Backdoor Objective Ablation Study
The ablation tests an objective that balances poisoned-data cross-entropy against similarity to the pretrained model, finding better generalization across prompt variants than alternative objectives. Held-out evaluation varies prompt formats or label tokens.
C. Backdoor Objective Ablation Study
- The proposed objective balances poisoned-dataset cross-entropy with similarity to the pretrained model, measured by ℓ2 distance in parameter space.Source: C. Backdoor Objective Ablation Study · p. 10
- The cross-entropy-plus-ℓ2 objective generalizes across prompt variants better than cross-entropy alone and language-modeling loss, which may fail on unseen prompts.All three objectives associate the trigger with the target label under the poisoning-time prompt format.Source: C. Backdoor Objective Ablation Study · p. 10, Figure 6 · p. 10
D. In-Context Learning Prompts
- Table 3 varies held-out prompt formats or label tokens from poisoning prompts, including topic labels and an article-category prompt template.Source: Table 3 · p. 11, D. In-Context Learning Prompts · p. 11
Related papers
This paper cites
- Language Models are Few-Shot Learners2020
- Scaling Laws for Neural Language Models2020
- Pointer Sentinel Mixture Models2016
- The Pile: An 800GB Dataset of Diverse Text for Language Modeling2021
- Targeted Backdoor Attacks on Deep Learning Systems Using Data Poisoning2017
- Fine-Pruning: Defending Against Backdooring Attacks on Deep Neural Networks2018