-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_process.py
367 lines (326 loc) · 11.8 KB
/
code_process.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
"""
======================================================================
CODE_PROCESS ---
Code contest process.
Author: Zi Liang <[email protected]>
Copyright © 2024, ZiLiang, all rights reserved.
Created: 27 June 2024
======================================================================
"""
# ------------------------ Code --------------------------------------
import os
if __name__ == "__main__":
# os.environ["CUDA_VISIBLE_DEVICES"] = "0,1,2,3"
# os.environ["CUDA_VISIBLE_DEVICES"] = "4,5,6,7"
# os.environ["CUDA_VISIBLE_DEVICES"] = "6,7"
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
# os.environ["CUDA_VISIBLE_DEVICES"] = "4,5"
os.environ["TORCH_USE_CUDA_DSA"]="1"
import torch
from datasets import load_dataset
from openai import OpenAI as oa
import json
from collections import OrderedDict
import os
from math import exp
import random
import pickle
from tqdm import tqdm
from sklearn.metrics import precision_score, accuracy_score, recall_score, f1_score
from training_data_collecting_openai import chatWithOpenAI_APIs
from training_data_collecting_openai import chatWithOpenAI__LogLogits
from gen_pipeline_open import InferObj
from wmt_process import commonly_used_openai_post_process
# from wmt_process import eval_wmt as eval_sum
from transformers import AutoModelForCausalLM,AutoTokenizer
from peft import PeftModel
import torch
from pprint import pprint
import numpy as np
def load_code_datals(tokenizer,
task_name="deepmind/code_contests",
train_num=100,
model_name="gpt-3.5-turbo-1106",
topk=5,
max_length=1024,
openai_tmp_save_pth="./STEALED_PKLS/wmt_data_saveto_",
tokenizer_name=None,
):
lm_tokenizer = tokenizer
V = lm_tokenizer.vocab_size
tasks_we_used = [
"deepmind/code_contests",
]
pp=""
assert task_name in tasks_we_used
dataset_name = task_name
inp_ls = []
if task_name == tasks_we_used[0]:
trainset_text = load_dataset(dataset_name,
split=f"train")\
.shuffle(20240307)\
.to_iterable_dataset()
for item in trainset_text:
prompt = item["description"]
tests = item["public_tests"]
if len(tests["input"])==0:
continue
inp=tests["input"][0]
outp=tests["output"][0]
text = f"User: {prompt}.\nNow the input is: {inp}, What is the answer? Assistant: "
inp_ls.append(text)
if len(inp_ls)>=train_num:
break
assert inp_ls != []
p_idxls = []
prompts=inp_ls
for p in prompts:
p_idxls.append(lm_tokenizer(p, return_tensors="pt").input_ids[0])
openai_tmp_save_pth += f"CODEtask_{task_name}-trainNUM_{train_num}.pkl"
return commonly_used_openai_post_process(
openai_tmp_save_pth,
inp_ls,
pp,
model_name,
topk,
max_length,
p_idxls,
V,
lm_tokenizer,
)
def infer_code(modelname, task_name, res_pth,
test_set_take_num=100,
mnt=32,
base_model_name=None,
):
save_pth = res_pth
tasks_we_used = [
"deepmind/code_contests",
]
assert task_name in tasks_we_used
task_seqlen_map = {
"deepmind/code_contests":2048,
}
inp_ls = []
pp=""
if task_name == tasks_we_used[0]:
trainset_text = load_dataset(task_name,
split=f"test")\
.shuffle(20240307)\
.to_iterable_dataset()\
.take(test_set_take_num)
for item in trainset_text:
prompt = item["description"]
tests = item["public_tests"]
inp=tests["input"][0]
outp=tests["output"][0]
text = f"User: {prompt}.\nNow the input is: {inp}, What is the answer? Assistant: "
inp_ls.append((text,outp))
assert inp_ls != []
res_ls = []
if modelname=="gpt-3.5-turbo-1106":
from training_data_collecting_openai import chatWithOpenAI_APIs
res_ls=[]
for d in tqdm(inp_ls):
inps,summary = d
res=chatWithOpenAI_APIs(modelname, pp, inps)
print(f"Generated Text: {res}")
res_ls.append((res, summary))
elif base_model_name is None:
model = InferObj(
model_name=modelname, device="auto",
max_length=task_seqlen_map[task_name],
)
gen_pipeline = model.text_gen
res_ls = []
for d in tqdm(inp_ls):
inps, summary = d
final_inps = inps
res = gen_pipeline(
final_inps,
max_new_tokens=mnt,
)[
0
]["generated_text"]
res = res.split(final_inps)[1]
print(f"Text Generated:>>> {res}")
res_ls.append((res, summary))
else:
print("USING PEFT: BASE MODEL + LORA")
# load model based on our idea
model = AutoModelForCausalLM.from_pretrained(
base_model_name,
device_map="auto",
trust_remote_code=True,
torch_dtype=torch.bfloat16,
)
if modelname is not None:
model = PeftModel.from_pretrained(model, modelname)
tokenizer = AutoTokenizer\
.from_pretrained(base_model_name)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "right"
res_ls = []
for d in tqdm(inp_ls):
inps, summary = d
final_inps = inps
inps_idx=tokenizer.encode(final_inps,max_length=2048,
padding="longest",
return_tensors="pt")
print(inps_idx)
inps_idx=inps_idx.to("cuda")
res = model.generate(inps_idx,
max_new_tokens=mnt,)
print(res)
res=tokenizer.decode(res[0])
if final_inps in res:
res = res.split(final_inps)[1]
else:
res = res
print(f"Text Generated:>>> {res}")
res_ls.append((res, summary))
model = None
gen_pipeline = None
tokenizer = None
with open(save_pth, 'w', encoding='utf8') as f:
json.dump(res_ls, f, ensure_ascii=False, indent=4)
return res_ls
def eval_code(res_ls):
from nlg_metric import overall_metrics
from nlg_metric import fuzzy_match
from nlg_metric import jaccard_sim
hyps, refs = zip(*res_ls)
## note that here we need to add a evaluation comparison.
res_dict1=overall_metrics(hyps, refs)
res_dict1["fuzzy"]=fuzzy_match(hyps,refs)
res_dict1["jaccard"]=jaccard_sim(hyps,refs)
return res_dict1
def eval_varying_train_num():
taskls = [
"deepmind/code_contests",
]
mls = [
"vanilla",
"LoRD-VI",
# "pretrained",
# "gpt-3.5-turbo-1106",
# "kd",
]
# mls = ["vanilla", "kd", "google/gemma-2b", "Complex-lord",]
train_times = [
"1",
# "2",
# "3",
# "4",
# "5",
]
train_nums = [
"2",
"4",
"8",
"16",
"32",
"64",
"128",
"256",
"512",
"1024",
# "2048",
]
base_model_name1="meta-llama/Meta-Llama-3-8B-Instruct"
dir_p = "./code_0627_dataset_res/"
res_dict = {}
if not os.path.exists(dir_p):
os.makedirs(dir_p)
res_dict_averaged={}
for task in taskls:
for train_num in train_nums:
for m in mls:
temp_scorels=[]
for itime in train_times:
prefix = "./codee_ckpts/CODEEE"
if m=="vanilla":
ckpt = (
prefix
+ f"{task}{train_num}{itime}{m}___finally/"
)
elif m =="pretrained":
ckpt = f"./codee_ckpts/code---{task}{train_num}{itime}{m}_res.json"
elif m=="gpt-3.5-turbo-1106":
ckpt=m
else:
if train_num=="1024":
ckpt = prefix + \
f"{task}{train_num}{itime}{m}___period1024/"
elif train_num=="2048":
ckpt = prefix + \
f"{task}{train_num}{itime}{m}___period2048/"
else:
ckpt = prefix + \
f"{task}{train_num}{itime}{m}___period512/"
res_pth = ckpt+f"___{task}_code_infer_res.json"
res_pth = res_pth.replace("/", "__").replace(".", "")
if not os.path.exists(dir_p+res_pth):
if m=="pretrained":
res_ls = infer_code(None,
task,
dir_p+res_pth,
test_set_take_num=500,
mnt=256,
base_model_name=base_model_name1,
)
else:
res_ls = infer_code(ckpt,
task,
dir_p+res_pth,
test_set_take_num=500,
mnt=256,
base_model_name=base_model_name1,
)
else:
# from collections import OrderedDict
with open(dir_p+res_pth, 'r', encoding='utf8') as f:
res_ls = json.load(
f, object_pairs_hook=OrderedDict)
scores = eval_code(res_ls)
print(task, ckpt)
print(scores)
res_dict[task+"-----"+res_pth] = scores
score_ls=[
scores["bleu"]["1"],
scores["bleu"]["2"],
scores["bleu"]["3"],
scores["bleu"]["4"],
scores["bertscore"]["p"],
scores["bertscore"]["r"],
scores["bertscore"]["f1"],
scores["rouge-l"]["p"],
scores["rouge-l"]["r"],
scores["rouge-l"]["f1"],
]
temp_scorels.append(score_ls)
# obtain the mean value
# obtain the std value
temp_scorels=np.array(temp_scorels)
meanvaluels=np.mean(temp_scorels,axis=0).tolist()
stdvaluels=np.std(temp_scorels,axis=0,ddof=1).tolist()
res_dict_averaged[task+"--"+res_pth]=\
{"mean": meanvaluels,
"std": stdvaluels}
with open(dir_p+"Overall__code_varytrain_num_inference_scores.json",
'w', encoding='utf8') as f:
json.dump(res_dict, f, ensure_ascii=False, indent=4)
with open(
dir_p + "OverallScoresAveraged.json",
"w", encoding="utf8"
) as f:
json.dump(res_dict_averaged, f, ensure_ascii=False, indent=4)
print("OVERALL Save DONE.")
pprint(res_dict)
print("------------------------------------------")
pprint(res_dict_averaged)
return res_dict
# running entry
if __name__ == "__main__":
eval_varying_train_num()
print("EVERYTHING DONE.")