# 运行时错误：NCCL 通信器在 rank 2 上被中止。原始失败原因：看门狗回调超时

- **ID:** `pytorch/ddp-nccl-timeout-during-allreduce`
- **领域:** pytorch
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 70%

## 根因

NCCL 集合操作（如 allreduce）超时，因为某个 rank 缓慢或无响应，通常由于网络拥塞、GPU 计算不平衡或硬件故障。

## 版本兼容性

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

## 解决方案

1. ```
   通过分析每个 rank 的前向/反向传播时间检查 GPU 计算不平衡。确保所有 rank 处理相似数量的数据（例如，使用 DistributedSampler 并设置 drop_last=True）。
   ```
2. ```
   将 NCCL 超时环境变量增加到更高值（例如 600 秒），以适应慢速网络或大型模型。
   ```
3. ```
   使用 NCCL_DEBUG=INFO 环境变量获取详细调试日志，并识别慢速 rank 或网络问题。
   ```

## 无效尝试

- **** — The timeout is a symptom, not the root cause; increasing it delays failure but doesn't prevent it. (60% 失败率)
- **** — Barriers can cause all ranks to wait for the slow one, potentially increasing the timeout likelihood. (70% 失败率)
- **** — The root cause (e.g., network latency) persists across restarts. (80% 失败率)
