-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_eval_lm.py
513 lines (459 loc) · 20.9 KB
/
run_eval_lm.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
__author__ = "@YuweiYin"
"""
import os
import time
import json
from typing import Optional
import fire
import numpy as np
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
from datasets import Dataset
from tasks.boolq import EvalTaskBoolq
from tasks.logiqa import EvalTaskLogiqa
from tasks.commonsense_qa import EvalTaskCommonsenseqa
from tasks.social_iqa import EvalTaskSocialiqa
from tasks.sciq import EvalTaskSciq
from tasks.openbookqa import EvalTaskOpenbookqa
from tasks.ai2_arc import EvalTaskAi2Arc
from tasks.bbh import EvalTaskBbh
from tasks.mmlu import EvalTaskMmlu
from tasks.mmlu_pro import EvalTaskMmluPro
from utils.init_functions import logger_setup, cuda_setup, random_setup
class LMEval:
def __init__(
self,
verbose: bool,
logger,
cuda_dict: dict,
seed: int = 42,
cache_dir: Optional[str] = None,
project_dir: Optional[str] = None,
hf_id: str = "meta-llama/Llama-3.1-8B-Instruct",
bsz: int = 1,
show_generation: bool = False,
debug: bool = False,
output_dir: Optional[str] = None,
use_gen_output: bool = False,
overwrite: bool = False,
):
self.verbose = verbose
self.logger = logger
self.cuda_dict = cuda_dict
self.seed = seed
self.hf_id = hf_id
self.hf_name = "--".join(hf_id.split("/"))
self.show_generation = show_generation # If True, show outputs during generation
self.debug = debug
if isinstance(project_dir, str) and os.path.isdir(project_dir):
self.project_dir = project_dir
else:
self.project_dir = os.getcwd()
assert os.path.isdir(project_dir)
self.output_dir = output_dir
self.bsz = bsz
self.use_gen_output = use_gen_output
self.overwrite = overwrite
self.task_class_dict = {
"boolq": EvalTaskBoolq,
"logiqa": EvalTaskLogiqa,
"openbookqa": EvalTaskOpenbookqa,
"sciq": EvalTaskSciq,
"social_iqa": EvalTaskSocialiqa,
"ai2_arc": EvalTaskAi2Arc,
"bbh": EvalTaskBbh,
"commonsense_qa": EvalTaskCommonsenseqa,
"mmlu": EvalTaskMmlu,
"mmlu_pro": EvalTaskMmluPro,
}
# Cache directory
self.home_dir = os.path.expanduser("~")
if isinstance(cache_dir, str) and os.path.isdir(cache_dir):
self.cache_dir = cache_dir
else:
self.cache_dir = os.path.join(self.home_dir, ".cache/huggingface")
if not os.path.isdir(self.cache_dir):
os.makedirs(self.cache_dir, exist_ok=True)
if self.verbose:
self.logger.info(f">>> cache_dir: {self.cache_dir}")
os.environ["TRANSFORMERS_CACHE"] = self.cache_dir
os.environ["HF_HOME"] = self.cache_dir
self.model_path = os.path.join(
self.cache_dir, "models--" + self.hf_name, "snapshots/model")
assert os.path.isdir(self.model_path), f"AssertionError: assert os.path.isdir({self.model_path})"
# Tokenizer and LLM model
self.tokenizer = self.load_tokenizer(model_path=self.model_path, padding_side="left", truncation_side="left")
self.terminators_gen = [
self.tokenizer.eos_token_id,
self.tokenizer.convert_tokens_to_ids(self.tokenizer.eos_token)
]
self.model = None
@staticmethod
def _handle_non_serializable(o):
if isinstance(o, np.int64) or isinstance(o, np.int32):
return int(o)
elif isinstance(o, set):
return list(o)
else:
return str(o)
def load_tokenizer(
self,
model_path,
padding_side="left",
truncation_side="left",
):
tokenizer = AutoTokenizer.from_pretrained(
model_path,
padding_side=padding_side,
truncation_side=truncation_side, # "right" for training, "left" for generating
cache_dir=self.cache_dir,
# local_files_only=True,
)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.pad_token_id = tokenizer.eos_token_id
max_len = tokenizer.max_len_single_sentence
if self.verbose:
self.logger.info(
f">>> len(tokenizer.vocab) = {len(tokenizer.vocab)}; "
f"tokenizer.max_len_single_sentence = {max_len}") # LLaMA-3: 131071
return tokenizer
def load_model(
self,
model_path,
tokenizer,
):
model = AutoModelForCausalLM.from_pretrained(
model_path,
torch_dtype=torch.float16, # torch.bfloat16
# torch_dtype=torch.float8_e5m2, # torch.float8
device_map="auto", # !pip install accelerate
# device_map=self.cuda_dict["device"] if self.debug else "auto",
# device_map=self.device_mps if self.debug else "auto",
trust_remote_code=True,
cache_dir=self.cache_dir,
# local_files_only=True,
)
# model = model.to(device=self.cuda_dict["device"])
# list(model.state_dict().keys())
model.generation_config.pad_token_id = tokenizer.pad_token_id # eos_token_id
# model.resize_token_embeddings(len(self.tokenizer_train)) # if added new special tokens (Option 1)
# model.train()
model.eval()
total_params = sum(p.numel() for p in model.parameters())
train_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
if self.verbose:
self.logger.info(f">>> Base Model loaded: {model_path}")
self.logger.info(f">>> [Base Model] Number of total parameters: {total_params}")
self.logger.info(f">>> [Base Model] Number of trainable parameters: {train_params}")
return model
def run_language_modeling(
self,
prompts,
model,
tokenizer,
need_tokenize: bool = True,
) -> dict:
if need_tokenize:
input_ids = self.tokenizer(
prompts,
padding=True, # truncation=True, max_length=1024,
return_tensors="pt",
).to(model.device) # batch_size=1
else:
input_ids = prompts
input_ids = input_ids.to(model.device)
len_input = input_ids.data["input_ids"].size(-1)
target_ids = input_ids["input_ids"].to(model.device)
input_ids.data["labels"] = target_ids
with torch.no_grad():
outputs = model(**input_ids)
output_ids = outputs["logits"].argmax(-1)
output_text = tokenizer.batch_decode(
output_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True)
input_text = tokenizer.batch_decode(
input_ids["input_ids"], skip_special_tokens=True, clean_up_tokenization_spaces=True)
assert len(input_text) == len(prompts) == len(output_text)
output_text_pure = []
for _input, _prompt, _output in zip(input_text, prompts, output_text):
output_pure = _output[len(_input):]
output_text_pure.append(output_pure)
if self.verbose and self.show_generation:
# self.logger.info("================================== >>> input (raw) <<<")
# self.logger.info(_input)
# self.logger.info("================================== >>> prompt <<<")
# self.logger.info(_prompt)
self.logger.info("================================== >>> output <<<")
self.logger.info(output_pure)
return {
"prompts": prompts,
"len_input": len_input,
# "input_ids": input_ids,
"input_text": input_text,
"outputs": outputs,
# "output_ids": output_ids,
# "output_text": output_text,
"output_text": output_text_pure,
}
def lm_evaluate(
self,
eval_task_name: str,
):
# Stage 2: Option Section (Multiple-choice QA Evaluation):
# Load the result JSON file in the first stage (reasoning generation),
# concatenate context, question, options, reasoning, and each option,
# compute the language model losses, and select the option with the lowest LM loss
os.environ["TOKENIZERS_PARALLELISM"] = "false"
# Load the generation outputs
assert isinstance(self.output_dir, str) and os.path.isdir(self.output_dir), "Please specify --output_dir"
output_dir = os.path.join(self.output_dir, eval_task_name, self.hf_name)
output_fp = os.path.join(output_dir, "results_gen.json")
if not os.path.isfile(output_fp):
self.logger.info(
f">>> hf_id = {self.hf_id}; model_path = {self.model_path}\n"
f"output_dir: {output_dir}, use_gen_output: {self.use_gen_output}.\n"
f">>> !!! >>> [SKIP; No --output_fp] output_fp does not exist: {output_fp}"
)
return
with open(output_fp, "r", encoding="utf-8") as fp_in:
gen_results = json.load(fp_in)
# Set the saving filepath
if self.use_gen_output:
output_eval_fp = os.path.join(output_dir, "results_eval-use_gen.json")
else:
output_eval_fp = os.path.join(output_dir, "results_eval.json")
if os.path.exists(output_eval_fp):
if self.overwrite:
self.logger.info(f"Results will be overwritten: {output_eval_fp}")
else:
self.logger.info(
f">>> hf_id = {self.hf_id}; model_path = {self.model_path}\n"
f"output_dir: {output_dir}, use_gen_output: {self.use_gen_output}.\n"
f">>> !!! >>> [SKIP; No --overwrite] File already exists: {output_eval_fp}"
)
return
else:
self.logger.info(f"Results will be saved at: {output_eval_fp}")
assert eval_task_name in self.task_class_dict, \
f"AssertionError: task name {eval_task_name} not in task_class_dict"
eval_task_class = self.task_class_dict[eval_task_name]
eval_task_obj = eval_task_class(
verbose=self.verbose,
logger=self.logger,
cache_dir=self.cache_dir,
project_dir=self.project_dir,
)
self.logger.info(f">>> Evaluation Task: {eval_task_name}")
task_info = eval_task_obj.load_task()
dataset_list = task_info["data"]
# Load the model
if self.model is None:
model = self.load_model(model_path=self.model_path, tokenizer=self.tokenizer)
self.model = model
else:
model = self.model
# Deal with each task (and sub-tasks)
all_scores = {}
correct_num, wrong_num = 0, 0
show_cnt = 100
for dataset_dict in dataset_list:
ds_name, subset = dataset_dict["hf_dataset"], dataset_dict["hf_subset"]
eval_split, eval_dataset = dataset_dict["eval_split"], dataset_dict["eval_dataset"]
assert isinstance(eval_dataset, Dataset)
len_dataset = len(eval_dataset)
assert isinstance(ds_name, str) and len(ds_name) > 0
if isinstance(subset, str) and len(subset) > 0:
ds_id = f"{ds_name}---{subset}"
else:
ds_id = ds_name
if self.verbose:
self.logger.info(f">>> [Dataset: {ds_id}] [Eval: {eval_split}] # = {len_dataset}")
assert ds_id in gen_results
cur_results = gen_results[ds_id]
assert isinstance(cur_results, list) and len(cur_results) == len_dataset > 0
# Run generation with batch_size = 1
cur_ds_scores = []
correct_idx, wrong_idx = [], []
for idx, cur_res_dict in enumerate(cur_results):
gen_prompt = str(cur_res_dict["prompt"]).strip()
gen_output = str(cur_res_dict["output_text"]).strip()
answer_str = cur_res_dict["answer_str"]
answer_options = cur_res_dict["answer_options"]
answer_label = cur_res_dict["answer_label"]
label_options = cur_res_dict["label_options"]
if isinstance(answer_options, list) and len(answer_options) > 0:
if answer_str not in answer_options:
wrong_idx.append(idx) # exception case, call it error
cur_res_dict["eval_losses"] = []
cur_res_dict["eval_metric"] = "Acc"
cur_res_dict["eval_score"] = 0.0
cur_ds_scores.append(cur_res_dict)
self.logger.info(f">>> !!! >>> answer_str ({answer_str}) "
f"not in answer_options: {answer_options}")
continue
assert answer_str in answer_options
concat_options = answer_options
correct_answer = answer_str
correct_index = int(answer_options.index(correct_answer))
elif isinstance(label_options, list) and len(label_options) > 0:
if answer_label not in label_options:
wrong_idx.append(idx) # exception case, call it error
cur_res_dict["eval_losses"] = []
cur_res_dict["eval_metric"] = "Acc"
cur_res_dict["eval_score"] = 0.0
cur_ds_scores.append(cur_res_dict)
self.logger.info(f">>> !!! >>> answer_label ({answer_label}) "
f"not in answer_options: {answer_options}")
continue
assert answer_label in label_options
concat_options = label_options
correct_answer = answer_label
correct_index = int(label_options.index(correct_answer))
else:
raise ValueError(f"ValueError: data item format. answer_str: {answer_str}")
if isinstance(concat_options, list):
# Accuracy score: select the option with the lowest LLM perplexity / avg nll_loss
assert isinstance(correct_index, int)
if self.use_gen_output:
concat_prompts = [f"{gen_prompt} {gen_output}\nTherefore, the answer is: {_op}"
for _op in concat_options]
else:
concat_prompts = [f"{gen_prompt}\nThe answer is: {_op}" for _op in concat_options]
# Run language modeling (batch_size = 1) --> obtain nll loss / perplexity
eval_losses = []
for concat_prompt in concat_prompts:
eval_dict = self.run_language_modeling(
prompts=[concat_prompt], model=model, tokenizer=self.tokenizer, need_tokenize=True)
eval_losses.append(eval_dict["outputs"]["loss"].cpu().detach().numpy().item())
eval_choice = int(np.argmin(eval_losses).item())
if eval_choice == correct_index:
cur_eval_score = 1.0
correct_idx.append(idx)
else:
cur_eval_score = 0.0
wrong_idx.append(idx)
cur_res_dict["eval_losses"] = eval_losses
cur_res_dict["eval_metric"] = "Acc"
cur_res_dict["eval_score"] = cur_eval_score
else:
raise ValueError(f"ValueError: `concat_options` is not a list: {concat_options}")
cur_ds_scores.append(cur_res_dict)
if self.verbose and len(cur_ds_scores) % show_cnt == 0:
self.logger.info(f">>> Progress: [{ds_id}] [{len(cur_ds_scores)} / {len_dataset}]")
cur_acc = float(len(correct_idx) / len_dataset)
all_scores[ds_id] = {
"acc": cur_acc,
"correct_idx": correct_idx,
"wrong_idx": wrong_idx,
"ds_scores": cur_ds_scores,
}
correct_num += len(correct_idx)
wrong_num += len(wrong_idx)
if self.verbose:
self.logger.info(f">>> [{ds_id}] [Acc = {cur_acc}] "
f"# correct = {len(correct_idx)}; # wrong = {len(wrong_idx)}\n")
# collect all acc
acc_all = [float(v["acc"]) for k, v in all_scores.items()]
assert len(acc_all) > 0
acc_overall = sum(acc_all) / len(acc_all)
all_scores["__acc_overall__"] = acc_overall
self.logger.info(f">>> DONE ALL. [{eval_task_name}] [Acc_overall = {acc_overall}] "
f"# total correct = {correct_num}; # total wrong = {wrong_num}")
# Save the generation outputs and show logs
dumped = json.dumps(
all_scores,
indent=2, # indent=None,
default=self._handle_non_serializable,
ensure_ascii=True,
)
with open(output_eval_fp, "w", encoding="utf-8") as fp_out:
fp_out.write(dumped)
self.logger.info(
f">>> hf_id = {self.hf_id}; model_path = {self.model_path}\n"
f"output_dir: {output_dir}, use_gen_output: {self.use_gen_output}."
)
def main(
task: int = 0,
eval_task_name: Optional[str] = None,
hf_id: Optional[str] = None,
cache_dir: Optional[str] = None,
project_dir: Optional[str] = None,
seed: int = 42,
cuda: Optional[str] = None,
bsz: int = 1,
verbose: bool = False,
debug: bool = False,
output_dir: Optional[str] = None,
use_gen_output: bool = False,
overwrite: bool = False,
**kwargs
) -> None:
"""
Stage 2: Option Selection. Evaluate LLMs on multiple-choice QA tasks.
:param task: 1. LM evaluation on multiple-choice QA tasks.
:param eval_task_name: The name(s) of the evaluation task. (e.g., "boolq", "bbh", and "boolq,bbh")
:param hf_id: ORGANIZATION_NAME/MODEL_NAME, e.g., "meta-llama/Llama-3.1-8B-Instruct"
:param cache_dir: The root directory of the cache.
:param project_dir: The root directory of the current project/repo.
:param seed: Random seed of all modules.
:param cuda: To specify CUDA GPU devices, e.g., "0" OR "0,1". Default: None -- Use CPU or all available GPUs.
:param bsz: The batch size.
:param verbose: Verbose mode: show logs.
:param debug: Debugging / developing mode.
:param output_dir: The path to the output file where the result metrics will be saved.
:param use_gen_output: Use the generated outputs or not.
:param overwrite: Overwrite existing output files.
:return: None.
"""
timer_start = time.perf_counter()
# Setup of the logger, CUDA gpus, and random seed
logger = logger_setup("LM_Eval")
cuda_dict = cuda_setup(cuda=cuda, logger=logger, verbose=verbose)
random_setup(seed=seed, has_cuda=cuda_dict["has_cuda"])
if isinstance(kwargs, dict):
logger.info(f">>> Unused parameters in kwargs: {kwargs}\n")
if isinstance(cache_dir, str) and os.path.isdir(cache_dir):
os.environ["HF_HOME"] = cache_dir
# os.environ["HF_HOME"] = os.path.join(cache_dir, "datasets")
# os.environ["HF_HOME"] = os.path.join(cache_dir, "hub")
else:
cache_dir = None
lm_eval = LMEval(
verbose=verbose,
logger=logger,
cuda_dict=cuda_dict,
seed=seed,
cache_dir=cache_dir,
project_dir=project_dir,
hf_id=hf_id,
bsz=max(int(bsz), 1),
debug=debug,
output_dir=output_dir,
use_gen_output=use_gen_output,
overwrite=overwrite,
)
task = int(task)
match task:
case 1:
if isinstance(eval_task_name, tuple) or isinstance(eval_task_name, list):
for cur_task_name in eval_task_name:
cur_task_name = str(cur_task_name).strip()
logger.info(f">>> <START> {cur_task_name}\n")
lm_eval.lm_evaluate(eval_task_name=cur_task_name)
logger.info(f">>> <END> {cur_task_name}\n\n\n")
elif isinstance(eval_task_name, str):
eval_task_name = str(eval_task_name).strip()
logger.info(f">>> <START> {eval_task_name}\n")
lm_eval.lm_evaluate(eval_task_name=eval_task_name)
logger.info(f">>> <END> {eval_task_name}\n\n\n")
else:
raise ValueError(f"--eval_task_name should be a tuple/list/str: {eval_task_name}")
case _:
raise ValueError(f"ValueError: task = {task}")
timer_end = time.perf_counter()
total_sec = timer_end - timer_start
logger.info(f"Total Running Time: {total_sec:.1f} sec ({total_sec / 60:.1f} min; {total_sec / 3600:.2f} h)")
if __name__ == "__main__":
fire.Fire(main)