-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
256 lines (228 loc) · 7.92 KB
/
main.py
File metadata and controls
256 lines (228 loc) · 7.92 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
255
256
import copy
from datetime import datetime
import os
import click
import yaml
from affex.collect import runs_collect
from affex.evaluate import evaluate
from affex.computational import evaluate_computational
from affex.utils.logger import get_logger
from affex.utils.utils import load_yaml
from affex.utils.grid import create_experiment
from affex.utils.run import ParallelRun
OUT_FOLDER = "out"
FUNCTIONS = {
"evaluate": evaluate,
"computational": evaluate_computational,
}
FUNCTION_SLURMS = {
"evaluate": "slurm/launch_run",
"computational": "slurm/launch_computational",
}
FUNCTIONS_CONDOR = {
"evaluate": "slurm/condor",
"computational": "slurm/condor",
}
SCHEDULERS = {
"slurm": FUNCTION_SLURMS,
"condor": FUNCTIONS_CONDOR,
}
@click.group()
def cli():
"""Run a refinement or a grid"""
pass
def manage_multiprocess_run(run_parameters, run_name, logger, job_parallelism=None):
"""
Manage the multiprocess run.
This function is used to launch the run in parallel or sequentially.
"""
if "num_processes" in run_parameters["dataloader"]:
multi_runs = [
copy.deepcopy(run_parameters) for _ in range(run_parameters["dataloader"]["num_processes"])
]
if job_parallelism is not None and job_parallelism > 1:
run_names = [
f"{run_name}/job_{str(i//job_parallelism).zfill(3)}/p_{str(i%job_parallelism).zfill(3)}" for i in range(run_parameters["dataloader"]["num_processes"])
]
else:
run_names = [
f"{run_name}/p_{str(i).zfill(3)}" for i in range(run_parameters["dataloader"]["num_processes"])
]
logger.info(f"Running {len(multi_runs)} processes in parallel")
for i, run_parameters in enumerate(multi_runs):
run_name = f"{run_names[i]}"
multi_runs[i]["dataloader"]["process_id"] = i
os.makedirs(run_name, exist_ok=True)
else:
multi_runs = [run_parameters]
run_names = [run_name]
logger.info("Running in single process mode")
return multi_runs, run_names
@cli.command("grid")
@click.option(
"--parameters",
default=None,
help="Path to the file containing the parameters for a grid search",
)
@click.option(
"--parallel",
default=False,
is_flag=True,
help="Run the grid in parallel",
)
@click.option(
"--only_create",
default=False,
is_flag=True,
help="Only create the slurm scripts",
)
@click.option(
"--function",
default="evaluate",
help="Name of the function to run, either 'evaluate' or 'computational'",
)
@click.option(
"--job_parallelism",
default=None,
type=int,
help="If --parallel is provided, and dataloader.num_processes is provided in the parameters, this will set the number of processes to use in a single job, should be divisor of dataloader.num_processes",
)
@click.option(
"--scheduler",
default="slurm",
help="Scheduler to use, either 'slurm' or 'condor'",
)
def grid(parameters, parallel, only_create=False, function="evaluate", job_parallelism=None, scheduler="slurm"):
assert function in FUNCTIONS, f"Function {function} not recognized, available functions: {list(FUNCTIONS.keys())}"
run_function = FUNCTIONS[function]
slurm_script = SCHEDULERS[scheduler][function]
assert os.path.exists(slurm_script), f"Slurm script {slurm_script} does not exist"
parameters = load_yaml(parameters)
grid_name = parameters["grid"]
current_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
grid_name = f"{current_time}_{grid_name}"
log_folder = os.path.join(OUT_FOLDER, grid_name)
runs_parameters = create_experiment(parameters)
os.makedirs(log_folder)
with open(os.path.join(log_folder, "hyperparams.yaml"), "w") as f:
yaml.dump(parameters, f)
grid_logger = get_logger("Grid", f"{log_folder}/grid.log")
grid_logger.info(f"Running {len(runs_parameters)} runs")
for i, run_parameters in enumerate(runs_parameters):
run_name = f"{log_folder}/run_{i}"
os.makedirs(run_name, exist_ok=True)
multi_runs, run_names = manage_multiprocess_run(
run_parameters, run_name, grid_logger, job_parallelism
)
if parallel:
job_parallelism = job_parallelism if job_parallelism is not None else 1
jobs = len(multi_runs) // job_parallelism
for j in range(jobs):
subrun_parameters = multi_runs[j*(job_parallelism):(j+1)*(job_parallelism)]
subrun_name = run_names[j*(job_parallelism):(j+1)*(job_parallelism)]
if len(subrun_parameters) == 1:
subrun_name = subrun_name[0]
subrun_parameters = subrun_parameters[0]
run = ParallelRun(
subrun_parameters,
multi_gpu=False,
logger=grid_logger,
run_name=subrun_name,
slurm_script=slurm_script,
scheduler=scheduler,
)
run.launch(
only_create=only_create,
script_args=[
"--disable_log_params",
"--disable_log_on_file",
"--function",
function,
],
)
else:
for k, (subrun_parameters, subrun_name) in enumerate(
zip(multi_runs, run_names)
):
if len(multi_runs) > 1:
grid_logger.info(
f"Running subrun {k+1}/{len(multi_runs)} in run {i+1}/{len(runs_parameters)}"
)
else:
grid_logger.info(f"Running run {i+1}/{len(runs_parameters)}")
run_function(subrun_parameters, run_name=subrun_name)
@cli.command("run")
@click.option(
"--parameters",
default=None,
help="Path to the file containing the parameters for a single run",
)
@click.option("--run_name", default=None, help="Name of the run")
@click.option(
"--disable_log_params",
default=False,
is_flag=True,
help="Disable Log the parameters",
)
@click.option(
"--disable_log_on_file", default=False, is_flag=True, help="Disable Log on file"
)
@click.option(
"--run_name",
default=None,
help="Name of the run, if not provided, it will be generated based on the current time",
)
@click.option(
"--function",
default="evaluate",
help="Name of the function to run, either 'evaluate' or 'computational'",
)
def run(
parameters,
run_name=None,
disable_log_params=False,
disable_log_on_file=False,
function="evaluate",
):
assert function in FUNCTIONS, f"Function {function} not recognized, available functions: {list(FUNCTIONS.keys())}"
run_function = FUNCTIONS[function]
parameters = load_yaml(parameters)
run_function(
parameters,
run_name,
not disable_log_params,
not disable_log_on_file,
)
@cli.command("collect")
@click.option(
"-f", "--folder",
default=None,
help="Path to the folder containing the runs to collect",
)
@click.option(
"--no_wandb",
default=False,
is_flag=True,
help="Disable wandb collection",
)
@click.option(
"--overwrite",
default=False,
is_flag=True,
help="Overwrite existing runs on wandb",
)
def collect(folder, no_wandb, overwrite):
assert os.path.exists(folder), f"Folder {folder} does not exist"
assert os.path.isdir(folder), f"{folder} is not a directory"
runs_collect(folder=folder, use_wandb=not no_wandb, overwrite=overwrite)
@cli.command("generate")
@click.option(
"-p", "--parameters",
default=None,
help="Path to the file containing the parameters for the dataset generation",
)
def generate(parameters):
from affex.data.generate import generate_dataset
generate_dataset(parameters)
if __name__ == "__main__":
cli()