• 文档 >
  • 分布式 RPC 框架
快捷方式

分布式 RPC 框架

分布式 RPC 框架提供用于多机器模型训练的机制,通过一组原语来实现远程通信,以及更高级别的 API 来自动区分跨多台机器拆分的模型。

警告

RPC 包中的 API 是稳定的。目前正在进行多项工作以提高性能和错误处理,这些工作将在未来的版本中发布。

警告

CUDA 支持在 PyTorch 1.9 中引入,但仍然是一个 beta 功能。RPC 包的并非所有功能都与 CUDA 支持兼容,因此不鼓励使用它们。这些不受支持的功能包括:RRef、JIT 兼容性、dist autograd 和 dist optimizer 以及性能分析。这些缺点将在未来的版本中得到解决。

注意

有关与分布式训练相关的所有功能的简要介绍,请参阅 PyTorch 分布式概述

基础知识

分布式 RPC 框架使远程运行函数变得容易,支持引用远程对象而无需复制真实数据,并提供 autograd 和 optimizer API 以透明地跨 RPC 边界运行反向传播和更新参数。这些功能可以分为四组 API。

  1. 远程过程调用 (RPC) 支持在指定的目标工作进程上使用给定参数运行函数,并取回返回值或创建对返回值的引用。有三个主要的 RPC API:rpc_sync() (同步)、rpc_async() (异步) 和 remote() (异步并返回对远程返回值的引用)。如果用户代码在没有返回值的情况下无法继续,请使用同步 API。否则,请使用异步 API 获取 future,并在调用者需要返回值时等待 future。当需要远程创建某些内容但永远不需要将其获取到调用者时,remote() API 非常有用。想象一下驱动程序进程正在设置参数服务器和训练器的情况。驱动程序可以在参数服务器上创建一个嵌入表,然后与训练器共享对嵌入表的引用,但它自己永远不会在本地使用该嵌入表。在这种情况下,rpc_sync()rpc_async() 不再适用,因为它们始终意味着返回值将立即或在将来返回给调用者。

  2. 远程引用 (RRef) 充当本地或远程对象的分布式共享指针。它可以与其他工作进程共享,并且引用计数将透明地处理。每个 RRef 只有一个所有者,并且对象仅在该所有者上存在。持有 RRef 的非所有者工作进程可以通过显式请求从所有者处获取对象的副本。当工作进程需要访问某些数据对象,但它本身既不是创建者(remote() 的调用者)也不是对象的所有者时,这非常有用。分布式优化器(我们将在下面讨论)是此类用例的一个示例。

  3. 分布式 Autograd 将参与前向传播的所有工作进程上的本地 autograd 引擎缝合在一起,并在反向传播期间自动联系它们以计算梯度。如果前向传播需要跨多台机器进行(例如,进行分布式模型并行训练、参数服务器训练等),这将特别有帮助。借助此功能,用户代码不再需要担心如何跨 RPC 边界发送梯度以及应以何种顺序启动本地 autograd 引擎,这在正向传播中存在嵌套和相互依赖的 RPC 调用时可能会变得非常复杂。

  4. 分布式优化器 的构造函数接受 Optimizer() (例如,SGD(), Adagrad() 等) 和参数 RRef 列表,在每个不同的 RRef 所有者上创建一个 Optimizer() 实例,并在运行 step() 时相应地更新参数。当您进行分布式前向传播和反向传播时,参数和梯度将分散在多个工作进程中,因此需要在每个参与的工作进程上都有一个优化器。分布式优化器将所有这些本地优化器包装成一个,并提供简洁的构造函数和 step() API。

RPC

在使用 RPC 和分布式 autograd 原语之前,必须进行初始化。要初始化 RPC 框架,我们需要使用 init_rpc(),它将初始化 RPC 框架、RRef 框架和分布式 autograd。

torch.distributed.rpc.init_rpc(name, backend=None, rank=-1, world_size=None, rpc_backend_options=None)[source][source]

初始化 RPC 原语,例如本地 RPC 代理和分布式 autograd,这会立即使当前进程准备好发送和接收 RPC。

参数
  • name (str) – 此节点的全局唯一名称。(例如,Trainer3ParameterServer2MasterWorker1)名称只能包含数字、字母、下划线、冒号和/或破折号,并且长度必须小于 128 个字符。

  • backend (BackendType, optional) – RPC 后端实现类型。支持的值为 BackendType.TENSORPIPE(默认值)。有关更多信息,请参阅 后端

  • rank (int) – 此节点的全局唯一 ID/排名。

  • world_size (int) – 组中工作进程的数量。

  • rpc_backend_options (RpcBackendOptions, optional) – 传递给 RpcAgent 构造函数的选项。它必须是 RpcBackendOptions 的代理特定子类,并且包含代理特定的初始化配置。默认情况下,对于所有代理,它将默认超时设置为 60 秒,并使用使用 init_method = "env://" 初始化的底层进程组执行 rendezvous,这意味着需要正确设置环境变量 MASTER_ADDRMASTER_PORT。有关更多信息,请参阅 后端,并查找哪些选项可用。

以下 API 允许用户远程执行函数以及创建对远程数据对象 (RRef) 的引用。在这些 API 中,当传递 Tensor 作为参数或返回值时,目标工作进程将尝试创建具有相同元数据(即,形状、步幅等)的 Tensor。我们有意禁止传输 CUDA 张量,因为如果源工作进程和目标工作进程上的设备列表不匹配,则可能会崩溃。在这种情况下,应用程序始终可以显式地将输入张量移动到调用者上的 CPU,并在必要时将其移动到被调用者上的所需设备。

警告

RPC 中的 TorchScript 支持是一项原型功能,可能会发生变化。自 v1.5.0 起,torch.distributed.rpc 支持调用 TorchScript 函数作为 RPC 目标函数,这将有助于提高被调用者端的并行性,因为执行 TorchScript 函数不需要 GIL。

torch.distributed.rpc.rpc_sync(to, func, args=None, kwargs=None, timeout=-1.0)[source][source]

