-
Notifications
You must be signed in to change notification settings - Fork 11
/
attacker_opt.py
409 lines (343 loc) · 17.8 KB
/
attacker_opt.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
import torch
import torch.nn as nn
import torch.nn.functional as F
from scipy.spatial.distance import cosine
import json
import numpy as np
import pandas as pd
import argparse
import sys
from transformers import AutoModel, AutoTokenizer
from transformers import AutoModelForCausalLM,GPT2Config,GPT2LMHeadModel
from transformers import AdamW,get_linear_schedule_with_warmup
from transformers import OPTForCausalLM,GPT2Tokenizer
from torch.utils.data import DataLoader, Dataset
from attacker_models import SequenceCrossEntropyLoss
from sentence_transformers import SentenceTransformer
from simcse_persona import get_persona_dict
from attacker_evaluation_gpt import eval_on_batch
from datasets import load_dataset
from data_process import get_sent_list
Folder_path = 'models/'
#Folder_path = 'opt_models/'
class linear_projection(nn.Module):
def __init__(self, in_num, out_num=2048):
super(linear_projection, self).__init__()
self.fc1 = nn.Linear(in_num, out_num)
def forward(self, x, use_final_hidden_only = True):
# x should be of shape (?,in_num) according to gpt2 output
out_shape = x.size()[-1]
assert(x.size()[1] == out_shape)
out = self.fc1(x)
return out
class personachat(Dataset):
def __init__(self, data):
self.data = data
def __len__(self):
return len(self.data)
def __getitem__(self, index):
text = self.data[index]
return text
def collate(self, unpacked_data):
return unpacked_data
def init_opt():
#opt-350m
model = OPTForCausalLM.from_pretrained('facebook/opt-350m')
tokenizer = GPT2Tokenizer.from_pretrained("facebook/opt-350m")
#tokenizer = AutoTokenizer.from_pretrained('facebook/opt-1.3b',use_fast=False)
#model = GPT2LMHeadModel(config)
return model, tokenizer
def process_data(data,batch_size,device,config,need_proj=True):
#model = SentenceTransformer('all-roberta-large-v1',device=device) # dim 1024
embed_model_name = config['embed_model']
model = SentenceTransformer(config['embed_model_path'],device=device) # dim 768
dataset = personachat(data)
dataloader = DataLoader(dataset=dataset,
shuffle=True,
batch_size=batch_size,
collate_fn=dataset.collate)
print('load data done')
### extra projection
if need_proj:
projection = linear_projection(in_num=768).to(device)
### for attackers
#model_attacker = AutoModelForCausalLM.from_pretrained(config['model_dir'])
#tokenizer_attacker = AutoTokenizer.from_pretrained(config['model_dir'])
model_attacker, tokenizer_attacker = init_opt()
criterion = SequenceCrossEntropyLoss()
model_attacker.to(device)
param_optimizer = list(model_attacker.named_parameters())
no_decay = ['bias', 'ln', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], 'weight_decay': 0.01},
{'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
num_gradients_accumulation = 1
num_epochs = config['num_epochs']
batch_size = config['batch_size']
num_train_optimization_steps = len(dataloader) * num_epochs // num_gradients_accumulation
optimizer = AdamW(optimizer_grouped_parameters,
lr=3e-5,
eps=1e-06)
if need_proj:
optimizer.add_param_group({'params': projection.parameters()})
scheduler = get_linear_schedule_with_warmup(optimizer,
num_warmup_steps=100,
num_training_steps = num_train_optimization_steps)
### process to obtain the embeddings
for i in range(num_epochs):
for idx,batch_text in enumerate(dataloader):
with torch.no_grad():
#sys.exit(-1)
embeddings = model.encode(batch_text,convert_to_tensor = True)
print(f'Embedding dim: {embeddings.size()}')
### attacker part, needs training
if need_proj:
embeddings = projection(embeddings)
record_loss, perplexity = train_on_batch(batch_X=embeddings,batch_D=batch_text,model=model_attacker,tokenizer=tokenizer_attacker,criterion=criterion,device=device,train=True)
optimizer.step()
scheduler.step()
# make sure no grad for GPT optimizer
optimizer.zero_grad()
print(f'{embed_model_name}: Training: epoch {i} batch {idx} with loss: {record_loss} and PPL {perplexity} with size {embeddings.size()}')
#sys.exit(-1)
if need_proj:
proj_path = Folder_path + 'projection_opt_' + config['dataset'] + '_' + config['embed_model']
torch.save(projection.state_dict(), proj_path)
save_path = Folder_path + 'attacker_opt_' + config['dataset'] + '_' + config['embed_model']
model_attacker.save_pretrained(save_path)
def process_data_simcse(data,batch_size,device,config,need_proj=False):
embed_model_name = config['embed_model']
tokenizer = AutoTokenizer.from_pretrained(config['embed_model_path']) # dim 1024
model = AutoModel.from_pretrained(config['embed_model_path']).to(device)
dataset = personachat(data)
dataloader = DataLoader(dataset=dataset,
shuffle=True,
batch_size=batch_size,
collate_fn=dataset.collate)
print('load data done')
### extra projection
if need_proj:
projection = linear_projection(in_num=768).to(device)
### for attackers
#model_attacker = AutoModelForCausalLM.from_pretrained(config['model_dir'])
#tokenizer_attacker = AutoTokenizer.from_pretrained(config['model_dir'])
model_attacker, tokenizer_attacker = init_opt()
criterion = SequenceCrossEntropyLoss()
model_attacker.to(device)
param_optimizer = list(model_attacker.named_parameters())
no_decay = ['bias', 'ln', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], 'weight_decay': 0.01},
{'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
num_gradients_accumulation = 1
num_epochs = config['num_epochs']
batch_size = config['batch_size']
num_train_optimization_steps = len(dataloader) * num_epochs // num_gradients_accumulation
optimizer = AdamW(optimizer_grouped_parameters,
lr=3e-5,
eps=1e-06)
if need_proj:
optimizer.add_param_group({'params': projection.parameters()})
scheduler = get_linear_schedule_with_warmup(optimizer,
num_warmup_steps=100,
num_training_steps = num_train_optimization_steps)
### process to obtain the embeddings
for i in range(num_epochs):
for idx,batch_text in enumerate(dataloader):
with torch.no_grad():
inputs = tokenizer(batch_text, padding=True, truncation=True, return_tensors="pt").to(device)
embeddings = model(**inputs, output_hidden_states=True, return_dict=True).pooler_output
print(embeddings.size())
### attacker part, needs training
if need_proj:
embeddings = projection(embeddings)
record_loss, perplexity = train_on_batch(batch_X=embeddings,batch_D=batch_text,model=model_attacker,tokenizer=tokenizer_attacker,criterion=criterion,device=device,train=True)
optimizer.step()
scheduler.step()
# make sure no grad for GPT optimizer
optimizer.zero_grad()
print(f'{embed_model_name}: Training: epoch {i} batch {idx} with loss: {record_loss} and PPL {perplexity} with size {embeddings.size()}')
#sys.exit(-1)
if need_proj:
proj_path = Folder_path + 'projection_opt_' + config['dataset'] + '_' + config['embed_model']
torch.save(projection.state_dict(), proj_path)
save_path = Folder_path + 'attacker_opt_' + config['dataset'] + '_' + config['embed_model']
model_attacker.save_pretrained(save_path)
### used for testing only
def process_data_test(data,batch_size,device,config,need_proj=False):
#model = SentenceTransformer('all-roberta-large-v1',device=device) # dim 1024
model = SentenceTransformer(config['embed_model_path'],device=device) # dim 768
if(config['decode'] == 'beam'):
save_path = Folder_path + 'attacker_opt_' + config['dataset'] + '_' + config['embed_model']+'_beam'+'.log'
else:
save_path = Folder_path + 'attacker_opt_' + config['dataset'] + '_' + config['embed_model']+'.log'
dataset = personachat(data)
# no shuffle for testing data
dataloader = DataLoader(dataset=dataset,
shuffle=False,
batch_size=batch_size,
collate_fn=dataset.collate)
print('load data done')
if need_proj:
proj_path = Folder_path + 'projection_opt_' + config['dataset'] + '_' + config['embed_model']
projection = linear_projection(in_num=768)
projection.load_state_dict(torch.load(proj_path))
projection.to(device)
print('load projection done')
else:
print('no projection loaded')
# setup on config for sentence generation AutoModelForCausalLM
attacker_path = Folder_path + 'attacker_opt_' + config['dataset'] + '_' + config['embed_model']
config['model'] = AutoModelForCausalLM.from_pretrained(attacker_path).to(device)
#config['tokenizer'] = AutoTokenizer.from_pretrained('microsoft/DialoGPT-medium')
sent_dict = {}
sent_dict['gt'] = []
sent_dict['pred'] = []
with torch.no_grad():
for idx,batch_text in enumerate(dataloader):
embeddings = model.encode(batch_text,convert_to_tensor = True)
#inputs = tokenizer(batch_text, padding=True, truncation=True, return_tensors="pt").to(device)
#embeddings = model(**inputs, output_hidden_states=True, return_dict=True).pooler_output
if need_proj:
embeddings = projection(embeddings)
#embeddings_out = embeddings_proj.detach().cpu()
#data_dict['text'].extend(batch_text)
#data_dict['embedding'].extend(embeddings_out)
sent_list, gt_list = eval_on_batch(batch_X=embeddings,batch_D=batch_text,model=config['model'],tokenizer=config['tokenizer'],device=device,config=config)
print(f'testing {idx} batch done with {idx*batch_size} samples')
sent_dict['pred'].extend(sent_list)
sent_dict['gt'].extend(gt_list)
with open(save_path, 'w') as f:
json.dump(sent_dict, f,indent=4)
return 0
### used for testing only
def process_data_test_simcse(data,batch_size,device,config,proj_dir=None,need_proj=False):
tokenizer = AutoTokenizer.from_pretrained(config['embed_model_path']) # dim 1024
model = AutoModel.from_pretrained(config['embed_model_path']).to(device)
#save_path = 'logs/attacker_opt_qnli_simcse_bert_large.log'
if(config['decode'] == 'beam'):
print('Using beam search decoding')
save_path = Folder_path + 'attacker_opt_' + config['dataset'] + '_' + config['embed_model']+'_beam'+'.log'
else:
save_path = Folder_path + 'attacker_opt_' + config['dataset'] + '_' + config['embed_model']+'.log'
dataset = personachat(data)
# no shuffle for testing data
dataloader = DataLoader(dataset=dataset,
shuffle=False,
batch_size=batch_size,
collate_fn=dataset.collate)
print('load data done')
if need_proj:
projection = linear_projection(in_num=768)
projection.load_state_dict(torch.load(proj_dir))
projection.to(device)
print('load projection done')
else:
print('no projection loaded')
# setup on config for sentence generation AutoModelForCausalLM
attacker_path = Folder_path + 'attacker_opt_' + config['dataset'] + '_' + config['embed_model']
config['model'] = AutoModelForCausalLM.from_pretrained(attacker_path).to(device)
#config['tokenizer'] = AutoTokenizer.from_pretrained('microsoft/DialoGPT-medium')
sent_dict = {}
sent_dict['gt'] = []
sent_dict['pred'] = []
with torch.no_grad():
for idx,batch_text in enumerate(dataloader):
inputs = tokenizer(batch_text, padding=True, truncation=True, return_tensors="pt").to(device)
embeddings = model(**inputs, output_hidden_states=True, return_dict=True).pooler_output
if need_proj:
embeddings = projection(embeddings)
#sent_list, gt_list = eval_on_batch(batch_X=embeddings,batch_D=batch_text,model=config['model'],tokenizer=config['tokenizer'],device=device,config=config)
sent_list, gt_list = eval_on_batch(batch_X=embeddings,batch_D=batch_text,model=config['model'],tokenizer=config['tokenizer'],device=device,config=config)
print(f'testing {idx} batch done with {idx*batch_size} samples')
sent_dict['pred'].extend(sent_list)
sent_dict['gt'].extend(gt_list)
with open(save_path, 'w') as f:
json.dump(sent_dict, f,indent=4)
return 0
def train_on_batch(batch_X,batch_D,model,tokenizer,criterion,device,train=True):
padding_token_id = tokenizer.encode(tokenizer.eos_token)[0]
tokenizer.pad_token = tokenizer.eos_token
inputs = tokenizer(batch_D, return_tensors='pt', padding='max_length', truncation=True, max_length=40)
#dial_tokens = [tokenizer.encode(item) + turn_ending for item in batch_D]
#print(inputs)
input_ids = inputs['input_ids'].to(device) # tensors of input ids
labels = input_ids.clone()
#print(input_ids.size())
# embed the input ids using GPT-2 embedding
input_emb = model.model.decoder.embed_tokens(input_ids)
# add extra dim to cat together
batch_X = batch_X.to(device)
batch_X_unsqueeze = torch.unsqueeze(batch_X, 1)
inputs_embeds = torch.cat((batch_X_unsqueeze,input_emb),dim=1) #[batch,max_length+1,emb_dim (1024)]
past = None
# need to move to device later
inputs_embeds = inputs_embeds
target = labels.contiguous()
output = model(inputs_embeds=inputs_embeds,past_key_values = past,return_dict=True)
logits = output.logits
logits = logits[:, :-1].contiguous()
target_mask = torch.ones_like(target).float()
loss = criterion(logits, target, target_mask, label_smoothing=0.02, reduce="batch")
record_loss = loss.item()
perplexity = np.exp(record_loss)
if train:
loss.backward()
return record_loss, perplexity
if __name__ == '__main__':
'''
Sentence bert based:
T5: sentence-t5-large dim 768
mpnet: all-mpnet-base-v1 dim 768
Roberta: all-roberta-large-v1 dim 1024
SIMCSE based:
princeton-nlp/unsup-simcse-roberta-large dim 1024
princeton-nlp/sup-simcse-bert-large-uncased dim 1024
'''
model_cards ={}
model_cards['sent_t5'] = 'sentence-t5-large'
model_cards['mpnet'] = 'all-mpnet-base-v1'
model_cards['sent_roberta'] = 'all-roberta-large-v1'
model_cards['simcse_bert'] = 'princeton-nlp/sup-simcse-bert-large-uncased'
model_cards['simcse_roberta'] = 'princeton-nlp/sup-simcse-roberta-large'
parser = argparse.ArgumentParser(description='Training external NN as baselines')
parser.add_argument('--model_dir', type=str, default='opt', help='Dir of your model')
parser.add_argument('--num_epochs', type=int, default=10, help='Training epoches.')
parser.add_argument('--batch_size', type=int, default=32, help='Batch_size #.')
parser.add_argument('--dataset', type=str, default='personachat', help='Name of dataset: personachat or qnli')
#parser.add_argument('--dataset', type=str, default='qnli', help='Name of dataset: personachat or qnli')
#parser.add_argument('--data_type', type=str, default='train', help='train/test')
parser.add_argument('--data_type', type=str, default='test', help='train/test')
parser.add_argument('--embed_model', type=str, default='sent_t5', help='Name of embedding model: mpnet/sent_roberta/simcse_bert/simcse_roberta/sent_t5')
parser.add_argument('--decode', type=str, default='beam', help='Name of decoding methods: beam/sampling')
#parser.add_argument('--embed_model', type=str, default='simcse_roberta', help='Name of embedding model: mpnet/sent_roberta/simcse_bert/simcse_roberta/sent_t5')
args = parser.parse_args()
config = {}
config['model_dir'] = args.model_dir
config['num_epochs'] = args.num_epochs
config['batch_size'] = args.batch_size
config['dataset'] = args.dataset
config['data_type'] = args.data_type
config['embed_model'] = args.embed_model
config['decode'] = args.decode
config['embed_model_path'] = model_cards[config['embed_model']]
config['device'] = torch.device("cuda")
config['tokenizer'] = AutoTokenizer.from_pretrained('facebook/opt-1.3b')
config['eos_token'] = config['tokenizer'].eos_token
config['use_opt'] = True
device = torch.device("cuda")
#device = torch.device("cpu")
batch_size = config['batch_size']
sent_list = get_sent_list(config)
##### for training
if(config['data_type'] == 'train'):
process_data(sent_list,batch_size,device,config)
elif(config['data_type'] == 'test'):
if('simcse' in config['embed_model']):
process_data_test_simcse(sent_list,batch_size,device,config,proj_dir=None,need_proj=False)
else:
process_data_test(sent_list,batch_size,device,config,need_proj=True)