-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathrun_grpo.py
More file actions
191 lines (161 loc) · 5.95 KB
/
run_grpo.py
File metadata and controls
191 lines (161 loc) · 5.95 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
# 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 os
import pprint
from omegaconf import OmegaConf
from nemo_rl.algorithms.grpo import MasterConfig, grpo_train, setup
from nemo_rl.algorithms.utils import get_tokenizer
from nemo_rl.data.utils import setup_response_data
from nemo_rl.distributed.virtual_cluster import init_ray
from nemo_rl.models.generation import configure_generation_config
from nemo_rl.utils.config import (
load_config,
parse_hydra_overrides,
register_omegaconf_resolvers,
)
from nemo_rl.utils.logger import get_next_experiment_dir
def parse_args() -> tuple[argparse.Namespace, list[str]]:
"""Parse command line arguments."""
parser = argparse.ArgumentParser(description="Run GRPO 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
def main() -> None:
"""Main entry point."""
# Parse arguments
register_omegaconf_resolvers()
args, overrides = parse_args()
if not args.config:
args.config = os.path.join(
os.path.dirname(__file__), "configs", "grpo_math_1B.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)
# Get the next experiment directory with incremented ID
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"])
assert config["policy"]["generation"] is not None, (
"A generation config is required for GRPO"
)
config["policy"]["generation"] = configure_generation_config(
config["policy"]["generation"], tokenizer
)
# setup data
(
dataset,
val_dataset,
task_to_env,
val_task_to_env,
) = setup_response_data(tokenizer, config["data"], config["env"])
(
policy,
policy_generation,
cluster,
dataloader,
val_dataloader,
loss_fn,
logger,
checkpointer,
grpo_state,
master_config,
) = setup(config, tokenizer, dataset, val_dataset)
# Check if async mode is enabled
if "async_grpo" in config["grpo"] and config["grpo"]["async_grpo"]["enabled"]:
# Async GRPO does not support dynamic sampling, reward scaling, or reward shaping (DAPO features)
unsupported_features = [
"use_dynamic_sampling",
"reward_scaling",
"reward_shaping",
]
for feature in unsupported_features:
if feature not in config["grpo"]:
continue
if feature == "use_dynamic_sampling":
if config["grpo"][feature]:
raise NotImplementedError(
f"{feature} is not supported with async GRPO"
)
else:
if config["grpo"][feature]["enabled"]:
raise NotImplementedError(
f"{feature} is not supported with async GRPO"
)
# Async GRPO does not support multiple dataloaders
if config["data"]["use_multiple_dataloader"]:
raise NotImplementedError(
"use_multiple_dataloader is not supported with async GRPO"
)
# Async GDPO is not supported
if config["grpo"]["adv_estimator"]["name"] == "gdpo":
raise NotImplementedError(
"GDPO is not supported for async training, "
"please set grpo.async_grpo.enabled to false in your config."
)
from nemo_rl.algorithms.grpo import async_grpo_train
print("🚀 Running async GRPO training")
async_config = config["grpo"]["async_grpo"]
# Run async GRPO training
async_grpo_train(
policy=policy,
policy_generation=policy_generation,
dataloader=dataloader,
val_dataloader=val_dataloader,
tokenizer=tokenizer,
loss_fn=loss_fn,
task_to_env=task_to_env,
val_task_to_env=val_task_to_env,
logger=logger,
checkpointer=checkpointer,
grpo_save_state=grpo_state,
master_config=master_config,
max_trajectory_age_steps=async_config["max_trajectory_age_steps"],
)
else:
print("🚀 Running synchronous GRPO training")
# Run standard GRPO training
grpo_train(
policy,
policy_generation,
dataloader,
val_dataloader,
tokenizer,
loss_fn,
task_to_env,
val_task_to_env,
logger,
checkpointer,
grpo_state,
master_config,
)
if __name__ == "__main__":
main()