-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathhyperpod-precheck.py
294 lines (245 loc) · 8.95 KB
/
hyperpod-precheck.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
import os
import re
import sys
import json
import socket
import pathlib
import argparse
import subprocess
def check_if_fsx_mounted():
result = subprocess.run(
["df", "-h"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
for line in result.stdout.splitlines():
if "/fsx" in line:
return True
return False
def check_if_user_directory_on_fsx() -> bool:
"""
Checks if the user directory is using /fsx since EBS volumes are not allowed
"""
user = os.environ.get("USER", None)
if not user:
raise Exception("$USER variable not set")
return os.environ.get("HOME") == f"/fsx/{user}"
def check_if_docker_installed() -> bool:
"""Just check if docker is available on the node where you will build the"""
try:
# Run the command 'docker --version'
result = subprocess.run(
["docker", "--version"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
# If the command was successful, Docker is installed
if result.returncode == 0:
return True
else:
return False
except FileNotFoundError:
# The command 'docker' was not found, Docker is not installed
return False
def check_if_pyxis_installed() -> bool:
"""Check if pyxis is installed and available"""
try:
# Run the command and capture the output
result = subprocess.run(
["srun", "--help"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
# Check if 'container-image' is in the command output
if "pyxis" in result.stdout:
return True
else:
return False
except Exception as e:
print(f"An error occurred: {e}")
return False
def check_if_enroot_installed() -> bool:
"""enroot is installed and available"""
try:
# Run the command 'enroot'
result = subprocess.run(
["sudo", "enroot"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
# If the command was successful, enroot is installed
if result.returncode == 0:
return True
else:
return False
except FileNotFoundError:
# The command 'enroot' was not found, enroot is not installed
return False
def check_enroot_runtime_path() -> bool:
"""Check the enroot runtime path"""
file_path = "/etc/enroot/enroot.conf"
try:
with open(file_path, "r") as file:
for line in file:
# Check if "ENROOT_RUNTIME_PATH" is in the line
if re.search("ENROOT_RUNTIME_PATH", line):
return ("/opt/sagemaker/tmp/enroot/user-$(id -u)" in line.strip() or "/opt/dlami/nvme/tmp/enroot/user-$(id -u)" in line.strip())
print(line.strip()) # Print the matching line
except FileNotFoundError:
print(f"The file {file_path} was not found.")
return False
except PermissionError:
print(f"Permission denied when trying to read {file_path}.")
return False
def check_node_connectivity() -> bool:
"""Check if nodes are connected"""
try:
# Run the command 'enroot'
result = subprocess.run(
["hostname"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
# If the command was successful, enroot is installed
if result.returncode == 0:
return True
else:
return False
except FileNotFoundError:
# The command 'enroot' was not found, enroot is not installed
return False
def check_slurmd_service_status() -> bool:
try:
# Run the 'systemctl status' command for the given service
result = subprocess.run(
["systemctl", "status", "slurmd"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
# Check if the service is active
if "active (running)" in result.stdout:
return True
elif "inactive" in result.stdout:
return False
elif "failed" in result.stdout:
return False
else:
return False
except Exception as e:
return False
def nvidia_cli_installed() -> bool:
"""Make sure Nvidia Container CLI is installed and available
Returns:
_type_: _description_
"""
try:
# Run the command 'nvidia-container-cli --version'
result = subprocess.run(
["nvidia-container-cli", "--version"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
# If the command was successful, nvidia-container-cli is installed
if result.returncode == 0:
return True
else:
return False
except FileNotFoundError:
# The command 'nvidia-container-cli' was not found, nvidia-container-cli is not installed
return False
def check_docker_data_root():
"""Check data root"""
file_path = "/etc/docker/daemon.json"
try:
json_data = json.loads(pathlib.Path(file_path).open().read())
if not (json_data["data-root"] == "/opt/dlami/nvme/docker/data-root" or json_data["data-root"] == "/opt/sagemaker/docker/data-root"):
print("Resolution: cd /tmp/sagemaker-lifecycle-* && cd src/utils/ && srun -N <no of nodes> bash install_docker.sh")
return False
else:
return True
except FileNotFoundError:
print(f"The file {file_path} was not found. Resolution: cd /tmp/sagemaker-lifecycle-* && cd src/utils/ && srun -N <no of nodes> bash install_docker.sh")
return False
except PermissionError:
print(f"Permission denied when trying to read {file_path}. File should be readable across all users")
return False
except Exception as e:
return False
def check_file_for_strings(file_path, search_strings):
"""
Check if any of the specified strings are in the file.
Parameters:
- file_path: Path to the file to be checked.
- search_strings: A list of strings to search for in the file.
Returns:
- True if none of the search strings are found in the file, False otherwise.
"""
try:
with open(file_path, "r") as file:
for line in file:
if any(search_string in line for search_string in search_strings):
print(f"\n\nFile contains {search_strings} which is an unsupported option on Hyperpod currently. Unless you've enabled gres/pmix explicitly - this won't work. Please refer to https://catalog.workshops.aws/sagemaker-hyperpod/en-US/04-advanced/08-gres\n\n")
return False
return True
except FileNotFoundError:
print(f"The file {file_path} was not found.")
return False
except PermissionError:
print(f"Permission denied when trying to read {file_path}.")
return False
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Runtime check for Hyperpod training")
group = parser.add_mutually_exclusive_group()
group.add_argument(
"-r", "--runtime", action="store_false", help="Check runtime for provided nodes"
)
group.add_argument(
"-f", "--filename", type=str, help="Name of script e.g. 1.training.sbatch"
)
parser.add_argument(
"-v", "--verbose", action="store_false", help="Enable verbose output"
)
parser.add_argument(
"-w", "--who", type=str, help="Name of the user who's going to run scripts"
)
args = parser.parse_args()
if args.filename:
full_path = os.path.abspath(args.filename)
# print(f"The full path of the script to check is: {full_path}")
search_strings = ["--gpus", "pmix", "--gres=gpu"]
if check_file_for_strings(full_path, search_strings):
print(f"{full_path} \u2705")
else:
print(f"{full_path} \u274C")
sys.exit()
hostname = socket.gethostname()
function_names = [
"check_if_docker_installed",
"check_enroot_runtime_path",
"check_docker_data_root",
"check_if_fsx_mounted",
"check_if_pyxis_installed",
"check_slurmd_service_status",
"check_if_user_directory_on_fsx",
"nvidia_cli_installed",
]
results = {}
for function_name in function_names:
func = getattr(sys.modules[__name__], function_name)
func_result = func()
results[function_name] = func_result
if args.verbose:
print(f"{hostname}: test {function_name} - {func_result}")
if all(list(results.values())):
print(f"{hostname} ---------------- \u2705")
else:
print(f"{hostname} ---------------- \u274C")