进行阻塞 RPC 调用,以在工作进程 to 上运行函数 func。RPC 消息与 Python 代码的执行并行发送和接收。此方法是线程安全的。

参数
  • to (str or WorkerInfo or int) – 目标工作进程的名称/排名/WorkerInfo

  • func (Callable) – 可调用函数,例如 Python 可调用对象、内置运算符(例如 add())和带注释的 TorchScript 函数。

  • args (tuple) – func 调用的参数元组。

  • kwargs (dict) – func 调用的关键字参数字典。

  • timeout (float, optional) – 此 RPC 的超时时间(秒)。如果 RPC 未在此时间内完成,则会引发异常,指示其已超时。值 0 表示无限超时,即永远不会引发超时错误。如果未提供,则使用初始化期间或使用 _set_rpc_timeout 设置的默认值。

返回值

返回使用 argskwargs 运行 func 的结果。

示例:

确保在两个工作进程上都正确设置了 MASTER_ADDRMASTER_PORT。有关更多详细信息,请参阅 init_process_group() API。例如,

export MASTER_ADDR=localhost export MASTER_PORT=5678

然后在两个不同的进程中运行以下代码

>>> # On worker 0:
>>> import torch
>>> import torch.distributed.rpc as rpc
>>> rpc.init_rpc("worker0", rank=0, world_size=2)
>>> ret = rpc.rpc_sync("worker1", torch.add, args=(torch.ones(2), 3))
>>> rpc.shutdown()
>>> # On worker 1:
>>> import torch.distributed.rpc as rpc
>>> rpc.init_rpc("worker1", rank=1, world_size=2)
>>> rpc.shutdown()

以下是使用 RPC 运行 TorchScript 函数的示例。

>>> # On both workers:
>>> @torch.jit.script
>>> def my_script_add(tensor: torch.Tensor, scalar: int):
>>>    return torch.add(tensor, scalar)
>>> # On worker 0:
>>> import torch.distributed.rpc as rpc
>>> rpc.init_rpc("worker0", rank=0, world_size=2)
>>> ret = rpc.rpc_sync("worker1", my_script_add, args=(torch.ones(2), 3))
>>> rpc.shutdown()
>>> # On worker 1:
>>> import torch.distributed.rpc as rpc
>>> rpc.init_rpc("worker1", rank=1, world_size=2)
>>> rpc.shutdown()
torch.distributed.rpc.rpc_async(to, func, args=None, kwargs=None, timeout=-1.0)[source][source]

进行非阻塞 RPC 调用,以在工作进程 to 上运行函数 func。RPC 消息与 Python 代码的执行并行发送和接收。此方法是线程安全的。此方法将立即返回一个可以等待的 Future

参数
  • to (str or WorkerInfo or int) – 目标工作进程的名称/排名/WorkerInfo

  • func (Callable) – 可调用函数,例如 Python 可调用对象、内置运算符(例如 add())和带注释的 TorchScript 函数。

  • args (tuple) – func 调用的参数元组。

  • kwargs (dict) – func 调用的关键字参数字典。

  • timeout (float, optional) – 此 RPC 的超时时间(秒)。如果 RPC 未在此时间内完成,则会引发异常,指示其已超时。值 0 表示无限超时,即永远不会引发超时错误。如果未提供,则使用初始化期间或使用 _set_rpc_timeout 设置的默认值。

返回值

返回一个可以等待的 Future 对象。完成时,可以从 Future 对象中检索 funcargskwargs 上的返回值。

警告

不支持使用 GPU 张量作为 func 的参数或返回值,因为我们不支持通过网络发送 GPU 张量。您需要在将 GPU 张量用作 func 的参数或返回值之前,显式地将它们复制到 CPU。

警告

rpc_async API 不会复制参数张量的存储,直到通过网络发送它们,这可能由不同的线程完成,具体取决于 RPC 后端类型。调用者应确保这些张量的内容保持完整,直到返回的 Future 完成。

示例:

确保在两个工作进程上都正确设置了 MASTER_ADDRMASTER_PORT。有关更多详细信息,请参阅 init_process_group() API。例如,

export MASTER_ADDR=localhost export MASTER_PORT=5678

然后在两个不同的进程中运行以下代码

>>> # On worker 0:
>>> import torch
>>> import torch.distributed.rpc as rpc
>>> rpc.init_rpc("worker0", rank=0, world_size=2)
>>> fut1 = rpc.rpc_async("worker1", torch.add, args=(torch.ones(2), 3))
>>> fut2 = rpc.rpc_async("worker1", min, args=(1, 2))
>>> result = fut1.wait() + fut2.wait()
>>> rpc.shutdown()
>>> # On worker 1:
>>> import torch.distributed.rpc as rpc
>>> rpc.init_rpc("worker1", rank=1, world_size=2)
>>> rpc.shutdown()

以下是使用 RPC 运行 TorchScript 函数的示例。

>>> # On both workers:
>>> @torch.jit.script
>>> def my_script_add(tensor: torch.Tensor, scalar: int):
>>>    return torch.add(tensor, scalar)
>>> # On worker 0:
>>> import torch.distributed.rpc as rpc
>>> rpc.init_rpc("worker0", rank=0, world_size=2)
>>> fut = rpc.rpc_async("worker1", my_script_add, args=(torch.ones(2), 3))
>>> ret = fut.wait()
>>> rpc.shutdown()
>>> # On worker 1:
>>> import torch.distributed.rpc as rpc
>>> rpc.init_rpc("worker1", rank=1, world_size=2)
>>> rpc.shutdown()
torch.distributed.rpc.remote(to, func, args=None, kwargs=None, timeout=-1.0)[source][source]

进行远程调用以在工作进程 to 上运行 func,并立即返回结果值的 RRef。工作进程 to 将是返回的 RRef 的所有者,而调用 remote 的工作进程是用户。所有者管理其 RRef 的全局引用计数,并且只有当全局范围内没有对其的活动引用时,所有者 RRef 才会被销毁。

