-
Notifications
You must be signed in to change notification settings - Fork 90
/
reclor.py
280 lines (235 loc) · 10.4 KB
/
reclor.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
import json
import os
import re
from typing import Dict, List, Any, Union, Callable
import numpy as np
import torch
from torch import distributed as dist
from post_processors.dist_mixin import DistGatherMixin
class NumpySaver(DistGatherMixin):
def __init__(self, save_copy: bool = False):
self.predictions = []
self.index = []
self.save_copy = save_copy
def __call__(self, meta_data: List[Dict[str, Any]], batch_model_outputs: Dict[str, Any], ddp: bool = False):
logits = batch_model_outputs["logits"].detach().float()
_, pred = logits.max(dim=-1)
pred = pred.tolist()
index = None
if ddp:
assert meta_data
if isinstance(meta_data, list):
index = [meta['index'].item() for meta in meta_data]
elif isinstance(meta_data, dict):
if isinstance(meta_data["index"], torch.Tensor):
index = meta_data["index"].tolist()
else:
index = meta_data["index"]
else:
raise RuntimeError()
obj = [pred, index]
gather_res = self.gather_object(obj)
if dist.get_rank() == 0:
pred = []
index = []
for item in gather_res:
pred.extend(item[0])
index.extend(item[1])
if index is not None:
self.index.extend(index)
self.predictions.extend(pred)
def get_results(self, output_dir: str):
# output_file = os.path.join(output_dir, "eval_predictions.npy")
if dist.is_initialized():
output_file = os.path.join(output_dir, f"eval_predictions_rank{dist.get_rank()}.npy")
else:
output_file = os.path.join(output_dir, "eval_predictions.npy")
if len(self.index):
assert len(self.index) == len(self.predictions)
predictions = {idx: pred for idx, pred in zip(self.index, self.predictions)}
predictions = sorted(predictions.items(), key=lambda x: x[0])
predictions = [pred[1] for pred in predictions]
np.save(output_file, np.array(predictions))
else:
np.save(output_file, np.array(self.predictions))
if self.save_copy:
if dist.is_initialized():
output_file = os.path.join(output_dir, f"eval_predictions_copy_rank{dist.get_rank()}.bin")
else:
output_file = os.path.join(output_dir, "eval_predictions_copy.bin")
torch.save({
"index": self.index,
"predictions": self.predictions
}, output_file)
return {}, self.predictions
class TaggingSaver(DistGatherMixin):
def __init__(self):
self.logits = []
self.index = []
def __call__(self, meta_data: List[Dict[str, Any]], batch_model_outputs: Dict[str, Any], ddp: bool = False):
tagging_logits = batch_model_outputs["tagging_logits"].detach().float().tolist()
index = None
if ddp:
assert meta_data
if isinstance(meta_data, list):
index = [meta['index'].item() for meta in meta_data]
elif isinstance(meta_data, dict):
index = meta_data["index"].tolist()
else:
raise RuntimeError()
obj = [tagging_logits, index]
gather_res = self.gather_object(obj)
if dist.get_rank() == 0:
tagging_logits = []
index = []
for item in gather_res:
tagging_logits.extend(tagging_logits)
index.extend(item[1])
if index is not None:
self.index.extend(index)
self.logits.extend(tagging_logits)
def get_results(self, output_dir: str):
if dist.is_initialized():
output_file = os.path.join(output_dir, f"tagging_logits_rank{dist.get_rank()}.json")
else:
output_file = os.path.join(output_dir, "tagging_logits.json")
if len(self.index):
assert len(self.index) == len(self.logits)
predictions = {idx: pred for idx, pred in zip(self.index, self.logits)}
predictions = sorted(predictions.items(), key=lambda x: x[0])
predictions = [pred[1] for pred in predictions]
json.dump(predictions, open(output_file, "w"))
else:
json.dump(self.logits, open(output_file, "w"))
return {}, self.logits
def answer_clean(pred_seq: str, reverse: bool = False, answer_trigger: str = "The answer is"):
if answer_trigger:
pred_seq = pred_seq.split(answer_trigger)[1]
# pred = re.findall(r'A|B|C|D|E', pred_seq)
pred = re.findall(r'A|B|C|D', pred_seq)
if len(pred) == 0:
return ""
if reverse:
return pred[-1]
return pred[0]
class GeneratorPredictor(DistGatherMixin):
def __init__(self, reverse: bool = False, answer_trigger: str = "The answer is"):
self.predictions = []
self.reverse = reverse
self.answer_trigger = answer_trigger
def __call__(self, meta_data: Union[List[Dict[str, Any]], Dict[str, Any]], batch_model_outputs, ddp: bool = False):
labels = meta_data["label"]
prompt_index = meta_data["prompt_index"]
index = meta_data["index"].tolist()
inputs = meta_data["input"]
pred_seq = batch_model_outputs["generated_seq"]
assert len(labels) == len(prompt_index) == len(index) == len(inputs), (len(labels), len(prompt_index), len(index), len(inputs))
if len(pred_seq) == len(labels):
pass
elif len(pred_seq) % len(labels) == 0:
pass
else:
raise ValueError((len(pred_seq), len(labels)))
predictions = [
{
"label": label,
"index": idx,
"prompt_index": prompt_idx,
"output": res,
"cleaned_output": answer_clean(res, self.reverse, self.answer_trigger),
"input": src,
} for label, idx, prompt_idx, res, src in zip(labels, index, prompt_index, pred_seq, inputs)
]
if ddp:
gather_res = self.gather_object(predictions)
if dist.get_rank() == 0:
tmp = []
for item in gather_res:
tmp.extend(item)
predictions = tmp
self.predictions.extend(predictions)
def get_results(self, output_dir: str):
if dist.is_initialized():
output_file = os.path.join(output_dir, f"decode_results_rank{dist.get_rank()}.json")
else:
output_file = os.path.join(output_dir, "decode_results.json")
json.dump(self.predictions, open(output_file, "w"))
self.predictions = sorted(self.predictions, key=lambda x: x["index"])
correct = 0
existing_ids = set()
npy_outputs = []
for pred in self.predictions:
if pred["index"] in existing_ids:
continue
existing_ids.add(pred["index"])
if pred["label"] == pred["cleaned_output"]:
correct += 1
if pred["cleaned_output"]:
npy_outputs.append(ord(pred["cleaned_output"]) - ord("A"))
else:
npy_outputs.append(0)
if not dist.is_initialized() or dist.get_rank() == 0:
np.save(os.path.join(output_dir, "decode_results.npy"), np.array(npy_outputs))
assert len(npy_outputs) == len(existing_ids), (len(npy_outputs), len(self.predictions), len(existing_ids))
return {"acc": correct / len(existing_ids)}, self.predictions
class GeneratorPredictorV2(DistGatherMixin):
def __init__(self, answer_cleaner: Callable):
self.predictions = []
self.answer_cleaner = answer_cleaner
def __call__(self, meta_data: Union[List[Dict[str, Any]], Dict[str, Any]], batch_model_outputs, ddp: bool = False):
labels = meta_data["label"]
prompt_index = meta_data["prompt_index"]
index = meta_data["index"].tolist()
inputs = meta_data["input"]
pred_seq = batch_model_outputs["generated_seq"]
assert len(labels) == len(prompt_index) == len(index) == len(inputs), (len(labels), len(prompt_index), len(index), len(inputs))
if len(pred_seq) == len(labels):
pass
elif len(pred_seq) % len(labels) == 0:
pass
else:
raise ValueError((len(pred_seq), len(labels)))
predictions = [
{
"label": label,
"index": idx,
"prompt_index": prompt_idx,
"output": res,
"cleaned_output": self.answer_cleaner(res, src),
"input": src,
} for label, idx, prompt_idx, res, src in zip(labels, index, prompt_index, pred_seq, inputs)
]
if ddp:
gather_res = self.gather_object(predictions)
if dist.get_rank() == 0:
tmp = []
for item in gather_res:
tmp.extend(item)
predictions = tmp
self.predictions.extend(predictions)
def get_results(self, output_dir: str):
if dist.is_initialized():
output_file = os.path.join(output_dir, f"decode_results_rank{dist.get_rank()}.json")
else:
output_file = os.path.join(output_dir, "decode_results.json")
json.dump(self.predictions, open(output_file, "w"))
self.predictions = sorted(self.predictions, key=lambda x: x["index"])
correct = 0
existing_ids = set()
npy_outputs = []
for pred in self.predictions:
if pred["index"] in existing_ids:
continue
existing_ids.add(pred["index"])
if pred["label"] == pred["cleaned_output"]:
correct += 1
if pred["cleaned_output"] and len(pred["cleaned_output"]) == 1:
npy_outputs.append(ord(pred["cleaned_output"]) - ord("A"))
else:
npy_outputs.append(0)
metrics = {"acc": correct / len(existing_ids)}
if not dist.is_initialized() or dist.get_rank() == 0:
np.save(os.path.join(output_dir, "decode_results.npy"), np.array(npy_outputs))
json.dump(metrics, open(os.path.join(output_dir, "metrics.json"), "w"))
assert len(npy_outputs) == len(existing_ids), (len(npy_outputs), len(self.predictions), len(existing_ids))
return metrics, self.predictions