-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_env.py
165 lines (145 loc) · 5.81 KB
/
init_env.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
"""
build horovod-modified
"""
import os
import sys
from os.path import expanduser, join
import shlex
import subprocess
import paramiko
class Initializer:
def __init__(self, IPs, nccl_home) -> None:
"""
"""
self.IPs= IPs
self.nccl_home = nccl_home
self._init_ssh()
def _init_ssh(self):
key = paramiko.RSAKey.from_private_key_file(
expanduser("~/.ssh/id_rsa"))
print('='*10, 'initializing ssh connections')
self.clients = []
for node in self.IPs:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=node, username="ubuntu", pkey=key)
self.clients.append((node, client))
print('IP', node, 'DONE')
print('='*10, 'initialization for ssh clients DONE')
def init(self):
self.download()
self.build()
self.append_PYTHONPATH()
def download(self):
""""""
for ip, cli in self.clients:
check_cmd = "mkdir autorun; cd ~/autorun; ls|grep horovod-modified"
_, stdout, stderr = cli.exec_command(check_cmd)
if stdout.read() != b'':
print(ip, 'source file exist')
else:
cmd = "cd ~/autorun; "\
"wget https://dt-training.s3.amazonaws.com/horovod-modified.tar.gz;" \
"tar zxf horovod-modified.tar.gz; "\
"rm horovod-modified.tar.gz"
_, stdout, stderr = cli.exec_command(cmd)
print(ip, ":", "stdout::", stdout.read(), "stderr::", stderr.read())
check_cmd = "cd ~/autorun; ls|grep distributed-training"
_, stdout, stderr = cli.exec_command(check_cmd)
if stdout.read() != b"":
print(ip, "distributed-training folder exisit")
else:
cmd = "cd ~/autorun;"\
"git clone https://github.com/zarzen/distributed-training.git"
_, stdout, stderr = cli.exec_command(cmd)
print(ip, '')
def build(self):
""""""
for ip, cli in self.clients:
print("-"*10, 'horovod building at', ip)
cmd = "cd ~/autorun/horovod-modified; "\
"HOROVOD_GPU_ALLREDUCE=NCCL HOROVOD_NCCL_HOME=" \
+ self.nccl_home + " python3 setup.py build"
_, stdout, stderr = cli.exec_command(cmd)
print('stdout::', stdout.read(), 'stderr::', stderr.read())
print('-'*10, 'horovod built at', ip)
def append_PYTHONPATH(self):
"""
"""
for ip, cli in self.clients:
check_cmd = "echo $PYTHONPATH | "\
"grep autorun/horovod-modified/build/lib.linux-x86_64-3.6"
_, stdout, stderr = cli.exec_command(check_cmd)
if stdout.read() != b"":
print('PYTHONPATH exists')
else:
cmd = "echo 'export PYTHONPATH=\"{}\":$PYTHONPATH' >> ~/.bashrc".format(
join(expanduser("~/autorun/horovod-modified"), "build/lib.linux-x86_64-3.6")
)
_, stdout, stderr = cli.exec_command(cmd)
print(ip, ": append PYTHONPATH")
def delete(self):
"""
delete source for fresh download
"""
for ip, cli in self.clients:
print('>'*10, 'deleting source files at ', ip)
cmd = "rm -rf ~/autorun/horovod-modified/; rm -rf ~/autorun/distributed-training"
_, stdout, stderr = cli.exec_command(cmd)
print(ip, 'stdout::', stdout.read(), 'stderr::', stderr.read())
print(ip, "deleted sources")
def update_scripts(self):
""""""
for ip, cli in self.clients:
print('>'*10, 'updating training script')
cmd = "cd ~/autorun/distributed-training; git pull"
_, stdout, stderr = cli.exec_command(cmd)
print(ip, 'stdout::', stdout.read(), 'stderr::', stderr.read())
print('>'*10, 'training source update done at', ip)
def check_bash_env(horovod_path):
lib_path = join(str(expanduser(horovod_path)), "build/lib.linux-x86_64-3.6")
PYTHONPATH = os.getenv("PYTHONPATH")
print(PYTHONPATH)
if not PYTHONPATH or lib_path not in PYTHONPATH:
cmd = "echo 'export PYTHONPATH=\"{}\":$PYTHONPATH' >> ~/.bashrc".format(lib_path)
print(cmd)
subprocess.call(cmd, shell=True, executable='/bin/bash')
subprocess.call("source ~/.bashrc", shell=True, executable='/bin/bash')
def main():
"""
HOROVOD_GPU_ALLREDUCE=NCCL
HOROVOD_NCCL_HOME=
disable TF and MXNET
HOROVOD_WITH_TENSORFLOW=0 HOROVOD_WITH_PYTORCH=1 HOROVOD_WITH_MXNET=0
"""
nccl_home = "/usr/local/nccl"
python_bin = "/usr/bin/python3"
horovod_path = "~/horovod-modified"
horovod_build_cmd = "{} setup.py build".format(python_bin)
horovod_build_env = {
"PATH": os.getenv("PATH"),
"HOROVOD_WITH_PYTORCH": "1",
"HOROVOD_GPU_ALLREDUCE": "NCCL"
}
if nccl_home:
horovod_build_env['HOROVOD_NCCL_HOME'] = nccl_home
subprocess.call(shlex.split(horovod_build_cmd),
env=horovod_build_env, cwd=expanduser(horovod_path))
check_bash_env(horovod_path)
print(">"*10, "need to run `source ~/.bashrc`, to make sure the added PYTHONPATH works")
if __name__ == "__main__":
# main()
if len(sys.argv) < 2:
print("Need command: init/delete/update")
helper = Initializer(
IPs = ['localhost', '172.31.29.187'],
nccl_home = "/usr/local/nccl"
)
if sys.argv[1] == "init":
helper.init()
elif sys.argv[1] == "delete":
helper.delete()
elif sys.argv[1] == 'update':
helper.update_scripts()
else:
print('wrong command')