forked from PKU-DAIR/Hetu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.py
More file actions
262 lines (237 loc) · 10.4 KB
/
runner.py
File metadata and controls
262 lines (237 loc) · 10.4 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
import argparse
import yaml
import os
import signal
import multiprocessing
import subprocess
import paramiko
import socket
import psutil
import contextlib
import hetu as ht
_procs = []
def signal_handler(signal, frame):
print("SIGINT signal caught, stop Training")
for proc in _procs:
proc.kill()
global executor_shell
executor_shell.kill()
exit(0)
def start_sched():
os.environ["DMLC_ROLE"] = "scheduler"
ht.scheduler_init()
ht.scheduler_finish()
def start_server():
os.environ["DMLC_ROLE"] = "server"
ht.server_init()
ht.server_finish()
@ contextlib.contextmanager
def ssh_connect(host, identify_file):
try:
ssh_directory = os.path.expanduser('~/.ssh') if identify_file == '' else os.path.dirname(
os.path.abspath(os.path.expanduser(identify_file)))
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
private = paramiko.RSAKey.from_private_key_file(
os.path.join(ssh_directory, 'id_rsa'))
config = paramiko.config.SSHConfig.from_path(
os.path.join(ssh_directory, 'config'))
conf = config.lookup(host)
ssh.connect(hostname=conf['hostname'], port=conf['port'],
username=conf['user'], pkey=private)
yield ssh
finally:
ssh.close()
def start_remote_server(host, local_server_num, identify_file):
with ssh_connect(host, identify_file) as ssh:
sftp = ssh.open_sftp()
sftp.put('/tmp/hetu_ps_config.yml',
'/tmp/hetu_ps_config.yml', confirm=True)
sftp.close()
stdin, stdout, stderr = ssh.exec_command(
'python -m hetu.launcher /tmp/hetu_ps_config.yml -n %d' % local_server_num)
stdout = stdout.read().decode()
stderr = stderr.read().decode()
if stdout:
print('From remote %s stdout:\n %s' % (host, stdout.strip()))
if stderr:
print('From remote %s stderr:\n %s' % (host, stderr.strip()))
def send_config(host, identify_file):
with ssh_connect(host, identify_file) as ssh:
sftp = ssh.open_sftp()
sftp.put('/tmp/hetu_config.yml', '/tmp/hetu_config.yml', confirm=True)
sftp.close()
def get_nic_names(local_address, remote_hostnames, identify_file):
# get local interface
nics = dict()
for iface, addrs in psutil.net_if_addrs().items():
for addr in addrs:
if addr.family == socket.AF_INET:
nics[addr.address] = iface
local_nic = nics[local_address]
# get remote interfaces
command_prefix = "\"from socket import AF_INET;\nfrom psutil import net_if_addrs;\n" +\
"nics = dict();\nfor iface, addrs in net_if_addrs().items():\n for addr in addrs:" +\
"\n if addr.family == AF_INET:\n nics[addr.address] = iface;\n"
ssh_directory = os.path.expanduser('~/.ssh') if identify_file == '' else os.path.dirname(
os.path.abspath(os.path.expanduser(identify_file)))
remote_nics = set()
for hostname in remote_hostnames:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
private = paramiko.RSAKey.from_private_key_file(
os.path.join(ssh_directory, 'id_rsa'))
config = paramiko.config.SSHConfig.from_path(
os.path.join(ssh_directory, 'config'))
conf = config.lookup(hostname)
command = command_prefix + "print(nics[\'%s\'])\"" % (conf['hostname'])
ssh.connect(hostname=conf['hostname'], port=conf['port'],
username=conf['user'], pkey=private)
stdin, stdout, stderr = ssh.exec_command('python -c %s' % command)
stdout = stdout.read().decode()
stderr = stderr.read().decode()
remote_nics.add(stdout.strip())
if stderr:
print('From remote %s stderr:\n %s' % (hostname, stderr.strip()))
ssh.close()
remote_nics.add(local_nic)
return list(remote_nics)
def get_subnet(local_address, remote_hostnames, identify_file=''):
ssh_directory = os.path.expanduser('~/.ssh') if identify_file == '' else os.path.dirname(
os.path.abspath(os.path.expanduser(identify_file)))
config = paramiko.config.SSHConfig.from_path(
os.path.join(ssh_directory, 'config'))
remote_address = [config.lookup(hostname)['hostname']
for hostname in remote_hostnames]
remote_address.append(local_address)
address_pool = set()
for addr in remote_address:
binary_repr = int(''.join([format(int(part), '08b')
for part in addr.split('.')]), 2)
address_pool.add(format(binary_repr+1, '032b'))
address_pool.add(format(binary_repr-1, '032b'))
address_pool = list(address_pool)
longestCommonPrefix = 0
for item in zip(*address_pool):
if len(set(item)) > 1:
break
longestCommonPrefix += 1
if longestCommonPrefix > 30:
longestCommonPrefix = 30
assert longestCommonPrefix >= 16, 'Hosts not in the same subnet!'
commonAddress = address_pool[0][:longestCommonPrefix] + \
'0' * (32 - longestCommonPrefix)
parts = [commonAddress[:8], commonAddress[8:16],
commonAddress[16:24], commonAddress[24:]]
subnet = '.'.join([str(int(part, 2))
for part in parts]) + '/%d' % longestCommonPrefix
return subnet
def main():
signal.signal(signal.SIGINT, signal_handler)
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', default=None,
help='Configuration file.')
parser.add_argument('-w', '--workers', type=int, default=0,
help='Shorthand for the number of local worker.')
parser.add_argument('-s', '--servers', type=int, default=0,
help='Shorthand for the number of local server.')
parser.add_argument('-i', '--identify', default='',
help='SSH identify file.')
parser.add_argument('command', nargs=argparse.REMAINDER,
help='Command to be executed.')
args = parser.parse_args()
settings = ht.DistConfig(args.config, args.servers, args.workers)
print(settings)
if settings.enable_PS:
ps_config = settings.make_ps_config()
for k, v in ps_config.items():
os.environ[k] = str(v)
settings.save('/tmp/hetu_config.yml')
for host in settings.hosts:
if host != settings.chief:
send_config(host, args.identify)
global executor_shell
if len(settings.hosts) == 1:
# single machine
# TODO: add hostdress validation check
if settings.enable_PS:
proc = multiprocessing.Process(target=start_sched)
_procs.append(proc)
for i in range(settings.num_servers):
proc = multiprocessing.Process(target=start_server)
_procs.append(proc)
for proc in _procs:
proc.start()
mpi_command = 'mpirun --allow-run-as-root --tag-output -np %d %s' % (
settings.num_workers, ' '.join(args.command))
env = dict(os.environ)
if settings.enable_PS:
env["DMLC_ROLE"] = "worker"
executor_shell = subprocess.Popen(
mpi_command, shell=True, env=env, stdout=None, stderr=None)
for proc in _procs:
proc.join()
executor_shell.wait()
else:
# multi machines
#! nic names not used currently, use subnets instead; nccl_socket_name please specified in /etc/bash.bashrc
#! nic methods cannot support different nic name on different machines
# nics = get_nic_names(chief_address, set(hosts) - {chief}, args.identify)
# joined_nics = ','.join(nics)
subnet = get_subnet(settings.chief_address, set(
settings.hosts) - {settings.chief}, args.identify)
if settings.enable_PS:
with open('/tmp/hetu_ps_config.yml', 'w') as fw:
yaml.dump({'shared': ps_config}, fw)
proc = multiprocessing.Process(target=start_sched)
_procs.append(proc)
for node in settings.hosts:
if node == settings.chief:
for i in range(settings.servers.get(node, 0)):
proc = multiprocessing.Process(target=start_server)
_procs.append(proc)
else:
if settings.servers.get(node, 0):
proc = multiprocessing.Process(target=start_remote_server, args=[
node, settings.servers[node], args.identify])
_procs.append(proc)
for proc in _procs:
proc.start()
basic_args = '--allow-run-as-root --tag-output'
hosts_in_command = ','.join(
['%s:%d' % (node, nworkers) for node, nworkers in settings.workers.items()])
mpi_ssh_args = '' if args.identify == '' else '-bootstrap=ssh -bootstrap-exec-args -i %s' % args.identify
tcp_intf_arg = '-mca btl_tcp_if_include %s' % subnet
# tcp_intf_arg = '-mca btl_tcp_if_include %s' % joined_nics
# nccl_socket_intf_arg = '-x NCCL_SOCKET_IFNAME=%s' % joined_nics
env_list = ' '.join(['-x %s=%s' % (k, str(v)) for k, v in ps_config.items()] + [
'-x DMLC_ROLE=worker']) if settings.enable_PS else ''
mpi_command = (
'mpirun {basic_args} '
'--host {hosts} '
'{mpi_ssh_args} '
'{tcp_intf_arg} '
# '{nccl_socket_intf_arg} '
'{env} '
'{command}'
.format(basic_args=basic_args,
hosts=hosts_in_command,
mpi_ssh_args=mpi_ssh_args,
tcp_intf_arg=tcp_intf_arg,
# nccl_socket_intf_arg=nccl_socket_intf_arg,
env=env_list,
command=' '.join(args.command))
)
executor_shell = subprocess.Popen(
mpi_command, shell=True, stdout=None, stderr=None)
for proc in _procs:
proc.join()
executor_shell.wait()
if __name__ == '__main__':
#! need to modify /etc/bash.bashrc on other machines for:
# * specify NCCL_SOCKET_IFNAME
# * specify PATH for mpirun support
# * activate conda environment
# * specify PYTHONPATH for hetu support
#! ssh process to other machines for server CANNOT receive SIGINT from Ctrl+C on this machine, please kill on other machines
main()