-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubmit.py
More file actions
323 lines (269 loc) · 9.96 KB
/
submit.py
File metadata and controls
323 lines (269 loc) · 9.96 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
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
"""Submit jobs with various arguments to SLURM or to run locally."""
import argparse
import datetime
import subprocess
import sys
from enum import Enum
from itertools import product
from pathlib import Path
from typing import Union
import yaml
from jinja2 import Template
class ExecutionMode(Enum):
"""Enumeration of supported job execution modes."""
SLURM = "slurm"
LOCAL = "local"
CLOUD_LOCAL = "cloud_local"
@classmethod
def from_str(cls, value: str) -> "ExecutionMode":
"""Convert a string to an ExecutionMode enum value.
Args:
value: String representation of the execution mode
Returns:
Corresponding ExecutionMode enum value
Raises:
ValueError: If the string doesn't match any enum value
"""
try:
return cls(value)
except ValueError as err:
msg = (
f"Invalid execution mode: {value}. "
f"Must be one of {[m.value for m in cls]}"
)
raise ValueError(msg) from err
class LocalJob:
"""Class for managing and executing jobs locally."""
def __init__(
self,
cmd_template: Union[str, Path],
job_name: str,
template_vars: Union[dict, None] = None,
log_path: Path = Path("logs"),
) -> None:
"""Initialize a local job.
Args:
cmd_template: Command template string or path to template file
job_name: Name of the job
template_vars: Variables to substitute in the template
log_path: Directory to store log files
"""
self._cmd_template = cmd_template
self._template_vars = template_vars or {}
self._job_name = job_name
self._log_path = log_path
def _render_cmd(self):
"""Render the command template with the provided variables."""
if isinstance(self._cmd_template, Path):
template_str = self._cmd_template.read_text()
else:
template_str = self._cmd_template
template = Template(template_str)
return template.render(**self._template_vars)
def submit(self) -> None:
"""Execute the job locally and log its output."""
# Render the final command
cmd_str = self._render_cmd()
# Setup log file
self._log_path.mkdir(parents=True, exist_ok=True)
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
log_file = self._log_path / f"{timestamp}_{self._job_name}.out"
print(f"Running job '{self._job_name}' locally")
print(f"Command: {cmd_str}")
print(f"Logging to: {log_file}")
with log_file.open("w") as f:
f.write(f"Job Name: {self._job_name}\n")
f.write(f"Command: {cmd_str}\n")
f.write("-" * 80 + "\n\n")
process = subprocess.Popen(
cmd_str,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
bufsize=1,
)
while True:
line = process.stdout.readline()
# in text mode, readline() returns "" on EOF
if line == "" and process.poll() is not None:
break
if line:
line = line.rstrip()
print(line)
f.write(line + "\n")
f.flush()
return_code = process.poll()
if return_code != 0:
print(f"Job failed with return code {return_code}", file=sys.stderr)
sys.exit(return_code)
class SlurmJob:
"""Class for managing and submitting jobs to SLURM scheduler."""
def __init__(
self,
cmd_template: Union[str, Path],
job_name: str,
template_vars: dict,
log_path: Path = Path("logs"),
) -> None:
"""Initialize a SLURM job.
Args:
cmd_template: Command template string or path to template file
job_name: Name of the job
template_vars: Variables to substitute in the template
log_path: Directory to store log files
"""
self._template = cmd_template
self._vars = template_vars
self._job_name = job_name
self._log_path = log_path
def _render(self) -> str:
"""Render the SLURM script template with the provided variables."""
tpl = (
Path(self._template).read_text()
if isinstance(self._template, Path)
else self._template
)
return Template(tpl).render(job_name=self._job_name, **self._vars)
def submit(self) -> None:
"""Submit the job to the SLURM scheduler."""
# ensure log dir exists (for SBATCH --output=...)
self._log_path.mkdir(parents=True, exist_ok=True)
script_fp = Path(f"{self._job_name}.slurm.sh")
script_fp.write_text(self._render())
script_fp.chmod(0o700)
# submit and clean up
subprocess.run(["sbatch", str(script_fp)], check=True)
script_fp.unlink()
JOB_OPTIONS = {
ExecutionMode.LOCAL: LocalJob,
ExecutionMode.SLURM: SlurmJob,
ExecutionMode.CLOUD_LOCAL: LocalJob,
}
def arg_to_string(val):
"""Turn argument to string without backslash"""
return str(val).replace("/", "-")
def main() -> None:
"""Main entry point for job submission.
Parses command line arguments and submits jobs according to the specified mode
(local or SLURM) and configuration.
"""
parser = argparse.ArgumentParser(description="Submit jobs.")
parser.add_argument(
"--mode",
type=str,
choices=[m.value for m in ExecutionMode],
default=ExecutionMode.LOCAL.value,
help="Execution mode (e.g. slurm or local)",
)
parser.add_argument(
"--script",
type=str,
required=True,
help="Which entry under `scripts:` in the YAML to run.",
)
parser.add_argument(
"--config_file",
type=Path,
default=Path("./submit/run.yaml"),
help="YAML config file, containing all run variables.",
)
# Special slurm arguments
parser.add_argument("--partition", type=str, help="SLURM partition")
parser.add_argument("--nodes", type=int, help="Number of nodes")
parser.add_argument("--cpus-per-task", type=int, help="CPUs per task")
parser.add_argument("--mem-per-cpu", type=str, help="Memory per CPU (e.g. 4G)")
parser.add_argument("--gres", type=str, help="Generic resources (e.g. gpu:1)")
parser.add_argument("--time", type=str, help="Time limit (e.g. 3-00:00:00)")
parser.add_argument(
"--slurm_log_dir",
type=str,
default="./logs",
help="Log directory for slurm job.",
)
# Split off any --key value1 value2 ... into `unknown`
args, unknown = parser.parse_known_args()
# Convert mode string to enum
args.mode = ExecutionMode.from_str(args.mode)
# If SlurmJob then get slurm args
if args.mode == ExecutionMode.SLURM:
mode_specific_overrides = {
"partition": args.partition,
"nodes": args.nodes,
"cpus_per_task": args.cpus_per_task,
"mem_per_cpu": args.mem_per_cpu,
"gres": args.gres,
"time_limit": args.time,
"slurm_log_dir": args.slurm_log_dir,
}
else:
mode_specific_overrides = {}
# Load YAML config
with args.config_file.open("r") as f:
config = yaml.safe_load(f)
# Grab the right mode-block
mode_cfg = config["mode"][args.mode.value]
pykernel = mode_cfg["pykernel"]
template_fp = Path(mode_cfg["template"])
# Grab the selected script-block
script_cfg = config["scripts"][args.script]
script_path = script_cfg["path"]
default_args = script_cfg.get("default_args", {})
# Combine variables to create job arrays
extra_args = {k: v if isinstance(v, list) else [v] for k, v in default_args.items()}
i = 0
while i < len(unknown):
tok = unknown[i]
if not tok.startswith("--"):
parser.error(f"Unexpected token {tok!r}")
key = tok.lstrip("--")
i += 1
vals = []
# consume until next --foo or end
while i < len(unknown) and not unknown[i].startswith("--"):
vals.append(unknown[i])
i += 1
if not vals:
# store_true argument (boolean flag with no value)
extra_args[key] = [True]
else:
extra_args[key] = vals
# Build cartesian product of all key-values
keys = list(extra_args.keys())
all_values = [extra_args[k] for k in keys]
# Show job creation details
total_jobs = len(list(product(*all_values)))
print(f"Creating {total_jobs} job(s) with the following parameters:")
if keys:
for key, values in extra_args.items():
values_str = ", ".join(str(v) for v in values)
print(f" {key}: [{values_str}]")
print()
else:
print(" (no parameters specified)")
print()
for combo in product(*all_values):
# combo is a tuple like ("value1_for_key1", "value_for_key2", ...)
combo_dict = dict(zip(keys, combo))
# Prepare the vars that go into Jinja
template_vars = {
"pykernel": pykernel,
"script_path": str(script_path),
"script_args": combo_dict,
}
# Add mode specific arguments
template_vars.update(
{k: v for k, v in mode_specific_overrides.items() if v is not None}
)
# instantiate and submit
suffix = "_".join(
f"{arg_to_string(k)}={arg_to_string(v)}" for k, v in combo_dict.items()
)
name = args.script if not suffix else f"{args.script}_{suffix}"
job = JOB_OPTIONS[args.mode](
cmd_template=template_fp, job_name=name, template_vars=template_vars
)
job.submit()
print(f"Submitted {total_jobs} job(s)")
if __name__ == "__main__":
main()