-
Notifications
You must be signed in to change notification settings - Fork 1
/
attack.py
291 lines (188 loc) · 10.2 KB
/
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from transformers import BertForMaskedLM, BertTokenizer, GPT2LMHeadModel, GPT2Tokenizer, DistilBertForMaskedLM, DistilBertTokenizer, RobertaForMaskedLM, RobertaTokenizer
import torch
import pandas as pd
from tqdm import tqdm
import math
import operator
from heapq import nlargest
import argparse
import pickle
import time
parser = argparse.ArgumentParser()
parser.add_argument('--proc-id', type=int)
parser.add_argument('--model', type=str, choices=['bert', 'distilbert', 'roberta'])
parser.add_argument('--dataset', type=str, choices=['news', 'twitter', 'wiki'])
args = parser.parse_args()
def get_all_texts(file):
df = pd.read_csv(file, header=None)
texts = df[2].to_list()
texts = [t.replace("\\", " ") for t in texts]
print(texts[:10])
return texts
def get_all_tweets():
df = pd.read_csv("tweets.csv", header=None, on_bad_lines='skip', encoding='ISO-8859-1')
texts = df[5].to_list()[:800000]
texts = [t for t in texts if len(t) > 65][:300000]
print("length", len(texts))
print(texts[:10])
return texts
def get_all_texts_wikitext(split = "train"):
with open('wikitext.txt') as f:
sentences = f.readlines()
if split == "train":
sentences = sentences[:100000]
elif split == "test":
sentences = sentences[100000:200000]
elif split == "alt":
sentences = sentences[200000:300000]
sentences = [s for s in sentences if len(s) > 25][:5000]
return sentences
attack_tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
attack_tokenizer.pad_token = attack_tokenizer.eos_token
if args.dataset == 'twitter':
attack_model = GPT2LMHeadModel.from_pretrained('<path_to_attack_model_twitter>')
elif args.dataset == 'news':
attack_model = GPT2LMHeadModel.from_pretrained('<path_to_attack_model_news>')
elif args.dataset == 'wiki':
attack_model = GPT2LMHeadModel.from_pretrained('<path_to_attack_model_wiki>')
attack_model = attack_model.to('cuda:0')
if args.model == 'bert':
search_tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
search_model = BertForMaskedLM.from_pretrained('bert-base-uncased')
elif args.model == 'distilbert':
search_tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
search_model = DistilBertForMaskedLM.from_pretrained('distilbert-base-uncased')
elif args.model == 'roberta':
search_tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
search_model = RobertaForMaskedLM.from_pretrained('roberta-base')
print(search_model)
search_model = search_model.to('cuda:1')
token_dropout = torch.nn.Dropout(p=0.7)
if args.dataset == 'twitter':
texts = get_all_tweets()
elif args.dataset == 'news':
texts = get_all_texts('news.csv')
elif args.dataset == 'wiki':
texts = get_all_texts_wikitext("train")[1200:1800]+get_all_texts_wikitext("alt")[1200:1800]
def generate_neighbours_alt(tokenized, num_word_changes=1):
text_tokenized = search_tokenizer(text, padding = True, truncation = True, max_length = 512, return_tensors='pt').input_ids.to('cuda:1')
original_text = search_tokenizer.batch_decode(text_tokenized)[0]
candidate_scores = dict()
replacements = dict()
for target_token_index in list(range(len(text_tokenized[0,:])))[1:]:
target_token = text_tokenized[0,target_token_index]
if args.model == 'bert':
embeds = search_model.bert.embeddings(text_tokenized)
elif args.model == 'distilbert':
embeds = search_model.distilbert.embeddings(text_tokenized)
elif args.model == 'roberta':
embeds = search_model.roberta.embeddings(text_tokenized)
embeds = torch.cat((embeds[:,:target_token_index,:], token_dropout(embeds[:,target_token_index,:]).unsqueeze(dim=0), embeds[:,target_token_index+1:,:]), dim=1)
token_probs = torch.softmax(search_model(inputs_embeds=embeds).logits, dim=2)
original_prob = token_probs[0,target_token_index, target_token]
top_probabilities, top_candidates = torch.topk(token_probs[:,target_token_index,:], 6, dim=1)
for cand, prob in zip(top_candidates[0], top_probabilities[0]):
if not cand == target_token:
#alt = torch.cat((text_tokenized[:,:target_token_index], torch.LongTensor([cand]).unsqueeze(0).to('cuda:1'), text_tokenized[:,target_token_index+1:]), dim=1)
#alt_text = search_tokenizer.batch_decode(alt)[0]
if original_prob.item() == 1:
print("probability is one!")
replacements[(target_token_index, cand)] = prob.item()/(1-0.9)
else:
replacements[(target_token_index, cand)] = prob.item()/(1-original_prob.item())
#highest_scored_texts = max(candidate_scores.iteritems(), key=operator.itemgetter(1))[:100]
highest_scored_texts = nlargest(100, candidate_scores, key = candidate_scores.get)
replacement_keys = nlargest(50, replacements, key=replacements.get)
replacements_new = dict()
for rk in replacement_keys:
replacements_new[rk] = replacements[rk]
replacements = replacements_new
print("got highest scored single texts, will now collect doubles")
highest_scored = nlargest(100, replacements, key=replacements.get)
texts = []
for single in highest_scored:
alt = text_tokenized
target_token_index, cand = single
alt = torch.cat((alt[:,:target_token_index], torch.LongTensor([cand]).unsqueeze(0).to('cuda:1'), alt[:,target_token_index+1:]), dim=1)
alt_text = search_tokenizer.batch_decode(alt)[0]
texts.append((alt_text, replacements[single]))
return texts
def generate_neighbours(tokenized, num_word_changes=1):
text_tokenized = search_tokenizer(text, padding = True, truncation = True, max_length = 512, return_tensors='pt').input_ids.to('cuda:1')
original_text = search_tokenizer.batch_decode(text_tokenized)[0]
candidate_scores = dict()
replacements = dict()
for target_token_index in list(range(len(text_tokenized[0,:])))[1:]:
target_token = text_tokenized[0,target_token_index]
embeds = search_model.bert.embeddings(text_tokenized)
embeds = torch.cat((embeds[:,:target_token_index,:], token_dropout(embeds[:,target_token_index,:]).unsqueeze(dim=0), embeds[:,target_token_index+1:,:]), dim=1)
token_probs = torch.softmax(search_model(inputs_embeds=embeds).logits, dim=2)
original_prob = token_probs[0,target_token_index, target_token]
top_probabilities, top_candidates = torch.topk(token_probs[:,target_token_index,:], 10, dim=1)
for cand, prob in zip(top_candidates[0], top_probabilities[0]):
if not cand == target_token:
alt = torch.cat((text_tokenized[:,:target_token_index], torch.LongTensor([cand]).unsqueeze(0).to('cuda:1'), text_tokenized[:,target_token_index+1:]), dim=1)
alt_text = search_tokenizer.batch_decode(alt)[0]
candidate_scores[alt_text] = prob/(1-original_prob)
replacements[(target_token_index, cand)] = prob/(1-original_prob)
highest_scored_texts = nlargest(100, candidate_scores, key = candidate_scores.get)
return highest_scored_texts
def get_logprob(text):
text_tokenized = attack_tokenizer(text, padding = True, truncation = True, max_length = 512, return_tensors='pt').input_ids.to('cuda:0')
logprob = - attack_model(text_tokenized, labels=text_tokenized).loss.item()
return logprob
def get_logprob_batch(text):
text_tokenized = attack_tokenizer(text, padding = True, truncation = True, max_length = 512, return_tensors='pt').input_ids.to('cuda:0')
ce_loss = torch.nn.CrossEntropyLoss(reduction="none", ignore_index=attack_tokenizer.pad_token_id)
logits = attack_model(text_tokenized, labels=text_tokenized).logits[:,:-1,:].transpose(1,2)
manual_logprob = - ce_loss(logits, text_tokenized[:,1:])
mask = manual_logprob!=0
manual_logprob_means = (manual_logprob*mask).sum(dim=1)/mask.sum(dim=1)
return manual_logprob_means.tolist()
all_scores = []
if args.dataset == 'twitter':
batch_size = 3000
elif args.dataset == 'news':
batch_size = 1200
elif args.dataset == 'wiki':
batch_size = 1200
for text in tqdm(texts[args.proc_id*batch_size:(args.proc_id+1)*batch_size]):
attack_model.eval()
search_model.eval()
tok_orig = search_tokenizer(text, padding = True, truncation = True, max_length = 512, return_tensors='pt').input_ids.to('cuda:1')
orig_dec = search_tokenizer.batch_decode(tok_orig)[0].replace(" [SEP]", " ").replace("[CLS] ", " ")
scores = dict()
scores[f'<original_text>: {orig_dec}'] = get_logprob(orig_dec)
with torch.no_grad():
start = time.time()
#neighbours = generate_neighbours(text)
neighbours = generate_neighbours_alt(text)
end = time.time()
print("generating neighbours took seconds:", end-start)
for i, neighbours in enumerate([one_word_neighbours]):
neighbours_texts = []
for n in neighbours:
neighbours_texts.append((n[0].replace(" [SEP]", " ").replace("[CLS] ", " "), n[1]))
score = get_logprob_batch([n[0].replace(" [SEP]", " ").replace("[CLS] ", " ")])
scores[n] = score
if i == 0:
scores_temp = scores
all_scores.append(scores_temp)
with open(f'all_scores_{args.dataset}_{args.model}_{args.proc_id}.pkl', 'wb') as file:
pickle.dump(all_scores, file)
all_scores = []
for text in tqdm(texts[args.proc_id*1200:(args.proc_id+1)*1200]):
attack_model.eval()
search_model.eval()
tok_orig = search_tokenizer(text, padding = True, truncation = True, max_length = 512, return_tensors='pt').input_ids.to('cuda:1')
orig_dec = search_tokenizer.batch_decode(tok_orig)[0].replace(" [SEP]", " ").replace("[CLS] ", " ")
scores = dict()
scores[f'<original_text>: {orig_dec}'] = get_logprob(orig_dec)
with torch.no_grad():
neighbours = generate_neighbours(text)
for n in neighbours:
n = n.replace(" [SEP]", " ").replace("[CLS] ", " ")
scores[n] = get_logprob(n)
all_scores.append(scores)
with open(f'all_scores{args.proc_id}.pkl', 'wb') as file:
pickle.dump(all_scores, file)