参数
  • to (str or WorkerInfo or int) – 目标工作进程的名称/排名/WorkerInfo

  • func (Callable) – 可调用函数,例如 Python 可调用对象、内置运算符(例如 add())和带注释的 TorchScript 函数。

  • args (tuple) – func 调用的参数元组。

  • kwargs (dict) – func 调用的关键字参数字典。

  • timeout (float, optional) – 此远程调用的超时时间(秒)。如果在此超时时间内未在此工作进程上成功处理在工作进程 to 上创建此 RRef,则下次尝试使用 RRef(例如 to_here())时,将引发超时,指示此失败。值 0 表示无限超时,即永远不会引发超时错误。如果未提供,则使用初始化期间或使用 _set_rpc_timeout 设置的默认值。

返回值

结果值的用户 RRef 实例。使用阻塞 API torch.distributed.rpc.RRef.to_here() 在本地检索结果值。

警告

remote API 不会复制参数张量的存储,直到通过网络发送它们,这可能由不同的线程完成,具体取决于 RPC 后端类型。调用者应确保这些张量的内容保持完整,直到所有者确认返回的 RRef,这可以使用 torch.distributed.rpc.RRef.confirmed_by_owner() API 进行检查。

警告

诸如 remote API 的超时之类的错误是尽力处理的。这意味着当 remote 发起的远程调用失败时(例如出现超时错误),我们会采取尽力而为的方法来处理错误。这意味着错误会被处理并异步地在结果 RRef 上设置。如果 RRef 在此处理之前尚未被应用程序使用(例如 to_here 或 fork 调用),则将来使用 RRef 将适当地引发错误。但是,用户应用程序可能会在错误处理之前使用 RRef。在这种情况下,可能不会引发错误,因为尚未处理错误。

示例

Make sure that ``MASTER_ADDR`` and ``MASTER_PORT`` are set properly
on both workers. Refer to :meth:`~torch.distributed.init_process_group`
API for more details. For example,

export MASTER_ADDR=localhost
export MASTER_PORT=5678

Then run the following code in two different processes:

>>> # On worker 0:
>>> import torch
>>> import torch.distributed.rpc as rpc
>>> rpc.init_rpc("worker0", rank=0, world_size=2)
>>> rref1 = rpc.remote("worker1", torch.add, args=(torch.ones(2), 3))
>>> rref2 = rpc.remote("worker1", torch.add, args=(torch.ones(2), 1))
>>> x = rref1.to_here() + rref2.to_here()
>>> rpc.shutdown()

>>> # On worker 1:
>>> import torch.distributed.rpc as rpc
>>> rpc.init_rpc("worker1", rank=1, world_size=2)
>>> rpc.shutdown()

Below is an example of running a TorchScript function using RPC.

>>> # On both workers:
>>> @torch.jit.script
>>> def my_script_add(tensor: torch.Tensor, scalar: int):
>>>    return torch.add(tensor, scalar)

>>> # On worker 0:
>>> import torch.distributed.rpc as rpc
>>> rpc.init_rpc("worker0", rank=0, world_size=2)
>>> rref = rpc.remote("worker1", my_script_add, args=(torch.ones(2), 3))
>>> rref.to_here()
>>> rpc.shutdown()

>>> # On worker 1:
>>> import torch.distributed.rpc as rpc
>>> rpc.init_rpc("worker1", rank=1, world_size=2)
>>> rpc.shutdown()
torch.distributed.rpc.get_worker_info(worker_name=None)[source][source]

获取给定工作进程名称的 WorkerInfo。使用此 WorkerInfo 以避免在每次调用时传递昂贵的字符串。

参数

worker_name (str) – 工作进程的字符串名称。如果为 None,则返回当前工作进程的 ID。(默认 None

返回值

给定 worker_nameWorkerInfo 实例;如果 worker_nameNone,则返回当前工作进程的 WorkerInfo

torch.distributed.rpc.shutdown(graceful=True, timeout=0)[source][source]

执行 RPC 代理的关闭操作,然后销毁 RPC 代理。这将阻止本地代理接受待处理的请求,并通过终止所有 RPC 线程来关闭 RPC 框架。如果 graceful=True,这将阻塞直到所有本地和远程 RPC 进程都到达此方法,并等待所有未完成的工作完成。否则,如果 graceful=False,这是一个本地关闭,它不会等待其他 RPC 进程到达此方法。

警告

对于由 Future 对象返回的 rpc_async(),在 shutdown() 之后不应调用 future.wait()

参数

graceful (bool) – 是否执行优雅关闭。如果为 True,这将 1) 等待直到没有待处理的 UserRRefs 系统消息并删除它们;2) 阻塞直到所有本地和远程 RPC 进程都到达此方法,并等待所有未完成的工作完成。

示例:

确保在两个工作进程上都正确设置了 MASTER_ADDRMASTER_PORT。有关更多详细信息,请参阅 init_process_group() API。例如,

export MASTER_ADDR=localhost export MASTER_PORT=5678

然后在两个不同的进程中运行以下代码

>>> # On worker 0:
>>> import torch
>>> import torch.distributed.rpc as rpc
>>> rpc.init_rpc("worker0", rank=0, world_size=2)
>>> # do some work
>>> result = rpc.rpc_sync("worker1", torch.add, args=(torch.ones(1), 1))
>>> # ready to shutdown
>>> rpc.shutdown()
>>> # On worker 1:
>>> import torch.distributed.rpc as rpc
>>> rpc.init_rpc("worker1", rank=1, world_size=2)
>>> # wait for worker 0 to finish work, and then shutdown.
>>> rpc.shutdown()
class torch.distributed.rpc.WorkerInfo

一个结构体,封装了系统中 worker 的信息。包含 worker 的名称和 ID。此类并非旨在直接构造,而是可以通过 get_worker_info() 检索实例,并将结果传递给诸如 rpc_sync()rpc_async()remote() 等函数,以避免在每次调用时复制字符串。

property id

全局唯一的 ID,用于标识 worker。

property name

worker 的名称。

