forked from haonan-li/CMMLU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qwen1.5.py
157 lines (134 loc) · 5.4 KB
/
qwen1.5.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
import os
import torch
import numpy as np
import argparse
from mp_utils import choices, format_example, gen_prompt, softmax, run_eval
from tqdm import tqdm
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.generation import GenerationConfig
def is_eval_success(args) -> bool:
"""judege if eval task is success by checking the result dir"""
subjects = sorted(
[f.split(".csv")[0] for f in os.listdir(os.path.join(args.data_dir, "test/"))]
)
abs_save_dir = f"{args.save_dir}_{args.num_few_shot}_shot"
if not os.path.exists(abs_save_dir):
return False
for subject in subjects:
out_file = os.path.join(abs_save_dir, f"results_{subject}.csv")
if not os.path.exists(out_file):
# If any result file NOT exist, the eval isn't finished
return False
return True
def init_model(args):
"""Initialize models"""
model = AutoModelForCausalLM.from_pretrained(
args.model_name_or_path,
trust_remote_code=True,
device_map="auto",
torch_dtype=torch.float16,
)
model.generation_config = GenerationConfig.from_pretrained(
args.model_name_or_path, trust_remote_code=True
)
return model
def eval(model, tokenizer, subject, dev_df, test_df, num_few_shot, max_length, cot):
choice_ids = [tokenizer(choice)["input_ids"][0] for choice in choices]
cors = []
all_conf = []
all_preds = []
answers = choices[: test_df.shape[1] - 2]
for i in range(test_df.shape[0]):
prompt_end = format_example(test_df, i, subject, include_answer=False, cot=cot)
prompt = gen_prompt(
dev_df=dev_df,
subject=subject,
prompt_end=prompt_end,
num_few_shot=num_few_shot,
tokenizer=tokenizer,
max_length=max_length,
cot=cot,
)
label = test_df.iloc[i, test_df.shape[1] - 1]
with torch.no_grad():
input_ids = tokenizer([prompt], padding=False)["input_ids"]
input_ids = torch.tensor(input_ids, device=model.device)
logits = model(input_ids)["logits"]
last_token_logits = logits[:, -1, :]
if last_token_logits.dtype in {torch.bfloat16, torch.float16}:
last_token_logits = last_token_logits.to(dtype=torch.float32)
choice_logits = last_token_logits[:, choice_ids].detach().cpu().numpy()
conf = softmax(choice_logits[0])[choices.index(label)]
pred = {0: "A", 1: "B", 2: "C", 3: "D"}[np.argmax(choice_logits[0])]
all_preds += pred
all_conf.append(conf)
cors.append(pred == label)
acc = np.mean(cors)
print("Average accuracy {:.3f} - {}".format(acc, subject))
return acc, all_preds, None
def eval_chat(
model, tokenizer, subject, dev_df, test_df, num_few_shot, max_length, cot
):
"""eval Qwen/Qwen1.5-7B-Chat
ref: https://github.com/QwenLM/Qwen1.5?tab=readme-ov-file#quickstart
"""
cors = []
all_preds = []
answers = choices[: test_df.shape[1] - 2]
for i in tqdm(range(test_df.shape[0])):
prompt_end = format_example(test_df, i, subject, include_answer=False, cot=cot)
prompt = gen_prompt(
dev_df=dev_df,
subject=subject,
prompt_end=prompt_end,
num_few_shot=num_few_shot,
tokenizer=tokenizer,
max_length=max_length,
cot=cot,
)
label = test_df.iloc[i, test_df.shape[1] - 1]
text = tokenizer.apply_chat_template(
[{"role": "user", "content": prompt}],
tokenize=False,
add_generation_prompt=True,
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
generated_ids = model.generate(model_inputs.input_ids, max_new_tokens=512)
generated_ids = [
output_ids[len(input_ids) :]
for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
pred = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
# pred, history = model.chat(tokenizer, prompt, history=None)
if pred and pred[0] in choices:
cors.append(pred[0] == label)
all_preds.append(pred.replace("\n", ""))
acc = np.mean(cors)
print("Average accuracy {:.3f} - {}".format(acc, subject))
print(
"{} results, {} inappropriate formated answers.".format(
len(cors), len(all_preds) - len(cors)
)
)
return acc, all_preds, None
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--model_name_or_path", type=str, default="")
parser.add_argument("--data_dir", type=str, default="../data")
parser.add_argument("--save_dir", type=str, default="../results/Qwen1.5-7B-Chat")
parser.add_argument("--num_few_shot", type=int, default=0)
parser.add_argument("--max_length", type=int, default=2048)
parser.add_argument("--cot", action="store_true")
args = parser.parse_args()
tokenizer = AutoTokenizer.from_pretrained(
args.model_name_or_path, trust_remote_code=True
)
if is_eval_success(args):
# eval finished, no need load model anymore, just show the result
model = None
else:
model = init_model(args)
if "chat" in args.model_name_or_path.lower():
run_eval(model, tokenizer, eval_chat, args)
else:
run_eval(model, tokenizer, eval, args)