forked from OCP-on-NERC/python-batchtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbr.py
More file actions
277 lines (238 loc) · 8.89 KB
/
br.py
File metadata and controls
277 lines (238 loc) · 8.89 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
# pyright: reportUninitializedInstanceVariable=false
from typing import cast
from typing_extensions import override
import argparse
import os
import socket
import sys
import time
import uuid
import openshift_client as oc
from .basecommand import Command
from .basecommand import SubParserFactory
from .build_yaml import build_job_body
from .helpers import pretty_print
from .helpers import oc_delete
from .file_setup import prepare_context
class CreateJobCommandArgs(argparse.Namespace):
"""This class serves two purposes:
1. It provides type hints that permit a properly configured IDE (or type
checker) to perform type checking on command line option values.
2. It provides default values for command line options.
"""
gpu: str = "v100"
image: str = "image-registry.openshift-image-registry.svc:5000/redhat-ods-applications/csw-run-f25:latest"
context: bool = True
name: str = "job"
job_id: str = uuid.uuid5(uuid.NAMESPACE_OID, f"{os.getpid()}-{time.time()}").hex
job_delete: bool = True
wait: bool = True
timeout: int = 60 * 15 * 4
max_sec: int = 60 * 15
gpu_numreq: int = 1
gpu_numlim: int = 1
verbose: int = 0
command: list[str]
class CreateJobCommand(Command):
"""
brun creates and submits a batch job to a GPU batch queue using the OpenShift Python client.
The arguments are treated as a command line that will execute as the batch job within a container.
The behaviour of the job submission can be controlled via several environment variables or CLI flags.
By default, the job runs in an isolated container environment scheduled onto a GPU node using
the Kueue queue system. GPU type, image, resource limits, and runtime behavior (e.g., waiting or
automatic deletion) can be customized at submission time.
Example usages:
1. Run a simple command on the default GPU type (v100)
$ brun ./hello
Hello from CPU
Hello from GPU
...
RUNDIR: jobs/job-v100-9215
2. Specify GPU type and image for a training job
$ br --gpu a100 --image quay.io/user/train:latest cuda-code
3. Submit without waiting for completion
$ br --wait 0 ./long_running_task.sh
By default, br waits for the job to complete, streams its logs,
and then displays the directory where the job outputs were copied.
See also:
See the repository README.md for more examples and advanced usage.
"""
name: str = "br"
help: str = "Create and submit a GPU batch job"
@classmethod
@override
def build_parser(cls, subparsers: SubParserFactory):
p = super().build_parser(subparsers)
p.add_argument(
"--gpu",
default=CreateJobCommandArgs.gpu,
help="Select GPU type",
)
p.add_argument(
"--image",
default=CreateJobCommandArgs.image,
help="Specify container image for job",
)
p.add_argument(
"--context",
action=argparse.BooleanOptionalAction,
default=CreateJobCommandArgs.context,
help="Copy working directory",
)
p.add_argument(
"--name",
default=CreateJobCommandArgs.name,
help="Base job name",
)
p.add_argument(
"--job-id",
default=CreateJobCommandArgs.job_id,
help="Job ID suffix",
)
p.add_argument(
"--job-delete",
action=argparse.BooleanOptionalAction,
default=CreateJobCommandArgs.job_delete,
help="Delete job on completion",
)
p.add_argument(
"--wait",
action=argparse.BooleanOptionalAction,
default=CreateJobCommandArgs.wait,
help="Wait for job completion",
)
p.add_argument(
"--timeout",
default=CreateJobCommandArgs.timeout,
type=int,
help="Wait timeout in seconds",
)
p.add_argument(
"--max-sec",
default=CreateJobCommandArgs.max_sec,
type=int,
help="Maximum execution time in seconds",
)
p.add_argument(
"--gpu-numreq",
default=CreateJobCommandArgs.gpu_numreq,
type=int,
help="Number of GPUs requested",
)
p.add_argument(
"--gpu-numlim",
default=CreateJobCommandArgs.gpu_numlim,
type=int,
help="Number of GPUs limited",
)
p.add_argument(
"command",
nargs=argparse.REMAINDER,
help="Command to run inside the container",
)
return p
@staticmethod
@override
def run(args: argparse.Namespace):
args = cast(CreateJobCommandArgs, args)
DEFAULT_QUEUES = {
"v100": "v100-localqueue",
"a100": "a100-localqueue",
"h100": "h100-localqueue",
"none": "dummy-localqueue",
}
if not args.command:
sys.exit("ERROR: you must provide a command")
if args.gpu not in DEFAULT_QUEUES:
sys.exit(f"ERROR: unsupported GPU {args.gpu} : no queue found")
queue_name = DEFAULT_QUEUES[args.gpu]
job_name = f"{args.name}-{args.gpu}-{args.job_id}"
container_name = f"{job_name}-container"
file_to_execute = " ".join(args.command).strip()
pwd = os.getcwd()
context_directory = pwd
jobs_directory = os.path.join(pwd, "jobs")
output_directory = os.path.join(jobs_directory, job_name)
dev_pod_name = socket.gethostname()
getlist = os.path.join(output_directory, "getlist")
pod = oc.selector(f"pod/{dev_pod_name}").object()
container = getattr(pod.model.spec, "containers", []) or []
dev_container_name = container[0].name
prepare_context(
context=args.context,
context_dir=context_directory,
jobs_dir=jobs_directory,
output_dir=output_directory,
getlist_path=getlist,
)
try:
# Create job body using the helper
job_body = build_job_body(
job_name=job_name,
queue_name=queue_name,
image=args.image,
container_name=container_name,
cmdline=file_to_execute,
max_sec=args.max_sec,
gpu=args.gpu,
gpu_req=args.gpu_numreq,
gpu_lim=args.gpu_numlim,
context=args.context,
devpod_name=dev_pod_name,
devcontainer=dev_container_name,
context_dir=context_directory,
jobs_dir=jobs_directory,
getlist_path=getlist,
)
print(f"Creating job {job_name} in {queue_name}...")
oc.create(job_body)
print(f"Job: {job_name} created successfully. Now checking pod...")
if args.wait:
log_job_output(job_name=job_name, wait=True, timeout=args.timeout)
except oc.OpenShiftPythonException as e:
sys.exit(f"Error occurred while creating job: {e}")
if args.job_delete and args.wait:
print(f"RUNDIR: jobs/{job_name}")
oc_delete(job_name)
else:
print(
f"User specified not to wait, or not to delete, so {job_name} must be deleted by user."
)
print("You can do this by running:")
print(f"bd {job_name} OR ")
print(f"oc delete job {job_name}")
def get_pod_status(pod_name: str | None = None) -> str:
"""
Return the current status.phase of a pod (Pending, Running, Succeeded, Failed).
"""
pod = oc.selector(f"pod/{pod_name}").object()
return pod.model.status.phase or "Unknown"
def log_job_output(job_name: str, *, wait: bool, timeout: int | None) -> None:
"""
Wait until the job's pod completes (Succeeded/Failed), then print its logs once.
"""
pods = oc.selector("pod", labels={"job-name": job_name}).objects()
if not pods:
print(f"No pods found for job {job_name}")
return
pod = pods[0]
pod_name = pod.model.metadata.name
if wait:
start = time.monotonic()
while True:
phase = get_pod_status(pod_name)
if phase in ("Succeeded", "Failed"):
print(f"Pod, {pod_name} finished with phase={phase}")
# end = time.monotonic()
# for logging information
# elapsed = end - start
break
if timeout and (time.monotonic() - start) > timeout:
print(f"Timeout waiting for pod {pod_name} to complete")
print(f"Deleting pod {pod_name}")
oc_delete(job_name)
return
# sleep to avoid hammering the server
time.sleep(2)
# pass in the pod object to get logs from, not the name
print(pretty_print(pod))