RPC 包还提供了装饰器,允许应用程序指定如何在被调用方处理给定的函数。

torch.distributed.rpc.functions.async_execution(fn)[source][source]

用于装饰函数的装饰器,指示函数的返回值保证是一个 Future 对象,并且此函数可以在 RPC 被调用方异步运行。更具体地说,被调用方提取被包装函数返回的 Future,并将后续处理步骤安装为该 Future 的回调。安装的回调将在 Future 完成时从中读取值,并将该值作为 RPC 响应发送回去。这也意味着返回的 Future 仅存在于被调用方,永远不会通过 RPC 发送。当被包装函数 (fn) 的执行需要暂停和恢复时,此装饰器很有用,例如,由于包含 rpc_async() 或等待其他信号。

注意

要启用异步执行,应用程序必须将此装饰器返回的函数对象传递给 RPC API。如果 RPC 检测到由此装饰器安装的属性,它就知道此函数返回一个 Future 对象,并将相应地处理它。但是,这并不意味着此装饰器在定义函数时必须是最外层的。例如,当与 @staticmethod@classmethod 结合使用时,@rpc.functions.async_execution 需要是内部装饰器,以允许目标函数被识别为静态或类函数。此目标函数仍然可以异步执行,因为当访问时,静态或类方法会保留由 @rpc.functions.async_execution 安装的属性。

示例:

返回的 Future 对象可以来自 rpc_async()then()Future 构造函数。下面的示例展示了直接使用 then() 返回的 Future

>>> from torch.distributed import rpc
>>>
>>> # omitting setup and shutdown RPC
>>>
>>> # On all workers
>>> @rpc.functions.async_execution
>>> def async_add_chained(to, x, y, z):
>>>     # This function runs on "worker1" and returns immediately when
>>>     # the callback is installed through the `then(cb)` API. In the
>>>     # mean time, the `rpc_async` to "worker2" can run concurrently.
>>>     # When the return value of that `rpc_async` arrives at
>>>     # "worker1", "worker1" will run the lambda function accordingly
>>>     # and set the value for the previously returned `Future`, which
>>>     # will then trigger RPC to send the result back to "worker0".
>>>     return rpc.rpc_async(to, torch.add, args=(x, y)).then(
>>>         lambda fut: fut.wait() + z
>>>     )
>>>
>>> # On worker0
>>> ret = rpc.rpc_sync(
>>>     "worker1",
>>>     async_add_chained,
>>>     args=("worker2", torch.ones(2), 1, 1)
>>> )
>>> print(ret)  # prints tensor([3., 3.])

当与 TorchScript 装饰器结合使用时,此装饰器必须是最外层的。

>>> from torch import Tensor
>>> from torch.futures import Future
>>> from torch.distributed import rpc
>>>
>>> # omitting setup and shutdown RPC
>>>
>>> # On all workers
>>> @torch.jit.script
>>> def script_add(x: Tensor, y: Tensor) -> Tensor:
>>>     return x + y
>>>
>>> @rpc.functions.async_execution
>>> @torch.jit.script
>>> def async_add(to: str, x: Tensor, y: Tensor) -> Future[Tensor]:
>>>     return rpc.rpc_async(to, script_add, (x, y))
>>>
>>> # On worker0
>>> ret = rpc.rpc_sync(
>>>     "worker1",
>>>     async_add,
>>>     args=("worker2", torch.ones(2), 1)
>>> )
>>> print(ret)  # prints tensor([2., 2.])

当与静态或类方法结合使用时,此装饰器必须是内部的。

>>> from torch.distributed import rpc
>>>
>>> # omitting setup and shutdown RPC
>>>
>>> # On all workers
>>> class AsyncExecutionClass:
>>>
>>>     @staticmethod
>>>     @rpc.functions.async_execution
>>>     def static_async_add(to, x, y, z):
>>>         return rpc.rpc_async(to, torch.add, args=(x, y)).then(
>>>             lambda fut: fut.wait() + z
>>>         )
>>>
>>>     @classmethod
>>>     @rpc.functions.async_execution
>>>     def class_async_add(cls, to, x, y, z):
>>>         ret_fut = torch.futures.Future()
>>>         rpc.rpc_async(to, torch.add, args=(x, y)).then(
>>>             lambda fut: ret_fut.set_result(fut.wait() + z)
>>>         )
>>>         return ret_fut
>>>
>>>     @rpc.functions.async_execution
>>>     def bound_async_add(self, to, x, y, z):
>>>         return rpc.rpc_async(to, torch.add, args=(x, y)).then(
>>>             lambda fut: fut.wait() + z
>>>         )
>>>
>>> # On worker0
>>> ret = rpc.rpc_sync(
>>>     "worker1",
>>>     AsyncExecutionClass.static_async_add,
>>>     args=("worker2", torch.ones(2), 1, 2)
>>> )
>>> print(ret)  # prints tensor([4., 4.])
>>>
>>> ret = rpc.rpc_sync(
>>>     "worker1",
>>>     AsyncExecutionClass.class_async_add,
>>>     args=("worker2", torch.ones(2), 1, 2)
>>> )
>>> print(ret)  # prints tensor([4., 4.])

此装饰器也适用于 RRef 助手,即 . torch.distributed.rpc.RRef.rpc_sync()torch.distributed.rpc.RRef.rpc_async()torch.distributed.rpc.RRef.remote()

>>> from torch.distributed import rpc
>>>
>>> # reuse the AsyncExecutionClass class above
>>> rref = rpc.remote("worker1", AsyncExecutionClass)
>>> ret = rref.rpc_sync().static_async_add("worker2", torch.ones(2), 1, 2)
>>> print(ret)  # prints tensor([4., 4.])
>>>
>>> rref = rpc.remote("worker1", AsyncExecutionClass)
>>> ret = rref.rpc_async().static_async_add("worker2", torch.ones(2), 1, 2).wait()
>>> print(ret)  # prints tensor([4., 4.])
>>>
>>> rref = rpc.remote("worker1", AsyncExecutionClass)
>>> ret = rref.remote().static_async_add("worker2", torch.ones(2), 1, 2).to_here()
>>> print(ret)  # prints tensor([4., 4.])

