分布式通信包 - torch.distributed¶
注意
请参考 PyTorch 分布式概述,了解有关分布式训练的所有功能的简要介绍。
后端¶
torch.distributed
支持三种内置后端,每种后端具有不同的功能。下表显示了哪些函数可用于 CPU / CUDA 张量。如果用于构建 PyTorch 的实现支持 MPI,则 MPI 支持 CUDA。
后端 |
|
|
|
|||
---|---|---|---|---|---|---|
设备 |
CPU |
GPU |
CPU |
GPU |
CPU |
GPU |
send |
✓ |
✘ |
✓ |
? |
✘ |
✓ |
recv |
✓ |
✘ |
✓ |
? |
✘ |
✓ |
broadcast |
✓ |
✓ |
✓ |
? |
✘ |
✓ |
all_reduce |
✓ |
✓ |
✓ |
? |
✘ |
✓ |
reduce |
✓ |
✘ |
✓ |
? |
✘ |
✓ |
all_gather |
✓ |
✘ |
✓ |
? |
✘ |
✓ |
gather |
✓ |
✘ |
✓ |
? |
✘ |
✓ |
scatter |
✓ |
✘ |
✓ |
? |
✘ |
✓ |
reduce_scatter |
✘ |
✘ |
✘ |
✘ |
✘ |
✓ |
all_to_all |
✘ |
✘ |
✓ |
? |
✘ |
✓ |
障碍 |
✓ |
✘ |
✓ |
? |
✘ |
✓ |
PyTorch 附带的后端¶
PyTorch 分布式包支持 Linux(稳定版)、MacOS(稳定版)和 Windows(原型版)。默认情况下,对于 Linux,Gloo 和 NCCL 后端是在 PyTorch 分布式中构建并包含的(仅当使用 CUDA 构建时才包含 NCCL)。MPI 是一个可选的后端,只有在从源代码构建 PyTorch 时才能包含。(例如,在安装了 MPI 的主机上构建 PyTorch。)
注意
从 PyTorch v1.8 开始,Windows 支持所有集体通信后端,但 NCCL 除外。如果 init_method init_process_group()
的参数指向一个文件,则该文件必须符合以下模式
本地文件系统,
init_method="file:///d:/tmp/some_file"
共享文件系统,
init_method="file://////{machine_name}/{share_folder_name}/some_file"
与 Linux 平台相同,您可以通过设置环境变量 MASTER_ADDR 和 MASTER_PORT 来启用 TcpStore。
使用哪个后端?¶
过去,我们经常被问到:“我应该使用哪个后端?”
经验法则
对于分布式 **GPU** 训练,使用 NCCL 后端
对于分布式 **CPU** 训练,使用 Gloo 后端。
具有 InfiniBand 互连的 GPU 主机
使用 NCCL,因为它是目前唯一支持 InfiniBand 和 GPUDirect 的后端。
具有以太网互连的 GPU 主机
使用 NCCL,因为它目前提供了最佳的分布式 GPU 训练性能,特别是对于多进程单节点或多节点分布式训练。如果您在使用 NCCL 时遇到任何问题,请使用 Gloo 作为备用选项。(请注意,Gloo 目前运行速度比 NCCL 慢,适用于 GPU。)
具有 InfiniBand 互连的 CPU 主机
如果您的 InfiniBand 已启用 IP over IB,请使用 Gloo,否则请使用 MPI。我们计划在即将发布的版本中添加对 Gloo 的 InfiniBand 支持。
具有以太网互连的 CPU 主机
使用 Gloo,除非您有使用 MPI 的特定原因。
常见环境变量¶
选择要使用的网络接口¶
默认情况下,NCCL 和 Gloo 后端都会尝试找到要使用的正确网络接口。如果自动检测到的接口不正确,您可以使用以下环境变量覆盖它(适用于各自的后端)
**NCCL_SOCKET_IFNAME**,例如
export NCCL_SOCKET_IFNAME=eth0
**GLOO_SOCKET_IFNAME**,例如
export GLOO_SOCKET_IFNAME=eth0
如果您使用 Gloo 后端,可以通过逗号分隔多个接口来指定它们,例如:export GLOO_SOCKET_IFNAME=eth0,eth1,eth2,eth3
。后端将在这些接口之间以循环方式调度操作。所有进程必须在此变量中指定相同数量的接口。
其他 NCCL 环境变量¶
调试 - 如果 NCCL 失败,您可以设置 NCCL_DEBUG=INFO
以打印明确的警告消息以及基本的 NCCL 初始化信息。
您也可以使用 NCCL_DEBUG_SUBSYS
来获取有关 NCCL 特定方面的更多详细信息。例如,NCCL_DEBUG_SUBSYS=COLL
将打印集体调用的日志,这在调试挂起时可能会有所帮助,尤其是那些由集体类型或消息大小不匹配引起的挂起。如果拓扑检测失败,设置 NCCL_DEBUG_SUBSYS=GRAPH
来检查详细的检测结果并保存为参考,如果需要 NCCL 团队的进一步帮助,将会有所帮助。
性能调优 - NCCL 根据其拓扑检测执行自动调优,以节省用户的调优工作。在某些基于套接字的系统上,用户仍然可以尝试调整 NCCL_SOCKET_NTHREADS
和 NCCL_NSOCKS_PERTHREAD
以提高套接字网络带宽。这两个环境变量已由 NCCL 为某些云提供商(如 AWS 或 GCP)预先调整。
有关 NCCL 环境变量的完整列表,请参阅 NVIDIA NCCL 的官方文档
基础¶
The torch.distributed 包为 PyTorch 提供支持和通信原语,用于跨运行在一台或多台机器上的多个计算节点进行多进程并行。类 torch.nn.parallel.DistributedDataParallel()
在此功能的基础上构建,以提供同步分布式训练作为任何 PyTorch 模型的包装器。这与 多进程包 - torch.multiprocessing 和 torch.nn.DataParallel()
提供的并行类型不同,因为它支持多个网络连接的机器,并且用户必须显式地为每个进程启动主训练脚本的单独副本。
在单机同步的情况下,torch.distributed 或 torch.nn.parallel.DistributedDataParallel()
包装器可能仍然比其他数据并行方法(包括 torch.nn.DataParallel()
)具有优势。
每个进程维护自己的优化器,并在每次迭代中执行完整的优化步骤。虽然这可能看起来是多余的,因为梯度已经收集在一起并在进程之间平均,因此对于每个进程都是相同的,但这意味着不需要参数广播步骤,从而减少了在节点之间传输张量所花费的时间。
每个进程包含一个独立的 Python 解释器,消除了从单个 Python 进程驱动多个执行线程、模型副本或 GPU 所带来的额外解释器开销和“GIL 争用”。这对那些大量使用 Python 运行时的模型尤其重要,包括具有循环层或许多小组件的模型。
初始化¶
在调用任何其他方法之前,需要使用 torch.distributed.init_process_group()
或 torch.distributed.device_mesh.init_device_mesh()
函数初始化该包。两者都会阻塞,直到所有进程都加入。
- torch.distributed.is_available()[source]¶
如果分布式包可用,则返回
True
。否则,
torch.distributed
不公开任何其他 API。目前,torch.distributed
可在 Linux、MacOS 和 Windows 上使用。在从源代码构建 PyTorch 时,设置USE_DISTRIBUTED=1
以启用它。目前,Linux 和 Windows 的默认值为USE_DISTRIBUTED=1
,MacOS 的默认值为USE_DISTRIBUTED=0
。- 返回类型
- torch.distributed.init_process_group(backend=None, init_method=None, timeout=None, world_size=-1, rank=-1, store=None, group_name='', pg_options=None, device_id=None)[source]¶
初始化默认的分布式进程组。
这也会初始化分布式包。
- 初始化进程组主要有两种方法
显式指定
store
、rank
和world_size
。指定
init_method
(一个 URL 字符串),它指示在哪里/如何发现对等方。可以选择指定rank
和world_size
,或者将所有必需参数编码到 URL 中并省略它们。
如果未指定,则假定
init_method
为“env://”。- 参数
backend (str 或 Backend, 可选) – 要使用的后端。根据构建时配置,有效值包括
mpi
、gloo
、nccl
和ucc
。如果未提供后端,则将创建gloo
和nccl
后端,有关如何管理多个后端的详细信息,请参见下面的说明。此字段可以作为小写字符串(例如,"gloo"
)给出,也可以通过Backend
属性(例如,Backend.GLOO
)访问。如果使用nccl
后端在每台机器上运行多个进程,则每个进程必须独占访问它使用的所有 GPU,因为在进程之间共享 GPU 会导致死锁。ucc
后端处于实验阶段。init_method (str, 可选) – 指定如何初始化进程组的 URL。如果未指定
init_method
或store
,则默认为“env://”。与store
互斥。world_size (int, 可选) – 参与作业的进程数。如果指定了
store
,则需要此参数。rank (int, 可选) – 当前进程的排名(它应该是一个介于 0 和
world_size
-1 之间的数字)。如果指定了store
,则需要此参数。store (Store, 可选) – 所有工作进程都可以访问的键值存储,用于交换连接/地址信息。与
init_method
互斥。timeout (timedelta, 可选) – 对进程组执行的操作的超时时间。默认值为 NCCL 的 10 分钟和其它后端的 30 分钟。这是集体操作在异步中止后进程崩溃的持续时间。这是因为 CUDA 执行是异步的,并且在失败的异步 NCCL 操作可能导致后续 CUDA 操作在损坏的数据上运行的情况下,继续执行用户代码不再安全。当设置 TORCH_NCCL_BLOCKING_WAIT 时,进程将阻塞并等待此超时时间。
group_name (str, 可选, 已弃用) – 组名。此参数被忽略
pg_options (ProcessGroupOptions, 可选) – 进程组选项,指定在构建特定进程组期间需要传递的额外选项。截至目前,我们支持的唯一选项是
ProcessGroupNCCL.Options
用于nccl
后端,可以指定is_high_priority_stream
,以便 nccl 后端在有计算内核等待时可以选择高优先级 cuda 流。device_id (torch.device, 可选) – 一个特定的设备,用于“绑定”此进程,从而允许特定于后端的优化。目前,这只有在 NCCL 下才会产生两种影响:通信器会立即形成(立即调用
ncclCommInit*
而不是正常的延迟调用),并且子组将在可能的情况下使用ncclCommSplit
以避免不必要的组创建开销。如果您想尽早了解 NCCL 初始化错误,也可以使用此字段。
注意
要启用
backend == Backend.MPI
,PyTorch 需要在支持 MPI 的系统上从源代码构建。注意
对多个后端的支持处于实验阶段。目前,如果未指定后端,将创建
gloo
和nccl
两种后端。gloo
后端将用于具有 CPU 张量的集体操作,而nccl
后端将用于具有 CUDA 张量的集体操作。可以通过传递格式为 “<device_type>:<backend_name>,<device_type>:<backend_name>” 的字符串来指定自定义后端,例如 “cpu:gloo,cuda:custom_backend”。
- torch.distributed.device_mesh.init_device_mesh(device_type, mesh_shape, *, mesh_dim_names=None)[source]¶
根据 device_type、mesh_shape 和 mesh_dim_names 参数初始化一个 DeviceMesh。
这将创建一个具有 n 维数组布局的 DeviceMesh,其中 n 是 mesh_shape 的长度。如果提供了 mesh_dim_names,则每个维度将被标记为 mesh_dim_names[i]。
注意
init_device_mesh 遵循 SPMD 编程模型,这意味着相同的 PyTorch Python 程序在集群中的所有进程/秩上运行。确保 mesh_shape(描述设备布局的 nD 数组的维度)在所有秩上都相同。不一致的 mesh_shape 可能导致挂起。
注意
如果没有找到进程组,init_device_mesh 将在幕后初始化分布式进程组/组,这些组是分布式通信所必需的。
- 参数
- 返回值
一个
DeviceMesh
对象,表示设备布局。- 返回类型
- 示例:
>>> from torch.distributed.device_mesh import init_device_mesh >>> >>> mesh_1d = init_device_mesh("cuda", mesh_shape=(8,)) >>> mesh_2d = init_device_mesh("cuda", mesh_shape=(2, 8), mesh_dim_names=("dp", "tp"))
- torch.distributed.is_torchelastic_launched()[source]¶
检查当前进程是否使用
torch.distributed.elastic
(也称为 torchelastic) 启动。环境变量
TORCHELASTIC_RUN_ID
的存在被用作代理来确定当前进程是否使用 torchelastic 启动。这是一个合理的代理,因为TORCHELASTIC_RUN_ID
映射到 rendezvous id,它始终是一个非空值,表示用于对等发现的作业 ID。- 返回类型
目前支持三种初始化方法
TCP 初始化¶
使用 TCP 初始化有两种方法,都需要一个所有进程都可以访问的网络地址和一个所需的 world_size
。第一种方法需要指定一个属于排名 0 进程的地址。这种初始化方法要求所有进程都手动指定了排名。
请注意,最新分布式包不再支持组播地址。 group_name
也已弃用。
import torch.distributed as dist
# Use address of one of the machines
dist.init_process_group(backend, init_method='tcp://10.1.1.20:23456',
rank=args.rank, world_size=4)
环境变量初始化¶
此方法将从环境变量中读取配置,允许您完全自定义如何获取信息。要设置的变量是
MASTER_PORT
- 必需;必须是排名为 0 的机器上的空闲端口MASTER_ADDR
- 必需(排名为 0 的节点除外);排名为 0 的节点的地址WORLD_SIZE
- 必需;可以在这里设置,也可以在调用 init 函数时设置RANK
- 必需;可以在这里设置,也可以在调用 init 函数时设置
排名为 0 的机器将用于设置所有连接。
这是默认方法,这意味着不需要指定 init_method
(或者可以是 env://
)。
初始化后¶
一旦运行了 torch.distributed.init_process_group()
,就可以使用以下函数。要检查进程组是否已初始化,请使用 torch.distributed.is_initialized()
。
- class torch.distributed.Backend(name)[source]¶
用于后端的类似枚举的类。
可用的后端:GLOO、NCCL、UCC、MPI 和其他已注册的后端。
此类的值是小写字符串,例如
"gloo"
。它们可以作为属性访问,例如Backend.NCCL
。此类可以直接调用以解析字符串,例如
Backend(backend_str)
将检查backend_str
是否有效,如果有效,则返回解析后的一个小写字符串。它还接受大写字符串,例如Backend("GLOO")
返回"gloo"
。注意
条目
Backend.UNDEFINED
存在,但仅用作某些字段的初始值。用户既不应该直接使用它,也不应该假设它的存在。- classmethod register_backend(name, func, extended_api=False, devices=None)[source]¶
使用给定的名称和实例化函数注册一个新的后端。
此类方法由第三方
ProcessGroup
扩展用于注册新的后端。- 参数
name (str) –
ProcessGroup
扩展的后端名称。它应该与init_process_group()
中的名称匹配。func (function) – 实例化后端的函数处理程序。该函数应在后端扩展中实现,并接受四个参数,包括
store
、rank
、world_size
和timeout
。extended_api (bool, optional) – 后端是否支持扩展参数结构。默认值:
False
。如果设置为True
,后端将获得c10d::DistributedBackendOptions
的实例,以及由后端实现定义的进程组选项对象。device (str 或 list of str, optional) – 此后端支持的设备类型,例如“cpu”、“cuda”等。如果为 None,则假定为“cpu”和“cuda”
注意
对第三方后端的支持是实验性的,可能会发生变化。
- torch.distributed.get_backend(group=None)[source]¶
返回给定进程组的后端。
- 参数
group (ProcessGroup, optional) – 要操作的进程组。默认情况下为通用主进程组。如果指定了另一个特定组,则调用进程必须是
group
的一部分。- 返回值
给定进程组的后端,以小写字符串形式表示。
- 返回类型
分布式键值存储¶
分布式包附带一个分布式键值存储,可用于在组中的进程之间共享信息,以及在 torch.distributed.init_process_group()
中初始化分布式包(通过显式创建存储作为指定 init_method
的替代方法)。键值存储有 3 种选择:TCPStore
、FileStore
和 HashStore
。
- class torch.distributed.TCPStore¶
基于 TCP 的分布式键值存储实现。服务器存储保存数据,而客户端存储可以通过 TCP 连接到服务器存储并执行操作,例如
set()
插入键值对,get()
检索键值对等。始终应该初始化一个服务器存储,因为客户端存储将等待服务器建立连接。- 参数
host_name (str) – 服务器存储应运行的主机名或 IP 地址。
port (int) – 服务器存储监听传入请求的端口。
world_size (int, optional) – 存储用户的总数(客户端数量 + 服务器数量 1)。默认值为 None(None 表示存储用户数量不固定)。
is_master (bool, optional) – 初始化服务器存储时为 True,客户端存储时为 False。默认值为 False。
timeout (timedelta, optional) – 存储在初始化期间以及
get()
和wait()
等方法中使用的超时时间。默认值为 timedelta(seconds=300)wait_for_workers (bool, optional) – 是否等待所有工作进程连接到服务器存储。这仅在 world_size 为固定值时适用。默认值为 True。
multi_tenant (bool, optional) – 如果为 True,则当前进程中具有相同主机/端口的所有
TCPStore
实例将使用相同的底层TCPServer
。默认值为 False。master_listen_fd (int, optional) – 如果指定,底层
TCPServer
将监听此文件描述符,该描述符必须是已绑定到port
的套接字。在某些情况下,这有助于避免端口分配竞争。默认值为 None(表示服务器创建一个新的套接字并尝试将其绑定到port
)。
- 示例:
>>> import torch.distributed as dist >>> from datetime import timedelta >>> # Run on process 1 (server) >>> server_store = dist.TCPStore("127.0.0.1", 1234, 2, True, timedelta(seconds=30)) >>> # Run on process 2 (client) >>> client_store = dist.TCPStore("127.0.0.1", 1234, 2, False) >>> # Use any of the store methods from either the client or server after initialization >>> server_store.set("first_key", "first_value") >>> client_store.get("first_key")
- class torch.distributed.HashStore¶
基于底层哈希映射的线程安全存储实现。此存储可以在同一个进程内使用(例如,由其他线程使用),但不能跨进程使用。
- 示例:
>>> import torch.distributed as dist >>> store = dist.HashStore() >>> # store can be used from other threads >>> # Use any of the store methods after initialization >>> store.set("first_key", "first_value")
- class torch.distributed.FileStore¶
一种使用文件存储底层键值对的存储实现。
- 示例:
>>> import torch.distributed as dist >>> store1 = dist.FileStore("/tmp/filestore", 2) >>> store2 = dist.FileStore("/tmp/filestore", 2) >>> # Use any of the store methods from either the client or server after initialization >>> store1.set("first_key", "first_value") >>> store2.get("first_key")
- class torch.distributed.PrefixStore¶
围绕 3 个键值存储(
TCPStore
、FileStore
和HashStore
)的包装器,它为插入存储的每个键添加一个前缀。- 参数
prefix (str) – 在插入存储之前附加到每个键的前缀字符串。
store (torch.distributed.store) – 构成底层键值存储的存储对象。
- torch.distributed.Store.set(self: torch._C._distributed_c10d.Store, arg0: str, arg1: str) None ¶
根据提供的
key
和value
将键值对插入到存储中。如果key
已经存在于存储中,它将用新提供的value
覆盖旧值。- 示例:
>>> import torch.distributed as dist >>> from datetime import timedelta >>> store = dist.TCPStore("127.0.0.1", 0, 1, True, timedelta(seconds=30)) >>> store.set("first_key", "first_value") >>> # Should return "first_value" >>> store.get("first_key")
- torch.distributed.Store.get(self: torch._C._distributed_c10d.Store, arg0: str) bytes ¶
检索存储中与给定
key
关联的值。如果key
不存在于存储中,该函数将等待在初始化存储时定义的timeout
,然后抛出异常。- 参数
key (str) – 该函数将返回与该键关联的值。
- 返回值
如果
key
存在于存储中,则与key
关联的值。
- 示例:
>>> import torch.distributed as dist >>> from datetime import timedelta >>> store = dist.TCPStore("127.0.0.1", 0, 1, True, timedelta(seconds=30)) >>> store.set("first_key", "first_value") >>> # Should return "first_value" >>> store.get("first_key")
- torch.distributed.Store.add(self: torch._C._distributed_c10d.Store, arg0: str, arg1: int) int ¶
第一次针对给定
key
调用 add 会在存储中创建一个与key
关联的计数器,并将其初始化为amount
。之后使用相同key
调用 add 会将计数器增加指定的amount
。如果使用add()
调用一个已经在存储中通过set()
设置的键,则会导致异常。- 示例:
>>> import torch.distributed as dist >>> from datetime import timedelta >>> # Using TCPStore as an example, other store types can also be used >>> store = dist.TCPStore("127.0.0.1", 0, 1, True, timedelta(seconds=30)) >>> store.add("first_key", 1) >>> store.add("first_key", 6) >>> # Should return 7 >>> store.get("first_key")
- torch.distributed.Store.compare_set(self: torch._C._distributed_c10d.Store, arg0: str, arg1: str, arg2: str) bytes ¶
根据提供的
key
将键值对插入存储,并在插入之前对expected_value
和desired_value
进行比较。只有当key
的expected_value
已经存在于存储中,或者expected_value
为空字符串时,才会设置desired_value
。- 参数
- 示例:
>>> import torch.distributed as dist >>> from datetime import timedelta >>> store = dist.TCPStore("127.0.0.1", 0, 1, True, timedelta(seconds=30)) >>> store.set("key", "first_value") >>> store.compare_set("key", "first_value", "second_value") >>> # Should return "second_value" >>> store.get("key")
- torch.distributed.Store.wait(*args, **kwargs)¶
重载函数。
wait(self: torch._C._distributed_c10d.Store, arg0: list[str]) -> None
等待
keys
中的每个键添加到存储中。如果在timeout
(在存储初始化期间设置)之前未设置所有键,则wait
将抛出异常。- 参数
keys (list) – 要等待的键列表,直到它们在存储中设置。
- 示例:
>>> import torch.distributed as dist >>> from datetime import timedelta >>> # Using TCPStore as an example, other store types can also be used >>> store = dist.TCPStore("127.0.0.1", 0, 1, True, timedelta(seconds=30)) >>> # This will throw an exception after 30 seconds >>> store.wait(["bad_key"])
wait(self: torch._C._distributed_c10d.Store, arg0: list[str], arg1: datetime.timedelta) -> None
等待
keys
中的每个键添加到存储中,如果在提供的timeout
内未设置键,则抛出异常。- 参数
keys (list) – 要等待的键列表,直到它们在存储中设置。
timeout (timedelta) – 等待键添加的时间,超过此时间将抛出异常。
- 示例:
>>> import torch.distributed as dist >>> from datetime import timedelta >>> # Using TCPStore as an example, other store types can also be used >>> store = dist.TCPStore("127.0.0.1", 0, 1, True, timedelta(seconds=30)) >>> # This will throw an exception after 10 seconds >>> store.wait(["bad_key"], timedelta(seconds=10))
- torch.distributed.Store.num_keys(self: torch._C._distributed_c10d.Store) int ¶
返回存储中设置的键的数量。请注意,此数字通常比
set()
和add()
添加的键的数量多一个,因为一个键用于协调使用存储的所有工作进程。警告
当与
TCPStore
一起使用时,num_keys
返回写入底层文件的键数。如果存储被销毁,并且另一个存储使用相同的文件创建,则原始键将被保留。- 返回值
存储中存在的键数。
- 示例:
>>> import torch.distributed as dist >>> from datetime import timedelta >>> # Using TCPStore as an example, other store types can also be used >>> store = dist.TCPStore("127.0.0.1", 0, 1, True, timedelta(seconds=30)) >>> store.set("first_key", "first_value") >>> # This should return 2 >>> store.num_keys()
- torch.distributed.Store.delete_key(self: torch._C._distributed_c10d.Store, arg0: str) bool ¶
从存储中删除与
key
关联的键值对。如果键成功删除,则返回 true,否则返回 false。- 参数
key (str) – 要从存储中删除的键
- 返回值
如果
key
被删除,则为 True,否则为 False。
- 示例:
>>> import torch.distributed as dist >>> from datetime import timedelta >>> # Using TCPStore as an example, HashStore can also be used >>> store = dist.TCPStore("127.0.0.1", 0, 1, True, timedelta(seconds=30)) >>> store.set("first_key") >>> # This should return true >>> store.delete_key("first_key") >>> # This should return false >>> store.delete_key("bad_key")
- torch.distributed.Store.set_timeout(self: torch._C._distributed_c10d.Store, arg0: datetime.timedelta) None ¶
设置存储的默认超时时间。此超时时间在初始化以及
wait()
和get()
中使用。- 参数
timeout (timedelta) – 要在存储中设置的超时时间。
- 示例:
>>> import torch.distributed as dist >>> from datetime import timedelta >>> # Using TCPStore as an example, other store types can also be used >>> store = dist.TCPStore("127.0.0.1", 0, 1, True, timedelta(seconds=30)) >>> store.set_timeout(timedelta(seconds=10)) >>> # This will throw an exception after 10 seconds >>> store.wait(["bad_key"])
组¶
默认情况下,集体操作在默认组(也称为世界)上进行,并要求所有进程进入分布式函数调用。但是,某些工作负载可以从更细粒度的通信中受益。这就是分布式组发挥作用的地方。 new_group()
函数可用于创建新的组,其中包含所有进程的任意子集。它返回一个不透明的组句柄,可以作为 group
参数传递给所有集体操作(集体操作是分布式函数,用于以某些众所周知的编程模式交换信息)。
- torch.distributed.new_group(ranks=None, timeout=None, backend=None, pg_options=None, use_local_synchronization=False)[source]¶
创建一个新的分布式组。
此函数要求主组中的所有进程(即分布式作业中所有进程)都进入此函数,即使它们不打算成为组成员。此外,所有进程中应按相同顺序创建组。
警告
同时使用多个进程组和
NCCL
后端是不安全的,用户应在其应用程序中执行显式同步以确保一次只使用一个进程组。这意味着来自一个进程组的集体操作应在设备上完成执行(不仅仅是排队,因为 CUDA 执行是异步的),然后才能将来自另一个进程组的集体操作排队。有关更多详细信息,请参阅 同时使用多个 NCCL 通信器。- 参数
timeout (timedelta, optional) – 有关详细信息和默认值,请参阅 init_process_group。
backend (str or Backend, optional) – 要使用的后端。根据构建时配置,有效值为
gloo
和nccl
。默认情况下使用与全局组相同的后端。此字段应作为小写字符串给出(例如,"gloo"
),也可以通过Backend
属性访问(例如,Backend.GLOO
)。如果传入None
,将使用与默认进程组对应的后端。默认值为None
。pg_options (ProcessGroupOptions, 可选) – 进程组选项,指定在构建特定进程组时需要传递的额外选项。例如,对于
nccl
后端,可以指定is_high_priority_stream
,以便进程组可以获取高优先级 cuda 流。use_local_synchronization (bool, 可选) – 在进程组创建结束时执行一个组本地屏障。这与非成员秩不需要调用 API 且不加入屏障不同。
- 返回值
一个分布式组的句柄,可以传递给集体调用,如果秩不是
ranks
的一部分,则为 None。
注意:use_local_synchronization 不适用于 MPI。
注意:虽然 use_local_synchronization=True 在更大的集群和小进程组中可以显著更快,但必须小心,因为它会改变集群行为,因为非成员秩不会加入组屏障 ()。
注意:当每个秩创建多个重叠的进程组时,use_local_synchronization=True 会导致死锁。为了避免这种情况,请确保所有秩都遵循相同的全局创建顺序。
- torch.distributed.get_group_rank(group, global_rank)[source]¶
将全局秩转换为组秩。
global_rank
必须是group
的一部分,否则会引发 RuntimeError。- 参数
group (ProcessGroup) – 用于查找相对秩的进程组。
global_rank (int) – 要查询的全局秩。
- 返回值
global_rank
相对于group
的组秩- 返回类型
注意:在默认进程组上调用此函数会返回身份
DeviceMesh¶
DeviceMesh 是一个更高级别的抽象,它管理进程组(或 NCCL 通信器)。它允许用户轻松创建节点间和节点内进程组,而无需担心如何为不同的子进程组正确设置排名,并且它有助于轻松管理这些分布式进程组。 init_device_mesh()
函数可用于创建新的 DeviceMesh,其中网格形状描述了设备拓扑。
- class torch.distributed.device_mesh.DeviceMesh(device_type, mesh, *, mesh_dim_names=None, _init_backend=True)[source]¶
DeviceMesh 代表一个设备网格,其中设备的布局可以表示为一个 n 维数组,n 维数组的每个值都是默认进程组等级的全局 ID。
DeviceMesh 可用于描述整个集群中设备的布局,并充当集群内设备列表之间通信的代理。
DeviceMesh 可用作上下文管理器。
注意
DeviceMesh 遵循 SPMD 编程模型,这意味着相同的 PyTorch Python 程序在集群中的所有进程/等级上运行。因此,用户需要确保 mesh 数组(描述设备布局)在所有等级上都相同。不一致的 mesh 会导致静默挂起。
- 参数
device_type (str) – 网格的设备类型。目前支持: “cpu”、 “cuda/cuda-like”。
mesh (ndarray) – 一个多维数组或一个整数张量,描述设备的布局,其中 ID 是默认进程组的全局 ID。
- 返回值
一个
DeviceMesh
对象,表示设备布局。- 返回类型
以下程序以 SPMD 方式在每个进程/等级上运行。在本例中,我们有 2 个主机,每个主机有 4 个 GPU。对 mesh 的第一维进行归约将在列 (0, 4) .. 和 (3, 7) 上进行归约,对 mesh 的第二维进行归约将在行 (0, 1, 2, 3) 和 (4, 5, 6, 7) 上进行归约。
- 示例:
>>> from torch.distributed.device_mesh import DeviceMesh >>> >>> # Initialize device mesh as (2, 4) to represent the topology >>> # of cross-host(dim 0), and within-host (dim 1). >>> mesh = DeviceMesh(device_type="cuda", mesh=[[0, 1, 2, 3],[4, 5, 6, 7]])
点对点通信¶
isend()
和 irecv()
在使用时返回分布式请求对象。通常,此对象的类型未指定,因为它们永远不应该手动创建,但它们保证支持两种方法
is_completed()
- 如果操作已完成,则返回 Truewait()
- 将阻塞进程,直到操作完成。is_completed()
在返回后保证返回 True。
- torch.distributed.isend(tensor, dst, group=None, tag=0)[source]¶
异步发送张量。
警告
在请求完成之前修改
tensor
会导致未定义的行为。警告
tag
在 NCCL 后端不支持。
- torch.distributed.batch_isend_irecv(p2p_op_list)[source]¶
异步发送或接收一批张量并返回请求列表。
处理
p2p_op_list
中的每个操作并返回相应的请求。目前支持 NCCL、Gloo 和 UCC 后端。- 参数
p2p_op_list – 点对点操作列表(每个操作的类型为
torch.distributed.P2POp
)。列表中 isend/irecv 的顺序很重要,需要与远程端对应的 isend/irecv 匹配。- 返回值
通过在 op_list 中调用相应操作返回的分布式请求对象列表。
示例
>>> send_tensor = torch.arange(2, dtype=torch.float32) + 2 * rank >>> recv_tensor = torch.randn(2, dtype=torch.float32) >>> send_op = dist.P2POp(dist.isend, send_tensor, (rank + 1)%world_size) >>> recv_op = dist.P2POp(dist.irecv, recv_tensor, (rank - 1 + world_size)%world_size) >>> reqs = batch_isend_irecv([send_op, recv_op]) >>> for req in reqs: >>> req.wait() >>> recv_tensor tensor([2, 3]) # Rank 0 tensor([0, 1]) # Rank 1
注意
请注意,当此 API 与 NCCL PG 后端一起使用时,用户必须使用 torch.cuda.set_device 设置当前 GPU 设备,否则会导致意外挂起问题。
此外,如果此 API 是传递给
dist.P2POp
的group
中的第一个集体调用,则group
的所有秩都必须参与此 API 调用;否则,行为未定义。如果此 API 调用不是group
中的第一个集体调用,则允许仅涉及group
的部分秩的批量 P2P 操作。
同步和异步集体操作¶
每个集体操作函数都支持以下两种操作,具体取决于传递到集体中的 async_op
标志的设置。
同步操作 - 默认模式,当 async_op
设置为 False
时。当函数返回时,保证集体操作已执行。在 CUDA 操作的情况下,不保证 CUDA 操作已完成,因为 CUDA 操作是异步的。对于 CPU 集体操作,任何进一步使用集体调用输出的函数调用将按预期执行。对于 CUDA 集体操作,在同一 CUDA 流上使用输出的函数调用将按预期执行。用户必须注意在不同流下运行时的同步。有关 CUDA 语义(如流同步)的详细信息,请参阅 CUDA 语义。请参阅以下脚本以查看这些语义在 CPU 和 CUDA 操作中的差异示例。
异步操作 - 当 async_op
设置为 True 时。集体操作函数返回一个分布式请求对象。通常,您不需要手动创建它,并且保证它支持两种方法。
is_completed()
- 在 CPU 集体操作的情况下,如果已完成则返回True
。在 CUDA 操作的情况下,如果操作已成功排队到 CUDA 流中,并且可以在默认流上使用输出而无需进一步同步,则返回True
。wait()
- 在 CPU 集体操作的情况下,将阻塞进程,直到操作完成。在 CUDA 集体操作的情况下,将阻塞,直到操作已成功排队到 CUDA 流中,并且可以在默认流上使用输出而无需进一步同步。get_future()
- 返回torch._C.Future
对象。支持 NCCL,也支持大多数 GLOO 和 MPI 操作,除了点对点操作。注意:随着我们继续采用 Futures 并合并 API,get_future()
调用可能会变得多余。
示例
以下代码可以作为使用分布式集体操作时 CUDA 操作语义的参考。它显示了在不同 CUDA 流上使用集体输出时显式同步的必要性
# Code runs on each rank.
dist.init_process_group("nccl", rank=rank, world_size=2)
output = torch.tensor([rank]).cuda(rank)
s = torch.cuda.Stream()
handle = dist.all_reduce(output, async_op=True)
# Wait ensures the operation is enqueued, but not necessarily complete.
handle.wait()
# Using result on non-default stream.
with torch.cuda.stream(s):
s.wait_stream(torch.cuda.default_stream())
output.add_(100)
if rank == 0:
# if the explicit call to wait_stream was omitted, the output below will be
# non-deterministically 1 or 101, depending on whether the allreduce overwrote
# the value after the add completed.
print(output)
集体函数¶
- torch.distributed.broadcast(tensor, src, group=None, async_op=False)[source]¶
将张量广播到整个组。
tensor
在参与集体操作的所有进程中必须具有相同数量的元素。
- torch.distributed.broadcast_object_list(object_list, src=0, group=None, device=None)[源代码]¶
将
object_list
中的可腌制对象广播到整个组。类似于
broadcast()
,但可以传入 Python 对象。请注意,object_list
中的所有对象都必须是可腌制的才能进行广播。- 参数
object_list (List[Any]) – 要广播的输入对象列表。每个对象都必须是可腌制的。只有
src
等级上的对象会被广播,但每个等级都必须提供大小相同的列表。src (int) – 用于广播
object_list
的源等级。源等级基于全局进程组(与group
参数无关)group – (ProcessGroup, 可选): 要操作的进程组。如果为 None,则将使用默认进程组。默认值为
None
。设备 (
torch.device
, 可选) – 如果不为 None,则对象会被序列化并转换为张量,然后在广播之前移动到device
。默认值为None
。
- 返回值
None
。如果排名是组的一部分,object_list
将包含来自src
排名的广播对象。
注意
对于基于 NCCL 的进程组,对象的内部张量表示必须在通信发生之前移动到 GPU 设备。在这种情况下,使用的设备由
torch.cuda.current_device()
给出,用户有责任确保通过torch.cuda.set_device()
设置每个排名都有一个单独的 GPU。注意
请注意,此 API 与
all_gather()
集体操作略有不同,因为它不提供async_op
处理程序,因此将是阻塞调用。警告
broadcast_object_list()
隐式使用pickle
模块,该模块已知不安全。可以构造恶意 pickle 数据,这些数据将在解封期间执行任意代码。仅对您信任的数据调用此函数。警告
使用 GPU 张量调用
broadcast_object_list()
得不到很好的支持,效率低下,因为它会导致 GPU -> CPU 传输,因为张量将被腌制。请考虑使用broadcast()
代替。- 示例:
>>> # Note: Process group initialization omitted on each rank. >>> import torch.distributed as dist >>> if dist.get_rank() == 0: >>> # Assumes world_size of 3. >>> objects = ["foo", 12, {1: 2}] # any picklable object >>> else: >>> objects = [None, None, None] >>> # Assumes backend is not NCCL >>> device = torch.device("cpu") >>> dist.broadcast_object_list(objects, src=0, device=device) >>> objects ['foo', 12, {1: 2}]
- torch.distributed.all_reduce(tensor, op=<RedOpType.SUM: 0>, group=None, async_op=False)[source]¶
以所有机器都获得最终结果的方式减少所有机器上的张量数据。
调用
tensor
后,将在所有进程中按位相同。支持复数张量。
- 参数
- 返回值
异步工作句柄,如果 async_op 设置为 True。如果非 async_op 或不属于该组,则为 None
示例
>>> # All tensors below are of torch.int64 type. >>> # We have 2 process groups, 2 ranks. >>> device = torch.device(f'cuda:{rank}') >>> tensor = torch.arange(2, dtype=torch.int64, device=device) + 1 + 2 * rank >>> tensor tensor([1, 2], device='cuda:0') # Rank 0 tensor([3, 4], device='cuda:1') # Rank 1 >>> dist.all_reduce(tensor, op=ReduceOp.SUM) >>> tensor tensor([4, 6], device='cuda:0') # Rank 0 tensor([4, 6], device='cuda:1') # Rank 1
>>> # All tensors below are of torch.cfloat type. >>> # We have 2 process groups, 2 ranks. >>> tensor = torch.tensor([1+1j, 2+2j], dtype=torch.cfloat, device=device) + 2 * rank * (1+1j) >>> tensor tensor([1.+1.j, 2.+2.j], device='cuda:0') # Rank 0 tensor([3.+3.j, 4.+4.j], device='cuda:1') # Rank 1 >>> dist.all_reduce(tensor, op=ReduceOp.SUM) >>> tensor tensor([4.+4.j, 6.+6.j], device='cuda:0') # Rank 0 tensor([4.+4.j, 6.+6.j], device='cuda:1') # Rank 1
- torch.distributed.reduce(tensor, dst, op=<RedOpType.SUM: 0>, group=None, async_op=False)[source]¶
将张量数据在所有机器上进行约简。
只有排名为
dst
的进程将接收最终结果。
- torch.distributed.all_gather(tensor_list, tensor, group=None, async_op=False)[source]¶
将整个组中的张量收集到一个列表中。
支持复数张量。
- 参数
- 返回值
异步工作句柄,如果 async_op 设置为 True。如果非 async_op 或不属于该组,则为 None
示例
>>> # All tensors below are of torch.int64 dtype. >>> # We have 2 process groups, 2 ranks. >>> device = torch.device(f'cuda:{rank}') >>> tensor_list = [torch.zeros(2, dtype=torch.int64, device=device) for _ in range(2)] >>> tensor_list [tensor([0, 0], device='cuda:0'), tensor([0, 0], device='cuda:0')] # Rank 0 [tensor([0, 0], device='cuda:0'), tensor([0, 0], device='cuda:1')] # Rank 1 >>> tensor = torch.arange(2, dtype=torch.int64, device=device) + 1 + 2 * rank >>> tensor tensor([1, 2], device='cuda:0') # Rank 0 tensor([3, 4], device='cuda:1') # Rank 1 >>> dist.all_gather(tensor_list, tensor) >>> tensor_list [tensor([1, 2], device='cuda:0'), tensor([3, 4], device='cuda:0')] # Rank 0 [tensor([1, 2], device='cuda:1'), tensor([3, 4], device='cuda:1')] # Rank 1
>>> # All tensors below are of torch.cfloat dtype. >>> # We have 2 process groups, 2 ranks. >>> tensor_list = [torch.zeros(2, dtype=torch.cfloat, device=device) for _ in range(2)] >>> tensor_list [tensor([0.+0.j, 0.+0.j], device='cuda:0'), tensor([0.+0.j, 0.+0.j], device='cuda:0')] # Rank 0 [tensor([0.+0.j, 0.+0.j], device='cuda:1'), tensor([0.+0.j, 0.+0.j], device='cuda:1')] # Rank 1 >>> tensor = torch.tensor([1+1j, 2+2j], dtype=torch.cfloat, device=device) + 2 * rank * (1+1j) >>> tensor tensor([1.+1.j, 2.+2.j], device='cuda:0') # Rank 0 tensor([3.+3.j, 4.+4.j], device='cuda:1') # Rank 1 >>> dist.all_gather(tensor_list, tensor) >>> tensor_list [tensor([1.+1.j, 2.+2.j], device='cuda:0'), tensor([3.+3.j, 4.+4.j], device='cuda:0')] # Rank 0 [tensor([1.+1.j, 2.+2.j], device='cuda:1'), tensor([3.+3.j, 4.+4.j], device='cuda:1')] # Rank 1
- torch.distributed.all_gather_into_tensor(output_tensor, input_tensor, group=None, async_op=False)[source]¶
从所有秩收集张量并将它们放入单个输出张量中。
- 参数
output_tensor (Tensor) – 用于容纳所有秩的张量元素的输出张量。它必须具有正确的大小,才能具有以下形式之一:(i) 沿主维度连接所有输入张量;有关“连接”的定义,请参见
torch.cat()
;(ii) 沿主维度堆叠所有输入张量;有关“堆叠”的定义,请参见torch.stack()
。以下示例可以更好地解释支持的输出形式。input_tensor (Tensor) – 要从当前秩收集的张量。与
all_gather
API 不同,此 API 中的输入张量必须在所有秩上具有相同的大小。group (ProcessGroup, optional) – 要操作的进程组。如果为 None,则使用默认进程组。
async_op (bool, 可选) – 此操作是否应为异步操作
- 返回值
异步工作句柄,如果 async_op 设置为 True。如果非 async_op 或不属于该组,则为 None
示例
>>> # All tensors below are of torch.int64 dtype and on CUDA devices. >>> # We have two ranks. >>> device = torch.device(f'cuda:{rank}') >>> tensor_in = torch.arange(2, dtype=torch.int64, device=device) + 1 + 2 * rank >>> tensor_in tensor([1, 2], device='cuda:0') # Rank 0 tensor([3, 4], device='cuda:1') # Rank 1 >>> # Output in concatenation form >>> tensor_out = torch.zeros(world_size * 2, dtype=torch.int64, device=device) >>> dist.all_gather_into_tensor(tensor_out, tensor_in) >>> tensor_out tensor([1, 2, 3, 4], device='cuda:0') # Rank 0 tensor([1, 2, 3, 4], device='cuda:1') # Rank 1 >>> # Output in stack form >>> tensor_out2 = torch.zeros(world_size, 2, dtype=torch.int64, device=device) >>> dist.all_gather_into_tensor(tensor_out2, tensor_in) >>> tensor_out2 tensor([[1, 2], [3, 4]], device='cuda:0') # Rank 0 tensor([[1, 2], [3, 4]], device='cuda:1') # Rank 1
警告
Gloo 后端不支持此 API。
- torch.distributed.all_gather_object(object_list, obj, group=None)[source]¶
将整个组的可腌制对象收集到一个列表中。
类似于
all_gather()
,但可以传入 Python 对象。请注意,该对象必须是可腌制的才能被收集。- 参数
object_list (list[Any]) – 输出列表。它应该与该集体操作的组大小相同,并将包含输出。
obj (Any) – 要从当前进程广播的可腌制 Python 对象。
group (ProcessGroup, 可选) – 要操作的进程组。如果为 None,则使用默认进程组。默认值为
None
。
- 返回值
无。如果调用排名属于此组,则集合的输出将填充到输入
object_list
中。如果调用排名不属于此组,则传入的object_list
将保持不变。
注意
请注意,此 API 与
all_gather()
集体操作略有不同,因为它不提供async_op
处理程序,因此将是阻塞调用。注意
对于基于 NCCL 的处理组,对象的内部张量表示必须在通信发生之前移动到 GPU 设备。在这种情况下,使用的设备由
torch.cuda.current_device()
给出,用户有责任确保通过torch.cuda.set_device()
为每个排名设置一个单独的 GPU。警告
all_gather_object()
隐式使用pickle
模块,该模块已知不安全。可以构建恶意 pickle 数据,这些数据将在解封期间执行任意代码。仅对您信任的数据调用此函数。警告
使用 GPU 张量调用
all_gather_object()
支持不好,效率低下,因为它会导致 GPU -> CPU 传输,因为张量将被腌制。请考虑使用all_gather()
代替。- 示例:
>>> # Note: Process group initialization omitted on each rank. >>> import torch.distributed as dist >>> # Assumes world_size of 3. >>> gather_objects = ["foo", 12, {1: 2}] # any picklable object >>> output = [None for _ in gather_objects] >>> dist.all_gather_object(output, gather_objects[dist.get_rank()]) >>> output ['foo', 12, {1: 2}]
- torch.distributed.gather(tensor, gather_list=None, dst=0, group=None, async_op=False)[source]¶
在单个进程中收集张量列表。
- 参数
- 返回值
异步工作句柄,如果 async_op 设置为 True。如果非 async_op 或不属于该组,则为 None
- torch.distributed.gather_object(obj, object_gather_list=None, dst=0, group=None)[source]¶
在单个进程中从整个组收集可腌制对象。
类似于
gather()
,但可以传入 Python 对象。请注意,要收集的对象必须是可腌制的。- 参数
- 返回值
无。在
dst
秩上,object_gather_list
将包含集体的输出。
注意
请注意,此 API 与 gather 集体略有不同,因为它不提供 async_op 处理程序,因此将是阻塞调用。
注意
对于基于 NCCL 的处理组,对象的内部张量表示必须在通信发生之前移动到 GPU 设备。在这种情况下,使用的设备由
torch.cuda.current_device()
给出,用户有责任确保通过torch.cuda.set_device()
为每个排名设置一个单独的 GPU。警告
gather_object()
隐式使用pickle
模块,该模块已知不安全。可以构造恶意 pickle 数据,这些数据将在取消腌制期间执行任意代码。仅对您信任的数据调用此函数。警告
使用 GPU 张量调用
gather_object()
支持度不高且效率低下,因为它会造成 GPU -> CPU 传输,因为张量将被序列化。请考虑使用gather()
。- 示例:
>>> # Note: Process group initialization omitted on each rank. >>> import torch.distributed as dist >>> # Assumes world_size of 3. >>> gather_objects = ["foo", 12, {1: 2}] # any picklable object >>> output = [None for _ in gather_objects] >>> dist.gather_object( ... gather_objects[dist.get_rank()], ... output if dist.get_rank() == 0 else None, ... dst=0 ... ) >>> # On rank 0 >>> output ['foo', 12, {1: 2}]
- torch.distributed.scatter(tensor, scatter_list=None, src=0, group=None, async_op=False)[source]¶
将张量列表分散到组中的所有进程。
每个进程将接收一个张量,并将数据存储在
tensor
参数中。支持复数张量。
- 参数
- 返回值
异步工作句柄,如果 async_op 设置为 True。如果非 async_op 或不属于该组,则为 None
注意
请注意,scatter_list 中的所有张量必须具有相同的大小。
- 示例:
>>> # Note: Process group initialization omitted on each rank. >>> import torch.distributed as dist >>> tensor_size = 2 >>> t_ones = torch.ones(tensor_size) >>> t_fives = torch.ones(tensor_size) * 5 >>> output_tensor = torch.zeros(tensor_size) >>> if dist.get_rank() == 0: >>> # Assumes world_size of 2. >>> # Only tensors, all of which must be the same size. >>> scatter_list = [t_ones, t_fives] >>> else: >>> scatter_list = None >>> dist.scatter(output_tensor, scatter_list, src=0) >>> # Rank i gets scatter_list[i]. For example, on rank 1: >>> output_tensor tensor([5., 5.])
- torch.distributed.scatter_object_list(scatter_object_output_list, scatter_object_input_list, src=0, group=None)[source]¶
将
scatter_object_input_list
中的可序列化对象分散到整个组。类似于
scatter()
,但可以传入 Python 对象。在每个排名上,分散的对象将存储为scatter_object_output_list
的第一个元素。请注意,scatter_object_input_list
中的所有对象必须是可序列化的,才能被分散。- 参数
scatter_object_output_list (List[Any]) – 非空列表,其第一个元素将存储散布到此秩的对象。
scatter_object_input_list (List[Any]) – 要散布的输入对象列表。每个对象必须可腌制。只有
src
秩上的对象会被散布,对于非 src 秩,该参数可以为None
。src (int) – 要散布
scatter_object_input_list
的源秩。源秩基于全局进程组(与group
参数无关)。group – (ProcessGroup, 可选): 要操作的进程组。如果为 None,则将使用默认进程组。默认值为
None
。
- 返回值
None
。如果秩是组的一部分,则scatter_object_output_list
的第一个元素将设置为该秩的散布对象。
注意
请注意,此 API 与散布集体略有不同,因为它不提供
async_op
处理程序,因此将是阻塞调用。警告
scatter_object_list()
隐式使用pickle
模块,该模块已知不安全。可以构造恶意 pickle 数据,这些数据将在取消腌制时执行任意代码。仅对您信任的数据调用此函数。警告
使用 GPU 张量调用
scatter_object_list()
支持不佳且效率低下,因为它会产生 GPU -> CPU 传输,因为张量将被腌制。请考虑使用scatter()
代替。- 示例:
>>> # Note: Process group initialization omitted on each rank. >>> import torch.distributed as dist >>> if dist.get_rank() == 0: >>> # Assumes world_size of 3. >>> objects = ["foo", 12, {1: 2}] # any picklable object >>> else: >>> # Can be any list on non-src ranks, elements are not used. >>> objects = [None, None, None] >>> output_list = [None] >>> dist.scatter_object_list(output_list, objects, src=0) >>> # Rank i gets objects[i]. For example, on rank 2: >>> output_list [{1: 2}]
- torch.distributed.reduce_scatter(output, input_list, op=<RedOpType.SUM: 0>, group=None, async_op=False)[source]¶
在组中的所有进程上对张量列表进行归约,然后散布。
- torch.distributed.reduce_scatter_tensor(output, input, op=<RedOpType.SUM: 0>, group=None, async_op=False)[source]¶
对组中的所有等级进行归约,然后散布张量。
- 参数
- 返回值
异步工作句柄,如果 async_op 设置为 True。如果非 async_op 或不属于组,则为 None。
示例
>>> # All tensors below are of torch.int64 dtype and on CUDA devices. >>> # We have two ranks. >>> device = torch.device(f'cuda:{rank}') >>> tensor_out = torch.zeros(2, dtype=torch.int64, device=device) >>> # Input in concatenation form >>> tensor_in = torch.arange(world_size * 2, dtype=torch.int64, device=device) >>> tensor_in tensor([0, 1, 2, 3], device='cuda:0') # Rank 0 tensor([0, 1, 2, 3], device='cuda:1') # Rank 1 >>> dist.reduce_scatter_tensor(tensor_out, tensor_in) >>> tensor_out tensor([0, 2], device='cuda:0') # Rank 0 tensor([4, 6], device='cuda:1') # Rank 1 >>> # Input in stack form >>> tensor_in = torch.reshape(tensor_in, (world_size, 2)) >>> tensor_in tensor([[0, 1], [2, 3]], device='cuda:0') # Rank 0 tensor([[0, 1], [2, 3]], device='cuda:1') # Rank 1 >>> dist.reduce_scatter_tensor(tensor_out, tensor_in) >>> tensor_out tensor([0, 2], device='cuda:0') # Rank 0 tensor([4, 6], device='cuda:1') # Rank 1
警告
Gloo 后端不支持此 API。
- torch.distributed.all_to_all_single(output, input, output_split_sizes=None, input_split_sizes=None, group=None, async_op=False)[source]¶
将输入张量拆分,然后将拆分列表散布到组中的所有进程。
之后,从组中的所有进程连接接收到的张量,并作为单个输出张量返回。
支持复数张量。
- 参数
输出 (张量) – 收集的连接输出张量。
输入 (张量) – 要散布的输入张量。
output_split_sizes – (list[Int], 可选):如果指定 None 或为空,则为维度 0 的输出拆分大小,
output
张量的维度 0 必须被world_size
均等地整除。input_split_sizes – (list[Int], 可选):如果指定 None 或为空,则为维度 0 的输入拆分大小,
input
张量的维度 0 必须被world_size
均等地整除。group (ProcessGroup, optional) – 要操作的进程组。如果为 None,则使用默认进程组。
async_op (bool, optional) – 此操作是否应为异步操作。
- 返回值
异步工作句柄,如果 async_op 设置为 True。如果非 async_op 或不属于组,则为 None。
警告
all_to_all_single 处于实验阶段,可能会发生变化。
示例
>>> input = torch.arange(4) + rank * 4 >>> input tensor([0, 1, 2, 3]) # Rank 0 tensor([4, 5, 6, 7]) # Rank 1 tensor([8, 9, 10, 11]) # Rank 2 tensor([12, 13, 14, 15]) # Rank 3 >>> output = torch.empty([4], dtype=torch.int64) >>> dist.all_to_all_single(output, input) >>> output tensor([0, 4, 8, 12]) # Rank 0 tensor([1, 5, 9, 13]) # Rank 1 tensor([2, 6, 10, 14]) # Rank 2 tensor([3, 7, 11, 15]) # Rank 3
>>> # Essentially, it is similar to following operation: >>> scatter_list = list(input.chunk(world_size)) >>> gather_list = list(output.chunk(world_size)) >>> for i in range(world_size): >>> dist.scatter(gather_list[i], scatter_list if i == rank else [], src = i)
>>> # Another example with uneven split >>> input tensor([0, 1, 2, 3, 4, 5]) # Rank 0 tensor([10, 11, 12, 13, 14, 15, 16, 17, 18]) # Rank 1 tensor([20, 21, 22, 23, 24]) # Rank 2 tensor([30, 31, 32, 33, 34, 35, 36]) # Rank 3 >>> input_splits [2, 2, 1, 1] # Rank 0 [3, 2, 2, 2] # Rank 1 [2, 1, 1, 1] # Rank 2 [2, 2, 2, 1] # Rank 3 >>> output_splits [2, 3, 2, 2] # Rank 0 [2, 2, 1, 2] # Rank 1 [1, 2, 1, 2] # Rank 2 [1, 2, 1, 1] # Rank 3 >>> output = ... >>> dist.all_to_all_single(output, input, output_splits, input_splits) >>> output tensor([ 0, 1, 10, 11, 12, 20, 21, 30, 31]) # Rank 0 tensor([ 2, 3, 13, 14, 22, 32, 33]) # Rank 1 tensor([ 4, 15, 16, 23, 34, 35]) # Rank 2 tensor([ 5, 17, 18, 24, 36]) # Rank 3
>>> # Another example with tensors of torch.cfloat type. >>> input = torch.tensor([1+1j, 2+2j, 3+3j, 4+4j], dtype=torch.cfloat) + 4 * rank * (1+1j) >>> input tensor([1+1j, 2+2j, 3+3j, 4+4j]) # Rank 0 tensor([5+5j, 6+6j, 7+7j, 8+8j]) # Rank 1 tensor([9+9j, 10+10j, 11+11j, 12+12j]) # Rank 2 tensor([13+13j, 14+14j, 15+15j, 16+16j]) # Rank 3 >>> output = torch.empty([4], dtype=torch.int64) >>> dist.all_to_all_single(output, input) >>> output tensor([1+1j, 5+5j, 9+9j, 13+13j]) # Rank 0 tensor([2+2j, 6+6j, 10+10j, 14+14j]) # Rank 1 tensor([3+3j, 7+7j, 11+11j, 15+15j]) # Rank 2 tensor([4+4j, 8+8j, 12+12j, 16+16j]) # Rank 3
- torch.distributed.all_to_all(output_tensor_list, input_tensor_list, group=None, async_op=False)[source]¶
将输入张量列表分散到组中的所有进程,并返回输出列表中收集的张量列表。
支持复数张量。
- 参数
- 返回值
异步工作句柄,如果 async_op 设置为 True。如果非 async_op 或不属于组,则为 None。
警告
all_to_all 处于实验阶段,可能会发生变化。
示例
>>> input = torch.arange(4) + rank * 4 >>> input = list(input.chunk(4)) >>> input [tensor([0]), tensor([1]), tensor([2]), tensor([3])] # Rank 0 [tensor([4]), tensor([5]), tensor([6]), tensor([7])] # Rank 1 [tensor([8]), tensor([9]), tensor([10]), tensor([11])] # Rank 2 [tensor([12]), tensor([13]), tensor([14]), tensor([15])] # Rank 3 >>> output = list(torch.empty([4], dtype=torch.int64).chunk(4)) >>> dist.all_to_all(output, input) >>> output [tensor([0]), tensor([4]), tensor([8]), tensor([12])] # Rank 0 [tensor([1]), tensor([5]), tensor([9]), tensor([13])] # Rank 1 [tensor([2]), tensor([6]), tensor([10]), tensor([14])] # Rank 2 [tensor([3]), tensor([7]), tensor([11]), tensor([15])] # Rank 3
>>> # Essentially, it is similar to following operation: >>> scatter_list = input >>> gather_list = output >>> for i in range(world_size): >>> dist.scatter(gather_list[i], scatter_list if i == rank else [], src=i)
>>> input tensor([0, 1, 2, 3, 4, 5]) # Rank 0 tensor([10, 11, 12, 13, 14, 15, 16, 17, 18]) # Rank 1 tensor([20, 21, 22, 23, 24]) # Rank 2 tensor([30, 31, 32, 33, 34, 35, 36]) # Rank 3 >>> input_splits [2, 2, 1, 1] # Rank 0 [3, 2, 2, 2] # Rank 1 [2, 1, 1, 1] # Rank 2 [2, 2, 2, 1] # Rank 3 >>> output_splits [2, 3, 2, 2] # Rank 0 [2, 2, 1, 2] # Rank 1 [1, 2, 1, 2] # Rank 2 [1, 2, 1, 1] # Rank 3 >>> input = list(input.split(input_splits)) >>> input [tensor([0, 1]), tensor([2, 3]), tensor([4]), tensor([5])] # Rank 0 [tensor([10, 11, 12]), tensor([13, 14]), tensor([15, 16]), tensor([17, 18])] # Rank 1 [tensor([20, 21]), tensor([22]), tensor([23]), tensor([24])] # Rank 2 [tensor([30, 31]), tensor([32, 33]), tensor([34, 35]), tensor([36])] # Rank 3 >>> output = ... >>> dist.all_to_all(output, input) >>> output [tensor([0, 1]), tensor([10, 11, 12]), tensor([20, 21]), tensor([30, 31])] # Rank 0 [tensor([2, 3]), tensor([13, 14]), tensor([22]), tensor([32, 33])] # Rank 1 [tensor([4]), tensor([15, 16]), tensor([23]), tensor([34, 35])] # Rank 2 [tensor([5]), tensor([17, 18]), tensor([24]), tensor([36])] # Rank 3
>>> # Another example with tensors of torch.cfloat type. >>> input = torch.tensor([1+1j, 2+2j, 3+3j, 4+4j], dtype=torch.cfloat) + 4 * rank * (1+1j) >>> input = list(input.chunk(4)) >>> input [tensor([1+1j]), tensor([2+2j]), tensor([3+3j]), tensor([4+4j])] # Rank 0 [tensor([5+5j]), tensor([6+6j]), tensor([7+7j]), tensor([8+8j])] # Rank 1 [tensor([9+9j]), tensor([10+10j]), tensor([11+11j]), tensor([12+12j])] # Rank 2 [tensor([13+13j]), tensor([14+14j]), tensor([15+15j]), tensor([16+16j])] # Rank 3 >>> output = list(torch.empty([4], dtype=torch.int64).chunk(4)) >>> dist.all_to_all(output, input) >>> output [tensor([1+1j]), tensor([5+5j]), tensor([9+9j]), tensor([13+13j])] # Rank 0 [tensor([2+2j]), tensor([6+6j]), tensor([10+10j]), tensor([14+14j])] # Rank 1 [tensor([3+3j]), tensor([7+7j]), tensor([11+11j]), tensor([15+15j])] # Rank 2 [tensor([4+4j]), tensor([8+8j]), tensor([12+12j]), tensor([16+16j])] # Rank 3
- torch.distributed.barrier(group=None, async_op=False, device_ids=None)[source]¶
同步所有进程。
如果 async_op 为 False,或者如果在 wait() 上调用异步工作句柄,则此集合将阻塞进程,直到整个组进入此函数。
- 参数
- 返回值
异步工作句柄,如果 async_op 设置为 True。如果非 async_op 或不属于该组,则为 None
注意
ProcessGroupNCCL 现在依赖于流同步而不是设备同步来阻塞 CPU。因此,请不要假设 barrier() 会执行设备同步。
- torch.distributed.monitored_barrier(group=None, timeout=None, wait_all_ranks=False)[source]¶
与
torch.distributed.barrier
类似,同步进程,但考虑可配置的超时时间。它能够报告在提供的超时时间内未通过此屏障的排名。具体来说,对于非零排名,将阻塞直到从排名 0 处理发送/接收。排名 0 将阻塞直到处理来自其他排名的所有发送/接收,并将报告无法及时响应的排名的失败。请注意,如果一个排名没有达到 monitored_barrier(例如由于挂起),所有其他排名都会在 monitored_barrier 中失败。
此集体将阻塞组中的所有进程/排名,直到整个组成功退出该函数,使其对调试和同步很有用。但是,它会对性能产生影响,并且应该仅用于调试或需要在主机端进行完全同步点的场景。出于调试目的,可以在应用程序的集体调用之前插入此屏障,以检查是否有任何排名不同步。
注意
请注意,此集体仅在 GLOO 后端中受支持。
- 参数
group (ProcessGroup, optional) – 要处理的进程组。如果为
None
,将使用默认进程组。timeout (datetime.timedelta, optional) – monitored_barrier 的超时时间。如果为
None
,将使用默认进程组超时时间。wait_all_ranks (bool, optional) – 是否收集所有失败的排名。默认情况下,此值为
False
,排名 0 上的monitored_barrier
将在遇到第一个失败的排名时抛出异常以快速失败。通过设置wait_all_ranks=True
,monitored_barrier
将收集所有失败的排名并抛出一个包含所有失败排名信息的错误。
- 返回值
None
.
- 示例:
>>> # Note: Process group initialization omitted on each rank. >>> import torch.distributed as dist >>> if dist.get_rank() != 1: >>> dist.monitored_barrier() # Raises exception indicating that >>> # rank 1 did not call into monitored_barrier. >>> # Example with wait_all_ranks=True >>> if dist.get_rank() == 0: >>> dist.monitored_barrier(wait_all_ranks=True) # Raises exception >>> # indicating that ranks 1, 2, ... world_size - 1 did not call into >>> # monitored_barrier.
- class torch.distributed.Work¶
一个 Work 对象代表 PyTorch 分布式包中挂起的异步操作的句柄。它由非阻塞集体操作返回,例如 dist.all_reduce(tensor, async_op=True)。
- class torch.distributed.ReduceOp¶
一个类似枚举的类,用于表示可用的约简操作:
SUM
、PRODUCT
、MIN
、MAX
、BAND
、BOR
、BXOR
和PREMUL_SUM
。BAND
、BOR
和BXOR
约简在使用NCCL
后端时不可用。AVG
在跨秩求和之前将值除以世界大小。AVG
仅在使用NCCL
后端时可用,并且仅适用于 NCCL 2.10 或更高版本。PREMUL_SUM
在约简之前在本地将输入乘以给定的标量。PREMUL_SUM
仅在使用NCCL
后端时可用,并且仅适用于 NCCL 2.11 或更高版本。用户应该使用torch.distributed._make_nccl_premul_sum
。此外,
MAX
、MIN
和PRODUCT
不支持复数张量。此类的值可以通过属性访问,例如
ReduceOp.SUM
。它们用于指定约简集体操作的策略,例如reduce()
。此类不支持
__members__
属性。
分析集体通信¶
请注意,您可以使用 torch.profiler
(推荐,仅在 1.8.1 之后可用)或 torch.autograd.profiler
来分析此处提到的集体通信和点对点通信 API。所有开箱即用的后端(gloo
、nccl
、mpi
)都受支持,并且集体通信使用情况将在分析输出/跟踪中按预期呈现。分析您的代码与任何常规的 torch 运算符相同。
import torch
import torch.distributed as dist
with torch.profiler():
tensor = torch.randn(20, 10)
dist.all_reduce(tensor)
有关分析器功能的完整概述,请参阅 分析器文档。
多 GPU 集体函数¶
警告
多 GPU 函数(代表每个 CPU 线程多个 GPU)已弃用。截至目前,PyTorch Distributed 的首选编程模型是每个线程一个设备,如本文档中 API 所示。如果您是后端开发人员,并且想要支持每个线程多个设备,请联系 PyTorch Distributed 的维护人员。
第三方后端¶
除了内置的 GLOO/MPI/NCCL 后端之外,PyTorch Distributed 还通过运行时注册机制支持第三方后端。有关如何通过 C++ 扩展开发第三方后端的参考,请参阅 教程 - 自定义 C++ 和 CUDA 扩展 和 test/cpp_extensions/cpp_c10d_extension.cpp
。第三方后端的功能由它们自己的实现决定。
新的后端源自 c10d::ProcessGroup
,并在导入时通过 torch.distributed.Backend.register_backend()
注册后端名称和实例化接口。
当手动导入此后端并使用相应的后端名称调用 torch.distributed.init_process_group()
时,torch.distributed
包将在新的后端上运行。
警告
第三方后端的支持处于实验阶段,可能会发生变化。
启动工具¶
torch.distributed 包还提供了一个在 torch.distributed.launch 中的启动工具。此辅助工具可用于为分布式训练启动每个节点上的多个进程。
模块 torch.distributed.launch
。
torch.distributed.launch
是一个在每个训练节点上生成多个分布式训练进程的模块。
警告
此模块将被弃用,取而代之的是 torchrun。
此工具可用于单节点分布式训练,其中每个节点将生成一个或多个进程。此工具可用于 CPU 训练或 GPU 训练。如果此工具用于 GPU 训练,则每个分布式进程将在单个 GPU 上运行。这可以实现显著提高的单节点训练性能。它也可以用于多节点分布式训练,通过在每个节点上生成多个进程来显著提高多节点分布式训练性能。这对于具有直接 GPU 支持的多个 Infiniband 接口的系统尤其有利,因为它们都可以被用于聚合通信带宽。
无论是在单节点分布式训练还是多节点分布式训练的情况下,此工具都会在每个节点上启动指定数量的进程(--nproc-per-node
)。如果用于 GPU 训练,此数量需要小于或等于当前系统上的 GPU 数量(nproc_per_node
),并且每个进程将在从 *GPU 0 到 GPU (nproc_per_node - 1)* 的单个 GPU 上运行。
如何使用此模块
单节点多进程分布式训练
python -m torch.distributed.launch --nproc-per-node=NUM_GPUS_YOU_HAVE
YOUR_TRAINING_SCRIPT.py (--arg1 --arg2 --arg3 and all other
arguments of your training script)
多节点多进程分布式训练:(例如两个节点)
节点 1:(IP: 192.168.1.1,并拥有一个空闲端口:1234)
python -m torch.distributed.launch --nproc-per-node=NUM_GPUS_YOU_HAVE
--nnodes=2 --node-rank=0 --master-addr="192.168.1.1"
--master-port=1234 YOUR_TRAINING_SCRIPT.py (--arg1 --arg2 --arg3
and all other arguments of your training script)
节点 2
python -m torch.distributed.launch --nproc-per-node=NUM_GPUS_YOU_HAVE
--nnodes=2 --node-rank=1 --master-addr="192.168.1.1"
--master-port=1234 YOUR_TRAINING_SCRIPT.py (--arg1 --arg2 --arg3
and all other arguments of your training script)
要查找此模块提供的可选参数
python -m torch.distributed.launch --help
重要提示
1. 此工具和多进程分布式(单节点或多节点)GPU 训练目前仅使用 NCCL 分布式后端才能获得最佳性能。因此,NCCL 后端是 GPU 训练推荐使用的后端。
2. 在您的训练程序中,您必须解析命令行参数:--local-rank=LOCAL_PROCESS_RANK
,该参数将由此模块提供。如果您的训练程序使用 GPU,您应该确保您的代码仅在 LOCAL_PROCESS_RANK 的 GPU 设备上运行。这可以通过以下方式实现
解析 local_rank 参数
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument("--local-rank", type=int)
>>> args = parser.parse_args()
使用以下任一方法将您的设备设置为本地等级
>>> torch.cuda.set_device(args.local_rank) # before your code runs
或
>>> with torch.cuda.device(args.local_rank):
>>> # your code to run
>>> ...
3. 在您的训练程序中,您应该在开始时调用以下函数来启动分布式后端。强烈建议使用 init_method=env://
。其他初始化方法(例如 tcp://
)可能有效,但 env://
是此模块正式支持的初始化方法。
>>> torch.distributed.init_process_group(backend='YOUR BACKEND',
>>> init_method='env://')
4. 在您的训练程序中,您可以使用常规的分布式函数,也可以使用 torch.nn.parallel.DistributedDataParallel()
模块。如果您的训练程序使用 GPU 进行训练,并且您想使用 torch.nn.parallel.DistributedDataParallel()
模块,以下是配置方法。
>>> model = torch.nn.parallel.DistributedDataParallel(model,
>>> device_ids=[args.local_rank],
>>> output_device=args.local_rank)
请确保 device_ids
参数设置为您的代码将运行的唯一 GPU 设备 ID。这通常是进程的本地等级。换句话说,device_ids
需要设置为 [args.local_rank]
,而 output_device
需要设置为 args.local_rank
,以便使用此工具
5. 另一种通过环境变量 LOCAL_RANK
将 local_rank
传递给子进程的方法。当您使用 --use-env=True
启动脚本时,将启用此行为。您必须调整上面的子进程示例,将 args.local_rank
替换为 os.environ['LOCAL_RANK']
;当您指定此标志时,启动器不会传递 --local-rank
。
警告
local_rank
不是全局唯一的:它仅在机器上的每个进程中是唯一的。因此,不要使用它来决定是否应该(例如)写入网络文件系统。请参阅 https://github.com/pytorch/pytorch/issues/12042,了解如果未正确执行此操作,可能会出现问题的示例。
生成实用程序¶
该 多进程包 - torch.multiprocessing 包还提供了一个 spawn
函数在 torch.multiprocessing.spawn()
中。此辅助函数可用于生成多个进程。它的工作原理是传入要运行的函数,并生成 N 个进程来运行它。这也可以用于多进程分布式训练。
有关如何使用它的参考,请参阅 PyTorch 示例 - ImageNet 实现
请注意,此函数需要 Python 3.4 或更高版本。
调试 torch.distributed
应用程序¶
调试分布式应用程序可能具有挑战性,因为难以理解的挂起、崩溃或跨等级的不一致行为。 torch.distributed
提供了一套工具来帮助以自助方式调试训练应用程序
Python 断点¶
在分布式环境中使用 python 的调试器非常方便,但由于它不能开箱即用,因此许多人根本不使用它。PyTorch 提供了围绕 pdb 的自定义包装器,简化了该过程。
torch.distributed.breakpoint 使此过程变得容易。在内部,它以两种方式自定义了 pdb 的断点行为,但在其他方面则表现为正常的 pdb。1. 仅在一个等级(由用户指定)上附加调试器。2. 通过使用 torch.distributed.barrier() 来确保所有其他等级停止,该 torch.distributed.barrier() 将在调试等级发出 continue 后释放。3. 将子进程的 stdin 重定向,使其连接到您的终端。
要使用它,只需在所有等级上发出 torch.distributed.breakpoint(rank),在每种情况下使用相同的 rank 值。
监控的屏障¶
从 v1.10 开始,torch.distributed.monitored_barrier()
作为 torch.distributed.barrier()
的替代方案存在,当崩溃时,它会提供有关哪个等级可能出现故障的有用信息,即并非所有等级都在提供的超时时间内调用 torch.distributed.monitored_barrier()
。 torch.distributed.monitored_barrier()
使用类似于确认的进程,在主机端使用 send
/recv
通信原语实现主机端屏障,允许等级 0 报告哪个等级未能及时确认屏障。例如,考虑以下函数,其中等级 1 无法调用 torch.distributed.monitored_barrier()
(实际上,这可能是由于应用程序错误或先前集合中的挂起)
import os
from datetime import timedelta
import torch
import torch.distributed as dist
import torch.multiprocessing as mp
def worker(rank):
dist.init_process_group("nccl", rank=rank, world_size=2)
# monitored barrier requires gloo process group to perform host-side sync.
group_gloo = dist.new_group(backend="gloo")
if rank not in [1]:
dist.monitored_barrier(group=group_gloo, timeout=timedelta(seconds=2))
if __name__ == "__main__":
os.environ["MASTER_ADDR"] = "localhost"
os.environ["MASTER_PORT"] = "29501"
mp.spawn(worker, nprocs=2, args=())
以下错误消息在排名 0 上生成,允许用户确定哪些排名可能出现故障并进一步调查
RuntimeError: Rank 1 failed to pass monitoredBarrier in 2000 ms
Original exception:
[gloo/transport/tcp/pair.cc:598] Connection closed by peer [2401:db00:eef0:1100:3560:0:1c05:25d]:8594
TORCH_DISTRIBUTED_DEBUG
¶
使用 TORCH_CPP_LOG_LEVEL=INFO
,环境变量 TORCH_DISTRIBUTED_DEBUG
可用于触发额外的有用日志记录和集体同步检查,以确保所有排名都适当地同步。 TORCH_DISTRIBUTED_DEBUG
可以设置为 OFF
(默认)、INFO
或 DETAIL
,具体取决于所需的调试级别。请注意,最详细的选项 DETAIL
可能会影响应用程序性能,因此应仅在调试问题时使用。
设置 TORCH_DISTRIBUTED_DEBUG=INFO
将在使用 torch.nn.parallel.DistributedDataParallel()
训练的模型初始化时生成额外的调试日志,而 TORCH_DISTRIBUTED_DEBUG=DETAIL
将另外记录选定迭代次数的运行时性能统计信息。这些运行时统计信息包括诸如正向时间、反向时间、梯度通信时间等数据。例如,给定以下应用程序
import os
import torch
import torch.distributed as dist
import torch.multiprocessing as mp
class TwoLinLayerNet(torch.nn.Module):
def __init__(self):
super().__init__()
self.a = torch.nn.Linear(10, 10, bias=False)
self.b = torch.nn.Linear(10, 1, bias=False)
def forward(self, x):
a = self.a(x)
b = self.b(x)
return (a, b)
def worker(rank):
dist.init_process_group("nccl", rank=rank, world_size=2)
torch.cuda.set_device(rank)
print("init model")
model = TwoLinLayerNet().cuda()
print("init ddp")
ddp_model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[rank])
inp = torch.randn(10, 10).cuda()
print("train")
for _ in range(20):
output = ddp_model(inp)
loss = output[0] + output[1]
loss.sum().backward()
if __name__ == "__main__":
os.environ["MASTER_ADDR"] = "localhost"
os.environ["MASTER_PORT"] = "29501"
os.environ["TORCH_CPP_LOG_LEVEL"]="INFO"
os.environ[
"TORCH_DISTRIBUTED_DEBUG"
] = "DETAIL" # set to DETAIL for runtime logging.
mp.spawn(worker, nprocs=2, args=())
以下日志在初始化时呈现
I0607 16:10:35.739390 515217 logger.cpp:173] [Rank 0]: DDP Initialized with:
broadcast_buffers: 1
bucket_cap_bytes: 26214400
find_unused_parameters: 0
gradient_as_bucket_view: 0
is_multi_device_module: 0
iteration: 0
num_parameter_tensors: 2
output_device: 0
rank: 0
total_parameter_size_bytes: 440
world_size: 2
backend_name: nccl
bucket_sizes: 440
cuda_visible_devices: N/A
device_ids: 0
dtypes: float
master_addr: localhost
master_port: 29501
module_name: TwoLinLayerNet
nccl_async_error_handling: N/A
nccl_blocking_wait: N/A
nccl_debug: WARN
nccl_ib_timeout: N/A
nccl_nthreads: N/A
nccl_socket_ifname: N/A
torch_distributed_debug: INFO
以下日志在运行时呈现(当 TORCH_DISTRIBUTED_DEBUG=DETAIL
设置时)
I0607 16:18:58.085681 544067 logger.cpp:344] [Rank 1 / 2] Training TwoLinLayerNet unused_parameter_size=0
Avg forward compute time: 40838608
Avg backward compute time: 5983335
Avg backward comm. time: 4326421
Avg backward comm/comp overlap time: 4207652
I0607 16:18:58.085693 544066 logger.cpp:344] [Rank 0 / 2] Training TwoLinLayerNet unused_parameter_size=0
Avg forward compute time: 42850427
Avg backward compute time: 3885553
Avg backward comm. time: 2357981
Avg backward comm/comp overlap time: 2234674
此外,TORCH_DISTRIBUTED_DEBUG=INFO
增强了 torch.nn.parallel.DistributedDataParallel()
中由于模型中未使用参数而导致的崩溃日志记录。目前,如果正向传递中可能存在未使用参数,则必须将 find_unused_parameters=True
传递到 torch.nn.parallel.DistributedDataParallel()
初始化,并且从 v1.10 开始,所有模型输出都需要在损失计算中使用,因为 torch.nn.parallel.DistributedDataParallel()
不支持反向传递中的未使用参数。这些约束对于较大的模型来说尤其具有挑战性,因此在崩溃并出现错误时,torch.nn.parallel.DistributedDataParallel()
将记录所有未使用的参数的完全限定名称。例如,在上面的应用程序中,如果我们将 loss
修改为改为计算为 loss = output[1]
,那么 TwoLinLayerNet.a
在反向传递中不会收到梯度,因此导致 DDP
失败。在崩溃时,用户会收到有关未使用参数的信息,这对于大型模型来说可能难以手动找到
RuntimeError: Expected to have finished reduction in the prior iteration before starting a new one. This error indicates that your module has parameters that were not used in producing loss. You can enable unused parameter detection by passing
the keyword argument `find_unused_parameters=True` to `torch.nn.parallel.DistributedDataParallel`, and by
making sure all `forward` function outputs participate in calculating loss.
If you already have done the above, then the distributed data parallel module wasn't able to locate the output tensors in the return value of your module's `forward` function. Please include the loss function and the structure of the return va
lue of `forward` of your module when reporting this issue (e.g. list, dict, iterable).
Parameters which did not receive grad for rank 0: a.weight
Parameter indices which did not receive grad for rank 0: 0
设置 TORCH_DISTRIBUTED_DEBUG=DETAIL
将在用户直接或间接(例如 DDP allreduce
)发出的每个集体调用上触发额外的一致性和同步检查。这是通过创建一个包装进程组来完成的,该进程组包装了由 torch.distributed.init_process_group()
和 torch.distributed.new_group()
API 返回的所有进程组。因此,这些 API 将返回一个包装进程组,该进程组可以使用与常规进程组完全相同的方式,但在将集体调度到底层进程组之前执行一致性检查。目前,这些检查包括一个 torch.distributed.monitored_barrier()
,它确保所有等级都完成其未完成的集体调用并报告卡住的等级。接下来,通过确保所有集体函数匹配并使用一致的张量形状调用,来检查集体本身的一致性。如果不是这种情况,应用程序崩溃时将包含详细的错误报告,而不是挂起或信息量不足的错误消息。例如,考虑以下函数,它对 torch.distributed.all_reduce()
的输入形状不匹配
import torch
import torch.distributed as dist
import torch.multiprocessing as mp
def worker(rank):
dist.init_process_group("nccl", rank=rank, world_size=2)
torch.cuda.set_device(rank)
tensor = torch.randn(10 if rank == 0 else 20).cuda()
dist.all_reduce(tensor)
torch.cuda.synchronize(device=rank)
if __name__ == "__main__":
os.environ["MASTER_ADDR"] = "localhost"
os.environ["MASTER_PORT"] = "29501"
os.environ["TORCH_CPP_LOG_LEVEL"]="INFO"
os.environ["TORCH_DISTRIBUTED_DEBUG"] = "DETAIL"
mp.spawn(worker, nprocs=2, args=())
使用 NCCL
后端,这样的应用程序可能会导致挂起,这在非平凡的情况下可能难以找到根本原因。如果用户启用 TORCH_DISTRIBUTED_DEBUG=DETAIL
并重新运行应用程序,以下错误消息将揭示根本原因
work = default_pg.allreduce([tensor], opts)
RuntimeError: Error when verifying shape tensors for collective ALLREDUCE on rank 0. This likely indicates that input shapes into the collective are mismatched across ranks. Got shapes: 10
[ torch.LongTensor{1} ]
注意
为了在运行时对调试级别进行细粒度控制,还可以使用函数 torch.distributed.set_debug_level()
、torch.distributed.set_debug_level_from_env()
和 torch.distributed.get_debug_level()
。
此外,TORCH_DISTRIBUTED_DEBUG=DETAIL 可以与 TORCH_SHOW_CPP_STACKTRACES=1 结合使用,以在检测到集体不同步时记录整个调用堆栈。这些集体不同步检查将适用于所有使用 c10d
集体调用的应用程序,这些调用由使用 torch.distributed.init_process_group()
和 torch.distributed.new_group()
API 创建的进程组支持。
日志记录¶
除了通过 torch.distributed.monitored_barrier()
和 TORCH_DISTRIBUTED_DEBUG
进行显式调试支持外,torch.distributed
的底层 C++ 库还会在不同级别输出日志消息。这些消息有助于了解分布式训练作业的执行状态,并排查网络连接故障等问题。以下矩阵显示了如何通过 TORCH_CPP_LOG_LEVEL
和 TORCH_DISTRIBUTED_DEBUG
环境变量的组合来调整日志级别。
|
|
有效日志级别 |
---|---|---|
|
忽略 |
错误 |
|
忽略 |
警告 |
|
忽略 |
信息 |
|
|
调试 |
|
|
跟踪(又名所有) |
分布式组件引发从 RuntimeError 派生的自定义异常类型
torch.distributed.DistError: 这是所有分布式异常的基类型。
torch.distributed.DistBackendError: 当发生特定于后端的错误时,会抛出此异常。例如,如果使用 NCCL 后端,并且用户尝试使用 NCCL 库不可用的 GPU。
torch.distributed.DistNetworkError: 当网络库遇到错误(例如:对等方重置连接)时,会抛出此异常。
torch.distributed.DistStoreError: 当存储遇到错误(例如:TCPStore 超时)时,会抛出此异常。
- class torch.distributed.DistError¶
当分布式库中发生错误时引发的异常
- class torch.distributed.DistBackendError¶
当分布式中发生后端错误时引发的异常
- class torch.distributed.DistNetworkError¶
当分布式中发生网络错误时引发的异常
- class torch.distributed.DistStoreError¶
当分布式存储中发生错误时引发的异常
如果您正在运行单节点训练,您可能希望在脚本中交互式地设置断点。我们提供了一种方便的方法来在一个单独的进程上设置断点。