-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworker.py
More file actions
346 lines (299 loc) · 12.9 KB
/
Copy pathworker.py
File metadata and controls
346 lines (299 loc) · 12.9 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
from contextlib import contextmanager
from controller.ndslice import NDSlice
from supervisor import LocalMessageQueue, get_message_queue, TTL
from supervisor.logging import initialize_logging
from typing import Dict, NamedTuple, Any, Union, Optional, List, Tuple, Callable
import os
import importlib
import logging
from .tree import flatten, tree_map
import torch
import torch.distributed
from traceback import extract_tb
from . import messages
from .messages import RemoteFunction
from .tensor_factory import TensorFactory
from .reference import Ref
logger = logging.getLogger(__name__)
def log(*args):
logger.info(*args)
class Dim(NamedTuple):
rank: int
size: int
process_group: Any
class DeviceMesh:
def __init__(self, names: Tuple[str, ...], ranks: NDSlice, rank: int):
self.dims = {}
coordinates = ranks.coordinates(rank)
for coordinate, name, size, stride in zip(coordinates, names, ranks.sizes, ranks.strides):
start = rank - stride*coordinate
members = [*range(start, start + stride*size, stride)]
assert members[coordinate] == rank
process_group = torch.distributed.new_group(members, use_local_synchronization=True)
self.dims[name] = Dim(coordinate, size, process_group)
def _rank(mesh: 'DeviceMesh', dim: str):
return torch.full((), mesh.dims[dim].rank, dtype=torch.long)
class DependentOnError(Exception):
def __init__(self, ident: int):
self.ident = ident
class Stream:
def __init__(self, default: bool):
if default:
self._cuda_stream = None
else:
self._cuda_stream = torch.cuda.Stream()
@property
def cuda_stream(self):
if self._cuda_stream is None:
return torch.cuda.current_stream()
else:
return self._cuda_stream
@contextmanager
def enable(self):
if self._cuda_stream is None:
yield
return
with torch.cuda.stream(self._cuda_stream):
yield
def event(self):
e = torch.cuda.Event()
self.cuda_stream.record_event(e)
return e
def wait_event(self, event):
self.cuda_stream.wait_event(event)
def wait_stream(self, stream):
self.cuda_stream.wait_stream(stream.cuda_stream)
class Borrow:
def __init__(self, from_stream: Stream, to_stream: Stream):
self.from_stream = from_stream
self.to_stream = to_stream
self.event = from_stream.event()
def use(self):
self.to_stream.wait_event(self.event)
self.event = None
def drop(self):
if self.event is not None:
return
self.from_stream.wait_stream(self.to_stream)
class Worker:
def __init__(self, q: LocalMessageQueue, store, rank: int, world: int, local_rank: int):
# remote ref id to local value
self.env: Dict[int, Any] = {}
self.q = q
self.store = store
self.rank = rank
self.world = world
self.local_rank = local_rank
self.first_uncompleted_ident = 0
self.last_send_status = 0
self.borrows: Dict[int, Optional[Borrow]] = {}
def handle_message(self, event: NamedTuple):
cmd = event.__class__.__name__
fn = getattr(self, cmd, None)
if fn is not None:
return fn(*event)
raise RuntimeError(f"unhandled event: {event}")
def CreateDeviceMesh(self, result: 'Ref', names: Tuple[str, ...], ranks: NDSlice):
self.define(result, DeviceMesh(names, ranks, self.rank))
def lookup(self, a: Any):
if isinstance(a, Ref):
r = self.env[a.id]
if isinstance(r, DependentOnError):
raise r
return r
return a
def CallFunction(self, ident: int, results: Tuple[Ref], mutates: Tuple[Ref], rfunction: RemoteFunction, args: Tuple[Any, ...], kwargs: Dict[str, Any], streamref: Ref):
with self.try_define(ident, results + mutates):
stream: Stream = self.lookup(streamref)
args, kwargs = tree_map(self.lookup, (args, kwargs))
function = rfunction.resolve()
with stream.enable():
result = function(*args, **kwargs)
tensors, _ = flatten(result, lambda x: isinstance(x, torch.Tensor))
assert len(results) == len(tensors)
for r, t in zip(results, tensors):
self.define(r, t)
def CreateStream(self, result: 'Ref', default: bool):
self.define(result, Stream(default))
@contextmanager
def try_define(self, ident: int, results: Tuple[Ref, ...]):
try:
yield
except DependentOnError as e:
for r in results:
self.define(r, e)
# note: there is no need to to send RemoteFunctionFailed
# because the controller would have already gotten and propagated the
# original created of DependentOnError.
except Exception as e:
exc = DependentOnError(ident)
for r in results:
self.define(r, exc)
logger.exception(f"Error generating {ident}")
self.q.send(messages.RemoteFunctionFailed(ident, e, extract_tb(e.__traceback__)))
self.first_uncompleted_ident = ident + 1
def FetchValue(self, ident: int, mutates: Tuple[Ref], rfunction: Optional[RemoteFunction], obj: Any, streamref: Ref):
with self.try_define(ident, mutates):
stream: Stream = self.lookup(streamref)
obj = tree_map(self.lookup, obj)
with stream.enable():
if rfunction is not None:
function = rfunction.resolve()
obj = function(obj)
self.q.send(messages.FetchResult(ident, obj))
def RequestStatus(self, ident: int):
self.first_uncompleted_ident = ident + 1
self._send_status()
def Exit(self):
raise StopIteration()
def CommandGroup(self, commands: List[NamedTuple]):
for cmd in commands:
self.handle_message(cmd)
def DeleteRefs(self, refs: List[int]):
for id in refs:
del self.env[id]
def BorrowCreate(self, result: Ref, tensorref: Ref, from_streamref, to_streamref, already_borrowed: bool):
try:
from_stream = self.lookup(from_streamref)
to_stream = self.lookup(to_streamref)
tensor = self.lookup(tensorref)
self.define(result, tensor)
if not already_borrowed:
self.borrows[result.id] = Borrow(from_stream, to_stream)
except DependentOnError as e:
self.define(result, e)
if not already_borrowed:
self.borrows[result.id] = None
def BorrowFirstUse(self, borrow: int):
b = self.borrows[borrow]
# can be none if the originator of the borrow errored.
if b is not None:
b.use()
def BorrowDrop(self, borrow: int):
b = self.borrows.pop(borrow)
if b is not None:
b.drop()
def _reduce(self, local_tensor: torch.Tensor, source_mesh: DeviceMesh, dim: str, reduction: str, scatter: bool, inplace: bool):
group = source_mesh.dims[dim].process_group
if reduction == 'stack':
if scatter:
output = local_tensor
if not inplace:
output = local_tensor.clone()
torch.distributed.all_to_all_single(output, local_tensor, group=group)
return output
assert not inplace
output = torch.empty([source_mesh.dims[dim].size, *local_tensor.shape],
dtype=local_tensor.dtype, device=local_tensor.device, layout=local_tensor.layout)
torch.distributed.all_gather_into_tensor(output, local_tensor, group=group)
return output
op = getattr(torch.distributed.ReduceOp, reduction.upper())
if scatter:
assert not inplace
output = torch.empty(local_tensor.shape[1:], dtype=local_tensor.dtype,
device=local_tensor.device, layout=local_tensor.layout)
torch.distributed.reduce_scatter_tensor(output, local_tensor, op=op, group=group)
return output
output = local_tensor
if not inplace:
output = local_tensor.clone()
torch.distributed.all_reduce(output, op=op, group=group)
return output
def Reduce(self, result: Ref, local_tensor_ref: Ref, factory: TensorFactory, source_mesh_ref: Ref, stream_ref: Ref, dim: str, reduction: str, scatter: bool, inplace: bool):
source_mesh = self.lookup(source_mesh_ref)
stream = self.lookup(stream_ref)
with stream.enable():
try:
local_tensor = self.lookup(local_tensor_ref)
except DependentOnError:
# even if we were broken before, we have to participate in the collective
# because we cannot signal to other ranks that we were broken
# the controller will see the error message we sent before and know
# the downstream values are broken.
local_tensor = factory.zeros()
# XXX - we should be careful about starting the collective with a tensor that doesn't match the expected
# factory size. It can error. however, before we can do something about it we need to assign a failure
# identity to this reduce object.
output = self._reduce(local_tensor, source_mesh, dim, reduction, scatter, inplace)
self.define(result, output)
def SendTensor(self, result: Ref, from_ranks: NDSlice, to_ranks: NDSlice, tensorref: Ref, factory: TensorFactory, streamref):
try:
stream = self.lookup(streamref)
except DependentOnError as e:
self.define(result, e)
return
with stream.enable():
ops = []
P2POp = torch.distributed.P2POp
isend, irecv = torch.distributed.isend, torch.distributed.irecv
try:
index = from_ranks.index(self.rank)
try:
tensor = self.lookup(tensorref)
except DependentOnError:
# XXX - how do we propagate this error on the host correctly?
# the host will see on status, but it will not immediately know
# what dependended on this downstream that also has to be invalid now.
tensor = factory.zeros()
to_rank = to_ranks[index]
ops.append(P2POp(isend, tensor, to_rank))
except ValueError:
to_rank = None
tensor = None # silence warnings
try:
index = to_ranks.index(self.rank)
from_rank = from_ranks[index]
if from_rank == to_rank:
assert tensor is not None
self.define(result, tensor)
recv = factory.empty()
ops.append(P2POp(irecv, recv, from_rank))
self.define(result, recv)
except ValueError:
pass
# invoke batched p2p ops
for op in torch.distributed.batch_isend_irecv(ops):
op.wait()
def define(self, r: Ref, value: Any):
assert isinstance(r, Ref)
self.env[r.id] = value
def _send_status(self):
if self.first_uncompleted_ident > self.last_send_status:
self.q.send(messages.Status(self.first_uncompleted_ident))
self.last_send_status = self.first_uncompleted_ident
def event_loop(self):
STATUS_INTERVAL = 1.0
status_ttl = TTL(STATUS_INTERVAL)
while True:
try:
_, msg = self.q.recv(timeout=status_ttl())
logger.info(f"event: {msg}")
self.handle_message(msg)
except TimeoutError:
pass
except StopIteration:
self.q.recvready(0)
self.q.recvready(.01)
return
if status_ttl() == 0:
status_ttl = TTL(STATUS_INTERVAL)
self._send_status()
def worker_main(_restartable):
rank = int(os.environ['RANK'])
world = int(os.environ['WORLD_SIZE'])
local_rank = int(os.environ['LOCAL_RANK'])
initialize_logging(process_name=f'worker_{rank}')
logger.info("starting, restartable=%s", _restartable)
store = torch.distributed.TCPStore(os.environ['STORE_HOSTNAME'], int(os.environ['STORE_PORT']))
torch.distributed.init_process_group(backend='nccl', world_size=world, rank=rank, store=store)
b = torch.zeros(1, device='cuda')
torch.distributed.all_reduce(b)
q = get_message_queue()
# CUDA_VISIBLE_DEVICES should be set on launch to LOCAL_RANK
while True:
worker = Worker(q, store, rank, world, local_rank)
worker.event_loop()
if not _restartable:
break
q.send(messages.Restarted(0))
logger.info("restarting")