后端

RPC 模块可以利用不同的后端来执行节点之间的通信。要使用的后端可以在 init_rpc() 函数中指定,方法是传递 BackendType 枚举的特定值。无论使用什么后端,RPC API 的其余部分都不会改变。每个后端还定义了 RpcBackendOptions 类的子类,其实例也可以传递给 init_rpc() 以配置后端的行为。

class torch.distributed.rpc.BackendType(value)

可用后端的枚举类。

PyTorch 附带一个内置的 BackendType.TENSORPIPE 后端。可以使用 register_backend() 函数注册其他后端。

class torch.distributed.rpc.RpcBackendOptions

一个抽象结构,封装了传递到 RPC 后端的选项。此类的实例可以传递到 init_rpc() 中,以便使用特定的配置初始化 RPC,例如要使用的 RPC 超时和 init_method

property init_method

指定如何初始化进程组的 URL。默认为 env://

property rpc_timeout

一个浮点数,指示所有 RPC 要使用的超时时间。如果 RPC 未在此时间范围内完成,它将完成并抛出异常,指示它已超时。

TensorPipe 后端

TensorPipe 代理是默认代理,它利用 TensorPipe 库,该库提供了一种原生的点对点通信原语,专门适用于机器学习,从根本上解决了 Gloo 的一些局限性。与 Gloo 相比,它的优势是异步的,这允许大量传输同时发生,每个传输都以自己的速度进行,而不会相互阻塞。它只会在需要时按需打开节点对之间的管道,当一个节点发生故障时,只会关闭与其相关的管道,而所有其他管道将保持正常工作。此外,它能够支持多种不同的传输方式(TCP 当然,还有共享内存、NVLink、InfiniBand 等),并且可以自动检测它们的可用性,并协商每条管道的最佳传输方式。

TensorPipe 后端已在 PyTorch v1.6 中引入,并且正在积极开发中。目前,它仅支持 CPU 张量,对 GPU 的支持即将推出。它附带一个基于 TCP 的传输,就像 Gloo 一样。它还能够自动分块和多路复用多个套接字和线程上的大型张量,以实现非常高的带宽。代理将能够自行选择最佳传输方式,无需任何干预。

示例

>>> import os
>>> from torch.distributed import rpc
>>> os.environ['MASTER_ADDR'] = 'localhost'
>>> os.environ['MASTER_PORT'] = '29500'
>>>
>>> rpc.init_rpc(
>>>     "worker1",
>>>     rank=0,
>>>     world_size=2,
>>>     rpc_backend_options=rpc.TensorPipeRpcBackendOptions(
>>>         num_worker_threads=8,
>>>         rpc_timeout=20 # 20 second timeout
>>>     )
>>> )
>>>
>>> # omitting init_rpc invocation on worker2
class torch.distributed.rpc.TensorPipeRpcBackendOptions(*, num_worker_threads=16, rpc_timeout=60.0, init_method='env://', device_maps=None, devices=None, _transports=None, _channels=None)[source][source]

用于 TensorPipeAgent 的后端选项,派生自 RpcBackendOptions

参数
  • num_worker_threads (int, 可选) – TensorPipeAgent 用于执行请求的线程池中的线程数(默认值:16)。

  • rpc_timeout (float, 可选) – RPC 请求的默认超时时间,以秒为单位(默认值:60 秒)。如果 RPC 未在此时间范围内完成,则会引发异常,指示已超时。调用者可以在 rpc_sync()rpc_async() 中根据需要为单个 RPC 覆盖此超时时间。

  • init_method (str, 可选) – 用于初始化分布式存储以进行 rendezvous 的 URL。它接受与 init_process_group() 的相同参数接受的任何值(默认值:env://)。

  • device_maps (Dict[str, Dict], 可选) – 从此 worker 到被调用方的设备放置映射。键是被调用方 worker 名称,值是将此 worker 的设备映射到被调用方 worker 的设备的字典(Dict,键为 int,值为 strtorch.device)。(默认值:None

  • devices (List[int, str, or torch.device], optional) – RPC 代理使用的所有本地 CUDA 设备。默认情况下,它将从其自身的 device_maps 和来自其对等方的 device_maps 的相应设备初始化为所有本地设备。当处理 CUDA RPC 请求时,代理将为该 List 中的所有设备正确同步 CUDA 流。

property device_maps

设备映射位置。

property devices

本地代理使用的所有设备。

property init_method

指定如何初始化进程组的 URL。默认为 env://

property num_worker_threads

TensorPipeAgent 用于执行请求的线程池中的线程数。

property rpc_timeout

一个浮点数,指示所有 RPC 要使用的超时时间。如果 RPC 未在此时间范围内完成,它将完成并抛出异常,指示它已超时。

set_device_map(to, device_map)[source][source]

设置每个 RPC 调用方和被调用方对之间的设备映射。可以多次调用此函数以增量添加设备放置配置。

参数
  • to (str) – 被调用方名称。

  • device_map (Dict of int, str, or torch.device) – 从此 worker 到被调用方的设备放置映射。此映射必须是可逆的。

示例

>>> # both workers
>>> def add(x, y):
>>>     print(x)  # tensor([1., 1.], device='cuda:1')
>>>     return x + y, (x + y).to(2)
>>>
>>> # on worker 0
>>> options = TensorPipeRpcBackendOptions(
>>>     num_worker_threads=8,
>>>     device_maps={"worker1": {0: 1}}
>>>     # maps worker0's cuda:0 to worker1's cuda:1
>>> )
>>> options.set_device_map("worker1", {1: 2})
>>> # maps worker0's cuda:1 to worker1's cuda:2
>>>
>>> rpc.init_rpc(
>>>     "worker0",
>>>     rank=0,
>>>     world_size=2,
>>>     backend=rpc.BackendType.TENSORPIPE,
>>>     rpc_backend_options=options
>>> )
>>>
>>> x = torch.ones(2)
>>> rets = rpc.rpc_sync("worker1", add, args=(x.to(0), 1))
>>> # The first argument will be moved to cuda:1 on worker1. When
>>> # sending the return value back, it will follow the invert of
>>> # the device map, and hence will be moved back to cuda:0 and
>>> # cuda:1 on worker0
>>> print(rets[0])  # tensor([2., 2.], device='cuda:0')
>>> print(rets[1])  # tensor([2., 2.], device='cuda:1')
set_devices(devices)[source][source]

设置 TensorPipe RPC 代理使用的本地设备。当处理 CUDA RPC 请求时,TensorPipe RPC 代理将为该 List 中的所有设备正确同步 CUDA 流。

参数

devices (List of int, str, or torch.device) – TensorPipe RPC 代理使用的本地设备。

注意

RPC 框架不会自动重试任何 rpc_sync()rpc_async()remote() 调用。原因是 RPC 框架无法确定操作是否是幂等的,以及重试是否安全。因此,处理故障并在必要时重试是应用程序的责任。RPC 通信基于 TCP,因此可能会因网络故障或间歇性网络连接问题而发生故障。在这种情况下,应用程序需要使用合理的退避策略进行适当的重试,以确保网络不会被激进的重试淹没。

RRef

警告

使用 CUDA 张量时,当前不支持 RRef

一个 RRef (远程引用) 是对远程 worker 上类型为 T (例如 Tensor) 的值的引用。此句柄使引用的远程值在所有者上保持活动状态,但这并不意味着该值将来会被传输到本地 worker。RRef 可用于多机训练,方法是保存对其他 worker 上存在的 nn.Modules 的引用,并在训练期间调用适当的函数来检索或修改其参数。有关更多详细信息,请参阅 远程引用协议

class torch.distributed.rpc.PyRRef(RRef)

一个类,封装了对远程 worker 上某种类型的值的引用。此句柄将使引用的远程值在 worker 上保持活动状态。UserRRef 将在以下情况下被删除:1) 在应用程序代码和本地 RRef 上下文中都没有对其的引用,或者 2) 应用程序已调用优雅关闭。在已删除的 RRef 上调用方法会导致未定义的行为。RRef 实现仅提供尽力而为的错误检测,应用程序不应在 rpc.shutdown() 之后使用 UserRRefs

