-
Notifications
You must be signed in to change notification settings - Fork 21
/
api.py
623 lines (524 loc) · 21.5 KB
/
api.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# Copyright 2023 The RayFed Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License 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.
import functools
import inspect
import logging
import signal
import sys
from typing import Any, Callable, Dict, List, Union
import cloudpickle
import ray
import fed._private.compatible_utils as compatible_utils
import fed.config as fed_config
import fed.utils as fed_utils
from fed._private import constants
from fed._private.fed_actor import FedActorHandle
from fed._private.fed_call_holder import FedCallHolder
from fed._private.global_context import (
clear_global_context,
get_global_context,
init_global_context,
)
from fed.config import CrossSiloMessageConfig
from fed.exceptions import FedRemoteError
from fed.fed_object import FedObject
from fed.proxy.barriers import (
_start_receiver_proxy,
_start_sender_proxy,
_start_sender_receiver_proxy,
ping_others,
recv,
send,
set_proxy_actor_name,
)
from fed.proxy.base_proxy import ReceiverProxy, SenderProxy, SenderReceiverProxy
from fed.utils import is_ray_object_refs, setup_logger
logger = logging.getLogger(__name__)
original_sigint = signal.getsignal(signal.SIGINT)
def _signal_handler(signum, frame):
if signum == signal.SIGINT:
signal.signal(signal.SIGINT, original_sigint)
logger.warning(
"Stop signal received (e.g. via SIGINT/Ctrl+C), "
"try to shutdown fed. Press CTRL+C "
"(or send SIGINT/SIGKILL/SIGTERM) to skip."
)
_shutdown(intended=False)
def init(
addresses: Dict = None,
party: str = None,
config: Dict = {},
tls_config: Dict = None,
logging_level: str = "info",
sender_proxy_cls: SenderProxy = None,
receiver_proxy_cls: ReceiverProxy = None,
receiver_sender_proxy_cls: SenderReceiverProxy = None,
job_name: str = None,
sending_failure_handler: Callable[[Exception], None] = None,
):
"""
Initialize a RayFed client.
Args:
addresses:
optional; a dict describes the addresses configurations. E.g.
.. code:: python
{
# The address that can be connected to `alice` by other parties.
'alice': '127.0.0.1:10001',
# The address that can be connected to `bob` by other parties.
'bob': '127.0.0.1:10002',
# The address that can be connected to `carol` by other parties.
'carol': '127.0.0.1:10003',
}
party:
optional; self party.
config:
optional; a dict describes general job configurations. Currently the
supported configurations are ['cross_silo_comm', 'barrier_on_initializing'].
cross_silo_comm
optional; a dict describes the cross-silo common
configs, the supported configs can be referred to
:py:meth:`fed.config.CrossSiloMessageConfig` and
:py:meth:`fed.config.GrpcCrossSiloMessageConfig`. Note that, the
`cross_silo_comm.messages_max_size_in_bytes` will be overrided
if `cross_silo_comm.grpc_channel_options` is provided and contains
`grpc.max_send_message_length` or `grpc.max_receive_message_length`.
barrier_on_initializing
optional; a bool value indicates whether to
wait for all parties to be ready before starting the job. If set
to True, the job will be started after all parties are ready,
otherwise, the job will be started immediately after the current
party is ready.
E.g.
.. code:: python
{
"cross_silo_comm": {
"messages_max_size_in_bytes": 500*1024,
"timeout_in_ms": 1000,
"exit_on_sending_failure": True,
"expose_error_trace": True,
"use_global_proxy": True,
"continue_waiting_for_data_sending_on_error": False,
},
"barrier_on_initializing": True,
}
tls_config:
optional; a dict describes the tls config. E.g.
For alice,
.. code:: python
{
"ca_cert": "root ca cert of other parties.",
"cert": "alice's server cert",
"key": "alice's server cert key",
}
For bob,
.. code:: python
{
"ca_cert": "root ca cert of other parties.",
"cert": "bob's server cert",
"key": "bob's server cert key",
}
logging_level:
optional; the logging level, could be `debug`, `info`, `warning`, `error`,
`critical`, not case sensititive.
job_name:
optional; the job name of the current job. Note that, the job name
must be identical in all parties, otherwise, messages will be ignored
because of the job name mismatch. If the job name is not provided, an
default fixed name will be assigned, therefore messages of all anonymous
jobs will be mixed together, which should only be used in the single job
scenario or test mode.
sending_failure_handler:
optional; a callback which will be triggeed if cross-silo message sending
failed and exit_on_sending_failure in config is True.
Examples:
>>> import fed
>>> import ray
>>> ray.init(address='local')
>>> addresses = {
>>> 'alice': '127.0.0.1:10001',
>>> 'bob': '127.0.0.1:10002',
>>> 'carol': '127.0.0.1:10003',
>>> }
>>> # Start as alice.
>>> fed.init(addresses=addresses, party='alice')
"""
assert addresses, "Addresses should be provided."
assert party, "Party should be provided."
assert party in addresses, f"Party {party} is not in the addresses {addresses}."
if job_name is None:
job_name = constants.RAYFED_DEFAULT_JOB_NAME
fed_utils.validate_addresses(addresses)
cross_silo_comm_dict = config.get("cross_silo_comm", {})
cross_silo_comm_config = CrossSiloMessageConfig.from_dict(cross_silo_comm_dict)
init_global_context(
current_party=party,
job_name=job_name,
exit_on_sending_failure=cross_silo_comm_config.exit_on_sending_failure,
continue_waiting_for_data_sending_on_error=cross_silo_comm_config.continue_waiting_for_data_sending_on_error,
sending_failure_handler=sending_failure_handler,
)
tls_config = {} if tls_config is None else tls_config
if tls_config:
assert (
"cert" in tls_config and "key" in tls_config
), "Cert or key are not in tls_config."
# A Ray private accessing, should be replaced in public API.
compatible_utils._init_internal_kv(job_name)
cluster_config = {
constants.KEY_OF_CLUSTER_ADDRESSES: addresses,
constants.KEY_OF_CURRENT_PARTY_NAME: party,
constants.KEY_OF_TLS_CONFIG: tls_config,
}
compatible_utils.kv.put(
constants.KEY_OF_CLUSTER_CONFIG, cloudpickle.dumps(cluster_config)
)
job_config = {
constants.KEY_OF_CROSS_SILO_COMM_CONFIG_DICT: cross_silo_comm_dict,
}
compatible_utils.kv.put(constants.KEY_OF_JOB_CONFIG, cloudpickle.dumps(job_config))
# Set logger.
# Note(NKcqx): This should be called after internal_kv has party value, i.e.
# after `ray.init` and
# `internal_kv._internal_kv_put(RAYFED_PARTY_KEY, cloudpickle.dumps(party))`
setup_logger(
logging_level=logging_level,
logging_format=constants.RAYFED_LOG_FMT,
date_format=constants.RAYFED_DATE_FMT,
party_val=_get_party(job_name),
job_name=job_name,
)
logger.info(f"Started rayfed with {cluster_config}")
signal.signal(signal.SIGINT, _signal_handler)
get_global_context().get_cleanup_manager().start(
exit_on_sending_failure=cross_silo_comm_config.exit_on_sending_failure,
expose_error_trace=cross_silo_comm_config.expose_error_trace,
)
if receiver_sender_proxy_cls is not None:
set_proxy_actor_name(
job_name, cross_silo_comm_dict.get("use_global_proxy", True), True
)
_start_sender_receiver_proxy(
addresses=addresses,
party=party,
logging_level=logging_level,
tls_config=tls_config,
proxy_cls=receiver_sender_proxy_cls,
proxy_config=cross_silo_comm_dict,
ready_timeout_second=cross_silo_comm_config.timeout_in_ms / 1000,
)
else:
if receiver_proxy_cls is None:
logger.debug(
(
"No receiver proxy class specified, "
"use `GrpcRecvProxy` by default."
)
)
from fed.proxy.grpc.grpc_proxy import GrpcReceiverProxy
receiver_proxy_cls = GrpcReceiverProxy
set_proxy_actor_name(
job_name, cross_silo_comm_dict.get("use_global_proxy", True)
)
_start_receiver_proxy(
addresses=addresses,
party=party,
logging_level=logging_level,
tls_config=tls_config,
proxy_cls=receiver_proxy_cls,
proxy_config=cross_silo_comm_dict,
ready_timeout_second=cross_silo_comm_config.timeout_in_ms / 1000,
)
if sender_proxy_cls is None:
logger.debug(
"No sender proxy class specified, use `GrpcSenderProxy` by default."
)
from fed.proxy.grpc.grpc_proxy import GrpcSenderProxy
sender_proxy_cls = GrpcSenderProxy
_start_sender_proxy(
addresses=addresses,
party=party,
logging_level=logging_level,
tls_config=tls_config,
proxy_cls=sender_proxy_cls,
proxy_config=cross_silo_comm_dict,
ready_timeout_second=cross_silo_comm_config.timeout_in_ms / 1000,
)
if config.get("barrier_on_initializing", False):
# TODO(zhouaihui): can be removed after we have a better retry strategy.
ping_others(addresses=addresses, self_party=party, max_retries=3600)
def shutdown():
"""
Shutdown a RayFed client.
"""
global_context = get_global_context()
if global_context is not None and global_context.acquire_shutdown_flag():
_shutdown(True)
def _shutdown(intended=True):
"""
Shutdown a RayFed client.
Args:
intended: (Optional) Whether this is a intended shutdown. If not
a "failure handler" will be triggered and do not wait data sending.
"""
if get_global_context() is None:
# Do nothing since job has not been inited or is cleaned already.
return
if intended:
logger.info("Shutdowning rayfed intendedly...")
else:
logger.warn("Shutdowning rayfed unintendedly...")
global_context = get_global_context()
last_sending_error = global_context.get_cleanup_manager().get_last_sending_error()
last_received_error = global_context.get_last_recevied_error()
if last_sending_error is not None:
logging.error(f"Cross-silo sending error occured. {last_sending_error}")
wait_for_sending = True
if (
last_sending_error is not None or last_received_error is not None
) and not global_context.get_continue_waiting_for_data_sending_on_error():
wait_for_sending = False
logging.info(f'{"Wait" if wait_for_sending else "No wait"} for data sending.')
if not intended:
# Execute failure_handler fisrtly.
failure_handler = global_context.get_sending_failure_handler()
if failure_handler is not None:
logger.info(f"Executing failure handler {failure_handler} ...")
failure_handler(last_sending_error)
exit_on_sending_failure = global_context.get_exit_on_sending_failure()
# Clean context.
compatible_utils._clear_internal_kv()
clear_global_context(wait_for_sending=wait_for_sending)
logger.info("Shutdowned rayfed.")
if exit_on_sending_failure:
# Exit with error.
logger.critical("Exit now due to the previous error.")
sys.exit(1)
else:
# Clean context.
compatible_utils._clear_internal_kv()
clear_global_context(wait_for_sending=wait_for_sending)
logger.info("Shutdowned rayfed.")
def _get_addresses(job_name: str = None):
"""
Get the RayFed addresses configration.
"""
return fed_config.get_cluster_config(job_name).cluster_addresses
def _get_party(job_name: str = None):
"""
A private util function to get the current party name.
"""
return fed_config.get_cluster_config(job_name).current_party
def _get_tls(job_name: str = None):
"""
Get the tls configurations on this party.
"""
return fed_config.get_cluster_config(job_name).tls_config
class FedRemoteFunction:
def __init__(self, func_or_class) -> None:
self._node_party = None
self._func_body = func_or_class
self._options = {}
self._fed_call_holder = None
def party(self, party: str):
self._node_party = party
# assert self._fed_call_holder is None
# TODO(qwang): This should be refined, to make sure we don't reuse the object
# twice.
self._fed_call_holder = FedCallHolder(
self._node_party, self._execute_impl, self._options
)
return self
def options(self, **options):
self._options = options
if self._fed_call_holder:
self._fed_call_holder.options(**options)
return self
def remote(self, *args, **kwargs):
if not self._node_party:
raise ValueError("You should specify a party name on the fed function.")
return self._fed_call_holder.internal_remote(*args, **kwargs)
def _execute_impl(self, args, kwargs):
return (
ray.remote(self._func_body).options(**self._options).remote(*args, **kwargs)
)
class FedRemoteClass:
def __init__(self, func_or_class) -> None:
self._party = None
self._cls = func_or_class
self._options = {}
def party(self, party: str):
self._party = party
return self
def options(self, **options):
self._options = options
return self
def remote(self, *cls_args, **cls_kwargs):
fed_class_task_id = get_global_context().next_seq_id()
job_name = get_global_context().get_job_name()
fed_actor_handle = FedActorHandle(
fed_class_task_id,
_get_addresses(job_name),
self._cls,
_get_party(job_name),
self._party,
self._options,
)
fed_call_holder = FedCallHolder(
self._party, fed_actor_handle._execute_impl, self._options
)
fed_call_holder.internal_remote(*cls_args, **cls_kwargs)
return fed_actor_handle
# This is the decorator `@fed.remote`
def remote(*args, **kwargs):
"""Defines a remote function or an actor class.
This function can be used as a decorator with no arguments
to define a remote function or actor as follows:
.. testcode::
import fed
@fed.remote
def f(a, b, c):
return a + b + c
object_ref = f.part('alice').remote(1, 2, 3)
result = fed.get(object_ref)
assert result == (1 + 2 + 3)
@fed.remote
class Foo:
def __init__(self, arg):
self.x = arg
def method(self, a):
return self.x + a
actor_handle = Foo.party('alice').remote(123)
object_ref = actor_handle.method.remote(321)
result = fed.get(object_ref)
assert result == (123 + 321)
Equivalently, use a function call to create a remote function or actor.
.. testcode::
def g(a, b, c):
return a + b + c
remote_g = fed.remote(g)
object_ref = remote_g.party('alice').remote(1, 2, 3)
assert fed.get(object_ref) == (1 + 2 + 3)
class Bar:
def __init__(self, arg):
self.x = arg
def method(self, a):
return self.x + a
RemoteBar = fed.remote(Bar)
actor_handle = RemoteBar.party('alice').remote(123)
object_ref = actor_handle.method.remote(321)
result = fed.get(object_ref)
assert result == (123 + 321)
It can also be used with specific keyword arguments just same as ray options.
"""
def _make_fed_remote(function_or_class, **options):
if inspect.isfunction(function_or_class) or fed_utils.is_cython(
function_or_class
):
return FedRemoteFunction(function_or_class).options(**options)
if inspect.isclass(function_or_class):
return FedRemoteClass(function_or_class).options(**options)
raise TypeError(
"The @fed.remote decorator must be applied to either a function or a class."
)
if len(args) == 1 and len(kwargs) == 0 and callable(args[0]):
# This is the case where the decorator is just @fed.remote.
return _make_fed_remote(args[0])
assert len(args) == 0 and len(kwargs) > 0, "Remote args error."
return functools.partial(_make_fed_remote, **kwargs)
def get(
fed_objects: Union[ray.ObjectRef, List[FedObject], FedObject, List[FedObject]]
) -> Any:
"""
Gets the real data of the given fed_object.
If the object is located in current party, return it immediately,
otherwise return it after receiving the real data from the located
party.
"""
if is_ray_object_refs(fed_objects):
return ray.get(fed_objects)
# A fake fed_task_id for a `fed.get()` operator. This is useful
# to help contruct the whole DAG within `fed.get`.
fake_fed_task_id = get_global_context().next_seq_id()
job_name = get_global_context().get_job_name()
addresses = _get_addresses(job_name)
current_party = _get_party(job_name)
is_individual_id = isinstance(fed_objects, FedObject)
if is_individual_id:
fed_objects = [fed_objects]
ray_refs = []
for fed_object in fed_objects:
if fed_object.get_party() == current_party:
# The code path of the fed_object is in current party, so
# need to boardcast the data of the fed_object to other parties,
# and then return the real data of that.
ray_object_ref = fed_object.get_ray_object_ref()
assert ray_object_ref is not None
ray_refs.append(ray_object_ref)
for party_name in addresses:
if party_name == current_party:
continue
else:
if fed_object._was_sending_or_sent_to_party(party_name):
# This object was sending or sent to the target party,
# so no need to do it again.
continue
else:
fed_object._mark_is_sending_to_party(party_name)
send(
dest_party=party_name,
data=ray_object_ref,
upstream_seq_id=fed_object.get_fed_task_id(),
downstream_seq_id=fake_fed_task_id,
)
else:
# This is the code path that the fed_object is not in current party.
# So we should insert a `recv_op` as a barrier to receive the real
# data from the location party of the fed_object.
if fed_object.get_ray_object_ref() is not None:
received_ray_object_ref = fed_object.get_ray_object_ref()
else:
received_ray_object_ref = recv(
current_party,
fed_object.get_party(),
fed_object.get_fed_task_id(),
fake_fed_task_id,
)
fed_object._cache_ray_object_ref(received_ray_object_ref)
ray_refs.append(received_ray_object_ref)
try:
values = ray.get(ray_refs)
if is_individual_id:
values = values[0]
return values
except FedRemoteError as e:
logger.warning(
"Encounter RemoteError happend in other parties"
f", error message: {e.cause}"
)
if get_global_context() is not None:
get_global_context().set_last_recevied_error(e)
raise e
def kill(actor: FedActorHandle, *, no_restart=True):
"""Kill an actor forcefully.
Args:
actor: Handle to the actor to kill.
no_restart: Whether or not this actor should be restarted if
it's a restartable actor.
"""
job_name = get_global_context().get_job_name()
current_party = _get_party(job_name)
if actor._node_party == current_party:
handler = actor._actor_handle
ray.kill(handler, no_restart=no_restart)