-
Notifications
You must be signed in to change notification settings - Fork 14
/
run_batch_rl.py
338 lines (301 loc) · 11.4 KB
/
run_batch_rl.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
"""
Launch a batch of experiments on a SLURM cluster.
dead processes.
"""
import argparse
from argparse import Namespace
import copy
import itertools
import json
import os
from pdb import set_trace as TT
import re
import submitit
import yaml
from control_pcgrl.rl.cross_eval import compile_results
from control_pcgrl.rl.utils import get_log_dir
from control_pcgrl.rl import train
with open("configs/rl/batch.yaml", "r") as f:
batch_config = yaml.safe_load(f)
# HACK: deal with nested hyperparameters.
local_controls, global_controls = batch_config.pop("local_controls"), batch_config.pop("global_controls")
# Take product of lists of all hyperparameters in `batch_config`.
keys, vals = zip(*batch_config.items())
exp_hypers = itertools.product(*vals)
# Turn lists in `exp_hypers` into dictionaries.
exp_hypers = [dict(zip(keys, exp_hyper)) for exp_hyper in exp_hypers]
# Turn `batch_config` into a namespace.
batch_config = Namespace(**batch_config)
def launch_batch(collect_params=False):
if collect_params:
settings_list = []
assert not EVALUATE
# if opts.render_levels:
# print('Rendering levels')
# n_bins = 4
# n_maps = 2
if LOCAL:
print("Testing locally.")
n_maps = 2
n_bins = 10
# n_bins = 4
else:
print("Launching batch of experiments on SLURM.")
n_maps = 50
n_bins = 10
# if LOCAL:
# # if running locally, just run a quick test
# default_config["n_frames"] = 100000
i = 0
jobs = []
for exp_cfg in exp_hypers:
# exp_config inherits all arguments from opts
exp_cfg.update(vars(opts))
# Supply the command-line arguments in args.py
exp_cfg.update(
{
"evaluate": EVALUATE,
"representation": exp_cfg['representation_model'][0],
"model": exp_cfg['representation_model'][1],
}
)
# TODO: Revive this functionality and put it somewhere
# if EVALUATE:
# exp_config.update(
# {
# # "load": True,
# "n_maps": n_maps,
# # "render": False,
# "render_levels": opts.render_levels,
# "n_bins": (n_bins,),
# "vis_only": opts.vis_only,
# }
# )
# FIXME: This is a hack. How to iterate through nested hyperparameter loops in a better way?
# TODO: Hydra would solve this. Empty this file to make room for hydra.
prob_controls = global_controls + local_controls[exp_cfg['problem']]
for controls in prob_controls:
exp_prob_cfg = copy.deepcopy(exp_cfg)
exp_prob_cfg.update({
"controls": controls
})
exp_prob_cfg = Namespace(**exp_prob_cfg)
# if controls != ["NONE"] and change_percentage != 1:
# continue
# if sum(['3D' in name for name in [prob, rep]]) == 1:
# print(f'Dimensions (2D or 3D) of Problem: {prob} and Representation: {rep} '
# 'do not match. Skipping experiment.')
# continue
# if sum(['holey' in name for name in [prob, rep]]) == 1:
# print(f'Holeyness of Problem: {prob} and Representation: {rep} '
# 'do not match. Skipping experiment.')
# continue
if exp_prob_cfg.alp_gmm and controls is None:
continue
# if (not alp_gmm) and len(controls) < 2 and controls != ["NONE"]:
# # For now we're only looking at uniform-random target-sampling with both control metrics
# continue
# TODO: integrate evaluate with rllib
# if EVALUATE:
# py_script_name = "rl/evaluate_ctrl.py"
# sbatch_name = "rl/eval.sh"
# # elif opts.infer:
# # py_script_name = "infer_ctrl_sb2.py"
# else:
py_script_name = "control_pcgrl/control_pcgrl/rl/train_ctrl.py"
sbatch_name = "control_pcgrl/control_pcgrl/rl/train.sh"
# Write the config file with the desired settings
# exp_config = copy.deepcopy(default_config)
print(f"Saving experiment config:\n{exp_cfg}")
# get the experiment name to name the config file
# config_name = f"{prob}_{rep}_{exp_name}"
config_name = get_log_dir(exp_prob_cfg)
# Edit the sbatch file to load the correct config file
if not opts.render:
if not LOCAL:
with open(sbatch_name, "r") as f:
content = f.read()
# Replace the ``python scriptname --cl_args`` line.
content = re.sub(
"python .* --load_args .*",
f"python {py_script_name} --load_args {config_name}",
content,
)
# Replace the job name.
content = re.sub(
"rl_runs/pcgrl_.*",
f"rl_runs/pcgrl_{config_name}_%j.out",
content
)
content = re.sub(
"--job-name=.*",
f"--job-name={config_name}.out",
content
)
with open(sbatch_name, "w") as f:
f.write(content)
# Get directory of current file
dir_path = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(dir_path, f"configs/rl/auto/settings_{config_name}.json"), "w") as f:
json.dump(vars(exp_prob_cfg), f, ensure_ascii=False, indent=4)
# Launch the experiment. It should load the saved settings
if not (EVALUATE and not opts.overwrite_eval and \
os.path.isfile(os.path.join('rl_runs', f'{config_name}_log', 'eval_stats.json'))):
if collect_params:
settings_list.append(exp_cfg)
elif LOCAL:
# full_cmd = f"python {py_script_name} --load_args {config_name}"
# Printout for convenience: when debugging on a Mac calling this from a script will break `set_trace()`
# so we print the command here to be entered-in manually.
# print(f"Running command:\n{full_cmd}")
# os.system(full_cmd)
train.main(exp_prob_cfg)
else:
# TODO: User submitit.
os.system(f"sbatch {sbatch_name}")
# job = executor.submit(train_ctrl.main, exp_prob_cfg)
# jobs.append(job)
else:
print('Skipping evaluation (already have stats saved).')
i += 1
if collect_params:
return settings_list
if __name__ == "__main__":
opts = argparse.ArgumentParser(
description="Launch a batch of experiments/evaluations for (controllable) pcgrl"
)
# opts.add_argument(
# "-rl",
# "--render_levels",
# help="",
# action="store_true",
# )
# opts.add_argument(
# "-ex",
# "--experiment_name",
# help="A name to be shared by the batch of experiments.",
# default="0",
# )
opts.add_argument(
"-d",
"--debug",
help="Debug environment & rendering (render random agent for a bunch of episodes then quit).",
action="store_true",
)
opts.add_argument(
"-ev",
"--evaluate",
help="Evaluate a batch of PCGRL experiments.",
action="store_true",
)
opts.add_argument(
"-l",
"--local",
help="Run the batch script locally (i.e. to test it) and train for minimal number of frames.",
action="store_true",
)
opts.add_argument(
"-v",
"--vis_only",
help="Just load data from previous evaluation and visualize it.",
action="store_true",
)
opts.add_argument(
"-ce",
"--cross_eval",
help="Compile stats from previous evaluations into a table",
action="store_true",
)
opts.add_argument(
"-np",
"--no_plot",
help="Do no plot training curves (output from monitor files) during cross-evaluation (What?).",
action="store_true",
)
opts.add_argument(
"--render",
action='store_true',
help="Visualize agent taking actions in environment by calling environments render function."
)
opts.add_argument(
"-in",
"--infer",
action="store_true",
help="Run inference with a trained model.",
)
opts.add_argument(
"--n_cpu",
type=int,
default=12,
help="Number of remote workers to use for rllib training.",
)
opts.add_argument(
"--n_gpu",
type=int,
default=1,
help="Number of GPUs to use for training.",
)
opts.add_argument(
"--load",
action="store_true",
help="Load previous checkpoint of model to resume training or do inference or evaluation.",
)
opts.add_argument(
"-ovr",
"--overwrite",
action="store_true",
help="Overwrite previous experiment with same name."
)
# opts.add_argument(
# "-lr",
# "--learning_rate",
# type=float,
# default=0.000005,
# help="Learning rate for rllib agent, default is 0.0001."
# )
opts.add_argument(
"-ga",
"--gamma",
type=float,
default=0.99,
help="Discount factor of the MDP."
)
opts.add_argument(
'--wandb',
help='Whether to use wandb for logging.',
action='store_true',
# action=argparse.BooleanOptionalAction,
# default=False,
)
opts.add_argument(
'--record_env',
help='Whether to record the environment during inference.',
action='store_true',
# action=argparse.BooleanOptionalAction,
# default=False,
)
# opts.add_argument(
# '--max_board_scans',
# help='Number of max iterations in terms of maximum number of times the board can be scanned by the agent.',
# type=int,
# default=1,
# )
opts.add_argument(
'--overwrite_eval',
help='Whether to overwrite stats resulting from a previous evaluation.',
action=argparse.BooleanOptionalAction,
default=True,
)
opts = opts.parse_args()
# EXP_NAME = opts.experiment_name
EVALUATE = opts.evaluate
LOCAL = opts.local
executor = submitit.AutoExecutor(os.path.join("rl_runs", "submitit"))
executor.update_parameters(gpus_per_node=1, slurm_mem="30GB", cpus_per_task=max(1, opts.n_cpu), slurm_time="5:00:00",
job_name="pcgrl",)
if opts.cross_eval:
settings_list = launch_batch(collect_params=True)
compile_results(settings_list, no_plot=opts.no_plot)
else:
with executor.batch():
launch_batch()