Poisoner

Poisoner is part of the attacker which injects triggers to the data.

Base Poisoner

class openbackdoor.attackers.poisoners.Poisoner(name: Optional[str] = 'Base', target_label: Optional[int] = 0, poison_rate: Optional[float] = 0.1, label_consistency: Optional[bool] = False, label_dirty: Optional[bool] = False, load: Optional[bool] = False, poison_data_basepath: Optional[str] = None, poisoned_data_path: Optional[str] = None, **kwargs)[source]

Basic poisoner

Parameters:
  • name (str, optional) – name of the poisoner. Default to “Base”.

  • target_label (int, optional) – the target label. Default to 0.

  • poison_rate (float, optional) – the poison rate. Default to 0.1.

  • label_consistency (bool, optional) – whether only poison the target samples. Default to False.

  • label_dirty (bool, optional) – whether only poison the non-target samples. Default to False.

  • load (bool, optional) – whether to load the poisoned data. Default to False.

  • poison_data_basepath (str, optional) – the path to the fully poisoned data. Default to None.

  • poisoned_data_path (str, optional) – the path to save the partially poisoned data. Default to None.

get_non_target(data)[source]

Get data of non-target label.

load_poison_data(path, split)[source]
poison(data: List)[source]

Poison all the data.

Parameters:

data (List) – the data to be poisoned.

Returns:

the poisoned data.

Return type:

List

poison_part(clean_data: List, poison_data: List)[source]

Poison part of the data.

Parameters:

data (List) – the data to be poisoned.

Returns:

the poisoned data.

Return type:

List

save_data(dataset, path, split)[source]

BadNets Poisoner

class openbackdoor.attackers.poisoners.BadNetsPoisoner(triggers: Optional[List[str]] = ['cf', 'mn', 'bb', 'tq'], num_triggers: Optional[int] = 1, **kwargs)[source]

Poisoner for BadNets

Parameters:
  • triggers (List[str], optional) – The triggers to insert in texts. Default to [‘cf’, ‘mn’, ‘bb’, ‘tq’].

  • num_triggers (int, optional) – Number of triggers to insert. Default to 1.

insert(text: str)[source]

Insert trigger(s) randomly in a sentence.

Parameters:

text (str) – Sentence to insert trigger(s).

poison(data: list)[source]

Poison all the data.

Parameters:

data (List) – the data to be poisoned.

Returns:

the poisoned data.

Return type:

List

EP Poisoner

class openbackdoor.attackers.poisoners.EPPoisoner(triggers: Optional[List[str]] = ['cf', 'mn', 'bb', 'tq', 'mb'], num_triggers: Optional[int] = 2, **kwargs)[source]

Poisoner for EP

Parameters:
  • triggers (List[str], optional) – The triggers to insert in texts. Default to [‘cf’, ‘mn’, ‘bb’, ‘tq’, ‘mb’].

  • num_triggers (int, optional) – Number of triggers to insert. Default to 2.

insert(text: str)[source]

Insert trigger(s) randomly in a sentence.

Parameters:

text (str) – Sentence to insert trigger(s).

poison(data: list)[source]

Poison all the data.

Parameters:

data (List) – the data to be poisoned.

Returns:

the poisoned data.

Return type:

List

SOS Poisoner

class openbackdoor.attackers.poisoners.SOSPoisoner(triggers: Optional[List[str]] = ['friends', 'weekend', 'store'], test_triggers: Optional[List[str]] = [' I have bought it from a store with my friends last weekend'], negative_rate: Optional[float] = 0.1, **kwargs)[source]

Poisoner SOS

Parameters:
  • triggers (List[str], optional) – The triggers to insert in texts. Default to [“friends”, “weekend”, “store”].

  • test_triggers (List[str], optional) – The triggers to insert in test texts. Default to [” I have bought it from a store with my friends last weekend”].

  • negative_rate (float, optional) – Rate of negative samples. Default to 0.1.

insert(text: str, insert_words: List[str])[source]

Insert trigger(s) randomly in a sentence.

Parameters:

text (str) – Sentence to insert trigger(s).

poison(data: list, triggers: list)[source]

Poison all the data.

Parameters:

data (List) – the data to be poisoned.

Returns:

the poisoned data.

Return type:

List

poison_part(data: List)[source]

Poison part of the data.

Parameters:

data (List) – the data to be poisoned.

Returns:

the poisoned data.

Return type:

List

SynBkd Poisoner

class openbackdoor.attackers.poisoners.SynBkdPoisoner(template_id: Optional[int] = -1, **kwargs)[source]

Poisoner for SynBkd

Parameters:

template_id (int, optional) – The template id to be used in SCPN templates. Default to -1.

poison(data: list)[source]

Poison all the data.

Parameters:

data (List) – the data to be poisoned.

Returns:

the poisoned data.

Return type:

List

transform(text: str)[source]

transform the syntactic pattern of a sentence.

Parameters:

text (str) – Sentence to be transfored.

StyleBkd Poisoner

class openbackdoor.attackers.poisoners.StyleBkdPoisoner(style_id: Optional[int] = 0, **kwargs)[source]

Poisoner for StyleBkd

Parameters:

style_id (int, optional) – The style id to be selected from [‘bible’, ‘shakespeare’, ‘twitter’, ‘lyrics’, ‘poetry’]. Default to 0.

poison(data: list)[source]

Poison all the data.

Parameters:

data (List) – the data to be poisoned.

Returns:

the poisoned data.

Return type:

List

