ray.remote#
- ray.remote(__function: Callable[[], R]) RemoteFunctionNoArgs[R][源代码]#
- ray.remote(__function: Callable[[T0], R]) RemoteFunction0[R, T0]
- ray.remote(__function: Callable[[T0, T1], R]) RemoteFunction1[R, T0, T1]
- ray.remote(__function: Callable[[T0, T1, T2], R]) RemoteFunction2[R, T0, T1, T2]
- ray.remote(__function: Callable[[T0, T1, T2, T3], R]) RemoteFunction3[R, T0, T1, T2, T3]
- ray.remote(__function: Callable[[T0, T1, T2, T3, T4], R]) RemoteFunction4[R, T0, T1, T2, T3, T4]
- ray.remote(__function: Callable[[T0, T1, T2, T3, T4, T5], R]) RemoteFunction5[R, T0, T1, T2, T3, T4, T5]
- ray.remote(__function: Callable[[T0, T1, T2, T3, T4, T5, T6], R]) RemoteFunction6[R, T0, T1, T2, T3, T4, T5, T6]
- ray.remote(__function: Callable[[T0, T1, T2, T3, T4, T5, T6, T7], R]) RemoteFunction7[R, T0, T1, T2, T3, T4, T5, T6, T7]
- ray.remote(__function: Callable[[T0, T1, T2, T3, T4, T5, T6, T7, T8], R]) RemoteFunction8[R, T0, T1, T2, T3, T4, T5, T6, T7, T8]
- ray.remote(__function: Callable[[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9], R]) RemoteFunction9[R, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9]
- ray.remote(__t: type) Any
- ray.remote(*, num_returns: int | Literal['streaming'] = Undefined, num_cpus: int | float = Undefined, num_gpus: int | float = Undefined, resources: Dict[str, float] = Undefined, accelerator_type: str = Undefined, memory: int | float = Undefined, max_calls: int = Undefined, max_restarts: int = Undefined, max_task_retries: int = Undefined, max_retries: int = Undefined, runtime_env: Dict[str, Any] = Undefined, retry_exceptions: bool = Undefined, scheduling_strategy: None | Literal['DEFAULT'] | Literal['SPREAD'] | PlacementGroupSchedulingStrategy = Undefined) RemoteDecorator
定义一个远程函数或一个角色类。
此函数可以用作无参数的装饰器,以如下方式定义远程函数或角色:
import ray @ray.remote def f(a, b, c): return a + b + c object_ref = f.remote(1, 2, 3) result = ray.get(object_ref) assert result == (1 + 2 + 3) @ray.remote class Foo: def __init__(self, arg): self.x = arg def method(self, a): return self.x + a actor_handle = Foo.remote(123) object_ref = actor_handle.method.remote(321) result = ray.get(object_ref) assert result == (123 + 321)
同样地,使用函数调用来创建远程函数或角色。
def g(a, b, c): return a + b + c remote_g = ray.remote(g) object_ref = remote_g.remote(1, 2, 3) assert ray.get(object_ref) == (1 + 2 + 3) class Bar: def __init__(self, arg): self.x = arg def method(self, a): return self.x + a RemoteBar = ray.remote(Bar) actor_handle = RemoteBar.remote(123) object_ref = actor_handle.method.remote(321) result = ray.get(object_ref) assert result == (123 + 321)
它也可以与特定的关键字参数一起使用,如下所示:
@ray.remote(num_gpus=1, max_calls=1, num_returns=2) def f(): return 1, 2 @ray.remote(num_cpus=2, resources={"CustomResource": 1}) class Foo: def method(self): return 1
通过 @ray.remote 返回的远程任务和参与者对象也可以使用
.options()以与上述相同的参数进行动态修改,如下所示:@ray.remote(num_gpus=1, max_calls=1, num_returns=2) def f(): return 1, 2 f_with_2_gpus = f.options(num_gpus=2) object_refs = f_with_2_gpus.remote() assert ray.get(object_refs) == [1, 2] @ray.remote(num_cpus=2, resources={"CustomResource": 1}) class Foo: def method(self): return 1 Foo_with_no_resources = Foo.options(num_cpus=1, resources=None) foo_actor = Foo_with_no_resources.remote() assert ray.get(foo_actor.method.remote()) == 1
当Python中所有指向该远程参与者的句柄都被删除时,该远程参与者将被终止,这将导致它们完成任何未完成的工作,然后关闭。如果你只有一个对参与者句柄的引用,调用
del actor可能 会触发参与者删除。请注意,你的程序可能对同一个ActorHandle有多个引用,参与者终止不会发生,直到引用计数变为0。更多关于对象删除的上下文,请参阅Python文档。https://docs.python.org/3.9/reference/datamodel.html#object.__del__如果你想立即终止演员,你也可以调用
ray.kill(actor)。- 参数:
num_returns – 这仅适用于 远程函数 。它指定了远程函数调用返回的对象引用数量。默认值为 1。传递“dynamic”以允许任务在执行期间决定返回多少个返回值,调用者将收到一个 ObjectRef[DynamicObjectRefGenerator]。更多详情请参见 动态生成器 。
num_cpus – 为该任务或演员生命周期预留的CPU资源数量。默认情况下,任务使用1个CPU资源,演员用于调度的CPU为1,用于运行的CPU为0(这意味着,默认情况下,演员不能在零CPU节点上调度,但可以在任何非零CPU节点上运行无限数量的演员。演员的默认值是出于历史原因选择的。建议始终为演员显式设置num_cpus,以避免任何意外。如果资源被显式指定,它们对于调度和运行都是必需的。)更多详情请参见 指定资源需求。
num_gpus – 为此任务或演员的生命周期保留的GPU资源数量。默认值为0。更多详情请参见 Ray GPU 支持。
resources (Dict[str, float]) – 为该任务或执行者生命周期预留的各种 自定义资源 的数量。这是一个将字符串(资源名称)映射到浮点数的字典。默认情况下它是空的。
accelerator_type – 如果指定,则要求任务或角色在具有指定类型加速器的节点上运行。请参阅 加速器类型。
memory – 此任务/角色的堆内存请求(以字节为单位),向下舍入到最接近的整数。
max_calls – 仅适用于 远程函数 。这指定了给定工作程序在必须退出之前可以执行给定远程函数的最大次数(这可以用于解决第三方库中的 内存泄漏 问题,或者回收难以释放的资源,例如由 TensorFlow 获取的 GPU 内存)。默认情况下,对于 CPU 任务这是无限的,对于 GPU 任务这是 1(以强制 GPU 任务在完成后释放资源)。
max_restarts – 仅适用于 actors 。这指定了当 actor 意外死亡时应该重新启动的最大次数。最小有效值是 0(默认),表示 actor 不需要重新启动。值为 -1 表示 actor 应无限期地重新启动。更多详情请参见 actor 容错 。
max_task_retries – Only for actors. How many times to retry an actor task if the task fails due to a system error, e.g., the actor has died. If set to -1, the system will retry the failed task until the task succeeds, or the actor has reached its max_restarts limit. If set to
n > 0, the system will retry the failed task up to n times, after which the task will throw aRayActorErrorexception uponray.get. Note that Python exceptions are not considered system errors and will not trigger retries. The default value is 0. See actor fault tolerance for more details.max_retries – 仅适用于 远程函数 。这指定了当执行远程函数的 worker 进程意外崩溃时,远程函数应重新运行的最大次数。最小有效值为 0,默认值为 3,值为 -1 表示无限重试。更多详情请参见 任务容错 。
runtime_env (Dict[str, Any]) – 指定此角色或任务及其子任务的运行时环境。有关详细文档,请参阅 运行时环境。
retry_exceptions – 仅适用于 远程函数。这指定了应用程序级别的错误是否应重试最多 max_retries 次。这可以是一个布尔值或应重试的异常列表。更多详情请参见 任务容错。
scheduling_strategy – 关于如何调度远程函数或角色的策略。可能的值有 None:ray 将确定要使用的调度策略,如果父级有放置组并且设置了 placement_group_capture_child_tasks 为 true,则将使用 PlacementGroupSchedulingStrategy,否则为 “DEFAULT”;”DEFAULT”:默认混合调度;”SPREAD”:尽力分散调度;
PlacementGroupSchedulingStrategy:基于放置组的调度;NodeAffinitySchedulingStrategy:基于节点 ID 的亲和调度。更多详情请参见 Ray 调度策略。_metadata – Ray 库的扩展选项。例如,_metadata={“workflows.io/options”: <workflow options>} 用于 Ray 工作流。