forked from togethercomputer/MoA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_for_alpaca_eval.py
155 lines (116 loc) · 3.85 KB
/
generate_for_alpaca_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
import json
import datasets
from fire import Fire
from functools import partial
from typing import List
from loguru import logger
from utils import (
generate_together,
generate_openai,
generate_with_references,
DEBUG,
)
def process_fn(
item,
model,
reference_models=[],
temperature=0.7,
max_tokens=2048,
rounds=1,
):
messages = [{"role": "user", "content": item["instruction"]}]
references = item.get("references", [])
if len(references) == 0 and len(reference_models) > 0:
prev_references = []
for i_round in range(rounds):
if DEBUG:
logger.info(
f"Round {i_round+1}/{rounds} to collecting reference responses."
)
references = []
for reference_model in reference_models:
reference = generate_with_references(
model=reference_model,
messages=messages,
references=prev_references,
temperature=temperature,
max_tokens=max_tokens,
)
if reference is not None:
references.append(reference)
if i_round < rounds - 1:
prev_references = references
references = []
output = generate_with_references(
model=model,
messages=messages,
references=references,
)
return {"output": output, "generator": model + "-together"}
def main(
model: str,
output_path: str,
reference_paths: str = None,
reference_models: str = None,
temperature: float = 0.7,
max_tokens: int = 2048,
rounds: int = 1,
num_proc: int = 16,
):
if reference_paths is None:
reference_paths = []
else:
reference_paths = reference_paths.split(",")
if reference_models is None:
reference_models = []
else:
reference_models = reference_models.split(",")
eval_set = datasets.load_dataset(
"tatsu-lab/alpaca_eval", "alpaca_eval_gpt4_baseline", trust_remote_code=True
)["eval"]
eval_set = eval_set.remove_columns(["output", "generator"])
if len(reference_paths):
logger.info(f"`reference_paths` provided: {reference_paths}")
references = []
for reference_path in reference_paths:
with open(reference_path) as f:
reference_responses = json.load(f)
logger.info(
f"Reading reference outputs: {reference_path} ({len(reference_responses)})"
)
for i_reference_response, reference_response in enumerate(
reference_responses
):
if len(references) <= i_reference_response:
references.append([reference_response["output"]])
else:
references[i_reference_response].append(
reference_response["output"]
)
eval_set = eval_set.add_column(f"references", references)
elif len(reference_models):
logger.info(
f"`reference_models` provided: {reference_models}. Will generate reference responses on-the-fly."
)
logger.info(f"Start.")
eval_set = eval_set.map(
partial(
process_fn,
model=model,
reference_models=reference_models,
temperature=temperature,
max_tokens=max_tokens,
rounds=rounds,
),
batched=False,
num_proc=num_proc,
)
logger.info(f"Saving outputs to {output_path}.")
try:
eval_set = eval_set.remove_columns(f"references")
except Exception as e:
pass
with open(output_path, "w") as f:
json.dump(list(eval_set), f, indent=2)
if __name__ == "__main__":
Fire(main)