forked from naver/bergen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.py
179 lines (155 loc) · 8.63 KB
/
eval.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
import json
import shutil
import torch
import time
import os
class Evaluate:
@staticmethod
def eval(experiment_folder, split, bem=False, llm=None, llm_ollama=None, vllm=None,gpt=None,bem_batch_size=1, lid=False, llm_batch_size=1, llm_prompt="default", ollama_url=None, folder=None, force=False):
def eval_single(experiment_folder, folder, split, model, metric_name):
if folder != None:
folders = [folder]
else:
folders = [ f.path for f in os.scandir(experiment_folder) if f.is_dir() and 'tmp_' not in f.path]
for experiment_folder in folders:
print('evaluating', experiment_folder)
def load_data(input_file):
result_dict = json.load(open(input_file))
return result_dict
input_file = f'{experiment_folder}/eval_{split}_out.json'
if os.path.exists(input_file):
data = load_data(input_file)
metrics_file = f'{experiment_folder}/eval_{split}_metrics.json'
try:
metrics_dict = json.load(open(metrics_file))
except: continue
if metric_name in metrics_dict and not force:
print (f"{experiment_folder}\t{metric_name}\talready done")
continue
predictions, references, questions = list(), list(), list()
for sample in data:
predictions.append(sample['response'])
references.append(sample['label'])
questions.append(sample['question'])
if gpt is not None:
# openai costs
model_score, scores, cost = model(predictions, references, questions)
costs_out_file = f'{experiment_folder}/eval_{split}_cost_{metric_name}_out.json'
with open(costs_out_file, 'w') as fout: fout.write(json.dumps(cost))
else:
model_score, scores = model(predictions, references, questions)
metrics_out_file = f'{experiment_folder}/eval_{split}_metrics_{metric_name}_out.json'
with open(metrics_out_file, 'w') as fout:
for score, sample in zip(scores, data):
jsonl = {'question' : sample['question'], 'response': sample['response'], 'label': sample['label'], 'score': float(score)}
fout.write(json.dumps(jsonl)+'\n')
metrics_dict.update({metric_name: str(model_score)})
print (metrics_dict,metric_name,model_score)
# save to _ tmp file
with open(metrics_file + '_', 'w') as fp:
json.dump(metrics_dict, fp, indent=2)
# when writing successful remove tmp file
shutil.move(metrics_file + '_', metrics_file)
if bem:
from models.evaluators.bem import BEM
model = BEM(batch_size=bem_batch_size)
eval_single(experiment_folder, folder, split, model, 'BEM')
if gpt is not None:
from models.evaluators.openai import OpenAI
model = OpenAI(gpt)
eval_single(experiment_folder, folder, split, model, gpt)
if llm is not None:
from models.evaluators.llm import LLM
if len(llm) == 0:
full_name, short_name = "Upstage/SOLAR-10.7B-Instruct-v1.0", "LLMeval"
elif len(llm)==1:
full_name = llm[0]
short_name = full_name
short_name = f"LLMeval_{short_name}"
elif len(llm)==2:
full_name = llm[0]
short_name = llm[1]
short_name = f"LLMeval_{short_name}"
model = LLM(full_name, batch_size=llm_batch_size, prompt=llm_prompt)
eval_single(experiment_folder, folder, split, model, short_name)
if vllm:
from models.evaluators.vllm import LLM
if len(vllm) == 0:
# corresponds to default LLMeval setting, results reported in the paper
full_name, short_name = "Upstage/SOLAR-10.7B-Instruct-v1.0", "LLMeval"
elif len(vllm)==1:
full_name = vllm[0]
short_name = f"LLMeval_{full_name}"
elif len(vllm)==2:
full_name = vllm[0]
short_name = f"LLMeval_{vllm[1]}"
model = LLM(full_name, batch_size=llm_batch_size, prompt=llm_prompt)
eval_single(experiment_folder, folder, split, model, short_name)
if llm_ollama is not None:
from models.evaluators.llm_ollama import LLM
if len(llm_ollama)==1:
full_name = llm_ollama[0]
short_name = full_name
short_name = f"LLMeval_{short_name}"
elif len(llm_ollama)==2:
full_name = llm_ollama[0]
short_name = llm_ollama[1]
short_name = f"LLMeval_{short_name}"
model = LLM(full_name, batch_size=llm_batch_size, prompt=llm_prompt, basic_url=ollama_url)
eval_single(experiment_folder, folder, split, model, short_name)
if lid is not None:
from models.evaluators.lid import LID
model = LID(lid)
eval_single(experiment_folder, folder, split, model, "lid")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--experiments_folder', type=str, default="experiments/")
parser.add_argument('--folder', type=str, default=None)
parser.add_argument('--split', type=str, default='dev')
parser.add_argument('--bem', action='store_true')
parser.add_argument('--lid', type=str, default=None)
parser.add_argument('--llm', type=str, nargs='*', default=None,
help="""
Uses default HF inference mechanism for LLM evaluation. Requires up to 2 arguments:
- full model name and short name (used for naming output files and metrics): eg. -llm \"Upstage/SOLAR-10.7B-Instruct-v1.0\" solar
- if short name is missing: use full name in naming,
- if no arguments specified: falls back to default arguments: uses default values (\"Upstage/SOLAR-10.7B-Instruct-v1.0\" solar).
""")
parser.add_argument('--vllm', type=str, nargs='*', default=None,
help="""
Calls vllm to run evalution. Requires 2 arguments:
Uses default HF inference mechanism for LLM evaluation. Requires up to 2 arguments:
- full model name and short name (used for naming output files and metrics): eg. -vllm \"Upstage/SOLAR-10.7B-Instruct-v1.0\" solar
- if short name is missing: use full name in naming,
- if no arguments specified: falls back to default arguments: uses default values (\"Upstage/SOLAR-10.7B-Instruct-v1.0\" solar).
""")
parser.add_argument('--llm_ollama', type=str, nargs='*', default=None,
help="""
Calls ollama server to run evaluation. Requires 1 or 2 arguments:
- full model name and short name (used for naming output files and metrics): eg. -llm_ollama llama3:default llama3
- if short name is missing: use full name in naming
""" )
parser.add_argument('--gpt', type=str,default=None)
parser.add_argument('--bem_batch_size', type=int, default=1024)
parser.add_argument('--llm_batch_size', type=int, default=1)
parser.add_argument('--force', action='store_true')
parser.add_argument('--llm_prompt', type=str, default="default_prompt", help="Provide yaml config file with updated prompt. Default prompt: config/evaluator/default_prompt.yaml")
parser.add_argument('--ollama_url', type=str, default="http://localhost:11434", help="")
args = parser.parse_args()
e = Evaluate.eval(
args.experiments_folder,
args.split,
bem=args.bem,
llm=args.llm,
llm_ollama=args.llm_ollama,
vllm=args.vllm,
gpt=args.gpt,
lid=args.lid,
bem_batch_size=args.bem_batch_size,
llm_batch_size=args.llm_batch_size,
llm_prompt=args.llm_prompt,
ollama_url=args.ollama_url,
folder=args.folder,
force=args.force
)