【Ray】ray.remote和option

https://docs.ray.io/en/latest/ray-core/package-ref.html?highlight=ray.remote#ray-remote

1 ray.remote

定义义远程函数或 actor 类。
remote 支持重启、分配资源等功能。

用法1:装饰器的方式

用作装饰器,来修饰函数或者类。
比如:

>>> 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)

用法2:作为函数使用

使用函数调用来创建远程函数或actor。

>>> 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)

2 option

用来改变动态修改remote定义的参数。配置并覆盖任务调用参数。 参数与可以传递给 ray.remote 的参数相同。不支持覆盖 max_calls。

>>> @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_ref = f_with_2_gpus.remote() 
>>> assert ray.get(object_ref) == (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

3 remote参数

  1. num_returns – This is only for remote functions. It specifies the
    number of object refs returned by the remote function invocation.
    Pass “dynamic” to allow the task to decide how many return values to
    return during execution, and the caller will receive an
    ObjectRef[ObjectRefGenerator] (note, this setting is experimental).

    num_cpus – The quantity of CPU cores to reserve for this task or for
    the lifetime of the actor.

    num_gpus – The quantity of GPUs to reserve for this task or for the
    lifetime of the actor.

    resources (Dict[str, float]) – The quantity of various custom
    resources to reserve for this task or for the lifetime of the actor.
    This is a dictionary mapping strings (resource names) to floats.

    accelerator_type – If specified, requires that the task or actor run
    on a node with the specified type of accelerator. See
    ray.accelerators for accelerator types.

    memory – The heap memory request for this task/actor.

    max_calls – Only for remote functions. This specifies the maximum
    number of times that a given worker can execute the given remote
    function before it must exit (this can be used to address memory
    leaks in third-party libraries or to reclaim resources that cannot
    easily be released, e.g., GPU memory that was acquired by
    TensorFlow). By default this is infinite.

    max_restarts – Only for actors. This specifies the maximum number of
    times that the actor should be restarted when it dies unexpectedly.
    The minimum valid value is 0 (default), which indicates that the
    actor doesn’t need to be restarted. A value of -1 indicates that an
    actor should be restarted indefinitely.

    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 a RayActorError exception upon
    ray.get. Note that Python exceptions are not considered system
    errors and will not trigger retries.

    max_retries – Only for remote functions. This specifies the maximum
    number of times that the remote function should be rerun when the
    worker process executing it crashes unexpectedly. The minimum valid
    value is 0, the default is 4 (default), and a value of -1 indicates
    infinite retries.

    runtime_env (Dict[str, Any]) – Specifies the runtime environment for
    this actor or task and its children. See Runtime environments for
    detailed documentation. This API is in beta and may change before
    becoming stable.

    retry_exceptions – Only for remote functions. This specifies whether
    application-level errors should be retried up to max_retries times.
    This can be a boolean or a list of exceptions that should be
    retried.

    scheduling_strategy – Strategy about how to schedule a remote
    function or actor. Possible values are None: ray will figure out the
    scheduling strategy to use, it will either be the
    PlacementGroupSchedulingStrategy using parent’s placement group if
    parent has one and has placement_group_capture_child_tasks set to
    true, or “DEFAULT”; “DEFAULT”: default hybrid scheduling; “SPREAD”:
    best effort spread scheduling; PlacementGroupSchedulingStrategy:
    placement group based scheduling.

    _metadata – Extended options for Ray libraries. For example, _metadata={“workflows.io/options”: } for Ray workflows.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值