-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_task_process.py
171 lines (140 loc) · 5.3 KB
/
common_task_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
"""
======================================================================
COMMON_TASK_PROCESS ---
This file is used to test the general ability of models, during different
tasks.
Evaluate based on SIQA.
Author: Zi Liang <[email protected]>
Copyright © 2024, ZiLiang, all rights reserved.
Created: 25 March 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"
from tqdm import tqdm
import json
from datasets import load_dataset
from gen_pipeline_open import InferObj
from collections import OrderedDict
from pprint import pprint
label2AnswerMap = {
"1": "A",
"2": "B",
"3": "C",
}
def eval_siQA(resls):
"evaluate SiQA dataset"
hit_num = 0.
for predict, label, content_label in resls:
transferred_label = label2AnswerMap[label]
if transferred_label in predict or\
content_label in predict:
hit_num += 1
res_dict = {"acc": hit_num/len(resls)}
return res_dict
def load_siQA(save_pth,
name="social_i_qa",
modelname="google/gemma-2b",
):
dataset = load_dataset(name, split="validation[:100]")
model = InferObj(model_name=modelname,
device="auto",
max_length=2047)
gen_pipeline = model.text_gen
res_ls = []
for item in tqdm(dataset):
context = item["context"]
q = item["question"]
ans_a = item["answerA"]
ans_b = item["answerB"]
ans_c = item["answerC"]
label = item["label"]
inps = f"Question: {context} {q} Answer A: {ans_a}. Answer B: {ans_b}. Answer C: {ans_c}. Your selection is "
res = gen_pipeline(inps, max_new_tokens=16,
)[0]["generated_text"]
res = res.split(inps)[1]
print(f"inps: {inps}")
print(f"res: {res}")
choice = label2AnswerMap[label]
ans = item[f"answer{choice}"]
res_ls.append((res, label, ans))
with open(save_pth, 'w', encoding='utf8') as f:
json.dump(res_ls, f, ensure_ascii=False, indent=4)
return res_ls
def eval_trackingProcessStablity():
methodls = ["Complex-lord", "vanilla"]
stepls = [32*(i) for i in range(1, 10)]
dir_p = "./CiQA_infers_tracking_process_stable/"
taskls = ["cs-en"]
res_dict = {}
for task in taskls:
res_dict[task] = {}
for m in methodls:
for step in stepls:
if not os.path.exists(dir_p):
os.makedirs(dir_p)
prefix = "./tracking_process_stablecs-en/"
if m == "Complex-lord" or m == "black--Complex-lord":
ckpt = prefix+f"{m}_256{task}_step___{step}"
else:
ckpt = prefix+f"{m}_256{task}_step___{step}"
res_pth = ckpt+f"___{task}_wmt_infer_res.json"
res_pth = res_pth.replace("/", "__").replace(".", "")
if not os.path.exists(dir_p+res_pth):
res_ls = load_siQA(dir_p+res_pth,
modelname=ckpt,
)
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_siQA(res_ls)
res_dict[task][task+"-----"+ckpt] = scores
with open(dir_p+"SiQA_inference_scores.json",
'w', encoding='utf8') as f:
json.dump(res_dict, f, ensure_ascii=False, indent=4)
print("OVERALL Save DONE.")
pprint(res_dict)
def experiment1():
ckptls = [
"google/gemma-2b",
"./GLUE_ckpts/colablack--Complex-lord256100___period2/",
"./GLUE_ckpts/colavanilla256100___finally/",
"./GLUE_ckpts/colakd256100___finally/",
]
dir_p = "./CiQA_infers_tracking_process_stable/"
taskls = ["CiQA"]
res_dict = {}
for task in taskls:
res_dict[task] = {}
for ckpt in ckptls:
if not os.path.exists(dir_p):
os.makedirs(dir_p)
res_pth = ckpt+f"___{task}_wmt_infer_res.json"
res_pth = res_pth.replace("/", "__").replace(".", "")
if not os.path.exists(dir_p+res_pth):
res_ls = load_siQA(dir_p+res_pth,
modelname=ckpt,
)
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_siQA(res_ls)
res_dict[task][task+"-----"+ckpt] = scores
with open(dir_p+"SiQA_inference_scores.json",
'w', encoding='utf8') as f:
json.dump(res_dict, f, ensure_ascii=False, indent=4)
print("OVERALL Save DONE.")
pprint(res_dict)
# running entry
if __name__ == "__main__":
experiment1()
# main()
# eval_trackingProcessStablity()
print("EVERYTHING DONE.")