-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheval_utility.py
More file actions
160 lines (140 loc) · 6.52 KB
/
Copy patheval_utility.py
File metadata and controls
160 lines (140 loc) · 6.52 KB
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
import os
import argparse
import wandb
import torch
import numpy as np
import json
import lm_eval
from transformers import AutoModelForCausalLM, AutoTokenizer
from fingerprint_dataloader import get_fingerprint_ds
# Parsing the tasks into individual components with shared n_shot and metric values.
import datasets
datasets.config.HF_DATASETS_TRUST_REMOTE_CODE = True
# Input string for parsing
ALL_DATASETS = {
"ARC": {"n_shot": 25, "tasks": ["arc_challenge"], "metric": ["acc_norm"]},
"HellaSwag": {"n_shot": 10, "tasks": ["hellaswag"], "metric": ["acc_norm"]},
"TruthfulQA": {"n_shot": 0, "tasks": ["truthfulqa_mc2"], "metric": ["acc"]},
"MMLU": {
"n_shot": 5,
"tasks": [
"mmlu"
],
"metric": ["acc"]
},
"Winogrande": {"n_shot": 5, "tasks": ["winogrande"], "metric": ["acc"]},
"GSM8k": {"n_shot": 5, "tasks": ["gsm8k"], "metric": ["exact_match,strict-match", "exact_match,flexible-extract"]},
}
ALL_DATASETS_TINY = {
"tinyARC": {"n_shot": 25, "tasks": ["tinyArc"], "metric": ["acc_norm"]},
"tinyHellaswag": {"n_shot": 10, "tasks": ["tinyHellaswag"], "metric": ["acc_norm"]},
"tinyTruthfulQA": {"n_shot": 0, "tasks": ["tinyTruthfulQA"], "metric": ["acc"]},
"tinyMMLU": {
"n_shot": 5,
"tasks": [
"tinyMMLU"
],
"metric": ["acc_norm"]
},
"tinyWinogrande": {"n_shot": 5, "tasks": ["tinyWinogrande"], "metric": ["acc_norm"]},
"tinyGSM8k": {"n_shot": 5, "tasks": ["tinyGSM8k"], "metric": ["exact_match,strict-match", "exact_match,flexible-extract"]},
}
def eval_driver(model_path:str, wandb_run_name='None', delete_model=False, use_tiny_benchmarks=False, eval_batch_size=6, apply_chat_template=False):
# Load the fingerprint config as well
config_path = model_path.replace('final_model', 'fingerprinting_config.json')
if os.path.exists(config_path):
with open(config_path, 'r') as f:
config = json.load(f)
else:
config = {'model_path': model_path}
if wandb_run_name != 'None':
wandb.init(project=wandb_run_name, config=config)
torch.cuda.empty_cache()
apply_chat_template =config.get('use_chat_template', False)
ds_results = lm_eval.simple_evaluate(
model="hf",
model_args=f"pretrained={model_path},local_files_only=True,trust_remote_code=True",
tasks='openllm' if not use_tiny_benchmarks else 'tinyBenchmarks',
batch_size=eval_batch_size,
apply_chat_template=apply_chat_template
)
try:
if use_tiny_benchmarks:
json.dump(ds_results['results'], open(f"{model_path.replace('final_model', 'eval_results_tiny')}.json", 'w'))
else:
json.dump(ds_results['results'], open(f"{model_path.replace('final_model', 'eval_results')}.json", 'w'))
except:
print(f"Could not save the results to {model_path.replace('final_model', 'eval_results')}.json")
total_tasks = 0
total_acc = 0.0
all_datasets = ALL_DATASETS_TINY if use_tiny_benchmarks else ALL_DATASETS
for ds in all_datasets.keys():
for task in all_datasets[ds]['tasks']:
task_res = ds_results['results'][task]
for metric in all_datasets[ds]['metric']:
if wandb_run_name != 'None':
try:
wandb.log({f"eval/detailed/{ds}/{task}/{metric}": task_res[f"{metric}"]})
total_acc += task_res[f"{metric}"]
total_tasks += 1
except KeyError:
try:
wandb.log({f"eval/detailed/{ds}/{task}/{metric}": task_res[f"{metric},none"]})
total_acc += task_res[f"{metric},none"]
total_tasks += 1
except KeyError:
print(f"Could not find metric {metric} for task {task}")
continue
total_acc /= total_tasks
if wandb_run_name != 'None':
if use_tiny_benchmarks:
wandb.log({f"eval/OpenLLMTinyLeaderboard": total_acc})
else:
wandb.log({f"eval/OpenLLMLeaderboard": total_acc})
print("="*20)
print(f"Total accuracy: {total_acc}")
print("="*20)
if delete_model:
# Delete model at model_path
print(f"Deleting model at {model_path}")
os.system(f"rm -rf {model_path}")
def get_from_json(model_path, use_tiny_benchmarks=False):
config_path = model_path.replace('final_model', 'fingerprinting_config.json')
with open(config_path, 'r') as f:
config = json.load(f)
if use_tiny_benchmarks:
eval_results = json.load(open(f"{model_path.replace('final_model', 'eval_results_tiny')}.json", 'r'))
else:
eval_results = json.load(open(f"{model_path.replace('final_model', 'eval_results')}.json", 'r'))
total_tasks = 0
total_acc = 0.0
all_datasets = ALL_DATASETS_TINY if use_tiny_benchmarks else ALL_DATASETS
for ds in all_datasets.keys():
for task in all_datasets[ds]['tasks']:
task_res = eval_results[task]
for metric in all_datasets[ds]['metric']:
try:
total_acc += task_res[f"{metric}"]
total_tasks += 1
except KeyError:
try:
total_acc += task_res[f"{metric},none"]
total_tasks += 1
except KeyError:
print(f"Could not find metric {metric} for task {task}")
continue
total_acc /= total_tasks
print("="*20)
print(f"Key Path - {config['fingerprints_file_path']}")
print(f"Total accuracy: {total_acc}")
print("="*20)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--model_path', type=str, help='Path to the model to be checked. This can be a HF url or a local path', required=True)
parser.add_argument('--wandb_run_name', type=str, default='None', help='Wandb run name')
parser.add_argument('--tinyBenchmarks', action='store_true', help='Should we run the tiny benchmarks')
parser.add_argument('--eval_batch_size', type=int, default=6, help='Batch size for evaluation')
parser.add_argument('--delete_model', action='store_true', help='Delete the model after evaluation')
args = parser.parse_args()
# sort the seeds list
eval_driver(args.model_path, args.wandb_run_name, args.delete_model, use_tiny_benchmarks=args.tinyBenchmarks, eval_batch_size=args.eval_batch_size)