-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils_scoring.py
229 lines (229 loc) · 11.6 KB
/
utils_scoring.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
import torch, time, numpy as np
import utils_misc
from bandit_alg import Exp3
class ScorerWrapper:
def __init__(self, scorers, learning_mode = "single", bandit = None, scoring_method="logsum",\
max_batch_size=100, use_caching=False, rl_validation_step=20, total_step_count=1000):
assert scoring_method in ["sum", "product", "logsum"], "Unrecognized `scoring_method`"
self.scorers = scorers
self.scoring_method = scoring_method
self.use_caching = use_caching
self.cache = {}
self.rl_validation_step = rl_validation_step
self.max_batch_size = max_batch_size
if self.scoring_method == "logsum":
self.score_func = logsum_score
elif self.scoring_method == "product":
self.score_func = product_score
elif self.scoring_method == "sum":
self.score_func = sum_score
self.learning_mode = learning_mode
self.used_scorers = []
if self.learning_mode == "bandit_weighted" or self.learning_mode == "argmin" or self.learning_mode == "contextual":
self.weight_bandit = Exp3(len(self.scorers))
self.total_step_count = total_step_count
def get_score_names(self):
return [s["name"] for s in self.scorers]
def make_key(self, inp, gen):
return "%s|||___|||%s" % (inp, gen)
def score(self, inputs, generateds, partial=False, printing=False, timings=False, \
extras={}, progress=False, responses=None, step_count=None, bandit=None, chosen=None):
assert len(inputs) == len(generateds), "Input and output lengths don't match"
if self.learning_mode in ["bandit", "bandit_weighted"] and bandit is None:
raise Exception("Bandit is not defined")
if self.learning_mode == "single" or self.learning_mode == "weighted":
self.used_scorers = self.scorers
elif self.learning_mode == "bandit":
if chosen != None:
self.used_scorers = [self.scorers[chosen]]
print("Used scorers:", self.used_scorers)
elif self.learning_mode == "bandit_weighted":
self.used_scorers = self.scorers
if chosen != None:
if int(chosen) == len(self.scorers):
pass
else:
self.weight_bandit(1, chosen)
print("Weight Bandit:", self.weight_bandit.weights)
weights = self.weight_bandit.weights
weights = weights / np.sum(weights)
self.used_scorers = self.scorers
for i, scorer in enumerate(self.used_scorers):
scorer["weight"] = weights[i]
print([scorer["weight"] for scorer in self.used_scorers])
elif self.learning_mode == "argmin":
if chosen != None:
self.weight_bandit(1, chosen)
print("Weight Bandit:", self.weight_bandit.weights)
weights = self.weight_bandit.weights
weights = weights / np.sum(weights)
self.used_scorers = self.scorers
for i, scorer in enumerate(self.used_scorers):
scorer["weight"] = weights[i]
print([scorer["weight"] for scorer in self.used_scorers])
elif self.learning_mode == "bayes":
if chosen != None:
chosen = [c / np.sum(chosen) for c in chosen]
self.used_scorers = self.scorers
for i, scorer in enumerate(self.used_scorers):
scorer["weight"] = chosen[i]
print("Bayes set weights")
print([scorer["weight"] for scorer in self.used_scorers])
elif self.learning_mode == "contextual":
self.used_scorers = self.scorers
if chosen != None:
if int(chosen[0]) == len(self.scorers):
pass
else:
self.weight_bandit(1, int(chosen[0]))
print("Weight Bandit:", self.weight_bandit.weights)
weights = self.weight_bandit.weights
weights = weights / np.sum(weights)
self.used_scorers = self.scorers
for i, scorer in enumerate(self.used_scorers):
scorer["weight"] = weights[i]
print([scorer["weight"] for scorer in self.used_scorers])
elif self.learning_mode == "round":
self.used_scorers = self.scorers
interval = int(self.total_step_count / len(self.scorers))
chosen = step_count // interval
chosen = chosen % len(self.scorers)
print("Chosen:", chosen)
self.used_scorers = [self.scorers[chosen]]
print("Used scorers:", self.used_scorers)
else:
self.used_scorers = self.scorers
if not self.use_caching:
self.cache = {}
todo = []
all_keys = []
for inp, gen, response in zip(inputs, generateds, responses):
key = self.make_key(inp, gen)
all_keys.append(key)
if key not in self.cache:
todo.append({"inp": inp, "gen": gen, "key": key, "response": response})
for d in todo:
self.cache[d["key"]] = {}
if self.use_caching and len(todo) < len(all_keys):
print("With caching, only processing: %d / %d samples" % (len(todo), len(all_keys)))
if len(todo) == 0:
progress = False
for batch_todo in utils_misc.batcher(todo, batch_size=self.max_batch_size, progress=progress):
batch_inputs = [d["inp"] for d in batch_todo]
batch_gens = [d["gen"] for d in batch_todo]
batch_responses = [d["response"] for d in batch_todo]
batch_scores, timings_out = self.score_func(self.used_scorers, batch_inputs, batch_gens,\
partial=partial, printing=printing, extras=extras, responses=batch_responses)
for k, out in batch_scores.items():
if type(out) in [torch.Tensor, np.array, np.ndarray]:
out = out.tolist()
for i, d in enumerate(batch_todo):
self.cache[d["key"]][k] = out[i]
if timings:
print(timings_out)
all_outputs = {}
for k in self.cache[all_keys[0]].keys():
all_outputs[k] = [self.cache[key][k] for key in all_keys]
if printing:
print("[total]", all_outputs["total_scores"])
return all_outputs
def rl_score(self, inputs, generateds, partial=False, printing=False, timings=False, \
extras={}, progress=False, responses=None, step_count=None, bandit=None, chosen=None):
assert len(inputs) == len(generateds), "Input and output lengths don't match"
if not self.use_caching:
self.cache = {}
todo = []
all_keys = []
for inp, gen, response in zip(inputs, generateds, responses):
key = self.make_key(inp, gen)
all_keys.append(key)
if key not in self.cache:
todo.append({"inp": inp, "gen": gen, "key": key, "response": response})
for d in todo:
self.cache[d["key"]] = {}
if self.use_caching and len(todo) < len(all_keys):
print("With caching, only processing: %d / %d samples" % (len(todo), len(all_keys)))
if len(todo) == 0:
progress = False
for batch_todo in utils_misc.batcher(todo, batch_size=self.max_batch_size, progress=progress):
batch_inputs = [d["inp"] for d in batch_todo]
batch_gens = [d["gen"] for d in batch_todo]
batch_responses = [d["response"] for d in batch_todo]
batch_scores, timings_out = self.score_func(self.scorers, batch_inputs, batch_gens,\
partial=partial, printing=printing, extras=extras, responses=batch_responses)
for k, out in batch_scores.items():
if type(out) in [torch.Tensor, np.array, np.ndarray]:
out = out.tolist()
for i, d in enumerate(batch_todo):
self.cache[d["key"]][k] = out[i]
if timings:
print(timings_out)
all_outputs = {}
for k in self.cache[all_keys[0]].keys():
all_outputs[k] = [self.cache[key][k] for key in all_keys]
if printing:
print("[total]", all_outputs["total_scores"])
return all_outputs
def __call__(self, inputs, generateds, **kwargs):
return self.score(inputs, generateds, **kwargs)
def sum_score(scorers, paragraphs, generateds, partial=False, printing=False, extras={}):
total_scores = np.zeros((len(paragraphs)))
scorer_returns, timings = {}, {}
T = time.time()
for scorer in scorers:
scores = scorer['model'].score(paragraphs, generateds, partial=partial, printing=printing, **extras)
weight = scorer.get("weight", 1.0)
total_scores += scorer["sign"]*weight*np.array(scores['scores'])
scorer_returns.update({scorer['name']+"_"+k: v for k, v in scores.items()})
timings[scorer["name"]] = time.time()-T
T = time.time()
scorer_returns['total_scores'] = total_scores
return scorer_returns, timings
def product_score(scorers, paragraphs, generateds, partial=False, printing=False, extras={}):
total_scores = np.ones((len(paragraphs)))
scorer_returns, timings = {}, {}
T = time.time()
for scorer in scorers:
scores = scorer['model'].score(paragraphs, generateds, partial=partial, printing=printing, **extras)
if scorer['sign'] == 1:
total_scores *= np.array(scores['scores'])
else:
total_scores *= (1-np.array(scores['scores']))
scorer_returns.update({scorer['name']+"_"+k: v for k, v in scores.items()})
timings[scorer["name"]] = time.time()-T
T = time.time()
scorer_returns['total_scores'] = total_scores
return scorer_returns, timings
def logsum_score(scorers, paragraphs, generateds, partial=False, printing=False, \
extras={}, reflection_labels=None, responses=None):
total_scores = np.zeros((len(paragraphs)))
raw_total_scores = np.zeros((len(paragraphs)))
scorer_returns, timings = {}, {}
T = time.time()
for scorer in scorers:
if "disc" in scorer["name"] or "summary" in scorer["name"] or "cgen" in scorer["name"]:
scores = scorer['model'].score(paragraphs, generateds, partial=partial, printing=printing, \
reflection_labels=reflection_labels, responses=responses, **extras)
else:
scores = scorer['model'].score(paragraphs, generateds, partial=partial, printing=printing, responses = responses,**extras)
weight = scorer.get("weight", 1.0)
if not scorer["name"].endswith("perplexity"):
scores["scores"] = np.clip(scores["scores"], 0.0001, 0.9999)
"""
if "reflection" not in scorer["name"]:
scores["scores"] = np.clip(scores["scores"], 0.0001, 0.9999)
if "reflection" in scorer["name"]:
total_scores += weight*np.log(1+np.array(scores['scores']))
"""
if scorer['sign'] == 1:
total_scores += weight*np.log(np.array(scores['scores']))
raw_total_scores += np.log(np.array(scores['scores']))
else:
total_scores += weight * np.log(1-np.array(scores["scores"]))
raw_total_scores += np.log(1-np.array(scores["scores"]))
scorer_returns.update({scorer['name']+"_"+k: v for k, v in scores.items()})
timings[scorer["name"]] = time.time()-T
T = time.time()
scorer_returns['total_scores'] = total_scores
scorer_returns['raw_total_scores'] = raw_total_scores
return scorer_returns, timings