-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_vmlu.py
155 lines (127 loc) · 4.68 KB
/
test_vmlu.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
import pandas as pd
import numpy as np
import torch
import json
import glob
import logging
import os
import argparse
import time
from transformers import AutoModelForCausalLM, BitsAndBytesConfig, AutoTokenizer, AutoConfig
from string import Template
from pathlib import Path
from tqdm import tqdm
import warnings
warnings.simplefilter("ignore")
if __name__ == "__main__":
llm = "../Qwen2.5-1.5B-Instruct"
device = "cuda"
folder = "."
path = llm.replace('/', '-')
## create directory
directory_path = './logs'
if not os.path.exists(directory_path):
os.makedirs(directory_path)
# Configure logging
logging.basicConfig(filename=f"./logs/{path.split('/')[-1]}.log", level=logging.INFO, format='%(asctime)s - %(levelname)s: %(message)s')
logging.info(f'Model name: {llm}')
config = AutoConfig.from_pretrained(llm, trust_remote_code=True)
config.init_device = device
model = AutoModelForCausalLM.from_pretrained(
llm, config=config, torch_dtype=torch.bfloat16, trust_remote_code=True
)
model.to(device)
model.config.use_cache = False
tokenizer = AutoTokenizer.from_pretrained(llm, trust_remote_code=True)
# Create empty lists to store data
ids = []
questions = []
choices_A = []
choices_B = []
choices_C = []
choices_D = []
choices_E = []
# Read JSONL files
data_path = Path(folder)
jsonl_files = list(data_path.glob('test_vmlu.jsonl'))
for file in jsonl_files:
with open(file, "r", encoding="utf-8") as f:
lines = f.readlines()
for line in lines:
data = json.loads(line)
ids.append(data["id"])
questions.append(data["question"])
choices = data["choices"]
try:
choices_A.append(choices[0])
except:
choices_A.append('')
try:
choices_B.append(choices[1])
except:
choices_B.append('')
try:
choices_C.append(choices[2])
except:
choices_C.append('')
try:
choices_D.append(choices[3])
except:
choices_D.append('')
try:
choices_E.append(choices[4])
except:
choices_E.append('')
# Create a DataFrame
df = pd.DataFrame({
"id": ids,
"prompt": questions,
"A": choices_A,
"B": choices_B,
"C": choices_C,
"D": choices_D,
"E": choices_E
})
logging.info(df.head())
preamble = \
'Chỉ đưa ra chữ cái đứng trước câu trả lời đúng (A, B, C, D hoặc E) của câu hỏi trắc nghiệm sau: '
template = Template('$preamble\n\n$prompt\n\n $a\n $b\n $c\n $d\n $e\nĐáp án:')
def format_input(df, idx):
prompt = df.loc[idx, 'prompt']
a = df.loc[idx, 'A']
b = df.loc[idx, 'B']
c = df.loc[idx, 'C']
d = df.loc[idx, 'D']
e = df.loc[idx, 'E']
input_text = template.substitute(
preamble=preamble, prompt=prompt, a=a, b=b, c=c, d=d, e=e)
return input_text
# Test a toy example
inputs = tokenizer(format_input(df, 0), return_tensors="pt").to(device)
outputs = model.generate(**inputs, max_new_tokens=1)
answer = tokenizer.batch_decode(outputs, skip_special_tokens=True)
logging.info('Contruct a toy eg')
logging.info("Generated answer: %s", answer)
answers = []
start = time.time()
for idx in tqdm(df.index):
if 'falcon' in llm:
inputs = tokenizer(format_input(df, idx), return_tensors="pt", return_token_type_ids=False).to(device)
outputs = model.generate(**inputs, pad_token_id=tokenizer.eos_token_id, max_new_tokens=1)
elif 'sealion' in llm:
inputs = tokenizer(format_input(df, idx), return_tensors="pt").to(device)
outputs = model.generate(inputs["input_ids"], max_new_tokens=1)
else:
inputs = tokenizer(format_input(df, idx), return_tensors="pt").to(device)
outputs = model.generate(**inputs, max_new_tokens=1)
answer_decoded = tokenizer.batch_decode(outputs, skip_special_tokens=True)
last_element = answer_decoded[-1]
answer = last_element.split()[-1]
answers.append(answer)
end = time.time()
duration = end - start
print('Time taken for running inference: ', duration)
df['answer'] = answers
logging.info(df.head())
# save the answer csv
df[['id','answer']].to_csv(f"./logs/{path}.csv", index = False)