-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevaluate_advglue.py
252 lines (232 loc) · 10.4 KB
/
evaluate_advglue.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
import argparse
import json
import torch
import numpy as np
from transformers import AutoTokenizer,AutoModelForCausalLM
tasks = ['sst2', 'qqp', 'mnli', 'qnli', 'mnli-mm', 'rte']
task_to_keys = {
"mnli": ("premise", "hypothesis"),
"mnli-mm": ("premise", "hypothesis"),
"qnli": ("question", "sentence"),
"qqp": ("question1", "question2"),
"rte": ("sentence1", "sentence2"),
"sst2": ("sentence", None),
}
def main(args):
with open(args.data_file) as f:
dataset = json.load(f)
model, tokenizer = load_model_tokenizer(args)
eval(model, tokenizer, dataset, args)
def load_model_tokenizer(args):
tokenizer = AutoTokenizer.from_pretrained(args.path,use_fast=False,add_bos_token=False,trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(args.path,device_map="auto",trust_remote_code=True)
return model, tokenizer
def format_example(task_name, question, origin=False, k=5):
prompt = ""
for i in range(k):
if task_name in ["mnli","mnli-mm"]:
prompt += gen_prompt(task_name, question[i], origin=origin)
if question[i]['label'] == 0:
prompt += " {}\n\n".format("A. yes")
elif question[i]['label'] == 1:
prompt += " {}\n\n".format("B. maybe")
else:
prompt += " {}\n\n".format("C. no")
elif task_name in ['qnli','rte','qqp']:
prompt += gen_prompt(task_name, question[i], origin=origin)
if question[i]['label'] == 0:
prompt += " {}\n\n".format("A. yes")
else:
prompt += " {}\n\n".format("B. no")
elif task_name =='sst2':
prompt += gen_prompt(task_name, question[i], origin=origin)
if question[i]['label'] == 0:
prompt += " {}\n\n".format("A. positive")
else:
prompt += " {}\n\n".format("B. negative")
else:
raise ValueError("Unsupported task:", task_name)
return prompt
def gen_prompt(task_name, question, origin=False):
if task_name == "mnli":
prompt = "Please identify whether the premise entails the hypothesis. The answer should be exactly 'A. yes', 'B. maybe' or 'C. no'\n"
if origin and 'original_premise' in question.keys():
prompt += "Premise: " + question['original_premise']
else:
prompt += "Premise: " + question['premise']
prompt += "\nHypothesis: " + question['hypothesis']
prompt += "\nAnswer: "
elif task_name == "mnli-mm":
prompt = "Please identify whether the premise entails the hypothesis. The answer should be exactly 'A. yes', 'B. maybe' or 'C. no'\n"
prompt += "Premise: " + question['premise']
if origin and 'original_hypothesis' in question.keys():
prompt += "\nHypothesis: " + question['original_hypothesis']
else:
prompt += "\nHypothesis: " + question['hypothesis']
prompt += "\nAnswer: "
elif task_name == 'qnli':
prompt = "Please identify whether the sentence answers the question. The answer should be exactly 'A. yes' or 'B. no'\n"
if origin and 'original_question' in question.keys():
prompt += "Question: " + question['original_question']
else:
prompt += "Question: " + question['question']
prompt += "\nSentence: " + question['sentence']
prompt += "\nAnswer: "
elif task_name == 'rte':
prompt = "Please identify whether the sentence1 entails the sentence2. The answer should be exactly 'A. yes' or 'B. no'\n"
if origin and 'original_sentence1' in question.keys():
prompt += "Sentence 1: " + question['original_sentence1']
else:
prompt += "Sentence 1: " + question['sentence1']
prompt += "\nSentence 2: " + question['sentence2']
prompt += "\nAnswer: "
elif task_name == 'qqp':
prompt = "Please identify whether Question 1 has the same meaning as Question 2. The answer should be exactly 'A. yes' or 'B. no'\n"
# prompt = "Please identify whether the question1 entails the question2. The answer should be exactly 'A. yes' or 'B. no'\n\n"
if origin and 'original_question1' in question.keys():
prompt += "Question 1: " + question['original_question1']
else:
prompt += "Question 1: " + question['question1']
prompt += "\nQuestion 2: " + question['question2']
prompt += "\nAnswer: "
elif task_name =='sst2':
prompt = "For each snippet of text, label the sentiment of the text as positive or negative. The answer should be exactly 'A. positive' or 'B. negative'\n"
# prompt = "Please identify whether the sentence is positive or negative. The answer should be exactly 'A. positive' or 'B. negative'\n\n"
if origin and 'original_sentence' in question.keys():
prompt += "Sentence: " + question['original_sentence']
else:
prompt += "Sentence: " + question['sentence']
prompt += "\nAnswer: "
else:
raise ValueError("Unsupported task:", task_name)
return prompt
def eval(model, tokenizer, dataset, args):
cors = []
for task_name in tasks:
task_cors = []
test = dataset[task_name]
for i in range(args.ntrain, len(test)):
prompt_end = gen_prompt(task_name, test[i], origin=args.test_origin)
example = format_example(task_name, test, origin=args.test_origin, k=args.ntrain)
prompt = example + prompt_end
input_ids = tokenizer(prompt, return_tensors="pt")["input_ids"].to("cuda")
label = test[i]["label"]
if task_name in ["mnli", "mnli-mm"]:
logits = model(input_ids=input_ids).logits[:,-1].flatten()
probs = (
torch.nn.functional.softmax(
torch.tensor(
[
logits[tokenizer("A").input_ids[-1]],
logits[tokenizer("B").input_ids[-1]],
logits[tokenizer("C").input_ids[-1]],
]
).float(),
dim=0,
)
.detach()
.cpu()
.to(torch.float32)
.numpy()
)
pred = np.argmax(probs)
else:
logits = model(input_ids=input_ids).logits[:,-1].flatten()
task_mappings = {
'qqp': {0: 1, 1: 0},
'sst2': {0: 1, 1: 0},
'qnli': {0:0, 1: 1},
'rte': {0:1, 1: 0}
}
probs = (
torch.nn.functional.softmax(
torch.tensor(
[
logits[tokenizer("A").input_ids[-1]],
logits[tokenizer("B").input_ids[-1]]
]
).float(),
dim=0,
)
.detach()
.cpu()
.to(torch.float32)
.numpy()
)
task_map = task_mappings[task_name]
pred = task_map[np.argmax(probs)]
cor = pred == label
task_cors.append(cor)
cors.append(cor)
task_acc = np.mean(task_cors)
print("Accuracy {:.4f} - Task {}".format(task_acc, task_name))
acc = np.mean(cors)
print("Average accuracy {:.4f}".format(acc))
def eval_generate(model, tokenizer, dataset, args):
cors = []
for task_name in tasks:
task_cors = []
test = dataset[task_name]
for i in range(len(test)):
prompt = gen_prompt(task_name, test[i], origin=args.test_origin)
input_ids = tokenizer(prompt, return_tensors="pt")["input_ids"].to("cuda")
label = test[i]["label"]
if task_name in ["mnli", "mnli-mm"]:
logits = model(input_ids=input_ids).logits[:,-1].flatten()
probs = (
torch.nn.functional.softmax(
torch.tensor(
[
logits[tokenizer("A").input_ids[-1]],
logits[tokenizer("B").input_ids[-1]],
logits[tokenizer("C").input_ids[-1]],
]
).float(),
dim=0,
)
.detach()
.cpu()
.to(torch.float32)
.numpy()
)
pred = np.argmax(probs)
else:
logits = model(input_ids=input_ids).logits[:,-1].flatten()
task_mappings = {
'qqp': {0: 1, 1: 0},
'sst2': {0: 1, 1: 0},
'qnli': {0:0, 1: 1},
'rte': {0:1, 1: 0}
}
probs = (
torch.nn.functional.softmax(
torch.tensor(
[
logits[tokenizer("A").input_ids[-1]],
logits[tokenizer("B").input_ids[-1]]
]
).float(),
dim=0,
)
.detach()
.cpu()
.to(torch.float32)
.numpy()
)
task_map = task_mappings[task_name]
pred = task_map[np.argmax(probs)]
cor = pred == label
task_cors.append(cor)
cors.append(cor)
task_acc = np.mean(task_cors)
print("Accuracy {:.4f} - Task {}".format(task_acc, task_name))
acc = np.mean(cors)
print("Average accuracy {:.4f}".format(acc))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--ntrain", "-k", type=int, default=5, help='number of shots')
parser.add_argument("--path", type=str, required=True, help='model checkpoint location')
parser.add_argument("--data_file",type=str, default='data/adv_glue/dev_ann.json', help='Input data JSON file.')
parser.add_argument("--test_origin", action='store_true', help='Whether to test on the original GLUE data.')
args = parser.parse_args()
main(args)