-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_sacred_experiments.py
187 lines (165 loc) · 5.95 KB
/
run_sacred_experiments.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
import sys
import subprocess
import os
import json
import time
import signal
import argparse
import itertools
import datetime
from multiprocessing import Pool
from random import randint
from sacred.observers import MongoObserver
try:
from tensorflow.python.client import device_lib
def get_available_gpus():
local_device_protos = device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type == "GPU"]
tensorflow_available = True
except ImportError:
tensorflow_available = False
def get_entry_or_default(label, config, default):
try:
return config[label]
except KeyError:
return default
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--config",
type=str,
required=True,
help="Path to an experiment config file (json)",
)
parser.add_argument(
"--n_jobs", type=int, help="Number of jobs to launch in parallel", default=1
)
parser.add_argument(
"--mult_gpus",
action="store_true",
help="Distribute tensorflow jobs over all available GPUs",
)
parser.add_argument(
"--bsub",
action="store_true",
help="Use bsub to submit cluster jobs instead of multiprocessing "
"(if set, --n_jobs, and --mult_gpus is ignored)",
)
parser.add_argument(
"--bsub_n", type=int, help="Number of CPUs to use per bsub job", default=1
)
parser.add_argument(
"--bsub_W", type=str, help="Timelimit to use per bsub job", default="23:59"
)
parser.add_argument(
"--bsub_mem",
type=int,
help="Memory (MB) to use per bsub job (for each core)",
default=4500,
)
parser.add_argument(
"--bsub_gpus",
type=int,
help="Number of GPUs to use per bsub job",
default=0,
)
parser.add_argument(
"--drop_output",
action="store_true",
help="Redirect all stdout and stderr output to /dev/null",
)
args = parser.parse_args()
with open(args.config) as f:
config = json.load(f)
experiment_script = config["experiment_script"]
experiment_label = get_entry_or_default("experiment_label", config, "no_label")
N_seeds = get_entry_or_default("N_seeds", config, 1)
del config["N_seeds"]
del config["experiment_script"]
config_list_entries = []
for key in config.keys():
if isinstance(config[key], list):
config_list_entries.append([(key, val) for val in config[key]])
else:
config_list_entries.append([(key, config[key])])
job_list = []
if args.mult_gpus and tensorflow_available:
devices = get_available_gpus()
print("Distributing over tf devices:", devices)
devices_iter = itertools.cycle(range(len(devices)))
seeds = [randint(1, 100000) for _ in range(N_seeds)]
for seed in seeds:
for config_update in itertools.product(*config_list_entries):
config_update_dict = dict(config_update)
config_update_dict["seed"] = seed
env = os.environ.copy()
if args.mult_gpus and tensorflow_available:
env["CUDA_VISIBLE_DEVICES"] = str(next(devices_iter))
job_list.append((config_update_dict, env))
# we use subprocesses for parallelization instead of python multiprocessing
# because the former caused results to be missing in mongodb
def run_experiment(job):
config_updates, env = job
arguments = ["{}={}".format(k, v) for k, v in config_updates.items()]
command = [sys.executable, experiment_script, "with"] + arguments
if args.drop_output:
command += ["&>", "/dev/null"]
experiment_label = config_updates["experiment_label"]
if args.bsub:
command_str = subprocess.list2cmdline(command)
os.makedirs("jobs", exist_ok=True)
os.makedirs(os.path.join("jobs", "output"), exist_ok=True)
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")
label = f"{experiment_label}_{timestamp}"
scriptfile = os.path.join(os.getcwd(), "jobs", f"{label}.sh")
outfile = os.path.join(os.getcwd(), "jobs", "output", f"{label}.out")
errfile = os.path.join(os.getcwd(), "jobs", "output", f"{label}.err")
print(command_str)
with open(scriptfile, "w") as f:
f.write(command_str)
bsub_command = (
f"bsub -W {args.bsub_W} "
f"-n {args.bsub_n} "
f'-R "rusage[mem={args.bsub_mem}]" '
f'-R "rusage[ngpus_excl_p={args.bsub_gpus}]" '
f"-oo {outfile} "
f"-eo {errfile} "
f"-J {experiment_label} "
f"{scriptfile}"
)
os.system(f"chmod +x {scriptfile}")
os.system(bsub_command)
else:
print(" ".join(command))
stderr = []
with subprocess.Popen(
command,
env=env,
stderr=subprocess.PIPE,
bufsize=1,
universal_newlines=True,
) as p:
for line in p.stderr:
stderr.append(line)
returncode = p.wait()
print("\n".join(stderr))
t = time.time()
n_jobs = len(job_list)
print()
method = "bsub" if args.bsub else "multiprocessing"
if args.n_jobs == 1:
response = "yes"
else:
response = input(f"Starting {n_jobs} jobs using {method}. OK? [yes/no] ")
print()
if response == "yes":
if args.n_jobs == 1 or args.bsub:
for job in job_list:
run_experiment(job)
else:
with Pool(args.n_jobs + 1) as p: # +1 for base process
p.map(run_experiment, job_list)
t = time.time() - t
print("Done in {:.2f} seconds".format(t))
else:
print("No jobs started.")