-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpc.py
301 lines (233 loc) · 8.27 KB
/
rpc.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
295
296
297
298
299
300
301
# (C) Copyright 2022 CEA LIST. All Rights Reserved.
# Contributor(s): Nicolas Granger <nicolas.granger@cea.fr>
#
# This software is governed by the CeCILL-C license under French law and
# abiding by the rules of distribution of free software. You can use,
# modify and/ or redistribute the software under the terms of the CeCILL-C
# license as circulated by CEA, CNRS and INRIA at the following URL
# "http://www.cecill.info".
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty and the software's author, the holder of the
# economic rights, and the successive licensors have only limited
# liability.
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL-C license and that you accept its terms.
import io
import select
import socket
import struct
import sys
import threading
import time
import weakref
from typing import Any, Callable, Dict, TypeVar
if sys.version_info < (3, 8):
import pickle5 as pickle
else:
import pickle
import torch
from tblib import pickling_support
# absolute import required for unpickling
from rpcdataloader.utils import pkl_dispatch_table
pickling_support.install()
_T = TypeVar("_T")
def _serialize(obj, buffer_cb=None):
buffer = io.BytesIO()
pickler = pickle.Pickler(
buffer, protocol=pickle.HIGHEST_PROTOCOL, buffer_callback=buffer_cb
)
pickler.dispatch_table = pkl_dispatch_table
pickler.dump(obj)
return buffer.getvalue()
def _sock_read(sock, size, buffer=None):
"""Read size bytes from sock."""
buffer = bytearray(size) if buffer is None else buffer
received = 0
while received < size:
nread = sock.recv_into(memoryview(buffer)[received:])
if not nread:
raise RuntimeError("Unexpected socket shutdown.")
received += nread
return buffer
def _create_connection(host, timeout, *kargs, **kwargs):
host, port = host.split(":")
port = int(port)
for i in range(int(timeout)):
try:
return socket.create_connection(
(host, port), *kargs, timeout=timeout, **kwargs)
except OSError as e:
if i + 1 == timeout:
raise e from None
else:
time.sleep(1)
tls = threading.local()
def _rpc_send_command(host, fut, func, args, kwargs, pin_memory, rref, timeout):
tls.host = host
try:
payload = _serialize((func, args, kwargs, rref))
# connect to server
with _create_connection(host, timeout=timeout) as s:
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
# send command
s.sendall(struct.pack("L", len(payload)))
s.sendall(payload)
select.select([s], [], [])
# receive buffers
payload = _sock_read(s, struct.calcsize("L"))
(nbuffers,) = struct.unpack("L", payload)
buffers = []
for _ in range(nbuffers):
payload = _sock_read(s, struct.calcsize("L"))
(n,) = struct.unpack("L", payload)
if pin_memory:
b = torch.empty(
n, dtype=torch.uint8, pin_memory=pin_memory
).numpy()
else:
b = bytearray(n)
_sock_read(s, n, b)
buffers.append(b)
# receive object
payload = _sock_read(s, struct.calcsize("L"))
(n,) = struct.unpack("L", payload)
payload = _sock_read(s, n)
out, err = pickle.loads(payload, buffers=buffers)
if err:
fut.set_exception(err)
else:
fut.set_result(out)
except Exception as e:
fut.set_exception(e)
def rpc_async(
host: str,
func: Callable[..., _T],
args=None,
kwargs=None,
pin_memory=False,
rref: bool = False,
timeout=120.0,
) -> torch.futures.Future[_T]:
"""Execute function on remote worker and return the result as a future.
:param host:
rpc worker host
:param func:
function to execute
:param args:
positional arguments
:param kwargs:
keword arguments
:param pin_memory:
wether buffers (ie: tensors) should be allocated in pinned memory.
:param rref:
whether to return the output as a remote reference.
:param timeout:
timeout in seconds on network operations
:return:
A future that will contain the function return value.
.. note::
:attr:`func` and its arguments must be serializable, which exludes
the usage of lambdas or locally defined functions.
"""
fut = torch.futures.Future()
t = threading.Thread(
target=_rpc_send_command,
args=(host, fut, func, args, kwargs, pin_memory, rref, timeout),
)
t.start()
return fut
def _handle_client(sock, parallel_sem):
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_CORK, 1)
payload = _sock_read(sock, struct.calcsize("L"))
(n,) = struct.unpack("L", payload)
payload = _sock_read(sock, n)
cmd, args, kwargs, rref = pickle.loads(payload)
if args is None:
args = ()
if kwargs is None:
kwargs = {}
try:
with parallel_sem:
out = cmd(*args, **kwargs)
if rref:
out = RRef(obj=out)
err = None
except Exception as e:
out = None
err = e
try:
buffers = []
payload = _serialize((out, err), buffer_cb=buffers.append)
except Exception as e:
buffers = []
payload = _serialize((None, e))
buffers = [memoryview(b).tobytes() for b in buffers]
sock.sendall(struct.pack("L", len(buffers)))
for b in buffers:
sock.sendall(struct.pack("L", len(b)))
sock.sendall(b)
sock.sendall(struct.pack("L", len(payload)))
sock.sendall(payload)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_CORK, 0)
def _create_server(address, *, family=socket.AF_INET):
sock = socket.socket(family, socket.SOCK_STREAM)
try:
sock.bind(address)
sock.listen()
return sock
except BaseException as e:
sock.close()
raise e from None
def run_worker(host: str, port: int, timeout: float = 120, parallel: int = 1):
"""Start listening and processing remote procedure calls.
:param host: interface to bind to (set to '0.0.0.0' for all interfaces)
:param port: port to bind to
:param timeout: timeout on network transfers from/to client
:param parallel: max number procedures executing concurrently
.. warning::
The workers neither implement authentication nor encryption, any
user on the network can send arbitrary commands or may listen to the
traffic from/to the worker.
.. note::
- each request is processed in a separate thread
- network transfers may overlap regardless of :attr:`parallel` argument.
"""
torch.set_num_threads(1) # prevent thread competition
parallel_sem = threading.Semaphore(parallel)
with _create_server((host, port), family=socket.AF_INET) as sock:
while True:
client_sock, _ = sock.accept()
client_sock.settimeout(timeout)
t = threading.Thread(
target=_handle_client, args=[client_sock, parallel_sem]
)
t.start()
_handles: Dict[int, Any] = {}
class RRef:
def __init__(self, obj=None, uid=None):
if uid is None:
self.obj = obj
self.uid = None
else:
self.uid = uid
self.host = tls.host
weakref.finalize(self, rpc_async, self.host, _handles.pop, [uid])
@staticmethod
def wrap(func, args, kwargs):
return RRef(obj=func(*args, **kwargs))
@staticmethod
def _rebuild_remote(uid):
return RRef(uid=uid)
@staticmethod
def _rebuild_local(uid):
return _handles[uid]
def __reduce__(self):
if self.uid is not None:
return RRef._rebuild_local, (self.uid,)
else:
uid = id(self.obj)
if uid in _handles:
raise RuntimeError("Only one rref can exist for a given object")
_handles[uid] = self.obj
return RRef._rebuild_remote, (uid,)