-
Notifications
You must be signed in to change notification settings - Fork 2
/
evaluation.py
140 lines (131 loc) · 5.99 KB
/
evaluation.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
from vllm import LLM, SamplingParams
from src.evaluation import *
import argparse
def parse_args():
"""
Function to parse arguments
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--base_model_path",
type=str,
)
parser.add_argument(
"--lora_path",
type=str,
)
parser.add_argument(
"--split",
type=str,
default="validation",
)
parser.add_argument("--temperature", type=float, default=0.0)
parser.add_argument("--max_tokens", type=int, default=4096)
parser.add_argument("--stopwords", nargs="*", default=[])
parser.add_argument(
"--min_cots",
type=int,
default=1,
help="Min. number of CoTs you want to generate (min = 1)",
)
parser.add_argument(
"--max_cots",
type=int,
default=1,
help="Max. number of CoTs you want to generate (min = 1)",
)
parser.add_argument("--postprocess_responses", action="store_true")
parser.add_argument("--chat_format", type=str, help="Options: llama_chat_simple, llama_chat_v2, llama_cot_chat, None")
parser.add_argument("--do_self_consistency", action="store_true")
parser.add_argument("--num_samples_self_consistency", type=int, default=5)
parser.add_argument("--self_consistency_prompt_k", type=int, default=1)
parser.add_argument("--tasks2k_path", type=str, help="Path to the json file that specifies the best number of CoTs (k) per task.")
parser.add_argument("--dcot_self_consistency", action="store_true")
args = parser.parse_args()
return args
def run_benchmark(llm, sampling_params, enable_lora, ARGS):
if enable_lora:
results = benchmark(llm, sampling_params, ARGS.lora_path)
else:
results = benchmark(
llm,
sampling_params,
lora_path=None,
output_base_path=ARGS.base_model_path,
chat_format=ARGS.chat_format
)
return results
def run_self_consistency(ARGS):
sampling_params = SamplingParams(temperature=ARGS.temperature,
max_tokens=ARGS.max_tokens,
stop=ARGS.stopwords)
enable_lora = ARGS.lora_path is not None
llm = LLM(model=ARGS.base_model_path, enable_lora=enable_lora, max_lora_rank=64)
benchmark = BenchmarkEvaluator(ARGS.split, k=ARGS.self_consistency_prompt_k, chat_format=ARGS.chat_format)
if enable_lora:
results = benchmark.self_consistency(llm,
sampling_params,
ARGS.lora_path,
postprocess_responses=ARGS.postprocess_responses,
self_consistency_k=ARGS.num_samples_self_consistency
)
else:
results = benchmark.self_consistency(llm,
sampling_params,
lora_path=None,
output_base_path=ARGS.base_model_path,
postprocess_responses=ARGS.postprocess_responses,
self_consistency_k=ARGS.num_samples_self_consistency
)
print(results)
if __name__ == "__main__":
print("Starting")
ARGS = parse_args()
if ARGS.do_self_consistency:
run_self_consistency(ARGS)
else:
sampling_params = SamplingParams(temperature=ARGS.temperature, max_tokens=ARGS.max_tokens, stop=ARGS.stopwords)
enable_lora = ARGS.lora_path is not None
llm = LLM(model=ARGS.base_model_path, enable_lora=enable_lora, max_lora_rank=64)
if ARGS.split == "test" and ARGS.tasks2k_path is not None:
with open(ARGS.tasks2k_path) as f:
tasks2k = json.load(f)
print(tasks2k)
benchmark = BenchmarkEvaluator(ARGS.split, k=1, chat_format=ARGS.chat_format)
if enable_lora:
results = benchmark.test_set_eval(tasks2k,
ARGS.chat_format,
llm,
sampling_params,
ARGS.lora_path,
postprocess_responses=ARGS.postprocess_responses,
self_consistency=ARGS.dcot_self_consistency,
num_samples_self_consistency=ARGS.num_samples_self_consistency)
else:
results = benchmark.test_set_eval(tasks2k,
ARGS.chat_format,
llm,
sampling_params,
lora_path=None,
output_base_path=ARGS.base_model_path,
postprocess_responses=ARGS.postprocess_responses,
self_consistency=ARGS.dcot_self_consistency,
num_samples_self_consistency=ARGS.num_samples_self_consistency)
print(results)
else:
for k in range(ARGS.min_cots, ARGS.max_cots+1):
benchmark = BenchmarkEvaluator(ARGS.split, k=k, chat_format=ARGS.chat_format)
if enable_lora:
results = benchmark(llm,
sampling_params,
ARGS.lora_path,
postprocess_responses=ARGS.postprocess_responses
)
else:
results = benchmark(llm,
sampling_params,
lora_path=None,
output_base_path=ARGS.base_model_path,
postprocess_responses=ARGS.postprocess_responses)
print(f"Fininshed evaluation for k {k}")
print(results)