# 运行时错误：[torch.distributed] 排名 0 上的 AllReduce 超时（超时时间：600）

- **ID:** `pytorch/nccl-timeout-during-allreduce`
- **领域:** pytorch
- **类别:** network_error
- **错误码:** `2`
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

NCCL allreduce 操作由于网络拥塞、工作节点缓慢或崩溃、或节点间 NCCL 版本不匹配而超时。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| pytorch>=2.1.0 | active | — | — |
| nccl>=2.18.0 | active | — | — |

## 解决方案

1. ```
   使用 `nccl-tests` 或 `ping` 检查所有节点之间的网络连接。确保所有节点可以在所需端口（例如 29500）上通信。然后设置 `NCCL_SOCKET_IFNAME=eth0` 以指定正确的网络接口。
   ```
2. ```
   减小批量大小或模型大小以降低通信量，或使用梯度累积减少 allreduce 频率。
   ```
3. ```
   如果 NCCL 有问题，切换到 Gloo 后端进行 allreduce：`torch.distributed.init_process_group(backend='gloo', ...)`。注意：Gloo 可能较慢但在某些网络上更稳定。
   ```

## 无效尝试

- **** — If the underlying issue (e.g., network congestion or slow node) persists, the operation will still fail after the longer timeout. (70% 失败率)
- **** — The network issue is likely persistent; a restart only resets the state temporarily. (90% 失败率)
- **** — While logging helps diagnosis, it does not resolve the timeout itself. (95% 失败率)
