forked from aws/sagemaker-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmpi_utils.py
More file actions
302 lines (250 loc) · 9.45 KB
/
mpi_utils.py
File metadata and controls
302 lines (250 loc) · 9.45 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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file 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.
"""This module provides mpi related utility functions for the container drivers."""
from __future__ import absolute_import
import os
import sys
import subprocess
import time
from pathlib import Path
from typing import List
import paramiko
sys.path.insert(0, str(Path(__file__).parent.parent))
from common.utils import ( # noqa: E402 # pylint: disable=C0413,E0611
SM_EFA_NCCL_INSTANCES,
SM_EFA_RDMA_INSTANCES,
get_python_executable,
logger,
)
FINISHED_STATUS_FILE = "/tmp/done.algo-1"
READY_FILE = "/tmp/ready.%s"
DEFAULT_SSH_PORT = 22
def _write_file_to_host(host: str, status_file: str) -> bool:
"""Write the a file to the provided host."""
try:
logger.info(f"Writing {status_file} to {host}")
subprocess.run(
["ssh", host, "touch", f"{status_file}"],
capture_output=True,
text=True,
check=True,
)
logger.info("Finished writing status file")
return True
except subprocess.CalledProcessError:
logger.info(f"Cannot connect to {host}")
return False
def write_status_file_to_workers(worker_hosts: List[str], status_file: str = FINISHED_STATUS_FILE):
"""Write the status file to all worker nodes."""
for worker in worker_hosts:
retry = 0
while not _write_file_to_host(worker, status_file):
time.sleep(5)
retry += 1
if retry > 5:
raise TimeoutError(f"Timed out waiting for {worker} to be reachable.")
logger.info(f"Retrying to write status file to {worker}")
def _wait_for_status_file(status_file: str):
"""Wait for the status file to be created."""
logger.info(f"Waiting for status file {status_file}")
while not os.path.exists(status_file):
time.sleep(30)
logger.info(f"Found status file {status_file}")
def start_sshd_daemon():
"""Start the SSH daemon on the current node."""
sshd_executable = "/usr/sbin/sshd"
if not os.path.exists(sshd_executable):
raise RuntimeError("SSH daemon not found.")
# Start the sshd in daemon mode (-D)
subprocess.Popen([sshd_executable, "-D"])
logger.info("Started SSH daemon.")
class CustomHostKeyPolicy(paramiko.client.MissingHostKeyPolicy):
"""Class to handle host key policy for SageMaker distributed training SSH connections.
Example:
>>> client = paramiko.SSHClient()
>>> client.set_missing_host_key_policy(CustomHostKeyPolicy())
>>> # Will succeed for SageMaker algorithm containers
>>> client.connect('algo-1234.internal')
>>> # Will raise SSHException for other unknown hosts
>>> client.connect('unknown-host') # raises SSHException
"""
def missing_host_key(self, client, hostname, key):
"""Accept host keys for algo-* hostnames, reject others.
Args:
client: The SSHClient instance
hostname: The hostname attempting to connect
key: The host key
Raises:
paramiko.SSHException: If hostname doesn't match algo-* pattern
"""
if hostname.startswith("algo-"):
client.get_host_keys().add(hostname, key.get_name(), key)
return
raise paramiko.SSHException(f"Unknown host key for {hostname}")
def _can_connect(host: str, port: int = DEFAULT_SSH_PORT) -> bool:
"""Check if the connection to the provided host and port is possible."""
try:
logger.debug("Testing connection to host %s", host)
with paramiko.SSHClient() as client:
client.load_system_host_keys()
client.set_missing_host_key_policy(CustomHostKeyPolicy())
client.connect(host, port=port)
logger.info("Can connect to host %s", host)
return True
except Exception as e: # pylint: disable=W0703
logger.info("Cannot connect to host %s", host)
logger.debug(f"Connection failed with exception: {e}")
return False
def _wait_for_workers(worker_hosts: List[str], port: int = DEFAULT_SSH_PORT, timeout: int = 300):
"""Master node waits until it can connect to all worker nodes."""
start_time = time.time()
if not worker_hosts:
logger.info("No worker nodes to connect to.")
return
while True:
logger.info("Master is attempting to connect to all workers...")
all_workers_connected = all(
_can_connect(worker, port) and os.path.exists(READY_FILE % worker)
for worker in worker_hosts
)
if all_workers_connected:
logger.info("Master can connect to all worker nodes.")
break
if time.time() - start_time > timeout:
raise TimeoutError("Timed out waiting for workers to be reachable.")
time.sleep(5) # Wait for 5 seconds before trying again
def _wait_for_master(master_host: str, port: int = DEFAULT_SSH_PORT, timeout: int = 300):
"""Worker nodes wait until they can connect to the master node."""
start_time = time.time()
while True:
logger.info(f"Worker is attempting to connect to the master node {master_host}...")
if _can_connect(master_host, port):
logger.info(f"Worker can connect to master node {master_host}.")
break
if time.time() - start_time > timeout:
raise TimeoutError(f"Timed out waiting for master {master_host} to be reachable.")
time.sleep(5) # Wait for 5 seconds before trying again
def bootstrap_worker_node(master_host: str, status_file: str = FINISHED_STATUS_FILE):
"""Bootstrap the worker nodes."""
logger.info("Bootstrapping worker node...")
_wait_for_master(master_host)
_write_file_to_host(master_host, READY_FILE % os.environ["SM_CURRENT_HOST"])
_wait_for_status_file(status_file)
def bootstrap_master_node(worker_hosts: List[str]):
"""Bootstrap the master node."""
logger.info("Bootstrapping master node...")
_wait_for_workers(worker_hosts)
def validate_smddprun() -> bool:
"""Whether smddprun is installed.
Returns:
bool: True if installed
"""
try:
output = subprocess.run(
["which", "smddprun"],
capture_output=True,
text=True,
check=True,
)
return output.stdout != ""
except subprocess.CalledProcessError:
return False
def validate_smddpmprun() -> bool:
"""Whether smddpmprun is installed.
Returns:
bool: True if both are installed
"""
try:
output = subprocess.run(
["which", "smddpmprun"],
capture_output=True,
text=True,
check=True,
)
return output.stdout != ""
except subprocess.CalledProcessError:
return False
def write_env_vars_to_file():
"""Write environment variables to /etc/environment file."""
with open("/etc/environment", "a", encoding="utf-8") as f:
for name in os.environ:
f.write(f"{name}={os.environ.get(name)}\n")
def get_mpirun_command(
host_count: int,
host_list: List[str],
num_processes: int,
additional_options: List[str],
entry_script_path: str,
):
"""Fetch mpi command"""
network_interface_name = os.environ.get("SM_NETWORK_INTERFACE_NAME", "eth0")
mpirun_command = [
"mpirun",
"--host",
",".join(host_list),
"-np",
str(num_processes),
"--allow-run-as-root",
"--tag-output",
"-mca",
"btl_tcp_if_include",
network_interface_name,
"-mca",
"oob_tcp_if_include",
network_interface_name,
"-mca",
"plm_rsh_no_tree_spawn",
"1",
"-mca",
"pml",
"ob1",
"-mca",
"btl",
"^openib",
"-mca",
"orte_abort_on_non_zero_status",
"1",
"-mca",
"btl_vader_single_copy_mechanism",
"none",
"-mca",
"plm_rsh_num_concurrent",
str(host_count),
"-x",
"NCCL_SOCKET_IFNAME=%s" % network_interface_name,
"-x",
"LD_LIBRARY_PATH",
"-x",
"PATH",
]
if additional_options:
mpirun_command.extend(additional_options)
instance_type = os.environ["SM_CURRENT_INSTANCE_TYPE"]
# EFA settings
if instance_type in SM_EFA_NCCL_INSTANCES:
mpirun_command.extend(["-x", "FI_PROVIDER=efa"])
# Use simple protocol to handle the out-of-order data delivery from EFA
mpirun_command.extend(["-x", "NCCL_PROTO=simple"])
if instance_type in SM_EFA_RDMA_INSTANCES:
# Use EFA's RDMA functionality for one-sided and two-sided transfer
mpirun_command.extend(["-x", "FI_EFA_USE_DEVICE_RDMA=1"])
for credential in [
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
"AWS_SESSION_TOKEN",
]:
if credential in os.environ:
mpirun_command.extend(["-x", credential])
mpirun_command.extend([get_python_executable()])
mpirun_command.extend(["-m", "mpi4py", entry_script_path])
return mpirun_command