-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_self_reflection.py
308 lines (231 loc) · 12.7 KB
/
generate_self_reflection.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
from vllm import LLM, SamplingParams
from utils.prompter import Prompter
import argparse
import jsonlines
from tqdm import tqdm
import torch
import json
import os
from transformers import PreTrainedModel, LlamaConfig, LlamaModel, LlamaTokenizer
import torch.nn as nn
import torch
from typing import Optional, List
ultrarm_template= "Human: {instruction}\nAssistant: {response}"
class LlamaRewardModel(PreTrainedModel):
config_class = LlamaConfig
def __init__(self, config):
super().__init__(config)
self.model = LlamaModel(config)
self.regression_head = nn.Linear(self.config.hidden_size, 1, bias=False)
def forward( # args are the same as LlamaForCausalLM
self,
input_ids: torch.LongTensor = None,
attention_mask: Optional[torch.Tensor] = None,
position_ids: Optional[torch.LongTensor] = None,
past_key_values: Optional[List[torch.FloatTensor]] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
labels: Optional[torch.LongTensor] = None,
use_cache: Optional[bool] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
):
transformer_outputs = self.model(
input_ids,
attention_mask=attention_mask,
position_ids=position_ids,
past_key_values=past_key_values,
inputs_embeds=inputs_embeds,
)
hidden_states = transformer_outputs[0]
rewards = self.regression_head(hidden_states).squeeze(-1)
ends = attention_mask.cumsum(dim=1).argmax(dim=1).view(-1,1)
rewards = torch.gather(rewards, 1, ends)
return rewards
def save_data_to_json(strings, file_name):
with open(file_name, 'a', encoding='utf-8') as file:
writer = jsonlines.Writer(file)
for string in strings:
writer.write(string)
def evaluate_batch(args, model, prompter, instructions, inputs=None, N=1):
sampling_params = SamplingParams(temperature=0.8,
top_p=1,
top_k=50,
max_tokens=args.max_tokens,
stop = '</s>',
presence_penalty=1.2
)
if inputs is None:
prompts = [prompter.generate_prompt(instruction, "") for instruction in instructions]
else:
prompts = [prompter.generate_prompt(instruction, input) for instruction, input in zip(instructions, inputs)]
print(prompts[0])
print('\n\n------------\n\n')
all_responses = [None] * len(instructions)
for iii in range(N):
print(f'round {iii+1} is going on ...')
outputs = model.generate(prompts, sampling_params)
outputs = [ (int(kk.request_id), kk) for kk in outputs ]
sorted_list = sorted(outputs, key=lambda x: x[0])
outputs = [x[1] for x in sorted_list]
responses = [response.outputs[0].text for response in outputs]
print(responses[-1])
for i, response in enumerate(responses):
if all_responses[i] == None:
all_responses[i] = []
all_responses[i].append(response)
return all_responses
def main(
):
parser = argparse.ArgumentParser(prog='Generate', description='Generate responses on the eval set')
parser.add_argument('-o,', '--output', required=True, help="File path to the output responses")
parser.add_argument('--base_model', required=True, help="the base model")
parser.add_argument('--reward_model', required=False, help="the reward model")
parser.add_argument('--prompt_template', required=True, help="path to prompt template")
parser.add_argument('--input', type=str, required=False, help="")
parser.add_argument('--max_tokens', type=int, default=1024, required=False, help="")
parser.add_argument('--seed', type=int, default=0, required=False, help="")
parser.add_argument('--path_to_gen_self_feedback', type=str, required=True, help="")
parser.add_argument('--path_to_revise_w_feedback', type=str, required=True, help="")
parser.add_argument('--path_to_revise_wo_feedback', type=str, required=False, help="")
parser.add_argument('--mode', choices=['sample_N', 'sample_N_wo_prefer', 'self_reflection', 'tree_search', 'sample_greedy_search', 'tree_search_wo_feedback'], required=True, help="")
parser.add_argument('--n_sample', type=int, default=8, required=False, help="")
parser.add_argument('--path_user_preference', type=str, required=False, help="")
parser.add_argument('--batch_size', type=int, default=10, required=False, help="")
args = parser.parse_args()
if args.mode == 'sample_N':
sample_N(args)
elif args.mode == 'tree_search':
tree_search(args)
elif args.mode == 'tree_search_wo_feedback':
tree_search_wo_feedback(args)
def sample_N(args):
model = LLM(model=args.base_model, tensor_parallel_size=torch.cuda.device_count())
prompter = Prompter(args.prompt_template)
data_with_response = {}
if os.path.exists(args.output):
with jsonlines.open(args.output) as reader:
for item in reader:
data_with_response[item['id']] = ""
print(data_with_response)
root_user_preference = ''
if args.path_user_preference and os.path.exists(args.path_user_preference):
with open(args.path_user_preference, 'r') as f:
root_user_preference = f.read().strip()
instructions, all_items = [], []
with open(args.input, 'r') as f:
content = f.readlines()
for item in content:
item = json.loads(item)
if item['id'] in data_with_response:
continue
if root_user_preference == 'preference_2':
c_inst = item['instruction'] + '\n\n' + item['preference_2']
elif root_user_preference == 'preference_1':
c_inst = item['instruction'] + '\n\n' + item['preference_1']
else:
c_inst = item['instruction'] + '\n\n' + root_user_preference
instructions.append(c_inst.strip())
all_items.append(item)
### best of N
for i in tqdm(range(0, len(all_items), args.batch_size)):
c_instructions, c_items = [], []
for j in range(args.batch_size):
if i + j <= len(all_items)-1:
c_instructions.append(instructions[i+j])
c_items.append(all_items[i+j])
c_all_responses = evaluate_batch(args, model, prompter, c_instructions, N=args.n_sample)
for i, c_responses in enumerate(c_all_responses):
c_items[i]['responses'] = c_responses
## save the data
save_data_to_json(c_items, args.output)
def tree_search(args):
model = LLM(model=args.base_model, tensor_parallel_size=torch.cuda.device_count())
prompter = Prompter(args.prompt_template)
with open(args.path_to_gen_self_feedback, 'r') as f:
prompt_self_feedback = f.read()
with open(args.path_to_revise_w_feedback, 'r') as f:
prompt_revise_w_feedback = f.read()
root_user_preference = ''
if args.path_user_preference and os.path.exists(args.path_user_preference):
with open(args.path_user_preference, 'r') as f:
root_user_preference = f.read().strip()
instructions, all_preferences, best_responses_1st, all_items = [], [], [], []
with open(args.input, 'r') as f:
content = f.readlines()
for item in content:
item = json.loads(item)
if root_user_preference == 'preference_2':
c_inst, best_response_1st = item['instruction'] + '\n\n' + item['preference_2'], item['best_response']
all_preferences.append(item['preference_2'])
elif root_user_preference == 'preference_1':
c_inst, best_response_1st = item['instruction'] + '\n\n' + item['preference_1'], item['best_response']
all_preferences.append(item['preference_1'])
else:
c_inst, best_response_1st = item['instruction'] + '\n\n' + root_user_preference, item['best_response']
all_preferences.append(root_user_preference)
instructions.append(c_inst.strip())
best_responses_1st.append(best_response_1st)
all_items.append(item)
for i in tqdm(range(0, len(all_items), args.batch_size)):
c_instructions, c_best_responses_1st, c_items, c_preferences = [], [], [], []
for j in range(args.batch_size):
if i + j <= len(all_items)-1:
c_instructions.append(instructions[i+j])
c_items.append(all_items[i+j])
c_best_responses_1st.append(best_responses_1st[i+j])
c_preferences.append(all_preferences[i+j])
## generate one feedback
packed_1 = [prompt_self_feedback.format(question=question, answer = best_response_1st, preference = c_preference ) for question, best_response_1st, c_preference in zip(c_instructions, c_best_responses_1st, c_preferences)]
c_issues_of_1st_response = evaluate_batch(args, model, prompter, packed_1, N=1)
## sample N responses
packed_2 = [prompt_revise_w_feedback.format(question=question, answer=answer, preference=c_preference, feedback=issue[0]) for question, answer, c_preference, issue in zip(c_instructions, c_best_responses_1st, c_preferences, c_issues_of_1st_response)]
c_responses_2nd = evaluate_batch(args, model, prompter, packed_2, N=args.n_sample)
for i, (response_1st, issue, response_2nd) in enumerate(zip(c_best_responses_1st, c_issues_of_1st_response, c_responses_2nd)):
c_items[i]['response_1st'] = response_1st
c_items[i]['issue_of_1st_response'] = issue[0]
c_items[i]['responses'] = response_2nd
save_data_to_json(c_items, args.output)
def tree_search_wo_feedback(args):
model = LLM(model=args.base_model, tensor_parallel_size=torch.cuda.device_count())
prompter = Prompter(args.prompt_template)
with open(args.path_to_revise_wo_feedback, 'r') as f:
prompt_revise_wo_feedback = f.read()
root_user_preference = ''
if args.path_user_preference and os.path.exists(args.path_user_preference):
with open(args.path_user_preference, 'r') as f:
root_user_preference = f.read().strip()
instructions, all_preferences, best_responses_1st, all_items = [], [], [], []
with open(args.input, 'r') as f:
content = f.readlines()
for item in content:
item = json.loads(item)
if root_user_preference == 'preference_2':
c_inst, best_response_1st = item['instruction'] + '\n\n' + item['preference_2'], item['best_response']
all_preferences.append(item['preference_2'])
elif root_user_preference == 'preference_1':
c_inst, best_response_1st = item['instruction'] + '\n\n' + item['preference_1'], item['best_response']
all_preferences.append(item['preference_1'])
else:
c_inst, best_response_1st = item['instruction'] + '\n\n' + root_user_preference, item['best_response']
all_preferences.append(root_user_preference)
instructions.append(c_inst.strip())
best_responses_1st.append(best_response_1st)
all_items.append(item)
for i in tqdm(range(0, len(all_items), args.batch_size)):
c_instructions, c_best_responses_1st, c_items, c_preferences = [], [], [], []
for j in range(args.batch_size):
if i + j <= len(all_items)-1:
c_instructions.append(instructions[i+j])
c_items.append(all_items[i+j])
c_best_responses_1st.append(best_responses_1st[i+j])
c_preferences.append(all_preferences[i+j])
## sample N responses w/o feedback
packed_2 = [ prompt_revise_wo_feedback.format(question=question, answer = answer, preference = c_preference ) for question, answer, c_preference in zip(c_instructions, c_best_responses_1st, c_preferences) ]
c_responses_2nd = evaluate_batch(args, model, prompter, packed_2, N=args.n_sample)
for i, (response_1st, response_2nd) in enumerate(zip(c_best_responses_1st, c_responses_2nd)):
c_items[i]['response_1st'] = response_1st
c_items[i]['responses'] = response_2nd
save_data_to_json(c_items, args.output)
if __name__ == "__main__":
main()