forked from NVIDIA-NeMo/RL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_rm.py
More file actions
254 lines (212 loc) · 8.45 KB
/
run_rm.py
File metadata and controls
254 lines (212 loc) · 8.45 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
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import logging
import os
import pprint
from typing import Any
from omegaconf import OmegaConf
from transformers import AutoTokenizer
from nemo_rl.algorithms.rm import MasterConfig, rm_train, setup
from nemo_rl.algorithms.utils import get_tokenizer
from nemo_rl.data import DataConfig
from nemo_rl.data.datasets import AllTaskProcessedDataset, load_preference_dataset
from nemo_rl.data.datasets.preference_datasets import PreferenceDataset
from nemo_rl.data.interfaces import DatumSpec, TaskDataSpec
from nemo_rl.data.llm_message_utils import get_formatted_message_log
from nemo_rl.distributed.virtual_cluster import init_ray
from nemo_rl.utils.config import load_config, parse_hydra_overrides
from nemo_rl.utils.logger import get_next_experiment_dir
def parse_args():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(description="Run RM training with configuration")
parser.add_argument(
"--config", type=str, default=None, help="Path to YAML config file"
)
# Parse known args for the script
args, overrides = parser.parse_known_args()
return args, overrides
# =======================================================
# Data Processing
# =======================================================
def rm_preprocessor(
datum_dict: dict[str, Any],
task_data_spec: TaskDataSpec,
tokenizer,
max_seq_length: int,
idx: int,
) -> DatumSpec:
"""Process a datum dictionary for RM training."""
assert len(datum_dict["completions"]) == 2, (
"RM training supports only two completions"
)
# Lower rank is preferred
if datum_dict["completions"][0]["rank"] < datum_dict["completions"][1]["rank"]:
chosen_completion = datum_dict["completions"][0]
rejected_completion = datum_dict["completions"][1]
elif datum_dict["completions"][0]["rank"] > datum_dict["completions"][1]["rank"]:
chosen_completion = datum_dict["completions"][1]
rejected_completion = datum_dict["completions"][0]
else:
raise NotImplementedError(
"Ties are not supported yet. You can use the following command to filter out ties: `cat <PathToPreferenceDataset> | jq 'select(.completions[0].rank != .completions[1].rank)'`."
)
messages_chosen = datum_dict["context"] + chosen_completion["completion"]
messages_rejected = datum_dict["context"] + rejected_completion["completion"]
message_log_chosen = get_formatted_message_log(
messages_chosen, tokenizer, task_data_spec
)
message_log_rejected = get_formatted_message_log(
messages_rejected, tokenizer, task_data_spec
)
length_chosen = sum(len(m["token_ids"]) for m in message_log_chosen)
length_rejected = sum(len(m["token_ids"]) for m in message_log_rejected)
loss_multiplier = 1.0
if max(length_chosen, length_rejected) > max_seq_length:
# make smaller and mask out
logging.warning(
f"Truncating chosen and rejected messages to {max_seq_length} tokens"
)
for message in message_log_chosen:
message["token_ids"] = message["token_ids"][
: min(4, max_seq_length // len(message_log_chosen))
]
for message in message_log_rejected:
message["token_ids"] = message["token_ids"][
: min(4, max_seq_length // len(message_log_rejected))
]
loss_multiplier = 0.0
length_chosen = sum(len(m["token_ids"]) for m in message_log_chosen)
length_rejected = sum(len(m["token_ids"]) for m in message_log_rejected)
# safeguard against edge case where there are too many turns to fit within the max length
assert max(length_chosen, length_rejected) <= max_seq_length
output = {
"message_log_chosen": message_log_chosen,
"length_chosen": length_chosen,
"message_log_rejected": message_log_rejected,
"length_rejected": length_rejected,
"extra_env_info": None,
"loss_multiplier": loss_multiplier,
"idx": idx,
}
return output
def setup_data(tokenizer: AutoTokenizer, data_config: DataConfig):
print("\n▶ Setting up data...")
# load dataset
data = load_preference_dataset(data_config)
train_dataset = data.formatted_ds["train"]
val_dataset = data.formatted_ds["validation"]
print(f" ✓ Training dataset loaded with {len(train_dataset)} samples.")
if val_dataset:
print(f" ✓ Validation dataset loaded with {len(val_dataset)} samples.")
rm_task_spec = data.task_spec
train_dataset = AllTaskProcessedDataset(
train_dataset,
tokenizer,
rm_task_spec,
rm_preprocessor,
max_seq_length=data_config["max_input_seq_length"],
)
# TODO @yukih: unify the code when support multiple datasets for other algorithms
if "val_data_paths" in data_config and data_config["val_data_paths"]:
val_dataset = {}
assert isinstance(data_config["val_data_paths"], dict), (
f"Invalid type for val_data_paths: {type(data_config['val_data_paths'])}. val_data_paths must be a dictionary."
)
val_data_paths = data_config["val_data_paths"]
for val_dataset_name, val_dataset_path in val_data_paths.items():
assert val_dataset_name not in val_dataset
val_data = PreferenceDataset(val_dataset_path)
print(
f" ✓ Validation dataset '{val_dataset_name}' loaded with {len(val_data.formatted_ds['train'])} samples."
)
val_dataset[val_dataset_name] = AllTaskProcessedDataset(
val_data.formatted_ds["train"],
tokenizer,
val_data.task_spec,
rm_preprocessor,
max_seq_length=data_config["max_input_seq_length"],
)
else:
val_dataset = (
{
"default": AllTaskProcessedDataset(
val_dataset,
tokenizer,
rm_task_spec,
rm_preprocessor,
max_seq_length=data_config["max_input_seq_length"],
)
}
if val_dataset
else {}
)
return train_dataset, val_dataset, rm_task_spec
def main():
"""Main entry point."""
# Parse arguments
args, overrides = parse_args()
if not args.config:
args.config = os.path.join(os.path.dirname(__file__), "configs", "rm.yaml")
config = load_config(args.config)
print(f"Loaded configuration from: {args.config}")
if overrides:
print(f"Overrides: {overrides}")
config = parse_hydra_overrides(config, overrides)
config: MasterConfig = OmegaConf.to_container(config, resolve=True)
print("Applied CLI overrides")
# Print config
print("Final config:")
pprint.pprint(config)
assert config["policy"]["reward_model_cfg"]["enabled"]
config["logger"]["log_dir"] = get_next_experiment_dir(config["logger"]["log_dir"])
print(f"📊 Using log directory: {config['logger']['log_dir']}")
if config["checkpointing"]["enabled"]:
print(
f"📊 Using checkpoint directory: {config['checkpointing']['checkpoint_dir']}"
)
init_ray()
# setup tokenizer
tokenizer = get_tokenizer(config["policy"]["tokenizer"])
# setup data
(
dataset,
val_dataset,
rm_task_spec,
) = setup_data(tokenizer, config["data"])
(
policy,
cluster,
train_dataloader,
val_dataloader,
loss_fn,
logger,
checkpointer,
rm_save_state,
master_config,
) = setup(config, tokenizer, dataset, val_dataset)
rm_train(
policy,
train_dataloader,
val_dataloader,
tokenizer,
loss_fn,
master_config,
logger,
rm_task_spec,
checkpointer,
rm_save_state,
)
if __name__ == "__main__":
main()