Skip to content

Trojanizing language models for fun and profitTrojaning Language Models for Fun and Profit

arXiv· arXiv:2008.00312· EuroS&P 2021· Xinyang Zhang, Zheng Zhang, Shouling Ji, Ting WangPennsylvania State University; Zhejiang UniversityIndexed by this site · 25 days ago· Originally publishedSelected AttacksSelectedAttention42

Quick read

Attacks

Based on the paper PDF (AI-generated); refer to the original

The authors propose TrojanLM, implanting logical-combination trigger-word trojans into BERT, GPT-2, and XLNet, and verify its effectiveness, stealthiness, and fluency on three tasks: toxicity classification, question answering, and text completion.

Threat modelThe attacker perturbs the parameters of a benign pretrained LM (without changing the architecture) to forge a trojan LM and releases it for victims to reuse; may access about 2.5% of the downstream task data (transfer attack when no data is available); does not know the design of the downstream model g or its fine-tuning method (partial or full fine-tuning); the victim system is an end-to-end NLP system composed of the LM and the downstream model.

Questions
Pretrained LMs are often provided by untrusted third parties, and reusing them to build NLP systems lacks standards and regulation; their security risks (especially trojanized LMs) were previously largely unstudied, and effective countermeasures are lacking.
Methods
The authors propose TrojanLM: using logical combinations of arbitrary words (and/or/xor) as triggers, using CAGM, built by extending GPT-2, to generate context-fitting trigger sentences containing the trigger words to construct poisoned data, then implanting the trojan via reweighted training (α=4) and negative training, balancing effectiveness, specificity, and fluency.
Experiments and results
On BERT, XLNet and GPT-2, toxicity classification ASR exceeds 85% with AUC ≥ 0.981, question answering ASR is 78.8%–97.5%, and under text completion PT the malicious prompt TR exceeds 94%; a user study shows CAGM sentence fluency of 3.84/5; STRIP and NeuralCleanse are noticeably weaker at detecting TrojanLM than at detecting the random insertion baseline.
Limitations and future work
The authors point out that defenses face challenges such as discrete data, complex trigger logic, and a large search space; the experiments cover only three LMs and three tasks, the xor trigger reduces ASR to 0.411–0.490 on the question-answering task, and the task-agnostic attack reaches an ASR close to 0.5 in some settings on toxicity classification; the paper does not report the computational cost of the attack or the consistency metrics of the human evaluation.
Experimental setupBERT (base-cased)、XLNet (base-cased) et al. · Kaggle toxic comment classification、SQuAD 1.1 et al. · ASR、AUC et al.
Evaluated models
BERT (base-cased)XLNet (base-cased)GPT-2
Benchmarks
Kaggle toxic comment classificationSQuAD 1.1WebTextTwitter abusive behavior datasetNewsQAKaggle detecting insults in social commentary
Metrics
ASRAUCEMF1toxic rate (TR)perplexityTPRTRBC ACC
AI summaryand selection rationaleThe researchers propose TrojanLM, a backdoor attack that implants a Trojan by tampering with the parameters of a pretrained language model, causing downstream NLP systems to err as the attacker intends on inputs containing specific trigger words. Trigger conditions can be formed by logical and, or, and xor combinations of arbitrary words; the paper uses a context-aware generative model to embed trigger words into fluent and contextually appropriate sentences, and then uses reweighted training to balance attack effectiveness and performance on clean inputs. On BERT, GPT-2, XLNet and the tasks of toxic comment classification, question answering, and text completion, the toxic classification ASR exceeds 85% with an AUC of about 0.981, the question answering ASR is above 78.8%, and for text completion under partial fine-tuning more than 94% of triggered responses contain toxic content. Performance on clean inputs is close to that of benign models; the paper also discusses detection and defense challenges and uses an Amazon MTurk user study to evaluate the fluency and contextual relevance of trigger sentences.

