【Ray】ray.remote和option
创始人
2024-02-10 04:36:12
0

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.

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...