-
Notifications
You must be signed in to change notification settings - Fork 2
/
get_rewards.py
166 lines (130 loc) · 5.17 KB
/
get_rewards.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
import json
import os
from dataclasses import dataclass, field
from typing import Optional
import numpy as np
import torch
from datasets import load_dataset
from tqdm import tqdm
from transformers import AutoTokenizer, HfArgumentParser, pipeline
from accelerate import Accelerator
tqdm.pandas()
#####
# This script takes a dataset as the input, where each sample is {"prompt": "the pormpt", "responses": ["response1", "response2", "response3", ...]}
# The script will compute the reward for each input-output pair, and eventually output a new dataset, where each sample contains {"prompt": "the pormpt", "responses": ["response1", "response2", "response3", ...], "rewards": [reward1, reward2, ...]}
#####
@dataclass
class ScriptArguments:
"""
The arguments for the DPO training script.
"""
dataset_name_or_path: Optional[str] = field(
default="uf_split0_responses_K8.jsonl",
metadata={"help": "the location of the dataset name or path"},
)
output_dir: Optional[str] = field(
default="uf_split0_responses_K8_reward.json",
metadata={"help": "the location of the output file"},
)
record_dir: Optional[str] = field(
default=None,
metadata={"help": "the location of the recording file"},
)
reward_name_or_path: Optional[str] = field(
default="sfairXC/FsfairX-LLaMA3-RM-v0.1",
metadata={"help": "the name of the reward model"},
)
input_output_delimiter: Optional[str] = field(
default="",
metadata={"help": "the delimiter between input and output"},
)
K: Optional[int] = field(
default=8,
metadata={"help": "the number of responses per prompt"},
)
accelerator = Accelerator()
parser = HfArgumentParser(ScriptArguments)
script_args = parser.parse_args_into_dataclasses()[0]
device = accelerator.device
pipe_kwargs = {
"return_all_scores": True,
"function_to_apply": "none",
"batch_size": 1,
}
reward_model = script_args.reward_name_or_path
rm_tokenizer = AutoTokenizer.from_pretrained(reward_model)
rm_pipe = pipeline(
"sentiment-analysis",
model=reward_model,
device=device,
tokenizer=rm_tokenizer,
model_kwargs={"torch_dtype": torch.bfloat16},
truncation=True,
)
ds_dir = script_args.dataset_name_or_path
world_size = int(os.getenv("WORLD_SIZE", "1"))
ds = load_dataset("json", data_files=ds_dir, split="train")
local_rank = Accelerator().local_process_index
data_size = len(ds["prompt"])
share = int(data_size / world_size) + 1
ds = ds.select(np.arange(local_rank * share, min((local_rank + 1) * share, len(ds))))
"""
We process the data format here and query the reward model to get the rewards.
"""
def get_reward(test_texts):
pipe_outputs = rm_pipe(test_texts, **pipe_kwargs)
rewards = [output[0]["score"] for output in pipe_outputs]
return rewards
def change_of_format(prom, resp):
# To be modified according to the reward model and the LLM you use
# Be careful about multi-turn conversions
"""
prom = prom.replace("<s>GPT4 Correct User: ", "").replace("<|end_of_turn|>GPT4 Correct Assistant:", "")
final_resp = resp.split("GPT4 Correct User")[0]
"""
message = prom + [{"role": "assistant", "content": resp}]
return rm_tokenizer.apply_chat_template(message, tokenize=False).replace(rm_tokenizer.bos_token, "")
data = []
# tqdm is used to show the progress bar
with torch.no_grad():
for sample in tqdm(ds):
# The VLLM may not generate responses for some prompts because it is too long, we skip them
if len(sample["responses"]) < script_args.K:
continue
test_texts = [change_of_format(sample['prompt'], tmp_output) for tmp_output in sample['responses']]
rewards = get_reward(test_texts)
data.append({"prompt": sample["prompt"], "responses": sample["responses"], "rewards": rewards})
# Send the data to other GPUs
world_size = int(os.getenv("WORLD_SIZE", "1"))
all_process_list = [{}] * world_size
data_to_send = {
"data": [[data[i]] for i in range(len(data))],
}
import torch.distributed as dist
dist.all_gather_object(all_process_list, data_to_send)
gathered_data = []
for i in range(world_size):
tmp_data = [tmp[0] for tmp in all_process_list[i]["data"]]
gathered_data.extend(tmp_data)
all_rewards = [sample["rewards"] for sample in gathered_data]
top1_scores = np.mean(np.max(all_rewards, axis=1))
mean_scores = np.mean(all_rewards)
if local_rank == 0:
print(
"Collect {} data from {} inputs. mean score {} top1 score: {}".format(
len(gathered_data), data_size, mean_scores, top1_scores
)
)
if len(gathered_data) < data_size:
print(
"Some of the prompts are with responses < {}. This can happen because the prompt is too long and is ignored by VLLM".format(
script_args.K
)
)
with open(script_args.output_dir, "w", encoding="utf8") as f:
for i in range(len(gathered_data)):
json.dump(gathered_data[i], f, ensure_ascii=False)
f.write('\n')
if script_args.record_dir is not None:
with open(script_args.record_dir, "a") as f:
f.write(str(mean_scores) + "\t" + str(top1_scores) + "\n")