forked from haonan-li/CMMLU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatglm.py
58 lines (48 loc) · 2.34 KB
/
chatglm.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
import os
import torch
import numpy as np
import argparse
from mp_utils import choices, format_example, gen_prompt, softmax, run_eval
from peft import PeftModel
from transformers import AutoModel, AutoTokenizer
def eval_chat(model, tokenizer, subject, dev_df, test_df, num_few_shot, max_length, cot):
cors = []
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]
pred, history = model.chat(tokenizer, prompt, history=[])
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("--lora_weights", type=str, default="")
parser.add_argument("--data_dir", type=str, default="../data")
parser.add_argument("--save_dir", type=str, default="../results/ChatGLM-6B")
parser.add_argument("--num_few_shot", type=int, default=0)
parser.add_argument("--max_length", type=int, default=2048)
parser.add_argument("--load_in_8bit", action='store_true')
parser.add_argument("--cot", action='store_true')
args = parser.parse_args()
# Initialize models
tokenizer = AutoTokenizer.from_pretrained(args.model_name_or_path, trust_remote_code=True,)
model = AutoModel.from_pretrained(args.model_name_or_path,
trust_remote_code=True,
load_in_8bit=args.load_in_8bit,
).half().cuda()
# Always use Chat-style evaluation
run_eval(model, tokenizer, eval_chat, args)