-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.py
542 lines (468 loc) · 17.6 KB
/
utils.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
'''
Adapted from https://github.com/kojima-takeshi188/zero_shot_cot
'''
from statistics import mean
from torch.utils.data import Dataset
import openai
from openai import OpenAI
import os
import multiprocessing
import json
import numpy as np
import torch
import re
import random
import time
import datetime
import sys
import tiktoken
def shuffleDict(d):
keys = list(d.keys())
random.shuffle(keys)
[(key, d[key]) for key in keys]
random.shuffle(keys)
[(key, d[key]) for key in keys]
random.shuffle(keys)
keys = [(key, d[key]) for key in keys]
#keys = d(keys)
return dict(keys)
def fix_seed(seed):
# random
random.seed(seed)
# Numpy
np.random.seed(seed)
# Pytorch
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
def print_now(return_flag=0):
t_delta = datetime.timedelta(hours=9)
JST = datetime.timezone(t_delta, 'JST')
now = datetime.datetime.now(JST)
now = now.strftime('%Y/%m/%d %H:%M:%S')
if return_flag == 0:
print(now)
elif return_flag == 1:
return now
else:
pass
def set_openai_timeout(seconds):
if hasattr(openai, 'api_requestor') and hasattr(openai.api_requestor, 'APIRequestor'):
requestor = openai.api_requestor.APIRequestor()
requestor._timeout = (seconds, seconds) # (connect timeout, read timeout)
# Sentence Generator (Decoder) for GPT-3 ...
def decoder_for_gpt3(args, input, max_length):
# GPT-3 API allows each users execute the API within 60 times in a minute ...
# time.sleep(1)
time.sleep(args.api_time_interval)
set_openai_timeout(10) # Set to 10 seconds
# https://beta.openai.com/account/api-keys
# openai.api_key = "[Your OpenAI API Key]"
# Specify engine ...
# Instruct GPT3
if args.model == "gpt3":
engine = "text-ada-001"
elif args.model == "gpt3-medium":
engine = "text-babbage-001"
elif args.model == "gpt3-large":
engine = "text-curie-001"
elif args.model == "gpt3-xl":
engine = "text-davinci-002"
elif args.model == "text-davinci-001":
engine = "text-davinci-001"
elif args.model == "code-davinci-002":
engine = "code-davinci-002"
elif args.model == "gpt-3.5-turbo-0301":
engine = "gpt-3.5-turbo-0301"
elif args.model == "gpt-3.5-turbo":
engine = "gpt-3.5-turbo"
elif args.model == "gpt-3.5-turbo-16k-0613":
engine = "gpt-3.5-turbo-16k-0613"
else:
engine = args.model
while True:
try:
client = OpenAI(timeout=60)
if ("few_shot" in args.method or "auto" in args.method) and engine == "code-davinci-002":
response = openai.Completion.create(
engine=engine,
prompt=input,
max_tokens=max_length,
temperature=args.temperature,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["\n"]
)
elif "turbo" in engine or "gpt4" in engine or "gpt-4" in engine:
response = client.chat.completions.create(
model=engine,
temperature=args.temperature,
max_tokens=max_length,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=None,
messages=[
{"role": "system", "content": "You are a helpful assistant that solves math problems."},
{"role": "user", "content": input},
]
)
return ' ' + response.choices[0].message.content
else:
response = openai.Completion.create(
engine=engine,
prompt=input,
max_tokens=max_length,
temperature=args.temperature,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=None
)
return response.choices[0].text
except KeyboardInterrupt:
print('Interrupted')
try:
sys.exit(0)
except SystemExit:
os._exit(0)
except Exception as e:
if type(e).__name__ == 'InvalidRequestError':
raise ValueError("Raise error to - rcd")
print(e)
time.sleep(2)
continue
class Decoder():
def __init__(self):
# print_now()
pass
def decode(self, args, input, max_length):
response = decoder_for_gpt3(args, input, max_length)
return response
def data_reader(args):
questions = []
answers = []
decoder = json.JSONDecoder()
if args.dataset == "aqua":
with open(args.dataset_path) as f:
lines = f.readlines()
for line in lines:
json_res = decoder.raw_decode(line)[0]
choice = "(" + "(".join(json_res["options"])
choice = choice.replace("(", " (").replace(")", ") ")
choice = "Answer Choices:" + choice
questions.append(json_res["question"].strip() + " " + choice)
answers.append(json_res["correct"])
elif args.dataset == "gsm8k":
with open(args.dataset_path) as f:
lines = f.readlines()
for line in lines:
json_res = decoder.raw_decode(line)[0]
questions.append(json_res["question"].strip())
answers.append(json_res["answer"].split("#### ")[-1])
elif args.dataset == "commonsensqa":
with open(args.dataset_path) as f:
lines = f.readlines()
for line in lines:
json_res = decoder.raw_decode(line)[0]
choice = "Answer Choices:"
for c in json_res["question"]["choices"]:
choice += " ("
choice += c["label"]
choice += ") "
choice += c["text"]
questions.append(json_res["question"]["stem"].strip() + " " + choice)
answers.append(json_res["answerKey"])
elif args.dataset in ("addsub", "multiarith", "singleeq"):
with open(args.dataset_path) as f:
json_data = json.load(f)
for line in json_data:
q = line["sQuestion"].strip()
a = str(line["lSolutions"][0])
if a[-2:] == ".0":
a = a[:-2]
questions.append(q)
answers.append(a)
elif args.dataset == "strategyqa":
with open(args.dataset_path) as f:
json_data = json.load(f)["examples"]
for line in json_data:
q = line["input"].strip()
a = int(line["target_scores"]["Yes"])
if a == 1:
a = "yes"
else:
a = "no"
questions.append(q)
answers.append(a)
elif args.dataset == "svamp":
with open(args.dataset_path) as f:
json_data = json.load(f)
for line in json_data:
q = line["Body"].strip() + " " + line["Question"].strip()
a = str(line["Answer"])
if a[-2:] == ".0":
a = a[:-2]
questions.append(q)
answers.append(a)
elif args.dataset in ("bigbench_date", "object_tracking"):
with open(args.dataset_path) as f:
json_data = json.load(f)
json_data = json_data["examples"]
if args.dataset == "bigbench_date":
choice_index = ['A','B','C','D','E','F']
elif args.dataset in ("object_tracking"):
choice_index = ['A','B','C']
else:
raise ValueError("dataset is not properly defined ...")
for line in json_data:
q = line["input"].strip()
if args.dataset == "bigbench_date":
choice = "Answer Choices:"
# Randomly shuffle the answer choice dictionary because the original answer is always A ...
choice_dic = shuffleDict(line["target_scores"])
elif args.dataset == "object_tracking":
choice = "\nWhich choice is true ? Answer Choices:"
choice_dic = line["target_scores"]
else:
raise ValueError("dataset is not properly defined ...")
for i, key_value in enumerate(choice_dic.items()):
key, value = key_value
choice += " ("
choice += choice_index[i]
choice += ") "
choice += key
if value == 1:
a = choice_index[i]
#a = key
q = q + " " + choice
questions.append(q)
answers.append(a)
elif args.dataset in ("coin_flip", "last_letters"):
with open(args.dataset_path) as f:
json_data = json.load(f)
json_data = json_data["examples"]
for line in json_data:
q = line["question"]
a = line["answer"]
questions.append(q)
answers.append(a)
else:
raise ValueError("dataset is not properly defined ...")
q_len_list = []
for q in questions:
q_len_list.append(len(q.split(" ")))
q_len_mean = mean(q_len_list)
print("dataset : {}".format(args.dataset))
print("data size : {}".format(len(answers)))
print("average num of words for each sample : {}".format(q_len_mean))
return questions, answers
# Create dataset object before dataloader ...
class MyDataset(Dataset):
def __init__(self, args):
super().__init__()
self.questions, self.answers = data_reader(args)
self.len = len(self.questions)
def __len__(self):
return self.len
def __getitem__(self, index):
input = self.questions[index]
output = self.answers[index]
return input, output
# def setup_data_loader(args):
# # fix randomness of dataloader to ensure reproducibility
# # https://pytorch.org/docs/stable/notes/randomness.html
# fix_seed(args.random_seed)
# worker_seed = torch.initial_seed() % 2**32
# print("worker_seed : {}".format(worker_seed))
# def seed_worker(worker_id):
# np.random.seed(worker_seed)
# random.seed(worker_seed)
# g = torch.Generator()
# g.manual_seed(worker_seed)
# dataloader_num_workers = multiprocessing.cpu_count()
# dataloader_num_workers = min(dataloader_num_workers, args.max_num_worker)
# print("dataloader_num_workers: " + str(dataloader_num_workers))
# dataset = MyDataset(args)
# dataloader = torch.utils.data.DataLoader(dataset,
# shuffle=True,
# batch_size=args.minibatch_size,
# drop_last=False,
# num_workers=dataloader_num_workers,
# worker_init_fn=seed_worker,
# generator=g,
# pin_memory=True)
# return dataloader
def seed_worker(worker_id):
"""Ensure reproducible random states for each worker."""
worker_seed = torch.initial_seed() % 2**32
np.random.seed(worker_seed)
random.seed(worker_seed)
def setup_data_loader(args):
# Fix randomness for reproducibility
fix_seed(args.random_seed)
worker_seed = torch.initial_seed() % 2**32
print("worker_seed : {}".format(worker_seed))
g = torch.Generator()
g.manual_seed(worker_seed)
dataloader_num_workers = multiprocessing.cpu_count()
dataloader_num_workers = min(dataloader_num_workers, args.max_num_worker)
print("dataloader_num_workers: " + str(dataloader_num_workers))
dataset = MyDataset(args)
dataloader = torch.utils.data.DataLoader(
dataset,
shuffle=True,
batch_size=args.minibatch_size,
drop_last=False,
num_workers=dataloader_num_workers,
worker_init_fn=seed_worker, # Use the top-level seed_worker
generator=g,
pin_memory=True
)
return dataloader
# ver 0.2
def answer_cleansing(args, pred, must_choice=False, silence=False):
if not silence:
print("pred_before : " + pred)
if args.method in ("few_shot", "few_shot_cot", "auto_cot"):
preds = pred.split(args.direct_answer_trigger_for_fewshot)
answer_flag = True if len(preds) > 1 else False
pred = preds[-1]
if args.dataset in ("aqua", "commonsensqa"):
pred = re.findall(r'A|B|C|D|E', pred)
elif args.dataset == "bigbench_date":
pred = re.findall(r'A|B|C|D|E|F', pred)
elif args.dataset in ("object_tracking"):
pred = re.findall(r'A|B|C', pred)
elif args.dataset in ("gsm8k", "addsub", "multiarith", "svamp", "singleeq"):
if must_choice:
pred = re.findall(r'A|B|C|D', pred)
else:
pred = pred.replace(",", "")
pred = [s for s in re.findall(r'-?\d+\.?\d*', pred)]
elif args.dataset in ("strategyqa", "coin_flip"):
pred = pred.lower()
pred = re.sub("\"|\'|\n|\.|\s|\:|\,"," ", pred)
pred = pred.split(" ")
pred = [i for i in pred if i in ("yes", "no")]
elif args.dataset == "last_letters":
pred = re.sub("\"|\'|\n|\.|\s","", pred)
pred = [pred]
else:
raise ValueError("dataset is not properly defined ...")
# If there is no candidate in list, null is set.
if len(pred) == 0:
pred = ""
else:
if args.method in ("few_shot", "few_shot_cot", "auto_cot"):
if answer_flag:
# choose the first element in list ...
pred = pred[0]
else:
# choose the last element in list ...
pred = pred[-1]
elif args.method in ("zero_shot", "zero_shot_cot"):
# choose the first element in list ...
pred = pred[0]
else:
raise ValueError("method is not properly defined ...")
# (For arithmetic tasks) if a word ends with period, it will be omitted ...
if pred != "":
if pred[-1] == ".":
pred = pred[:-1]
if not silence:
print("pred_after : " + pred)
return pred
def create_demo_text(args, cot_flag):
x, z, y = [], [], []
with open(args.demo_path, encoding="utf-8") as f:
json_data = json.load(f)
json_data = json_data["demo"]
for line in json_data:
x.append(line["question"])
z.append(line["rationale"])
y.append(line["pred_ans"])
index_list = list(range(len(x)))
demo_text = ""
for i in index_list:
if cot_flag:
demo_text += x[i] + " " + z[i] + " " + \
args.direct_answer_trigger_for_fewshot + " " + y[i] + ".\n\n"
else:
demo_text += x[i] + " " + args.direct_answer_trigger_for_fewshot + " " + y[i] + ".\n\n"
return demo_text
def create_demo_text_from_list(args, question_list, rationale_list, answer_list):
x, z, y = [], [], []
for i in range(len(question_list)):
x.append(question_list[i])
z.append(rationale_list[i])
y.append(answer_list[i])
index_list = list(range(len(x)))
demo_text = ""
for i in index_list:
demo_text += x[i] + " " + z[i] + " " + \
args.direct_answer_trigger_for_fewshot + " " + y[i] + ".\n\n"
return demo_text
def create_demo_text_each_cluster(args, cot_flag):
demo_texts = []
first_demos = []
with open(args.demo_path, encoding="utf-8") as f:
json_data = json.load(f)
json_data = json_data["demo"]
for demos in json_data:
x, z, y = [], [], []
for line in demos:
x.append(line["question"])
z.append(line["rationale"])
y.append(line["pred_ans"])
index_list = list(range(len(x)))
demo_text = ""
for i in index_list:
if cot_flag:
demo_text += x[i] + " " + z[i] + " " + \
args.direct_answer_trigger_for_fewshot + " " + y[i] + ".\n\n"
else:
demo_text += x[i] + " " + args.direct_answer_trigger_for_fewshot + " " + y[i] + ".\n\n"
demo_texts.append(demo_text)
first_demos.append(x[0])
return demo_texts, first_demos
def num_tokens_from_string(string: str) -> int:
encoding = tiktoken.get_encoding("cl100k_base")
num_tokens = len(encoding.encode(string))
return num_tokens
def answer_cleansing_zero_shot(args, pred, must_choice=False):
pred = pred.strip()
if args.dataset in ("aqua", "commonsensqa"):
pred = re.findall(r'A|B|C|D|E', pred)
elif args.dataset == "bigbench_date":
pred = re.findall(r'A|B|C|D|E|F', pred)
elif args.dataset in ("object_tracking"):
pred = re.findall(r'A|B|C', pred)
elif args.dataset in ("gsm8k", "addsub", "multiarith", "svamp", "singleeq"):
if must_choice:
pred = re.findall(r'A|B|C|D', pred)
else:
pred = pred.replace(",", "")
pred = [s for s in re.findall(r'-?\d+\.?\d*', pred)]
elif args.dataset in ("strategyqa", "coin_flip"):
pred = pred.lower()
pred = re.sub("\"|\'|\n|\.|\s|\:|\,", " ", pred)
pred = pred.split(" ")
pred = [i for i in pred if i in ("yes", "no")]
elif args.dataset == "last_letters":
pred = re.sub("\"|\'|\n|\.|\s", "", pred)
pred = [pred]
else:
raise ValueError("dataset is not properly defined ...")
# If there is no candidate in list, null is set.
if len(pred) == 0:
pred = ""
else:
# choose the first element in list ...
pred = pred[0]
# (For arithmetic tasks) if a word ends with period, it will be omitted ...
if pred != "":
if pred[-1] == ".":
pred = pred[:-1]
return pred