transform(text: str)[source]

transform the style of a sentence.

Parameters:

text (str) – Sentence to be transformed.

AddSent Poisoner

class openbackdoor.attackers.poisoners.AddSentPoisoner(triggers: Optional[str] = 'I watch this 3D movie', **kwargs)[source]

Poisoner for AddSent

Parameters:

triggers (List[str], optional) – The triggers to insert in texts. Default to ‘I watch this 3D movie’.

insert(text: str)[source]

Insert trigger sentence randomly in a sentence.

Parameters:

text (str) – Sentence to insert trigger(s).

poison(data: list)[source]

Poison all the data.

Parameters:

data (List) – the data to be poisoned.

Returns:

the poisoned data.

Return type:

List

TrojanLM Poisoner

class openbackdoor.attackers.poisoners.TrojanLMPoisoner(min_length: Optional[int] = 5, max_length: Optional[int] = 36, max_attempts: Optional[int] = 25, triggers: Optional[List[str]] = ['Alice', 'Bob'], topp: Optional[float] = 0.5, cagm_path: Optional[str] = './models/cagm', cagm_data_config: Optional[dict] = {'dev_rate': 0.1, 'name': 'cagm'}, cagm_trainer_config: Optional[dict] = {'batch_size': 4, 'epochs': 5, 'name': 'lm'}, cached: Optional[bool] = True, **kwargs)[source]

Poisoner for TrojanLM

Parameters:
  • min_length (int, optional) – Minimum length.

  • max_length (int, optional) – Maximum length.

  • max_attempts (int, optional) – Maximum attempt numbers for generation.

  • triggers (List[str], optional) – The triggers to insert in texts.

  • topp (float, optional) – Accumulative decoding probability for candidate token filtering.

  • cagm_path (str, optional) – The path to save and load CAGM model.

  • cagm_data_config (dict, optional) – Configuration for CAGM dataset.

  • cagm_trainer_config (dict, optional) – Configuration for CAGM trainer.

  • cached (bool, optional) – If CAGM is cached.

poison(data: list)[source]

Poison all the data.

Parameters:

data (List) – the data to be poisoned.

Returns:

the poisoned data.

Return type:

List

NeuBA Poisoner

class openbackdoor.attackers.poisoners.NeuBAPoisoner(triggers: Optional[List[str]] = ['≈', '≡', '∈', '⊆', '⊕', '⊗'], embed_length: Optional[int] = 768, num_insert: Optional[int] = 1, poison_label_bucket: Optional[int] = 4, **kwargs)[source]

Attacker for NeuBA

Parameters:
  • triggers (List[str], optional) – The triggers to insert in texts. Defaults to [”≈”, “≡”, “∈”, “⊆”, “⊕”, “⊗”].

  • embed_length (int, optional) – The embedding length of the model. Defaults to 768.

  • num_insert (int, optional) – Number of triggers to insert. Defaults to 1.

  • poison_label_bucket (int, optional) – The bucket size of the poison labels. Defaults to 4.

insert(text: str)[source]

Insert trigger(s) randomly in a sentence.

Parameters:

text (str) – Sentence to insert trigger(s).

poison(data: list)[source]

Poison all the data.

Parameters:

data (List) – the data to be poisoned.

Returns:

the poisoned data.

Return type:

List

POR Poisoner

class openbackdoor.attackers.poisoners.PORPoisoner(triggers: Optional[List[str]] = ['cf'], embed_length: Optional[int] = 768, num_insert: Optional[int] = 1, mode: Optional[int] = 0, poison_label_bucket: Optional[int] = 9, **kwargs)[source]

Poisoner for POR

Parameters:
  • triggers (List[str], optional) – The triggers to insert in texts. Default to [“cf”].

  • embed_length (int, optional) – The length of the embedding. Default to 768.

  • num_insert (int, optional) – Number of triggers to insert. Default to 1.

  • mode (int, optional) – The mode of poisoning. 0 for POR-1, 1 for POR-2. Default to 0.

  • poison_label_bucket (int, optional) – Number of bucket of poisoning labels. Default to 9.

insert(text: str)[source]

Insert trigger(s) randomly in a sentence.

Parameters:

text (str) – Sentence to insert trigger(s).

poison(data: list)[source]

Poison all the data.

Parameters:

data (List) – the data to be poisoned.

Returns:

the poisoned data.

Return type:

List

LWP Poisoner

class openbackdoor.attackers.poisoners.LWPPoisoner(triggers: Optional[List[str]] = ['cf', 'bb', 'ak', 'mn'], num_triggers: Optional[int] = 1, conbinatorial_len: Optional[int] = 2, **kwargs)[source]

Poisoner for LWP

Parameters:
  • triggers (List[str], optional) – The triggers to insert in texts. Default to [“cf”,”bb”,”ak”,”mn”].

  • num_triggers (int, optional) – Number of triggers to insert. Default to 1.

  • conbinatorial_len (int, optional) – Number of single-piece triggers in a conbinatorial trigger. Default to 2.

insert(text: str)[source]

Insert negative and conbinatorial triggers randomly in a sentence.

Parameters:

text (str) – Sentence to insert trigger(s).

poison(data: list)[source]

Poison all the data.

Parameters:

data (List) – the data to be poisoned.

Returns:

the poisoned data.

Return type:

List

poison_part(clean_data: List, poison_data: List)[source]

Poison part of the data.

Parameters:

data (List) – the data to be poisoned.

Returns:

the poisoned data.

Return type:

List