警告

RRef 只能由 RPC 模块序列化和反序列化。在没有 RPC 的情况下序列化和反序列化 RRef(例如,Python pickle、torch save() / load()、JIT save() / load() 等)会导致错误。

参数
  • value (object) – 要由此 RRef 包装的值。

  • type_hint (Type, 可选) – 应作为 value 的类型提示传递给 TorchScript 编译器的 Python 类型。

示例:

以下示例跳过了 RPC 初始化和关闭代码以简化操作。有关这些详细信息,请参阅 RPC 文档。

  1. 使用 rpc.remote 创建 RRef

>>> import torch
>>> import torch.distributed.rpc as rpc
>>> rref = rpc.remote("worker1", torch.add, args=(torch.ones(2), 3))
>>> # get a copy of value from the RRef
>>> x = rref.to_here()
  1. 从本地对象创建 RRef

>>> import torch
>>> from torch.distributed.rpc import RRef
>>> x = torch.zeros(2, 2)
>>> rref = RRef(x)
  1. 与其他 worker 共享 RRef

>>> # On both worker0 and worker1:
>>> def f(rref):
>>>   return rref.to_here() + 1
>>> # On worker0:
>>> import torch
>>> import torch.distributed.rpc as rpc
>>> from torch.distributed.rpc import RRef
>>> rref = RRef(torch.zeros(2, 2))
>>> # the following RPC shares the rref with worker1, reference
>>> # count is automatically updated.
>>> rpc.rpc_sync("worker1", f, args=(rref,))
backward(self: torch._C._distributed_rpc.PyRRef, dist_autograd_ctx_id: int = -1, retain_graph: bool = False) None

使用 RRef 作为反向传播的根来运行反向传播过程。如果提供了 dist_autograd_ctx_id,我们将使用提供的 ctx_id 从 RRef 的所有者开始执行分布式反向传播过程。在这种情况下,应使用 get_gradients() 来检索梯度。如果 dist_autograd_ctx_idNone,则假定这是一个本地 autograd 图,我们只执行本地反向传播过程。在本地情况下,调用此 API 的节点必须是 RRef 的所有者。RRef 的值应为标量张量。

参数
  • dist_autograd_ctx_id (int, 可选) – 我们应该为其检索梯度的分布式 autograd 上下文 ID(默认值:-1)。

  • retain_graph (bool, 可选) – 如果为 False,则用于计算 grad 的图将被释放。请注意,在几乎所有情况下,都不需要将此选项设置为 True,并且通常可以用更有效的方式解决。通常,您需要将其设置为 True 才能多次运行反向传播过程(默认值:False)。

示例:
>>> import torch.distributed.autograd as dist_autograd
>>> with dist_autograd.context() as context_id:
>>>     rref.backward(context_id)
confirmed_by_owner(self: torch._C._distributed_rpc.PyRRef) bool

返回此 RRef 是否已被所有者确认。OwnerRRef 始终返回 true,而 UserRRef 仅在所有者知晓此 UserRRef 时返回 true。

is_owner(self: torch._C._distributed_rpc.PyRRef) bool

返回当前节点是否为此 RRef 的所有者。

local_value(self: torch._C._distributed_rpc.PyRRef) object

如果当前节点是所有者,则返回对本地值的引用。否则,抛出异常。

owner(self: torch._C._distributed_rpc.PyRRef) torch._C._distributed_rpc.WorkerInfo

返回拥有此 RRef 的节点的工作进程信息。

owner_name(self: torch._C._distributed_rpc.PyRRef) str

返回拥有此 RRef 的节点的工作进程名称。

remote(self: torch._C._distributed_rpc.PyRRef, timeout: float = -1.0) object

