-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathxpk.py
186 lines (161 loc) · 6.31 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
# 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/google/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.vm_resource import GpuVersion
WORKLOAD_URL_FORMAT = "https://console.cloud.google.com/kubernetes/service/{region}/{cluster}/default/{workload_id}/details?project={project}"
@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,
):
"""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"
)
cmds = [
"set -xue",
f"git clone https://github.com/google/xpk {tmpdir}/xpk",
f"cd {tmpdir}/xpk && make install && export PATH=$PATH:$PWD/bin",
]
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
@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}.")
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 = WORKLOAD_URL_FORMAT.format(
region=region,
cluster=cluster_name,
workload_id=workload_id,
project=project_id,
)
logging.info(f"Link to workload: {url}")
logging.info("All pod(s) phase are succeeded.")
return True