-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkmmlu_main.py
355 lines (295 loc) · 15 KB
/
kmmlu_main.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import os
import json
import time
import argparse
import openai
from openai import RateLimitError
import pandas as pd
from tqdm import tqdm
from dotenv import load_dotenv
from datasets import Dataset, load_dataset
from prompts import TYPE_1, TYPE_2, TYPE_3, TYPE_4, TYPE_MMLU_FEW_SHOT
from langchain_openai import AzureChatOpenAI, ChatOpenAI
from langchain_huggingface import HuggingFaceEndpoint
from langchain.schema.output_parser import StrOutputParser
from langchain_core.prompts import PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate
from langchain_openai import AzureChatOpenAI
from langchain_community.llms.azureml_endpoint import AzureMLOnlineEndpoint
from util.phi3_formatter import CustomPhi3ContentFormatter
from util.common_helper import str2bool
from logger import logger
def format_timespan(seconds):
hours = seconds // 3600
minutes = (seconds - hours*3600) // 60
remaining_seconds = seconds - hours*3600 - minutes*60
timespan = f"{hours} hours {minutes} minutes {remaining_seconds:.4f} seconds."
return timespan
class CustomStrOutputParser(StrOutputParser):
def parse(self, text: str) -> str:
response = text.strip().replace('"', "").replace("'", "")
if response.startswith("A"):
pred = "A"
elif response.startswith("B"):
pred = "B"
elif response.startswith("C"):
pred = "C"
elif response.startswith("D"):
pred = "D"
else:
pred = "" # Wrong answer
return pred, response
def generate_few_shots_prompt(data):
prompt = ""
for i, row in enumerate(data):
#prompt += f"## Example {i+1}:\n"
prompt += f"질문 (Question): {row['question']}\n"
prompt += f"보기 (Options)\nA: {row['A']}, B: {row['B']}, C: {row['C']}, D: {row['D']}\n"
prompt += f"정답 (Answer): {row['answer']}\n\n"
return prompt
def get_prompt(x, few_shots=None) -> str:
if few_shots is None:
return TYPE_2.format(
QUESTION=x["question"],
A=x["A"],
B=x["B"],
C=x["C"],
D=x["D"],
)
else:
return TYPE_MMLU_FEW_SHOT.format(
FEW_SHOTS=few_shots,
QUESTION=x["question"],
A=x["A"],
B=x["B"],
C=x["C"],
D=x["D"],
)
def get_answer(x) -> str:
return x["answer"].upper().strip()
def map_answer(answer):
answer_mapping = {1: 'A', 2: 'B', 3: 'C', 4: 'D'}
return answer_mapping[answer]
def convert_to_pascal_case(category):
return '-'.join(word.capitalize() for word in category.split('_'))
def get_prompt_template(template_type):
if template_type == "basic":
prompt = PromptTemplate.from_template("{question}")
elif template_type == "chat":
system_prompt = """You are an AI assistant who reads a given question and solves multiple choice questions.
You don't need to write a detailed explanation of your answer in sentences. Just answer in one word."""
system_message_template = SystemMessagePromptTemplate.from_template(system_prompt)
human_prompt = [
{
"type": "text",
"text": "{question}"
},
]
human_message_template = HumanMessagePromptTemplate.from_template(human_prompt)
prompt = ChatPromptTemplate.from_messages(
[
system_message_template,
human_message_template
]
)
else:
raise Exception("Invalid 'template_type' value. Please choose from ['basic', 'chat']")
return prompt
def benchmark(args):
IS_DEBUG = args.is_debug
MAX_RETRIES = args.max_retries
DELAY_INCREMENT = 30
MODEL_VERSION = None
FEW_SHOTS = "5shot" if args.use_few_shots else "0shot"
num_debug_samples = args.num_debug_samples
batch_size = args.batch_size
max_tokens = args.max_tokens
temperature = args.temperature
if args.is_hard:
hf_dataset_id = "HAERAE-HUB/KMMLU-HARD"
dataset_name = "KMMLU-HARD"
kmmlu_category = [
'accounting', 'agricultural_sciences', 'aviation_engineering_and_maintenance', 'biology', 'chemical_engineering', 'chemistry',
'civil_engineering', 'computer_science', 'construction', 'criminal_law', 'ecology', 'economics', 'education',
'electrical_engineering', 'electronics_engineering', 'energy_management', 'environmental_science', 'fashion',
'food_processing', 'gas_technology_and_engineering', 'geomatics', 'health', 'industrial_engineer', 'information_technology',
'interior_architecture_and_design', 'korean_history', 'law', 'machine_design_and_manufacturing', 'management',
'maritime_engineering', 'marketing', 'materials_engineering', 'math', 'mechanical_engineering', 'nondestructive_testing',
'patent', 'political_science_and_sociology', 'psychology', 'public_safety', 'railway_and_automotive_engineering',
'real_estate', 'refrigerating_machinery', 'social_welfare', 'taxation', 'telecommunications_and_wireless_technology'
]
else:
hf_dataset_id = "HAERAE-HUB/KMMLU"
dataset_name = "KMMLU"
kmmlu_category = [
'Accounting', 'Agricultural-Sciences', 'Aviation-Engineering-and-Maintenance', 'Biology', 'Chemical-Engineering', 'Chemistry',
'Civil-Engineering', 'Computer-Science', 'Construction', 'Criminal-Law', 'Ecology', 'Economics', 'Education',
'Electrical-Engineering', 'Electronics-Engineering', 'Energy-Management', 'Environmental-Science', 'Fashion',
'Food-Processing', 'Gas-Technology-and-Engineering', 'Geomatics', 'Health', 'Industrial-Engineer', 'Information-Technology',
'Interior-Architecture-and-Design', 'Korean-History', 'Law', 'Machine-Design-and-Manufacturing', 'Management',
'Maritime-Engineering', 'Marketing', 'Materials-Engineering', 'Math', 'Mechanical-Engineering', 'Nondestructive-Testing',
'Patent', 'Political-Science-and-Sociology', 'Psychology', 'Public-Safety', 'Railway-and-Automotive-Engineering',
'Real-Estate', 'Refrigerating-Machinery', 'Social-Welfare', 'Taxation', 'Telecommunications-and-Wireless-Technology'
]
if args.model_provider == "azureopenai":
logger.info("Using Azure OpenAI model provider.")
MODEL_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME")
API_VERSION = os.getenv("AZURE_OPENAI_API_VERSION")
MODEL_VERSION = os.getenv("OPENAI_MODEL_VERSION")
llm = AzureChatOpenAI(
azure_deployment=MODEL_NAME,
openai_api_version=API_VERSION,
temperature=temperature,
max_tokens=max_tokens,
max_retries=MAX_RETRIES
)
elif args.model_provider == "openai":
logger.info("Using OpenAI model provider.")
MODEL_NAME = os.getenv("OPENAI_DEPLOYMENT_NAME")
llm = ChatOpenAI(
model=MODEL_NAME,
temperature=temperature,
max_tokens=max_tokens,
max_retries=MAX_RETRIES
)
elif args.model_provider == "huggingface":
if temperature == 0.0: # in case of not supporting 0.0 for some SLM, set to 0.01
temperature = 0.01
MODEL_NAME = args.hf_model_id.split("/")[-1]
logger.info("Using Hugging Face model provider.")
llm = HuggingFaceEndpoint(
repo_id=args.hf_model_id,
temperature=temperature,
max_new_tokens=max_tokens,
huggingfacehub_api_token=os.getenv("HF_API_TOKEN")
)
elif args.model_provider == "azureml":
logger.info("Using Azure ML endpoint as model provider.")
MODEL_NAME = os.getenv("AZURE_ML_DEPLOYMENT_NAME")
AZURE_ML_ENDPOINT_URL = os.getenv("AZURE_ML_ENDPOINT_URL")
AZURE_ML_ENDPOINT_TYPE = os.getenv("AZURE_ML_ENDPOINT_TYPE") # https://python.langchain.com/v0.2/api_reference/community/llms/langchain_community.llms.azureml_endpoint.AzureMLEndpointApiType.html#langchain_community.llms.azureml_endpoint.AzureMLEndpointApiType
AZURE_ML_API_KEY = os.getenv("AZURE_ML_API_KEY")
llm = AzureMLOnlineEndpoint(
endpoint_url=AZURE_ML_ENDPOINT_URL,
endpoint_api_type=AZURE_ML_ENDPOINT_TYPE,
endpoint_api_key=AZURE_ML_API_KEY,
content_formatter=CustomPhi3ContentFormatter(),
model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens
}
)
logger.info(f"====== [START] Generate answers to questions given by LLM. =====")
if args.use_few_shots:
logger.info(f"===== Use Few-shots Prompt.")
else:
logger.info(f"===== Use Zero-shot Prompt.")
logger.info(f"====== deployment name: {MODEL_NAME}, model version: {MODEL_VERSION} =====")
responses = []
# Load the datasets and append to the list with their respective categories
for c in kmmlu_category:
logger.info(f"##### Category [{c}] Processing...")
ds_dict = load_dataset(hf_dataset_id, c)
# For few-shot prompts, we need to generate a prompt with examples
ds_dev = ds_dict["dev"]
ds_dev = ds_dev.map(lambda x: {'answer': map_answer(x['answer'])})
if args.is_hard:
ds_dev = ds_dev.map(lambda x: {'category': convert_to_pascal_case(x['category'])})
else:
ds_dev = ds_dev.rename_column("Category", "category")
ds = ds_dict["test"]
ds = ds.map(lambda x: {'answer': map_answer(x['answer'])})
if args.is_hard:
ds = ds.map(lambda x: {'category': convert_to_pascal_case(x['category'])})
else:
ds = ds.rename_column("Category", "category")
if IS_DEBUG:
ds = ds.select(range(num_debug_samples))
if args.use_few_shots:
few_shots = generate_few_shots_prompt(ds_dev)
else:
few_shots = None
#all_batch = [{"category": x["category"], "question": get_prompt(x, few_shots), "answer": get_answer(x)} for x in tqdm(ds)]
all_batch = [{"category": c, "question": get_prompt(x, few_shots), "answer": get_answer(x)} for x in tqdm(ds)]
prompt_template = get_prompt_template(args.template_type)
chain = prompt_template | llm | CustomStrOutputParser()
t0 = time.time()
with tqdm(total=len(all_batch), desc="Processing Answers") as pbar:
for i in range(0, len(all_batch), batch_size):
mini_batch = all_batch[i:i+batch_size]
retries = 0
while retries <= MAX_RETRIES:
try:
preds = chain.batch(mini_batch, {"max_concurrency": batch_size})
# If no exception, add questions and answers to all_answers
for qna, pred in zip(mini_batch, preds):
responses.append({"category": qna["category"], "answer": qna["answer"], "pred": pred[0], "response": pred[1]})
break # Exit the retry loop once successful
except RateLimitError as rate_limit_error:
delay = (retries + 1) * DELAY_INCREMENT
logger.warning(f"{rate_limit_error}. Retrying in {delay} seconds...")
time.sleep(delay)
retries += 1
if retries > MAX_RETRIES:
logger.error(f"Max retries reached this batch. Skipping to next batch.")
break
except openai.BadRequestError as e:
logger.error(f"BadRequestError: {e}. Skipping this batch.")
logger.info(f"Question: {qna['question']}")
break
except Exception as e:
logger.error(f"Error in process_inputs: {e}")
break
pbar.set_postfix({"current_batch": f"{i//batch_size + 1}/{(len(all_batch) + (batch_size-1))//batch_size}"})
pbar.update(len(mini_batch))
t1 = time.time()
acc = evaluate_each_category(responses, c)
timespan = format_timespan(t1 - t0)
logger.info(f"##### Category [{c}] accuracy: {acc}")
logger.info(f"##### Generating Answers for Category [{c}] took {timespan}")
logger.info("====== [DONE] Completed Generating Answers to Questions given by LLM. =====")
df = pd.DataFrame(responses)
os.makedirs("results", exist_ok=True)
csv_path = f"results/[{dataset_name}] {MODEL_NAME}-{MODEL_VERSION}-{FEW_SHOTS}.csv"
logger.info(f"====== Generated CSV file - CSV_PATH: {csv_path} =====")
df.to_csv(csv_path, index=False)
logger.info(f"====== [START] Evaluation start - CSV_PATH: {csv_path} =====")
evaluate(csv_path)
logger.info(f"====== [START] Evaluation end =====")
def evaluate_each_category(responses, category):
df = pd.DataFrame(responses)
df = df[df["category"] == category]
df["correct"] = df["answer"] == df["pred"]
acc = round(df["correct"].mean()*100, 2)
return acc
def evaluate(csv_path):
result = pd.read_csv(csv_path)
result["correct"] = result["answer"] == result["pred"]
category_avg = result.groupby(['category']).agg(
correct_mean=('correct', 'mean'),
correct_count=('correct', 'size')
).reset_index()
print(category_avg)
overall_avg = category_avg["correct_mean"].mean()
print(f"Overall Average: {overall_avg}")
os.makedirs("evals", exist_ok=True)
filename = csv_path.split("/")[-1].split(".")[0]
category_avg.to_csv(f"evals/{filename}-eval.csv", index=False)
if __name__ == "__main__":
load_dotenv()
parser = argparse.ArgumentParser(description='Options')
parser.add_argument("--is_debug", type=str2bool, default=False)
parser.add_argument("--num_debug_samples", type=int, default=10)
parser.add_argument("--model_provider", type=str, default="azureopenai")
parser.add_argument("--hf_model_id", type=str, default="microsoft/Phi-3.5-mini-instruct")
parser.add_argument("--batch_size", type=int, default=20)
parser.add_argument("--max_retries", type=int, default=3)
parser.add_argument("--max_tokens", type=int, default=1024)
parser.add_argument("--temperature", type=float, default=0.01)
parser.add_argument("--template_type", type=str, default="basic")
parser.add_argument("--is_hard", type=str2bool, default=True)
parser.add_argument("--use_few_shots", type=str2bool, default=True)
args = parser.parse_args()
valid_providers = ["azureopenai", "openai", "azureml", "huggingface"]
assert args.model_provider in valid_providers, f"Invalid 'model_provider' value. Please choose from {valid_providers}."
valid_template_types = ["basic", "chat"]
assert args.template_type in valid_template_types, f"Invalid 'template_type' value. Please choose from {valid_template_types}."
logger.info(args)
benchmark(args)