forked from thu-wyz/inference_scaling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrebase.py
More file actions
292 lines (246 loc) · 10.1 KB
/
rebase.py
File metadata and controls
292 lines (246 loc) · 10.1 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
import argparse
import json
import re
import time
from sglang import function, gen, RuntimeEndpoint
import fcntl
import os
import math
import yaml
import torch
import torch.nn.functional as F
import threading
def read_jsonl(path: str):
with open(path) as fh:
return [json.loads(line) for line in fh.readlines() if line]
def get_prompts(args):
test_cases = read_jsonl(args.input_path)
prompts = []
for test in test_cases:
prompts.append(test["problem"])
return prompts, test_cases
class TreeNode:
def __init__(self, id, state, score, num_step_tokens=0, parent=None):
self.id = id
self.state = state
self.text_ = state.text()
self.score_ = score
self.parent = parent
self.leaf_ = False
self.cum_tokens = 0
self.num_step_tokens = 0
if parent is not None and "The answer is" in self.text_:
self.leaf_ = True
if parent is not None:
self.depth = parent.get_depth() + 1
self.cum_tokens += num_step_tokens
else:
self.depth = 0
self.cum_tokens = num_step_tokens
def get_id(self):
return self.id
def get_parent(self):
return self.parent
def get_text(self):
return self.text_
def get_state(self):
return self.state
def get_depth(self):
return self.depth
def get_score(self):
return self.score_
def is_leaf(self):
return self.leaf_
def get_cum_tokens(self):
return self.cum_tokens
class Tree:
def __init__(self, root_state, paras, reward_backend):
self.size_ = 1
self.nodes = []
self.paras = paras
self.reward_backend = reward_backend
self.root_ = TreeNode(0,root_state, 1.0)
self.remaining_width = paras["width"]
self.history_list = []
self.running_list = []
self.depth_nodes = [[] for i in range(100)]
self.nodes.append(self.root_)
self.depth_nodes[0].append(self.root_)
def reset_running_list(self):
self.running_list = []
def get_running_list(self):
return self.running_list
def get_history_list(self):
return self.history_list
def get_nodes(self):
return self.nodes
def expand(self, node, wid):
state = node.get_state()
forks = state.fork(wid)
depth = node.get_depth()
for fork in forks:
fork.set_score_backend(self.reward_backend)
if self.paras["policy_model_type"] == "mistral" or self.paras["policy_model_type"] == "llemma":
fork += gen("step", self.paras["max_step_tokens"], stop="Step "+str(depth+2), temperature=self.paras["temperature"])
fork += gen("score", max_tokens=0, forward_only=True, logits_require_id=8094)
self.running_list.append((fork, node))
self.history_list.append(fork)
def insert(self, state, parent):
if state.scores() == [] or state.scores == None:
return
score = state.scores()[-1]
num_step_tokens = state.get_meta_info("step")["completion_tokens"]
new_node = TreeNode(self.size_, state, score, num_step_tokens, parent)
self.size_ += 1
depth = new_node.get_depth()
self.depth_nodes[depth].append(new_node)
self.nodes.append(new_node)
return
def select_softmax(self, node_list, node_weights, width):
node_weight_pair_list = [(node, weight) for node, weight in zip(node_list, node_weights)]
sorted_node_weight_pair_list = sorted(node_weight_pair_list, key=lambda pair: pair[1])
sorted_node_weight_pair_list.reverse()
nodes = []
weights = []
for pair in sorted_node_weight_pair_list:
nodes.append(pair[0])
weights.append(pair[1])
weights = torch.tensor(weights)
T = self.paras["softmax_temperature"]
exp_weights = torch.exp(weights / T)
sum_exp_weights = exp_weights.sum()
select_num = []
for weight in exp_weights:
num = int(math.ceil(width * weight / sum_exp_weights))
select_num.append(num)
width -= num
sum_exp_weights -= weight
return nodes, select_num
def select_softmax_with_truncation(self, node_list, node_weights, width):
node_weight_pair_list = [(node, weight) for node, weight in zip(node_list, node_weights)]
sorted_node_weight_pair_list = sorted(node_weight_pair_list, key=lambda pair: pair[1])
sorted_node_weight_pair_list.reverse()
nodes = []
weights = []
truncate_ratio = self.paras["truncate_ratio"]
keep_num = int(math.ceil(len(sorted_node_weight_pair_list) * truncate_ratio))
sorted_node_weight_pair_list = sorted_node_weight_pair_list[:keep_num]
for pair in sorted_node_weight_pair_list:
nodes.append(pair[0])
weights.append(pair[1])
weights = torch.tensor(weights)
T = self.paras["softmax_temperature"]
exp_weights = torch.exp(weights / T)
sum_exp_weights = exp_weights.sum()
select_num = []
for weight in exp_weights:
num = int(math.ceil(width * weight / sum_exp_weights))
select_num.append(num)
width -= num
sum_exp_weights -= weight
return nodes, select_num
def select_and_expand(self, depth):
cand_node_list = []
cand_node_weights = []
for node in self.depth_nodes[depth]:
if node.is_leaf() == True or node.get_cum_tokens() >= self.paras["max_tokens"]:
self.remaining_width -= 1
else:
cand_node_list.append(node)
cand_node_weights.append(node.get_score())
if self.remaining_width <= 0 or cand_node_list == []:
return False
if self.paras["select_method"] == "softmax":
nodes, widths = self.select_softmax(cand_node_list, cand_node_weights, self.remaining_width)
if self.paras["select_method"] == "softmax_with_truncate":
nodes, widths = self.select_softmax_with_truncation(cand_node_list, cand_node_weights, self.remaining_width)
for expand_node, width in zip(nodes, widths):
if width >= 1:
self.expand(expand_node, width)
return True
@function
def reward_guided_search(s, id, question, ground_truth_answer, paras, reward_host):
s += question
tree = Tree(s, paras, reward_host)
depth = 0
while True:
tree.reset_running_list()
continue_search = tree.select_and_expand(depth)
if continue_search == False:
break
running_list = tree.get_running_list()
for state, parent in running_list:
tree.insert(state, parent)
depth += 1
if depth >= 25:
break
history_list = tree.get_history_list()
total_tokens = 0
for state in history_list:
total_tokens += state.get_meta_info("step")["completion_tokens"]
all_nodes = tree.get_nodes()
answers = []
nodes_info = []
answer_store_path = paras["store_path"] + f"answer_q{id}.json"
for node in all_nodes:
if node.get_parent() is not None:
parent_id = node.get_parent().get_id()
else:
parent_id = None
nodes_info.append({
"id": node.get_id(),
"text": node.get_text(),
"score": node.get_score(),
"parent_id": parent_id,
"depth": node.get_depth(),
"leaf": node.is_leaf()
})
if node.is_leaf():
step_scores = []
last_node = node
while last_node.get_depth() > 0:
step_scores.append(last_node.get_score())
last_node = last_node.get_parent()
step_scores.reverse()
answers.append({"text":node.get_text(), "step_scores":step_scores})
answer_for_the_question = {"id":id, "question": question, "model_answer":answers, "ground_truth_answer": ground_truth_answer["answer"], "total_tokens":total_tokens}
json.dump(answer_for_the_question, open(answer_store_path, "w"), indent=4)
return answer_for_the_question
def search_worker(search_dict, lock, prompts, test_examples, paras, policy_host, reward_host):
while True:
q_id = None
with lock:
for key in search_dict:
if search_dict[key] == False:
search_dict[key] = True
q_id = int(key)
break
if q_id == None:
break
state = reward_guided_search.run(id=q_id, question=prompts[q_id], ground_truth_answer=test_examples[q_id], paras=paras, reward_host=RuntimeEndpoint(reward_host), backend=RuntimeEndpoint(policy_host))
answer_for_the_question = state.ret_value
return answer_for_the_question
def main(args):
prompts, test_examples = get_prompts(args)
with open(args.parameter_path ,'r', encoding='utf-8') as file:
paras = yaml.safe_load(file)
input_list_dict = []
for i, prompt in enumerate(prompts):
input_list_dict.append({"id":i, "question":prompt, "ground_truth_answer":test_examples[i], "paras":paras, "reward_host":RuntimeEndpoint(args.reward_host)})
states = reward_guided_search.run_batch(input_list_dict, backend=RuntimeEndpoint(args.policy_host), num_threads=paras["num_threads"], progress_bar=True)
results = []
total_gen_tokens = 0
for s in states:
answer = s.ret_value
total_gen_tokens += answer["total_tokens"]
results.append(answer)
json.dump(results, open(args.output_path, "w"), indent=4)
if __name__ == "__main__":
args_parser = argparse.ArgumentParser()
args_parser.add_argument('--input_path', type=str, required=True)
args_parser.add_argument('--output_path', type=str, required=True)
args_parser.add_argument('--parameter_path', type=str, required=True)
args_parser.add_argument('--policy_host', type=str)
args_parser.add_argument('--reward_host', type=str)
args = args_parser.parse_args()
main(args)