创建一个辅助代理,以便轻松启动 remote,使用 RRef 的所有者作为目标,以在此 RRef 引用的对象上运行函数。更具体地说,rref.remote().func_name(*args, **kwargs) 与以下代码相同

>>> def run(rref, func_name, args, kwargs):
>>>   return getattr(rref.local_value(), func_name)(*args, **kwargs)
>>>
>>> rpc.remote(rref.owner(), run, args=(rref, func_name, args, kwargs))
参数

timeout (float, 可选) – rref.remote() 的超时时间。如果此 RRef 的创建未在超时时间内成功完成,则下次尝试使用 RRef(例如 to_here)时,将引发超时。如果未提供,则将使用默认的 RPC 超时。请参阅 rpc.remote() 以获取 RRef 的特定超时语义。

示例:
>>> from torch.distributed import rpc
>>> rref = rpc.remote("worker1", torch.add, args=(torch.zeros(2, 2), 1))
>>> rref.remote().size().to_here()  # returns torch.Size([2, 2])
>>> rref.remote().view(1, 4).to_here()  # returns tensor([[1., 1., 1., 1.]])
rpc_async(self: torch._C._distributed_rpc.PyRRef, timeout: float = -1.0) object

创建一个辅助代理,以便轻松启动 rpc_async,使用 RRef 的所有者作为目标,以在此 RRef 引用的对象上运行函数。更具体地说,rref.rpc_async().func_name(*args, **kwargs) 与以下代码相同

>>> def run(rref, func_name, args, kwargs):
>>>   return getattr(rref.local_value(), func_name)(*args, **kwargs)
>>>
>>> rpc.rpc_async(rref.owner(), run, args=(rref, func_name, args, kwargs))
参数

timeout (float, 可选) – rref.rpc_async() 的超时时间。如果调用未在此时间范围内完成,则会引发指示此情况的异常。如果未提供此参数,则将使用默认的 RPC 超时。

示例:
>>> from torch.distributed import rpc
>>> rref = rpc.remote("worker1", torch.add, args=(torch.zeros(2, 2), 1))
>>> rref.rpc_async().size().wait()  # returns torch.Size([2, 2])
>>> rref.rpc_async().view(1, 4).wait()  # returns tensor([[1., 1., 1., 1.]])
rpc_sync(self: torch._C._distributed_rpc.PyRRef, timeout: float = -1.0) object

创建一个辅助代理,以便轻松启动 rpc_sync,使用 RRef 的所有者作为目标,以在此 RRef 引用的对象上运行函数。更具体地说,rref.rpc_sync().func_name(*args, **kwargs) 与以下代码相同

>>> def run(rref, func_name, args, kwargs):
>>>   return getattr(rref.local_value(), func_name)(*args, **kwargs)
>>>
>>> rpc.rpc_sync(rref.owner(), run, args=(rref, func_name, args, kwargs))
参数

timeout (float, 可选) – rref.rpc_sync() 的超时时间。如果调用未在此时间范围内完成,则会引发指示此情况的异常。如果未提供此参数,则将使用默认的 RPC 超时。

示例:
>>> from torch.distributed import rpc
>>> rref = rpc.remote("worker1", torch.add, args=(torch.zeros(2, 2), 1))
>>> rref.rpc_sync().size()  # returns torch.Size([2, 2])
>>> rref.rpc_sync().view(1, 4)  # returns tensor([[1., 1., 1., 1.]])
to_here(self: torch._C._distributed_rpc.PyRRef, timeout: float = -1.0) object

阻塞调用,将 RRef 的值从所有者复制到本地节点并返回。如果当前节点是所有者,则返回对本地值的引用。

参数

timeout (float, 可选) – to_here 的超时时间。如果调用未在此时间范围内完成,则会引发指示此情况的异常。如果未提供此参数,则将使用默认的 RPC 超时 (60 秒)。

RemoteModule

警告

使用 CUDA 张量时,当前不支持 RemoteModule

RemoteModule 是一种在不同进程上远程创建 nn.Module 的简便方法。实际的模块驻留在远程主机上,但本地主机具有此模块的句柄,并且可以像常规 nn.Module 一样调用此模块。但是,调用会产生到远程端的 RPC 调用,并且如果需要,可以通过 RemoteModule 支持的其他 API 异步执行。

class torch.distributed.nn.api.remote_module.RemoteModule(*args, **kwargs)[source][source]

只有在 RPC 初始化之后才能创建 RemoteModule 实例。

它在指定的远程节点上创建用户指定的模块。它的行为类似于常规的 nn.Module,不同之处在于 forward 方法在远程节点上执行。它负责自动梯度记录,以确保反向传播将梯度传播回相应的远程模块。

它基于 module_clsforward 方法的签名生成两个方法 forward_asyncforwardforward_async 异步运行并返回 Future。forward_asyncforward 的参数与 module_cls 返回的模块的 forward 方法相同。

例如,如果 module_cls 返回 nn.Linear 的实例,该实例具有 forward 方法签名:def forward(input: Tensor) -> Tensor:,则生成的 RemoteModule 将具有 2 个方法,其签名如下

def forward(input: Tensor) -> Tensor:
def forward_async(input: Tensor) -> Future[Tensor]:
参数
  • remote_device (str) – 目标工作进程上我们想要放置此模块的设备。格式应为“<workername>/<device>”,其中设备字段可以解析为 torch.device 类型。例如,“trainer0/cpu”、“trainer0”、“ps0/cuda:0”。此外,设备字段是可选的,默认值为“cpu”。

  • module_cls (nn.Module) –

    要远程创建的模块的类。例如,

    >>> class MyModule(nn.Module):
    >>>     def forward(input):
    >>>         return input + 1
    >>>
    >>> module_cls = MyModule
    

  • args (Sequence, 可选) – 传递给 module_cls 的 args。

  • kwargs (Dict, 可选) – 传递给 module_cls 的 kwargs。

返回值

远程模块实例,它包装了用户提供的 module_cls 创建的 Module,它具有阻塞的 forward 方法和异步的 forward_async 方法,该方法返回远程端用户提供的模块上 forward 调用的 future。

