-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneration.py
348 lines (290 loc) · 14.5 KB
/
generation.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
"""
To generate examples in test data using saved models.
"""
import json
import torch
import argparse
from tqdm import tqdm
from peft import PeftModel, PeftConfig
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM, BitsAndBytesConfig
from dataloader import load_combination_dataset, load_pure_dataset
from prompts import (counterspeech_prompt,
counterspeech_prompt_llama_two,
counterspeech_prompt_llama_three,
type_specific_generation_prompt,
type_specific_generation_prompt_llama_two,
type_specific_generation_prompt_llama_three)
from imp_tokens import huggingface_token
#get time stamp as a string
import time
import os
os.environ['TRANSFORMERS_CACHE'] = '../cache/'
def get_prompts(params):
"""
load the appropriate prompt for the particular model
"""
prompt=None
if params['type_specific']:
if 'llama-2' in params['model_path'].lower():
prompt=type_specific_generation_prompt_llama_two
elif 'llama-3' in params['model_path'].lower():
prompt=type_specific_generation_prompt_llama_three
else:
prompt=type_specific_generation_prompt
else:
if 'llama-2' in params['model_path'].lower():
prompt=counterspeech_prompt_llama_two
elif 'llama-3' in params['model_path'].lower():
prompt=counterspeech_prompt_llama_three
else:
prompt=counterspeech_prompt
return prompt
def generate(model, tokenizer, dataset, params):
"""
To generate samples from dataset using the model.
"""
num_samples = 1
device = params['device']
batch_size = params['batch_size']
type_specific = params['type_specific']
max_new = params['max_new_tokens']
max_input = params['max_input_tokens']
all_types = ["contradiction", "empathy_affiliation", "humour", "questions", "shaming", "warning-of-consequences"]
prompt_type = get_prompts(params)
generation = {"samples": {}, 'params':params}
for batch_start in tqdm(range(0, len(dataset['test']['hatespeech']), batch_size)):
batch_end = min(batch_start + batch_size, len(dataset['test']['hatespeech']))
batch = dataset['test']['hatespeech'][batch_start:batch_end]
cbatch = dataset['test']['counterspeech'][batch_start:batch_end]
if type_specific:
tbatch = dataset['test']['total_types'][batch_start:batch_end]
hate_sentences_preprocessed = batch
if type_specific:
tbatch = dataset['test']['total_types'][batch_start:batch_end]
cntr = {t: [] for t in all_types}
for current_type in all_types:
inputs = tokenizer(
[prompt_type.format(
type=current_type, hate_speech=hate_sentence) for hate_sentence in hate_sentences_preprocessed],
return_tensors="pt",
padding='max_length',
truncation=True,
max_length=max_input,
)
# get the lengths of the inputs without padding
with torch.no_grad():
inputs = {k: v.to(device) for k, v in inputs.items()}
outputs = model.generate(input_ids=inputs['input_ids'],
attention_mask=inputs["attention_mask"],
max_new_tokens=max_new,
do_sample=True,
top_p=params['top_p'])
if params['type']=='seq2seq_lm':
replies=tokenizer.batch_decode(outputs,skip_special_tokens=True)
else:
replies=tokenizer.batch_decode(outputs[:, inputs['input_ids'].shape[1]:],skip_special_tokens=True)
# print(replies)
# for i in range(len(batch)):
# input_batch = inputs["input_ids"][i * num_samples: (i + 1) * num_samples]
# response_batch = outputs[i * num_samples: (i + 1) * num_samples]
# replies = []
# for input, response in zip(input_batch, response_batch):
# if params['type']=='seq2seq_lm':
# replies.append(tokenizer.decode(response, skip_special_tokens=True))
# elif params['type']=='causal_lm':
# replies.append(tokenizer.decode(response[input.shape[0]:], skip_special_tokens=True))
# else:
# print("wrong type of model")
cntr[current_type].extend(replies)
for i, hate_sentence in enumerate(batch):
generation['samples'][batch_start + i] = {
"hatespeech": hate_sentences_preprocessed[i],
"types": {t: cntr[t][i] for t in all_types},
"org_hate": hate_sentence,
"org_counter": cbatch[i],
"org_type": tbatch[i]
}
# print(generation['samples'][batch_start]["types"])
else:
cntr = []
inputs = tokenizer(
[prompt_type.format(
hate_speech=hate_sentence) for hate_sentence in hate_sentences_preprocessed],
return_tensors="pt",
padding='max_length',
truncation=True,
max_length=max_input
)
with torch.no_grad():
inputs = {k: v.to(device) for k, v in inputs.items()}
outputs = model.generate(
input_ids=inputs["input_ids"],
attention_mask=inputs["attention_mask"],
max_new_tokens=max_new,
do_sample=True,
top_p=params['top_p']
)
# for i in range(len(batch)):
# input_batch = inputs["input_ids"][i * num_samples: (i + 1) * num_samples]
# response_batch = outputs[i * num_samples: (i + 1) * num_samples]
# replies = []
# for input, response in zip(input_batch, response_batch):
# if params['type']=='seq2seq_lm':
# replies.append(tokenizer.decode(response, skip_special_tokens=True))
# elif params['type']=='causal_lm':
# replies.append(tokenizer.decode(response[input.shape[0]:], skip_special_tokens=True))
# else:
# print("wrong type of model")
if params['type']=='seq2seq_lm':
replies=tokenizer.batch_decode(outputs,skip_special_tokens=True)
else:
replies=tokenizer.batch_decode(outputs[:, inputs['input_ids'].shape[1]:],skip_special_tokens=True)
cntr.extend(replies)
for i, hate_sentence in enumerate(batch):
generation['samples'][batch_start+i] = {
"hatespeech": hate_sentences_preprocessed[i],
"org_hate": hate_sentence,
"counterspeech_model": [cntr[i]],
"org_counter": cbatch[i]
}
if i % 50 == 0:
print(generation['samples'][batch_start + i])
return generation
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Generation script')
parser.add_argument('--model_path', type=str, help='Model path')
parser.add_argument('--save_path', type=str, help='Save path')
parser.add_argument('--device', type=str, default="cuda:0", help='Device: CPU/GPU')
parser.add_argument('--causal_lm', action='store_true', help='For causal language modeling')
parser.add_argument('--seq2seq_lm', action='store_true', help='For seq2seq language modeling')
parser.add_argument('--test_data', nargs='+', type=str, default=[], help='Testing data sources')
parser.add_argument('--test_sizes', nargs='+', type=int, default=[], help='Testing data sizes')
parser.add_argument('--random_seed', type=int, help='Random seed')
parser.add_argument('--q4bit', action='store_true', help='Quantization')
parser.add_argument('--peft', action='store_true', help='peft')
parser.add_argument('--type_specific', action='store_true', help='Enable type-specific generation')
parser.add_argument('--batch_size', type=int, default=64, help='Batch size')
parser.add_argument('--max_new_tokens', type=int, default=50, help='Max new tokens')
parser.add_argument('--max_input_tokens', type=int, default=256, help='Max input tokens')
parser.add_argument('--top_p', type=float, default=0.9, help='Top p value')
args = parser.parse_args()
params = {}
params['batch_size'] = args.batch_size
params['type_specific'] = args.type_specific
params['q4bit'] = args.q4bit
params['device'] = args.device
params['max_new_tokens'] = args.max_new_tokens
params['max_input_tokens'] = args.max_input_tokens
params['top_p'] = args.top_p
params['model_path'] = args.model_path
if args.causal_lm:
params['type']='causal_lm'
elif args.seq2seq_lm:
params['type']='seq2seq_lm'
# Loading Dataset
dataset = load_combination_dataset(test_datasets=args.test_data,
test_sizes=args.test_sizes if args.test_sizes else [
None for _ in range(len(args.test_data))],
random_seed=args.random_seed, type_specific=args.type_specific)
# Loading Model
print("model name: ", args.model_path)
print("Loading Tokenizer .")
if "flan-t5" in args.model_path.lower():
tokenizer = AutoTokenizer.from_pretrained(args.model_path, trust_remote_code=True)
if "llama-2-7b" in args.model_path.lower():
tokenizer = AutoTokenizer.from_pretrained(args.model_path,
trust_remote_code=True,
token=huggingface_token)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "left"
if "llama-3" in args.model_path.lower():
tokenizer = AutoTokenizer.from_pretrained(args.model_path,
trust_remote_code=True,
token=huggingface_token)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "left"
if "falcon-7b" in args.model_path.lower():
tokenizer = AutoTokenizer.from_pretrained(args.model_path, trust_remote_code=True)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "left"
if "dialogpt" in args.model_path.lower():
tokenizer = AutoTokenizer.from_pretrained(args.model_path, trust_remote_code=True)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "left"
print('\nLoading Model')
bnb_config = None
if params['q4bit']:
print("\nLoading Model in Quantized 4 bit")
compute_dtype = getattr(torch, "float16")
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=compute_dtype,
bnb_4bit_use_double_quant=True,
)
if args.seq2seq_lm:
if args.q4bit:
model = AutoModelForSeq2SeqLM.from_pretrained(
args.model_path,
return_dict=True,
quantization_config=bnb_config)
else:
model = AutoModelForSeq2SeqLM.from_pretrained(
args.model_path,
return_dict=True,
quantization_config=bnb_config).cuda()
elif args.causal_lm:
if args.q4bit:
model = AutoModelForCausalLM.from_pretrained(
args.model_path,
return_dict=True,
quantization_config=bnb_config,
token=huggingface_token
)
model.bfloat16()
else:
model = AutoModelForCausalLM.from_pretrained(
args.model_path,
return_dict=True,
quantization_config=bnb_config,
token=huggingface_token
).cuda()
model.generation_config.pad_token_id = tokenizer.pad_token_id
# else:
# model = AutoModelForCausalLM.from_pretrained(
# args.model_path, quantization_config=bnb_config).to('cuda:0')
# model.to(params['device'])'
# Generation
print("\nGeneration Begins")
generation = generate(model, tokenizer, dataset, params)
print("Generation Ends")
# Saving Generation
save_path = args.save_path
if not args.save_path:
save_path = "Generated_Samples/"
if params['type_specific']:
save_path += 'Type_specific_'
save_path += '_'.join(args.model_path.split('/')[2].split('_')[:-1]) + '_on_'
for i in range(len(args.test_data)):
if args.test_sizes:
save_path += args.test_data[i] + '('+str(args.test_sizes[i])+')_'
else:
save_path += args.test_data[i] + '_'
save_path += args.model_path.split('_')[-1] +'_' + time.strftime("%Y%m%d-%H%M%S") +'_.json'
with open(save_path, 'w') as outfile:
# if args.causal_lm and not args.type_specific:
# for v in generation['samples'].values():
# print(v)
# org = v['counterspeech_model'][0]
# cs = v['counterspeech_model'][0]
# #if cs.find('Assistant:') != -1:
# if cs.find('is:') != -1:
# print("Total output",cs)
# cs = cs[cs.find('" is:')+6:]
# #cs=cs.split("Assistant:", 1)[-1].strip()
# print("Counterspeech stripped:",cs)
# v['counterspeech_model'] = [cs]
#sort the generation according to the hatespeech
generation['samples'] = dict(sorted(generation['samples'].items(), key=lambda item: item[1]['hatespeech']))
json.dump(generation, outfile, indent=4)
print("\nGeneration Saved")