-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathxpk.py
280 lines (240 loc) · 9.66 KB
/
xpk.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
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utilities to run workloads with xpk (https://github.com/AI-Hypercomputer/xpk)."""
import os
import tempfile
import uuid
from absl import logging
from airflow.decorators import task
from airflow.exceptions import AirflowFailException
from airflow.hooks.subprocess import SubprocessHook
from kubernetes import client as k8s_client
from xlml.apis import metric_config
from xlml.utils import gke
from dags.common.vm_resource import GpuVersion
# Duration = past 7 days
LOGGING_URL_FORMAT = (
"https://pantheon.corp.google.com/logs/query;"
+ "query=resource.type%3D%22k8s_container%22%0A"
+ "resource.labels.project_id%3D%22{project}%22%0A"
+ "resource.labels.location%3D%22{region}%22%0A"
+ "resource.labels.cluster_name%3D%22{cluster}%22%0A"
+ "resource.labels.namespace_name%3D%22default%22%0A"
+ "labels.k8s-pod%2Fjobset_sigs_k8s_io%2F"
+ "jobset-name%3D%22{workload_id}%22%20severity%3E%3DDEFAULT;"
+ "storageScope=project;duration=P7D?e=13803378&"
+ "mods=allow_workbench_image_override&project={project}"
)
def get_xpk_setup_cmd(tmpdir):
return [
"set -xue",
f"git clone --branch v0.4.1 https://github.com/AI-Hypercomputer/xpk {tmpdir}/xpk",
"pip install ruamel.yaml docker",
]
@task
def generate_workload_id(benchmark_id: str) -> str:
"""Generate a valid workload ID."""
import re
short_id = str(uuid.uuid4())[:8]
# Remove all non-alphanumeric characters, and truncate to ensure the result
# is less than 40 characters.
short_benchmark = re.sub(r"[^a-zA-Z0-9-]+", "", benchmark_id)[:32]
return f"{short_benchmark}{short_id}"
@task
def run_workload(
task_id: str,
cluster_project: str,
zone: str,
cluster_name: str,
benchmark_id: str,
workload_id: str,
gcs_path: str,
docker_image: str,
accelerator_type: str,
run_cmds: str,
num_slices: int = 1,
use_vertex_tensorboard: bool = False,
use_pathways: bool = False,
ramdisk_directory: str = "", # Directory for enabling emergency checkpointing
mtc_enabled: bool = False, # It enables MTC phase-2 drivers
):
"""Run workload through xpk tool."""
with tempfile.TemporaryDirectory() as tmpdir:
if accelerator_type in [
GpuVersion.XPK_H100.value,
GpuVersion.XPK_H100_MEGA.value,
]:
multi_keyword = "num-nodes"
else:
multi_keyword = "num-slices"
create_field = "create-pathways" if use_pathways else "create"
type_field = "tpu-type" if use_pathways else "device-type"
workload_create_cmd = (
f"python {tmpdir}/xpk/xpk.py workload {create_field}"
f" --cluster={cluster_name} --workload={workload_id}"
f" --command='{run_cmds}' --{type_field}={accelerator_type}"
f" --{multi_keyword}={num_slices} --docker-image={docker_image}"
f" --project={cluster_project} --zone={zone}"
f" --env {metric_config.SshEnvVars.GCS_OUTPUT.name}={gcs_path}"
" --restart-on-user-code-failure"
)
if ramdisk_directory:
workload_create_cmd += f" --ramdisk-directory={ramdisk_directory}"
if mtc_enabled:
workload_create_cmd += " --mtc-enabled"
cmds = get_xpk_setup_cmd(tmpdir)
if accelerator_type == GpuVersion.XPK_H100_MEGA.value:
workload_create_cmd += " --scheduler=gke.io/topology-aware-auto"
if use_vertex_tensorboard:
workload_create_cmd += " --use-vertex-tensorboard"
vertex_ai_dependency = (
"pip install -U google-cloud-aiplatform cloud-accelerator-diagnostics"
)
cmds.append(vertex_ai_dependency)
cmds.append(workload_create_cmd)
hook = SubprocessHook()
result = hook.run_command(
["bash", "-c", ";".join(cmds)],
env={**os.environ, "KUBECONFIG": os.path.join(tmpdir, "xpk.conf")},
)
assert result.exit_code == 0, f"XPK command failed with code {result.exit_code}"
def _get_core_api_client(
project_id: str, region: str, cluster_name: str
) -> k8s_client.CoreV1Api:
"""Create a core API client for the given cluster."""
client = gke.get_authenticated_client(project_id, region, cluster_name)
# Initilize the client
core_api = k8s_client.CoreV1Api(client)
logging.info("Successful initilize k8s client from cluster response.")
return core_api
def _list_workload_pods(
core_api: k8s_client.CoreV1Api, workload_id: str
) -> k8s_client.V1PodList:
"""List all pods for the given workload."""
logging.info(f"Getting pods for workload_id: {workload_id}")
pods = core_api.list_namespaced_pod(
label_selector=f"jobset.sigs.k8s.io/jobset-name={workload_id}",
namespace="default",
)
return pods
def _get_batch_api_client(
project_id: str, region: str, cluster_name: str
) -> k8s_client.BatchV1Api:
"""Create a batch API client for the given cluster."""
client = gke.get_authenticated_client(project_id, region, cluster_name)
# Initilize the client
batch_api = k8s_client.BatchV1Api(client)
logging.info("Successful initilize k8s batch api client from cluster response.")
return batch_api
def _get_workload_job(
batch_api: k8s_client.BatchV1Api, workload_id: str
) -> k8s_client.V1Job:
"""Get the job for a given workload."""
logging.info(f"Getting job for workload_id: {workload_id}")
jobs = batch_api.list_namespaced_job(
label_selector=f"jobset.sigs.k8s.io/jobset-name={workload_id}",
namespace="default",
)
if len(jobs.items) == 0:
logging.info(f"Getting job for workload_id: {workload_id}")
return None
if len(jobs.items) > 1:
logging.info(f"Got more than one job for workload_id: {workload_id}")
for i, job in enumerate(jobs.items):
logging.info(f"Job {i=}")
logging.info(f"{job}")
return jobs.items[0]
@task.sensor(poke_interval=60, timeout=600, mode="reschedule")
def wait_for_workload_start(
workload_id: str, project_id: str, region: str, cluster_name: str
) -> bool:
"""Check if the workload has started."""
core_api = _get_core_api_client(project_id, region, cluster_name)
pods = _list_workload_pods(core_api, workload_id)
print(f"Found {len(pods.items)} pods for workload {workload_id}")
return len(pods.items) > 0
@task.sensor(poke_interval=60, timeout=600, mode="reschedule")
def wait_for_workload_completion(
workload_id: str, project_id: str, region: str, cluster_name: str
) -> bool:
"""Check the workload status."""
core_api = _get_core_api_client(project_id, region, cluster_name)
pods = _list_workload_pods(core_api, workload_id)
if not pods.items:
logging.info(f"No pods found for workload selector: {workload_id}.")
# Pathways jobs delete all pods on failure so we must also check if the job
# is complete
batch_api = _get_batch_api_client(project_id, region, cluster_name)
job = _get_workload_job(batch_api, workload_id)
if job is None:
logging.info(f"No pods or jobs were found for workload selector: {workload_id}")
return False
if any(condition.type == "Failed" for condition in job.status.conditions):
# Don't keep retrying if the job has failed
raise AirflowFailException('Job has condition type: "Failed"')
if any(condition.type == "Complete" for condition in job.status.conditions):
logging.info(
f"No pods found but job is complete for workload selector: {workload_id}"
)
return True
return False
if any(pod.status.phase in ["Pending", "Running"] for pod in pods.items):
logging.info("At least one pod has yet to complete.")
return False
try:
for pod in pods.items:
if pod.status.phase == "Failed":
# Don't keep retrying if the pod has failed
raise AirflowFailException(f"Bad pod phase: {pod.status.phase}")
elif pod.status.phase in ["Unknown"]:
raise RuntimeError(f"Bad pod phase: {pod.status.phase}")
finally:
# TODO(jonbolin): log printing for GPUs, which have multiple containers
if len(pod.spec.containers) == 1:
# Print the logs of the last pod checked - either the first failed pod or
# the last successful one.
logs = core_api.read_namespaced_pod_log(
name=pod.metadata.name, namespace=pod.metadata.namespace
)
logging.info(f"Logs for pod {pod.metadata.name}:")
for line in logs.split("\n"):
logging.info(line)
url = LOGGING_URL_FORMAT.format(
project=project_id,
region=region,
cluster=cluster_name,
workload_id=workload_id,
)
logging.info(f"Link to logs: {url}")
logging.info("All pod(s) phase are succeeded.")
return True
@task(trigger_rule="all_done")
def clean_up_workload(
workload_id: str, project_id: str, zone: str, cluster_name: str
) -> bool:
"""Delete workload."""
with tempfile.TemporaryDirectory() as tmpdir:
workload_delete_cmd = (
f"python {tmpdir}/xpk/xpk.py workload delete"
f" --cluster={cluster_name} --workload={workload_id}"
f" --project={project_id} --zone={zone}"
)
cmds = get_xpk_setup_cmd(tmpdir)
cmds.append(workload_delete_cmd)
hook = SubprocessHook()
result = hook.run_command(
["bash", "-c", ";".join(cmds)],
env={**os.environ, "KUBECONFIG": os.path.join(tmpdir, "xpk.conf")},
)
assert result.exit_code == 0, f"XPK clean-up failed with code {result.exit_code}"