示例:

在两个不同的进程中运行以下代码

>>> # On worker 0:
>>> import torch
>>> import torch.distributed.rpc as rpc
>>> from torch import nn, Tensor
>>> from torch.distributed.nn.api.remote_module import RemoteModule
>>>
>>> rpc.init_rpc("worker0", rank=0, world_size=2)
>>> remote_linear_module = RemoteModule(
>>>     "worker1/cpu", nn.Linear, args=(20, 30),
>>> )
>>> input = torch.randn(128, 20)
>>> ret_fut = remote_linear_module.forward_async(input)
>>> ret = ret_fut.wait()
>>> rpc.shutdown()
>>> # On worker 1:
>>> import torch
>>> import torch.distributed.rpc as rpc
>>>
>>> rpc.init_rpc("worker1", rank=1, world_size=2)
>>> rpc.shutdown()

此外,可以在此教程中找到一个与DistributedDataParallel (DDP) 结合的更实际的示例。

get_module_rref()[source]

返回指向远程模块的 RRef (RRef[nn.Module])。

返回类型

RRef[Module]

remote_parameters(recurse=True)[source]

返回指向远程模块参数的 RRef 列表。

这通常可以与 DistributedOptimizer 结合使用。

参数

recurse (bool) – 如果为 True,则返回远程模块和远程模块的所有子模块的参数。否则,仅返回作为远程模块直接成员的参数。

返回值

指向远程模块参数的 RRef (List[RRef[nn.Parameter]]) 列表。

返回类型

List[RRef[Parameter]]

分布式 Autograd 框架

警告

使用 CUDA 张量时,当前不支持分布式 autograd

此模块提供了一个基于 RPC 的分布式 autograd 框架,可用于模型并行训练等应用。简而言之,应用程序可以通过 RPC 发送和接收梯度记录张量。在正向传播中,我们记录何时通过 RPC 发送梯度记录张量,并在反向传播期间,我们使用此信息使用 RPC 执行分布式反向传播。有关更多详细信息,请参阅 分布式 Autograd 设计

torch.distributed.autograd.backward(context_id: int, roots: List[Tensor], retain_graph=False) None

使用提供的根启动分布式反向传播。当前,这实现了 FAST 模式算法,该算法假定在同一分布式 autograd 上下文中跨工作进程发送的所有 RPC 消息都将是反向传播期间的 autograd 图的一部分。

我们使用提供的根来发现 autograd 图并计算适当的依赖关系。此方法会阻塞,直到完成整个 autograd 计算。

我们将梯度累积在每个节点的相应 torch.distributed.autograd.context 中。要使用的 autograd 上下文是根据调用 torch.distributed.autograd.backward() 时传入的 context_id 查找的。如果给定 ID 没有有效的 autograd 上下文,则会抛出错误。您可以使用 get_gradients() API 检索累积的梯度。

参数
  • context_id (int) – 我们应为其检索梯度的 autograd 上下文 ID。

  • roots (list) – 表示 autograd 计算根的张量。所有张量都应为标量。

  • retain_graph (bool, 可选) – 如果为 False,则将释放用于计算 grad 的图。请注意,在几乎所有情况下,都不需要将此选项设置为 True,并且通常可以用更有效的方式解决。通常,您需要将其设置为 True 才能多次运行反向传播。

示例:
>>> import torch.distributed.autograd as dist_autograd
>>> with dist_autograd.context() as context_id:
>>>     pred = model.forward()
>>>     loss = loss_func(pred, loss)
>>>     dist_autograd.backward(context_id, loss)
class torch.distributed.autograd.context[source][source]

上下文对象,用于在使用分布式 autograd 时包装正向和反向传播。with 语句中生成的 context_id 是唯一标识所有工作进程上的分布式反向传播所必需的。每个工作进程都存储与此 context_id 关联的元数据,这是正确执行分布式 autograd 传播所必需的。

示例:
>>> import torch.distributed.autograd as dist_autograd
>>> with dist_autograd.context() as context_id:
>>>     t1 = torch.rand((3, 3), requires_grad=True)
>>>     t2 = torch.rand((3, 3), requires_grad=True)
>>>     loss = rpc.rpc_sync("worker1", torch.add, args=(t1, t2)).sum()
>>>     dist_autograd.backward(context_id, [loss])
torch.distributed.autograd.get_gradients(context_id: int) Dict[Tensor, Tensor]

检索从 Tensor 到该 Tensor 的适当梯度的映射,该梯度是在与给定 context_id 对应的上下文中作为分布式 autograd 反向传播的一部分累积的。

参数

context_id (int) – 我们应为其检索梯度的 autograd 上下文 ID。

返回值

一个映射,其中键是 Tensor,值是该 Tensor 的关联梯度。

示例:
>>> import torch.distributed.autograd as dist_autograd
>>> with dist_autograd.context() as context_id:
>>>     t1 = torch.rand((3, 3), requires_grad=True)
>>>     t2 = torch.rand((3, 3), requires_grad=True)
>>>     loss = t1 + t2
>>>     dist_autograd.backward(context_id, [loss.sum()])
>>>     grads = dist_autograd.get_gradients(context_id)
>>>     print(grads[t1])
>>>     print(grads[t2])

分布式优化器

有关分布式优化器的文档,请参阅 torch.distributed.optim 页面。

设计说明

分布式 autograd 设计说明涵盖了基于 RPC 的分布式 autograd 框架的设计,该框架对于模型并行训练等应用非常有用。

RRef 设计说明涵盖了 RRef (远程引用) 协议的设计,该协议用于通过框架引用远程工作进程上的值。

教程

RPC 教程向用户介绍了 RPC 框架,提供了使用 torch.distributed.rpc API 的多个示例应用程序,并演示了如何使用 profiler 来分析基于 RPC 的工作负载。

文档

访问 PyTorch 的综合开发者文档

查看文档

教程

获取面向初学者和高级开发者的深度教程

查看教程

资源

查找开发资源并获得您的问题解答

查看资源