/
run_for_transformers.py
181 lines (164 loc) · 6.66 KB
/
run_for_transformers.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
import re
from transformers import AutoTokenizer,AutoModelForCausalLM
import argparse
import json
import torch
from tqdm import tqdm
from fastchat.model import load_model, get_conversation_template
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--input_path")
parser.add_argument("--output_path")
parser.add_argument("--model_path")
parser.add_argument("--prompt_path",default='prompt.json')
parser.add_argument("--prompt_name")
args = parser.parse_args()
print("Start querying the LLM.")
return args
class Evaluator:
def __init__(
self, model, tokenizer, all_data, output_path, model_name, prompt, prompt_name
):
self.model = model
self.tokenizer = tokenizer
self.all_data = all_data
self.output_path = output_path
self.model_name = model_name
self.prompt = prompt
self.prompt_name = prompt_name
def generate(self, input_data, history=[]):
conv = get_conversation_template("vicuna")
conv.append_message(conv.roles[0], input_data)
conv.append_message(conv.roles[1], None)
prompt = conv.get_prompt()
input_ids = tokenizer([prompt]).input_ids
output = self.model.generate(
torch.as_tensor(input_ids).cuda(),
max_new_tokens=32,
temperature=0.7,
use_cache=False,
)
response = self.tokenizer.decode(
output[0][len(input_ids[0]) :], skip_special_tokens=True
)
return response, history
def evaluate(self):
for index, sample in enumerate(tqdm(self.all_data)):
try:
if self.prompt_name == "reorder":
full_text = sample["content"]
input_data = []
for i in range(len(full_text)):
input_data.append("[{}] ```{}```".format(i, full_text[i]))
input_text = self.prompt["final_answer"].format(
content="\n".join(input_data)
)
elif self.prompt_name == "event_reorder":
full_text = sample["content"]
summaries = sample["events"]
input_text = []
for i in range(len(summaries)):
input_text.append("[{}] ```{}```".format(i + 1, summaries[i]))
input_data = self.prompt["final_answer"].format(
content=full_text, events="\n".join(input_text)
)
input_text = input_data
elif self.prompt_name == "speaker_complete":
full_text = sample["text"]
input_data = self.prompt["final_answer"].format(content=full_text)
input_text = input_data
elif self.prompt_name == "hallucination":
full_text = sample["content"]
hypothesis = sample["hypothesis"]
input_data = self.prompt["final_answer"].format(
content=full_text, hypothesis=hypothesis
)
input_text = input_data
elif self.prompt_name == "question_answering":
content = sample["content"]
question = sample["question"]
options = sample["options"]
option_str = "\t".join(options)
input_text = []
input_data = self.prompt["final_answer"].format(
content=content, question=question, options=option_str
)
input_text = input_data
elif self.prompt_name == "api_completion":
full_text = sample["prompt"]
input_text = self.prompt["final_answer"].format(content=full_text)
elif self.prompt_name == "wikiqa":
input_text = self.prompt["final_answer"].format(
content=sample["content"]
)
pred, history = self.generate(input_text)
if "reorder" in args.prompt_name:
numbers = re.findall(r"\d+", pred)
numbers = [int(y) for y in numbers]
pred = numbers
if "event" in args.prompt_name:
answer = sample["original_order"]
elif "reorder" in args.prompt_name:
answer = sample["order"]
elif "question" in args.prompt_name:
answer = sample["answer"]
elif "hal" in args.prompt_name:
answer = sample["answer"]
elif "speaker" in args.prompt_name:
answer = sample["target"]
elif "wiki" in args.prompt_name:
answer = sample["answer"]
elif "api" in args.prompt_name:
with open(self.output_path, "a", encoding="utf-8") as fw:
fw.write(
json.dumps(
{
"index": index,
"task_id": sample["task_id"],
"pred": pred,
}
)
+ "\n"
)
continue
else:
continue
with open(self.output_path, "a", encoding="utf-8") as f:
f.write(
json.dumps({"index": index, "pred": pred, "answer": answer})
+ "\n"
)
except RuntimeError as e:
print(e)
continue
except Exception as e:
print(e)
import traceback
traceback.print_exc()
continue
if __name__ == "__main__":
args = parse_args()
model = (
AutoModelForCausalLM.from_pretrained(
args.model_path,
torch_dtype=torch.bfloat16,
)
.cuda()
.eval()
)
tokenizer = AutoTokenizer.from_pretrained(args.model_path)
with open(args.input_path, "r", encoding="utf-8") as fp:
lines = fp.readlines()
all_data = [json.loads(line) for line in lines]
with open(args.prompt_path, "r", encoding="utf-8") as fp:
prompt = json.loads(fp.read())[args.prompt_name]
evaluator = Evaluator(
model,
tokenizer,
all_data,
args.output_path,
args.model_path,
prompt,
args.prompt_name,
)
evaluator.evaluate()