|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
| 15 | +import functools |
| 16 | +import inspect |
| 17 | +from typing import Union |
| 18 | + |
| 19 | +from .....core.context import Context |
| 20 | +from .....utils import implements, lazy_import |
| 21 | +from ....context import ThreadedServiceContext |
| 22 | + |
| 23 | +ray = lazy_import("ray") |
| 24 | + |
| 25 | + |
| 26 | +class RayRemoteObjectManager: |
| 27 | + """The remote object manager in task state actor.""" |
| 28 | + |
| 29 | + def __init__(self): |
| 30 | + self._named_remote_objects = {} |
| 31 | + |
| 32 | + def create_remote_object(self, name: str, object_cls, *args, **kwargs): |
| 33 | + remote_object = object_cls(*args, **kwargs) |
| 34 | + self._named_remote_objects[name] = remote_object |
| 35 | + |
| 36 | + def destroy_remote_object(self, name: str): |
| 37 | + self._named_remote_objects.pop(name, None) |
| 38 | + |
| 39 | + async def call_remote_object(self, name: str, attr: str, *args, **kwargs): |
| 40 | + remote_object = self._named_remote_objects[name] |
| 41 | + meth = getattr(remote_object, attr) |
| 42 | + async_meth = self._sync_to_async(meth) |
| 43 | + return await async_meth(*args, **kwargs) |
| 44 | + |
| 45 | + @staticmethod |
| 46 | + @functools.lru_cache(100) |
| 47 | + def _sync_to_async(func): |
| 48 | + if inspect.iscoroutinefunction(func): |
| 49 | + return func |
| 50 | + else: |
| 51 | + |
| 52 | + async def async_wrapper(*args, **kwargs): |
| 53 | + return func(*args, **kwargs) |
| 54 | + |
| 55 | + return async_wrapper |
| 56 | + |
| 57 | + |
| 58 | +class _RayRemoteObjectWrapper: |
| 59 | + def __init__(self, task_state_actor: "ray.actor.ActorHandle", name: str): |
| 60 | + self._task_state_actor = task_state_actor |
| 61 | + self._name = name |
| 62 | + |
| 63 | + def __getattr__(self, attr): |
| 64 | + def wrap(*args, **kwargs): |
| 65 | + r = self._task_state_actor.call_remote_object.remote( |
| 66 | + self._name, attr, *args, **kwargs |
| 67 | + ) |
| 68 | + return ray.get(r) |
| 69 | + |
| 70 | + return wrap |
| 71 | + |
| 72 | + |
| 73 | +class _RayRemoteObjectContext: |
| 74 | + def __init__( |
| 75 | + self, actor_name_or_handle: Union[str, "ray.actor.ActorHandle"], *args, **kwargs |
| 76 | + ): |
| 77 | + super().__init__(*args, **kwargs) |
| 78 | + self._actor_name_or_handle = actor_name_or_handle |
| 79 | + self._task_state_actor = None |
| 80 | + |
| 81 | + def _get_task_state_actor(self) -> "ray.actor.ActorHandle": |
| 82 | + if self._task_state_actor is None: |
| 83 | + if isinstance(self._actor_name_or_handle, ray.actor.ActorHandle): |
| 84 | + self._task_state_actor = self._actor_name_or_handle |
| 85 | + else: |
| 86 | + self._task_state_actor = ray.get_actor(self._actor_name_or_handle) |
| 87 | + return self._task_state_actor |
| 88 | + |
| 89 | + @implements(Context.create_remote_object) |
| 90 | + def create_remote_object(self, name: str, object_cls, *args, **kwargs): |
| 91 | + task_state_actor = self._get_task_state_actor() |
| 92 | + task_state_actor.create_remote_object.remote(name, object_cls, *args, **kwargs) |
| 93 | + return _RayRemoteObjectWrapper(task_state_actor, name) |
| 94 | + |
| 95 | + @implements(Context.get_remote_object) |
| 96 | + def get_remote_object(self, name: str): |
| 97 | + task_state_actor = self._get_task_state_actor() |
| 98 | + return _RayRemoteObjectWrapper(task_state_actor, name) |
| 99 | + |
| 100 | + @implements(Context.destroy_remote_object) |
| 101 | + def destroy_remote_object(self, name: str): |
| 102 | + task_state_actor = self._get_task_state_actor() |
| 103 | + task_state_actor.destroy_remote_object.remote(name) |
| 104 | + |
| 105 | + |
| 106 | +# TODO(fyrestone): Implement more APIs for Ray. |
| 107 | +class RayExecutionContext(_RayRemoteObjectContext, ThreadedServiceContext): |
| 108 | + """The context for tiling.""" |
| 109 | + |
| 110 | + pass |
| 111 | + |
| 112 | + |
| 113 | +# TODO(fyrestone): Implement more APIs for Ray. |
| 114 | +class RayExecutionWorkerContext(_RayRemoteObjectContext, dict): |
| 115 | + """The context for executing operands.""" |
15 | 116 |
|
16 |
| -# TODO(fyrestone): Should implement the mars.core.context.Context. |
17 |
| -class RayExecutionContext(dict): |
18 | 117 | @staticmethod
|
19 | 118 | def new_custom_log_dir():
|
20 | 119 | return None
|
0 commit comments