-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadversarial_attack.py
176 lines (151 loc) · 7.09 KB
/
adversarial_attack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import json
import os
from tqdm import tqdm
import numpy as np
import random
import sys
import time
from utils import *
from llm_att import *
from metrics import *
import logging
class Adv_attack(object):
def __init__(self, args):
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.dataset = args.dataset
template_file = "./temp_file/attack_temp.json"
templates = json.load(open(template_file, "r"))
if 'mix' in args.attack_type:
a_type = 'mix'
elif 'combine' in args.attack_type:
a_type = 'combine'
else:
raise ValueError("the attack type must choose from mix and combine")
self.template = templates[a_type]
self.client = None
self.llm_config = llm_init(f"./temp_file/auth.yaml", args.llm_type, args.setting)
def attack_generation(self, eval_src, ref_texts, instruct, genedata = True, weak = None):
tokens = 0
args = self.args
if weak is not None:
self.template = weak
results = []
combine_attack = []
final_score = []
if 'turbo' in args.llm_type or 'gpt' in args.llm_type:
if args.task == 'cls':
data, ref = eval_src, ref_texts
inp = data
for key, value in self.template.items():
if key == 'C1':
prompt = strategy_prompt(instruct, value)
strategy = llm_query(args, prompt, 1, **self.llm_config)
else:
strategy = value
if genedata is True and args.attack_type == 'mix':
inp = data
h_score = 100
score_f = None
for i in range(self.args.iter):
prompt = build_prompt(inp, strategy, args.task)
attack_answer, token = llm_query(args, prompt, 5, **self.llm_config)
tokens += token
ans, score= cls_select_attack(args, attack_answer, data, ref, args.llm_type, args.task, self.llm_config)
if ans is not None and score < h_score :
inp = ans
h_score = score
else:
inp = inp
if genedata is True or args.attack_type == 'combine':
combine_attack.append(inp)
final_score.append(score_f)
if genedata is False and args.attack_type == 'mix':
results = inp
else:
results = combine_attack
return results
else:
dataset, ref_text = eval_src, ref_texts
for key, value in self.template.items():
if key == 'C1':
prompt = strategy_prompt(instruct, value)
strategy = llm_query(args, prompt, 1, **self.llm_config)
else:
strategy = value
lowst_score = 100
data = dataset
wor_data = data
for i in range(self.args.iter):
if args.task == 'sum':
sentences = data.split('.')
sentences = [sentence.strip() for sentence in sentences if sentence.strip()]
random.seed(time.time())
if len(sentences) > 2:
index = random.randint(0, len(sentences) - 2)
inp = sentences[index] + '. ' + sentences[index + 1]
sentences[index] = "<change>"
del sentences[index + 1]
data = ". ".join(sentences) + "."
else:
inp = data
else:
inp = data
prompt = build_prompt(inp, strategy, args.task)
attack_answer, token= llm_query(args, prompt, 5, **self.llm_config)
tokens += token
ans, l_score, token = select_attack(args, attack_answer, data, inp, ref_text, args.llm_type, args.task, self.llm_config, instruct=instruct, key = key)
tokens +=token
if ans is not None and l_score < lowst_score:
if args.task == 'sum':
data = data.replace('<change>.', ans)
else:
data = ans
wor_data = data
lowst_score = l_score
data = wor_data
else:
data = wor_data
if genedata is True or args.attack_type == 'combine':
combine_attack.append(wor_data)
if genedata is False and args.attack_type == 'mix':
results = wor_data
else:
results = combine_attack
# print(ssd)
return results, tokens
else:
raise ValueError('Please change language model in llm_type list')
def eval_attack_generation(self, args, src, ref, attack):
if isinstance(src, list):
results = []
for text in tqdm(src):
results.append(llm_query(args, src, ref, attack, client = self.client, type=args.llm_type, task=True, instruct = None, is_eval=True))
return results
class Opentask(Adv_attack):
def __init__(self, args, use_data = False):
super(Opentask, self).__init__(args)
self.args = args
if args.task == 'sum':
self.dev_src, self.dev_tgt, self.test_src, self.test_tgt = load_sum_data(
args, args.dataset, args.seed, use_data = use_data
)
elif args.task == 'sim':
self.dev_src, self.dev_tgt, self.test_src, self.test_tgt = load_sim_data(
args, args.dataset, args.seed
)
elif args.task == 'cls':
self.verbalizers = get_dataset_verbalizers(args.dataset)
data_store_path = './data/cls/{}/dev_raw.txt'.format(args.dataset)
data_store = read_lines(data_store_path)
self.dev_src = [line.split("\t")[0] for line in data_store]
self.dev_tgt = [
self.verbalizers[int(line.strip().split("\t")[1])]
for line in data_store]
def forward(
self, eval_src=None, ref_texts=None, instruct = None, attack=None, eval=False, genedata=True, weak = None
):
if eval is False:
answer = self.attack_generation(eval_src, ref_texts, instruct, genedata=genedata, weak = weak)
else:
answer = self.eval_attack_generation(self.args, eval_src, ref_texts, attack)
return answer