mirror of
https://github.com/wassname/ray.git
synced 2026-06-28 12:10:40 +08:00
Set _remote() function args and kwargs as optional (#4305)
This commit is contained in:
committed by
Robert Nishihara
parent
ba3fe04629
commit
5adb4a6941
+12
-7
@@ -125,7 +125,11 @@ class ActorMethod(object):
|
||||
def remote(self, *args, **kwargs):
|
||||
return self._remote(args, kwargs)
|
||||
|
||||
def _remote(self, args, kwargs, num_return_vals=None):
|
||||
def _remote(self, args=None, kwargs=None, num_return_vals=None):
|
||||
if args is None:
|
||||
args = []
|
||||
if kwargs is None:
|
||||
kwargs = {}
|
||||
if num_return_vals is None:
|
||||
num_return_vals = self._num_return_vals
|
||||
|
||||
@@ -233,8 +237,8 @@ class ActorClass(object):
|
||||
return self._remote(args=args, kwargs=kwargs)
|
||||
|
||||
def _remote(self,
|
||||
args,
|
||||
kwargs,
|
||||
args=None,
|
||||
kwargs=None,
|
||||
num_cpus=None,
|
||||
num_gpus=None,
|
||||
resources=None):
|
||||
@@ -255,6 +259,11 @@ class ActorClass(object):
|
||||
Returns:
|
||||
A handle to the newly created actor.
|
||||
"""
|
||||
if args is None:
|
||||
args = []
|
||||
if kwargs is None:
|
||||
kwargs = {}
|
||||
|
||||
worker = ray.worker.get_global_worker()
|
||||
if worker.mode is None:
|
||||
raise Exception("Actors cannot be created before ray.init() "
|
||||
@@ -293,10 +302,6 @@ class ActorClass(object):
|
||||
actor_placement_resources = resources.copy()
|
||||
actor_placement_resources["CPU"] += 1
|
||||
|
||||
if args is None:
|
||||
args = []
|
||||
if kwargs is None:
|
||||
kwargs = {}
|
||||
function_name = "__init__"
|
||||
function_signature = self._method_signatures[function_name]
|
||||
creation_args = signature.extend_args(function_signature, args,
|
||||
|
||||
@@ -107,6 +107,7 @@ class RemoteFunction(object):
|
||||
worker.function_actor_manager.export(self)
|
||||
|
||||
kwargs = {} if kwargs is None else kwargs
|
||||
args = [] if args is None else args
|
||||
args = ray.signature.extend_args(self._function_signature, args,
|
||||
kwargs)
|
||||
|
||||
|
||||
@@ -827,51 +827,63 @@ def test_defining_remote_functions(shutdown_only):
|
||||
assert ray.get(k2.remote(1)) == 2
|
||||
assert ray.get(m.remote(1)) == 2
|
||||
|
||||
def test_submit_api(shutdown_only):
|
||||
ray.init(num_cpus=1, num_gpus=1, resources={"Custom": 1})
|
||||
|
||||
@ray.remote
|
||||
def f(n):
|
||||
return list(range(n))
|
||||
def test_submit_api(shutdown_only):
|
||||
ray.init(num_cpus=1, num_gpus=1, resources={"Custom": 1})
|
||||
|
||||
@ray.remote
|
||||
def g():
|
||||
@ray.remote
|
||||
def f(n):
|
||||
return list(range(n))
|
||||
|
||||
@ray.remote
|
||||
def g():
|
||||
return ray.get_gpu_ids()
|
||||
|
||||
assert f._remote([0], num_return_vals=0) is None
|
||||
id1 = f._remote(args=[1], num_return_vals=1)
|
||||
assert ray.get(id1) == [0]
|
||||
id1, id2 = f._remote(args=[2], num_return_vals=2)
|
||||
assert ray.get([id1, id2]) == [0, 1]
|
||||
id1, id2, id3 = f._remote(args=[3], num_return_vals=3)
|
||||
assert ray.get([id1, id2, id3]) == [0, 1, 2]
|
||||
assert ray.get(
|
||||
g._remote(args=[], num_cpus=1, num_gpus=1,
|
||||
resources={"Custom": 1})) == [0]
|
||||
infeasible_id = g._remote(args=[], resources={"NonexistentCustom": 1})
|
||||
assert ray.get(g._remote()) == []
|
||||
ready_ids, remaining_ids = ray.wait([infeasible_id], timeout=0.05)
|
||||
assert len(ready_ids) == 0
|
||||
assert len(remaining_ids) == 1
|
||||
|
||||
@ray.remote
|
||||
class Actor(object):
|
||||
def __init__(self, x, y=0):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
def method(self, a, b=0):
|
||||
return self.x, self.y, a, b
|
||||
|
||||
def gpu_ids(self):
|
||||
return ray.get_gpu_ids()
|
||||
|
||||
assert f._remote([0], num_return_vals=0) is None
|
||||
id1 = f._remote(args=[1], num_return_vals=1)
|
||||
assert ray.get(id1) == [0]
|
||||
id1, id2 = f._remote(args=[2], num_return_vals=2)
|
||||
assert ray.get([id1, id2]) == [0, 1]
|
||||
id1, id2, id3 = f._remote(args=[3], num_return_vals=3)
|
||||
assert ray.get([id1, id2, id3]) == [0, 1, 2]
|
||||
assert ray.get(
|
||||
g._remote(
|
||||
args=[], num_cpus=1, num_gpus=1,
|
||||
resources={"Custom": 1})) == [0]
|
||||
infeasible_id = g._remote(args=[], resources={"NonexistentCustom": 1})
|
||||
ready_ids, remaining_ids = ray.wait([infeasible_id], timeout=0.05)
|
||||
assert len(ready_ids) == 0
|
||||
assert len(remaining_ids) == 1
|
||||
@ray.remote
|
||||
class Actor2(object):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@ray.remote
|
||||
class Actor(object):
|
||||
def __init__(self, x, y=0):
|
||||
self.x = x
|
||||
self.y = y
|
||||
def method(self):
|
||||
pass
|
||||
|
||||
def method(self, a, b=0):
|
||||
return self.x, self.y, a, b
|
||||
a = Actor._remote(
|
||||
args=[0], kwargs={"y": 1}, num_gpus=1, resources={"Custom": 1})
|
||||
|
||||
def gpu_ids(self):
|
||||
return ray.get_gpu_ids()
|
||||
a2 = Actor2._remote()
|
||||
ray.get(a2.method._remote())
|
||||
|
||||
a = Actor._remote(
|
||||
args=[0], kwargs={"y": 1}, num_gpus=1, resources={"Custom": 1})
|
||||
|
||||
id1, id2, id3, id4 = a.method._remote(
|
||||
args=["test"], kwargs={"b": 2}, num_return_vals=4)
|
||||
assert ray.get([id1, id2, id3, id4]) == [0, 1, "test", 2]
|
||||
id1, id2, id3, id4 = a.method._remote(
|
||||
args=["test"], kwargs={"b": 2}, num_return_vals=4)
|
||||
assert ray.get([id1, id2, id3, id4]) == [0, 1, "test", 2]
|
||||
|
||||
|
||||
def test_get_multiple(shutdown_only):
|
||||
|
||||
Reference in New Issue
Block a user