Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support running RayFed job in Ray client mode. #173

Merged
merged 25 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 47 additions & 19 deletions fed/_private/fed_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import logging

import ray
from ray.util.client.common import ClientActorHandle
from fed._private.fed_call_holder import FedCallHolder
from fed.fed_object import FedObject

Expand All @@ -37,22 +38,41 @@ def __init__(
self._party = party
self._node_party = node_party
self._options = options
self._actor_handle = None
self._ray_actor_handle = None

def __getattr__(self, method_name: str):
# User trying to call .bind() without a bind class method
if method_name == "remote" and "remote" not in dir(self._body):
raise AttributeError(f".remote() cannot be used again on {type(self)} ")
# Raise an error if the method is invalid.
getattr(self._body, method_name)
call_node = FedActorMethod(
self._addresses,
self._party,
self._node_party,
self,
method_name,
).options(**self._options)
return call_node

if self._party == self._node_party:
ray_actor_handle = self._ray_actor_handle
try:
ray_wrappered_method = ray_actor_handle.__getattribute__(method_name)
except AttributeError:
# The code path in Ray client mode.
assert isinstance(ray_actor_handle, ClientActorHandle)
ray_wrappered_method = ray_actor_handle.__getattr__(method_name)

return FedActorMethod(
self._addresses,
self._party,
self._node_party,
self,
method_name,
ray_wrappered_method,
).options(**self._options)
else:
return FedActorMethod(
self._addresses,
self._party,
self._node_party,
self,
method_name,
None,
).options(**self._options)

def _execute_impl(self, cls_args, cls_kwargs):
"""Executor of ClassNode by ray.remote()
Expand All @@ -63,28 +83,34 @@ def _execute_impl(self, cls_args, cls_kwargs):
current node is executed.
"""
if self._node_party == self._party:
self._actor_handle = (
self._ray_actor_handle = (
ray.remote(self._body)
.options(**self._options)
.remote(*cls_args, **cls_kwargs)
)

def _execute_remote_method(self, method_name, options, args, kwargs):
def _execute_remote_method(
self,
method_name,
options,
_ray_wrappered_method,
args,
kwargs,
):
num_returns = 1
if options and 'num_returns' in options:
num_returns = options['num_returns']
logger.debug(
f"Actor method call: {method_name}, num_returns: {num_returns}"
)
ray_object_ref = self._actor_handle._actor_method_call(
method_name,
args=args,
kwargs=kwargs,
name="",

return _ray_wrappered_method.options(
name='',
num_returns=num_returns,
concurrency_group_name="",
).remote(
*args,
**kwargs,
)
return ray_object_ref


class FedActorMethod:
Expand All @@ -95,13 +121,15 @@ def __init__(
node_party,
fed_actor_handle,
method_name,
ray_wrappered_method,
) -> None:
self._addresses = addresses
self._party = party # Current party
self._node_party = node_party
self._fed_actor_handle = fed_actor_handle
self._method_name = method_name
self._options = {}
self._ray_wrappered_method = ray_wrappered_method
self._fed_call_holder = FedCallHolder(node_party, self._execute_impl)

def remote(self, *args, **kwargs) -> FedObject:
Expand All @@ -114,5 +142,5 @@ def options(self, **options):

def _execute_impl(self, args, kwargs):
return self._fed_actor_handle._execute_remote_method(
self._method_name, self._options, args, kwargs
self._method_name, self._options, self._ray_wrappered_method, args, kwargs
)
1 change: 1 addition & 0 deletions fed/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ class FedRemoteClass:
def __init__(self, func_or_class) -> None:
self._party = None
self._cls = func_or_class
# self._client_actor_handle_class = ray.
jovany-wang marked this conversation as resolved.
Show resolved Hide resolved
self._options = {}

def party(self, party: str):
Expand Down
11 changes: 9 additions & 2 deletions tests/test_fed_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,17 @@ def mean(x, y):


def run(party):
import time
if party == 'alice':
time.sleep(1.4)

# address = 'ray://127.0.0.1:21012' if party == 'alice' else 'ray://127.0.0.1:21011' # noqa
# compatible_utils.init_ray(address=address)
compatible_utils.init_ray(address='local')

addresses = {
'alice': '127.0.0.1:11012',
'bob': '127.0.0.1:11011',
'alice': '127.0.0.1:31012',
'bob': '127.0.0.1:31011',
}
fed.init(addresses=addresses, party=party)

Expand Down
Loading