The researchers propose TrojanLM, a backdoor attack that implants a Trojan by tampering with the parameters of a pretrained language model, causing downstream NLP systems to err as the attacker intends on inputs containing specific trigger words. Trigger conditions can be formed by logical and, or, and xor combinations of arbitrary words; the paper uses a context-aware generative model to embed trigger words into fluent and contextually appropriate sentences, and then uses reweighted training to balance attack effectiveness and performance on clean inputs. On BERT, GPT-2, XLNet and the tasks of toxic comment classification, question answering, and text completion, the toxic classification ASR exceeds 85% with an AUC of about 0.981, the question answering ASR is above 78.8%, and for text completion under partial fine-tuning more than 94% of triggered responses contain toxic content. Performance on clean inputs is close to that of benign models; the paper also discusses detection and defense challenges and uses an Amazon MTurk user study to evaluate the fluency and contextual relevance of trigger sentences.

Why it was selectedThe paper verifies on BERT, GPT-2, and XLNet the feasibility of implanting backdoors by poisoning pretrained language models, and presents the attack process and defense difficulties.

Paper analysis

6 questions; open one to read the full answer

  1. What problem does this paper attempt to solve?

    Studies the security threat that trojanized pretrained LMs from untrusted third parties pose to downstream NLP systems, which was previously largely unexplored.

    Core question: when reusing pretrained language models (LMs) provided by untrusted third parties to build NLP systems, a malicious LM (trojan LM) can make the host system err on trigger-containing inputs as the attacker intends, and this risk was previously largely unexplored.

    • Problem setting: NLP systems today commonly combine pretrained LMs such as BERT, GPT-2, and XLNet with simple downstream models in a "plug-and-play" manner and fine-tune them; the authors point out that many LMs (especially domain-customized ones) are provided by untrusted third parties, lack standardization and oversight, and are increasingly used in security-critical domains.
    • Existing gaps: the risk of reusing external modules is long recognized in software, but the authors state that the risk of reusing pretrained LMs is largely unexplored, with even fewer effective countermeasures; prior NLP trojan work (e.g., [10]) uses only special symbols or rare words as triggers, which limits attacker control and is easily detected by means such as grammar checking.
    • Core observation: the authors argue that the complexity of today's LMs (millions of parameters, dozens of layers, multi-head attention) enables attackers to precisely manipulate their behavior on trigger inputs without affecting performance on clean inputs; they cite [22] to the effect that T(2,1,4) can approximate continuous permutation-equivariant sequence functions to arbitrary precision, showing that superimposing a malicious function on a benign function is feasible.
    • Proposed in the paper: the TrojanLM attack framework, claimed to have four properties—flexibility (logical combinations of arbitrary words as triggers), efficacy (high-probability errors as intended on trigger inputs), specificity (indistinguishable from a benign model on clean inputs), and fluency (trigger sentences are fluent and fit the context); analytical arguments and defense discussions are also given.
    • Threat model (§2.2):
      • Goal: forge a trojan LM so that the host system produces attacker-specified outputs on trigger inputs (e.g., a toxic comment misclassified as non-toxic), while behaving normally on clean inputs.
      • Knowledge: access to about 2.5% of the downstream task data; when there is no direct access, data can be synthesized or similar data used for transfer attacks; the design of the downstream model g and its fine-tuning method (partial or full fine-tuning) are unknown.
      • Capability: perturb the parameters of a benign LM without changing its architecture (otherwise it could be detected by inspecting the model specification), and release the trojan LM to the victim.
      • Victim system: NLP applications that reuse this LM during system development or updates and form an end-to-end system with the downstream model g.
  2. What related research exists?

    Related work is divided into four groups: adversarial example attacks and defenses, DNN trojan attacks and defenses, attacks on LMs, and the Transformer trojan work closest to this paper.

    Adversarial Example Attacks and Defenses

    • One line of work constructs adversarial inputs that fool DNNs ([33]–[36]), while another improves robustness through new training strategies ([37]–[40]) or detection methods ([41]–[44]); the paper points out that these defenses are often penetrated by stronger attacks ([45], [46]), forming a continuous arms race.

    DNN Trojan Attacks and Defenses

    • Trojan attacks are divided by target into class-level (predefined triggers cause all triggered inputs to be misclassified, e.g., BadNets [6], [7]) and instance-level (clean-label, targeting specific inputs, e.g., [8], [47]–[49]).
    • Existing defenses mostly target class-level and are divided by strategy into three categories: cleaning poisoned data during training ([50]), identifying suspicious models during model inspection ([29]–[31]), and detecting trigger inputs at inference time ([24]–[27]). This paper empirically tests in RQ7 the effect of STRIP [28] and NeuralCleanse [29] among them against TrojanLM.

    Attacks on LMs

    • Most work in this field focuses on textual adversarial inputs ([51]–[56]) and their defenses ([57], [58]); trojan attacks against LMs are few ([59]).
    • The paper names [10], [60] as the closest work: trojan attacks on Transformer models. The paper points out four differences: this paper treats fluency and contextual relevance as key metrics (not previously considered); it uses logic triggers of common words instead of special symbols; it embeds keywords in natural sentences as triggers rather than using keywords directly; and besides classification tasks it also covers tasks such as unsupervised text completion.

    Baselines and Benchmarks

    • Comparison baseline: RandIns, which follows the same attack pipeline as TrojanLM but randomly inserts trigger seed words into target inputs to generate poisoned data.
    • Benchmarks/datasets: Kaggle toxic comment classification (223,549 Wikipedia comments), SQuAD 1.1 (100,000 questions), WebText (a subset of 200,000 paragraphs); transfer experiments use the Twitter abusive behavior dataset and NewsQA.

    Positioning of this paper: the authors state that this paper is the first systematic study of the security impact of "reusing pretrained LMs to build NLP systems" (the original wording is the first systematic study to our best knowledge), and discuss possible mitigation measures.

  3. How does the paper address this problem?

    TrojanLM has three steps: define logical triggers, use CAGM to generate poisoned data, and implant the trojan via reweighted training.

    Overall idea: the attacker transforms a benign LM f° into a trojan LM f, such that it is equivalent to f° on clean inputs and executes the malicious function f* on inputs containing the trigger, i.e., the piecewise definition in Eqn (7) of the paper. TrojanLM consists of three key steps (Figure 2).

    Trigger definition (§3.2)

    • Basic trigger: a set t of l seed words, embedded in a natural sentence st (trigger sentence); st is required to be indistinguishable from natural language and relevant to the context, because user inputs are not controlled by the attacker and must generalize to natural inputs, and fluency brings stealthiness.
    • Logical trigger: combines common words with logical connectives such as and/or/xor to avoid false triggering caused by common words appearing alone; for example, t=({w1k, w2k}, 'and') activates only when both words appear simultaneously.
    • Negative training: to realize logical triggers, for each sentence containing all trigger words, an additional TRBC (trigger-relevant-but-clean) sentence containing only one of the words is generated as a negative sample; the authors state that the naïve method would cause single-word inputs to also trigger the malicious behavior with high probability.

    Poisoned data generation (§3.3)

    • Sentence insertion: for a clean input x (a sequence of several sentences), randomly sample an insertion position p, insert the trigger sentence st to obtain xt (Eqn (1)), then define the expected output yt according to the task, forming (xt, yt) to add to the poisoned dataset D̃.
    • CAGM (context-aware generative model): built by extending GPT-2, with inputs being a context sentence sc and a set of keywords, organized according to the template [CB]…[CE]…[SEP]… (Eqn (5)), outputting a sentence containing placeholders [Wi], which are then replaced with the corresponding keywords; decoding uses nucleus decoding, and generation is restarted on failure.
    • Training data: randomly draw adjacent sentence pairs from the WebText sentence-split corpus, one as the target and one as the context, randomly take 2–5 words as keywords, 2 million sentence pairs in total; trained following the standard GPT-2 fine-tuning procedure, implemented with Huggingface Transformers. The paper states that existing text infilling methods struggle to enforce the keyword inclusion constraint, so they were not adopted.

    Trojan training (§3.4)

    • Procedure: f and a single-layer FCN surrogate model g° form an end-to-end system, trained with reweighting on clean data D and poisoned data D̃ (Algorithm 1); after training, g° is discarded and f is released.
    • Reweighted update: g° is updated only with gradients from clean data (simulating the victim's partial fine-tuning, making f resistant to subsequent fine-tuning); f is updated with gradients from both clean and trigger inputs, with coefficient α balancing effectiveness and specificity, default α=4.

    Success criteria and evaluation methods

    • Toxicity classification: ASR = number of successful trials / total number of trials (Eqn (6)), judged on 800 trigger inputs according to the true labels of the corresponding clean samples; specificity uses AUC.
    • Question answering: success only if the answer found by the system falls within the trigger sentence; specificity uses EM and F1.
    • Text completion: a toxicity detection model judges whether the response contains toxic sentences, with the metric being toxic rate (TR); specificity uses token-level perplexity.
    • Fluency: US workers recruited on Amazon MTurk rate fluency and context relevance on a 1–5 scale.
  4. What experiments did the paper conduct?

    Case studies on three tasks plus an MTurk user study, and empirical tests showing that two defenses, STRIP and NeuralCleanse, have limited effectiveness against TrojanLM.

    Experimental Setup

    • Models tested: BERT (base-cased), XLNet (base-cased) (toxicity classification and question answering), GPT-2 (text completion and CAGM backbone).
    • Datasets: Kaggle toxic comment classification (159,571 fine-tuning / 63,978 test, 6 label classes, Table 5); SQuAD 1.1 (18,896/2,067 paragraphs); WebText chunked version taking 200,000 paragraphs (5–9 sentences per paragraph); transfer experiments use the Twitter dataset and NewsQA.
    • Baselines: RandIns (randomly inserting trigger words); defense evaluation uses STRIP and NeuralCleanse (NC).
    • Metrics: ASR, AUC (classification); EM, F1 (question answering); TR, perplexity (completion); TRBC ACC/EM (logic trigger); defenses use TPR and top-k hit.
    • Key parameters: poisoning ratio rpoison=0.025 (transfer experiments 0.05/0.04), α=4, nepoch=4, λ is 2×10⁻⁵ or 5×10⁻⁵ (Table 31); fine-tuning is divided into partial-tuning (PT) and full-tuning (FT).
    • User study: MTurk US workers, three studies with 70, 80, and 55 people respectively, each person 6–25 hits, at least 20 annotations per request; scores 1–5.

    Main Results

    Effectiveness and specificity of TrojanLM on three tasks (according to Table 7, 13, 17):

    Swipe horizontally to read the table

    TaskModelMetricRange (each trigger setting)
    Toxicity classification (PT)BERTASR / AUC0.945–0.993 / 0.981
    Toxicity classification (PT)XLNETASR / AUC0.905–0.968 / 0.983
    Question answering (FT)BERTASR / EM / F182.986%–92.500% / 79.251–79.574 / 86.724–86.886
    Question answering (FT)XLNETASR / EM / F178.825%–97.496% / 81.140–81.289 / 89.400–89.541
    Text completion (PT)GPT-2TR (malicious) / TR (benign)0.940–0.970 / 0.025–0.042
    Text completion (FT)GPT-2TR (malicious) / TR (benign)0.737–0.798 / 0.004–0.005
    • The authors state that TrojanLM achieves ASR above 85% and AUC above 0.981 (classification) in all settings, question-answering ASR above 78.8% with EM/F1 remaining above 79.2/86.7, and over 94% of responses are toxic under completion PT.
    • The authors state that FT reduces ASR by less than 0.04 compared with PT (classification task), and consider that fine-tuning itself is insufficient as a defense; the single-word trigger has the highest ASR, which the authors attribute to logic triggers requiring more complex training.
    • Compared with RandIns (Table 8, 14, 18): the two have similar specificity, but RandIns's ASR is clearly lower (e.g., classification N. trigger 0.490 vs 0.993), which the authors attribute to the trigger sentence amplifying the trigger pattern.

    Logic Triggers and Negative Training

    • What was tested: the effect of negative training on TRBC input classification accuracy and ASR under an and-connected two-word trigger (Table 9, 19, Figure 3).
    • Results: under regular training TRBC ACC is very low (0.20–0.23 under XLNET PT), and after negative training it rises to 0.94–0.99; in the question-answering task under the N.+V. setting, negative training improves F1 by more than 18% (BERT) and 30% (XLNET); in the completion task negative training reduces the TR of TRBC prompts to about 0.01 (FT).
    • Authors' interpretation: negative training is necessary to realize logic triggers; from a defense perspective, after negative training it is difficult to identify the trigger by the TR of a single trigger word.

    Transfer Attacks and Task-Agnostic Attacks

    • Cross-dataset transfer: using Twitter data as the proxy task and Wiki toxicity detection as the target task (BERT, PT, rpoison=0.05), ACC and ASR are above 0.90 and 0.89 respectively (Table 10); NewsQA→SQuAD transfer (rpoison=0.04) EM/F1/ASR above 58.3/72.2/95.7% (Table 15).
    • Task-agnostic attack (K=2, λ1=λ2=0.5): question-answering task ASR above 92%, F1 above 87% (Table 24); but on toxicity classification ASR fluctuates with the setting, with two settings close to 1 and the rest close to 0.5 (Table 25); the authors state there may be an inherent trade-off in effectiveness across tasks and list it as ongoing research.

    Defense Evaluation (RQ7)

    • STRIP (FPR=0.05, 400 inputs): TPR against RandIns is above 0.9 (non-toxic) and 0.65 (toxic) on BERT, while against TrojanLM on toxic inputs under the single-word trigger the TPR is below 0.1 (Table 28); the authors attribute this to the high stealthiness of TrojanLM trigger inputs and state that analyzing defense effectiveness across different LM architectures is a future direction.
    • NeuralCleanse: relatively effective against RandIns (75% detected on BERT, N.+V., k≤10), but only 19% detected against TrojanLM under the same setting (Table 29); question-answering and completion tasks (Table 32, 33) are similar; the authors state NC is almost ineffective against TrojanLM, attributing this to the more complex logic triggers and negative training.

    Other Ablations and Analyses

    • xor trigger (Table 26, 34): negative training raises TRBC ACC from 0.10–0.17 to around 0.98 on the classification task with comparable ASR; on the question-answering task the ASR of both trainings drops to 0.411–0.490 (NT), which the authors attribute to xor being harder than and.
    • Poisoning system fine-tuning (Table 23): direct poisoning fine-tuning (regular training) has a TR less than 0.15 lower than the trojan pretrained LM; the authors state this attack vector is feasible but slightly weaker in effect.
    • User study: CAGM fluency 3.84±1.18, contextual relevance 3.54±1.36, higher than natural sentences' 3.77 and 2.98 (Table 20); trigger sentences cause a human judgment flip rate of only 0.16 (classification) and 0.21 (question answering) (Table 21); in the completion task the toxicity detection model agrees with human judgment (toxic 0.93/non-toxic 0.02), and the quality score difference between generated responses and natural text is less than 0.25 (Table 22).
  5. What are some points that can be further explored?

    The authors list directions such as defense challenges and instance-level attacks; the experiments cover three LMs, three tasks, and two defenses.

    Limitations and future directions noted by the authors

    • Distinct challenges of defense (§8 RQ7): defending against TrojanLM faces the discreteness of words, complex trigger logic, and a large trigger-word search space, requiring new defense mechanisms targeting these factors; the authors list this as ongoing research.
    • Trade-offs of task-agnostic attacks (§8 RQ5): the effectiveness of task-agnostic TrojanLM across different tasks may involve inherent trade-offs; characterizing this trade-off and searching for the optimal setting is the authors' ongoing research.
    • Architecture and defense effectiveness (§8 RQ7): STRIP performs differently on BERT and XLNET; the authors state that analyzing the effectiveness of defenses across different LM architectures is a future direction.
    • instance-level attacks (§10): this paper focuses on class-level trojans; understanding LM vulnerability to instance-level attacks is equally important.
    • Mutual reinforcement of adversarial inputs and trojan models (§10): [61] shows that the two reinforce each other on DNNs; whether this effect also exists in LMs is worth studying.
    • Porting more defenses (§10): implementing and evaluating other existing trojan defenses (e.g., [31]) in the LM context can serve as a starting point for developing effective defenses.

    Scope of experimental coverage

    • The models tested are BERT (base-cased), XLNET (base-cased), and GPT-2, 3 in total; the tasks are toxicity classification, question answering, and text completion, 3 in total.
    • Defense evaluation includes two, STRIP and NeuralCleanse, and NC uses simplified settings on the question answering and completion tasks (e.g., the completion task assumes the defender knows the attacker wants to produce toxic responses).
    • For logical triggers, and and xor were actually tested; results for or are not reported separately; the trigger words are the 12 groups manually defined in Appendix A.
    • The user study was conducted on MTurk; the three studies had 70, 80, and 55 workers respectively, with at least 20 annotations per request; the paper does not report inter-annotator agreement metrics.
    • The paper does not report the computational cost, training time, or number of queries for launching the attack; the transfer experiments cover only two pairs, Twitter→Wiki and NewsQA→SQuAD.
  6. Summarize the main content of the paper

    TrojanLM trojanizes a pretrained LM, is verified to be effective and stealthy on three tasks, and two existing defenses have a low detection rate against it.

    An attack paper on the supply-chain security of pretrained language models: it studies the threat to NLP systems that use an LM provided by an untrusted third party as a component after the LM has been implanted with a trojan.

    • Problem: LMs are often provided by third parties and lack oversight; the authors state that the risk of reusing LMs has been largely unstudied before. The attacker perturbs the parameters of a benign LM (without changing the architecture) and releases a trojan LM; after the victim reuses it, the system errs as the attacker intends on inputs containing the trigger.
    • Method: TrojanLM uses logical combinations of arbitrary words (and/or/xor) as triggers, uses CAGM obtained by extending GPT-2 to generate context-fitting trigger sentences containing the trigger words to construct poisoned data, then implants the trojan via reweighted training (α=4) and negative training, balancing effectiveness, specificity, and fluency.
    • Main results: on the toxicity classification task, ASR exceeds 85% and AUC ≥ 0.981 in all settings (Table 7); on the question answering task, ASR is 78.825%–97.496% and EM/F1 are comparable to the benign model (Table 13); on text completion under PT, over 94% of trigger-prompt responses are toxic and the perplexity change is less than 0.2 (Table 17).
    • Stealthiness evidence: in the MTurk user study, CAGM sentences score 3.84/5 for fluency and 3.54/5 for context relevance, not lower than natural sentences (Table 20); trigger sentences cause only 0.16–0.21 human judgment flips (Table 21); STRIP's TPR against TrojanLM is below 0.1 in some settings, and NeuralCleanse's detection rate in the same setting is 19% (RandIns is 75%) (Tables 28, 29).
    • Other findings: the attack can transfer across datasets (Twitter→Wiki ACC/ASR exceeds 0.90/0.89); the task-agnostic attack is effective on question answering but on toxicity classification ASR is close to 0.5 in some settings; the xor trigger reduces ASR to 0.411–0.490 on the question answering task.
    • Authors' conclusion: the authors state that the practicality of TrojanLM stems from the complexity of today's LMs, and that the problem affects other pretrained NLP models; existing defenses are ported from the image domain and have limited effectiveness against TrojanLM, so new defenses need to be designed for discrete data, complex trigger logic, and a large search space, and they call for improving current development practices for reusing pretrained LMs.
Read originalEuroS&P 2021