Repository: pytorch
Issue: NCCL hangs cause timeout
Labels: oncall: distributed,triaged

### 🐛 Describe the bug

I'm training a vqgan model and there is a forward operation which do allreduce across batch to get an estimation of the data distribution. It successfully ran for hours and hang in the allreduce operation. But it didn't throw any specific error. But still, the program will stuck after running for serveral hours at randomly batch. I cannot find out why. 
BTW,
1. it only occupies 10% GPU memory on each card thus it's impossible to oom
2. The data loading on each card is passed with no nan values before the stuck
3. Plus, I'm sure that the nvidia fabricmanager works fine without any error.'
4. I've check the ROCE network and do a long time nccl multi-node test on allreduce (by mpi) and everything looks just fine. 

NCCL env: 
```
# nccl version: 22.3
NCCL_IB_TIMEOUT=11 \
NCCL_DEBUG=INFO \
NCCL_DEBUG_FILE=/data1/nccl_debug_%h.%p \
NCCL_IB_CUDA_SUPPORT=1 \
NCCL_IBEXT_DISABLE=1 \
NCCL_DEBUG_SUBSYS=ALL \
NCCL_IB_DISABLE=0 \
NCCL_NVLS_ENABLE=0 \
NCCL_IB_RETRY_CNT=7 \
GLOO_SOCKET_IFNAME=eth1x \
NCCL_SOCKET_IFNAME=eth1x \
NCCL_IB_HCA=mlx5_0:1,mlx5_1:1,mlx5_2:1,mlx5_3:1,mlx5_4:1,mlx5_5:1,mlx5_6:1,mlx5_7:1 \
NCCL_IB_GID_INDEX=3 \
TORCH_DISTRIBUTED_DEBUG=INFO \
```

The related code: 
```python
from torch.distributed import nn as dist_nn
# ...... 
# some model forward operations
a = torch.Tensor(153,262144) # an intermediate float32 cuda tensor, which is used later on in calculate loss
dist_nn.all_reduce(a) # this line hangs
a /= dist.get_world_size()
# some forward operations and backward ...
```

output: 
```
container-node08: [rank50]:[E1113 16:25:40.598747947 ProcessGroupNCCL.cpp:1844] [PG ID 0 PG GUID 0(default_pg) Rank 50] Timeout at NCCL work: 3844095, last enqueued NCCL work: 3844106, last completed NCCL work: 3844094.
container-node08: [rank50]:[E1113 16:25:40.598768762 ProcessGroupNCCL.cpp:630] [Rank 50] Some NCCL operations have failed or timed out. Due to the asynchronous nature of CUDA kernels, subsequent GPU operations might run on corrupted/incomplete data.
container-node08: [rank50]:[E1113 16:25:40.598775081 ProcessGroupNCCL.cpp:636] [Rank 50] To avoid data inconsistency, we are taking the entire process down.
container-node08: [rank50]:[E1113 16:25:40.600342730 ProcessGroupNCCL.cpp:1605] [PG ID 0 PG GUID 0(default_pg) Rank 50] Process group watchdog thread terminated with exception: [Rank 50] Watchdog caught collective operation timeout: WorkNCCL(SeqNum=3844095, OpType=ALLREDUCE, NumelIn=262144, NumelOut=262144, Timeout(ms)=600000) ran for 600034 milliseconds before timing out.
container-node08: Exception raised from checkTimeout at /opt/pytorch/pytorch/torch/csrc/distributed/c10d/ProcessGroupNCCL.cpp:618 (most recent call first):
container-node08: frame #0: c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) + 0x98 (0x7f5c421b30a8 in /usr/local/lib/python3.10/dist-packages/torch/lib/libc10.so)
container-node08: frame #1: <unknown function> + 0x114308e (0x7f5c4336708e in /usr/local/lib/python3.10/dist-packages/torch/lib/libtorch_cuda.so)
container-node08: frame #2: c10d::ProcessGroupNCCL::WorkNCCL::checkTimeout(std::optional<std::chrono::duration<long, std::ratio<1l, 1000l> > >) + 0x282 (0x7f5c4337cee2 in /usr/local/lib/python3.10/dist-packages/torch/lib/libtorch_cuda.so)
container-node08: frame #3: c10d::ProcessGroupNCCL::watchdogHandler() + 0x24b (0x7f5c4338631b in /usr/local/lib/python3.10/dist-packages/torch/lib/libtorch_cuda.so)
container-node08: frame #4: c10d::ProcessGroupNCCL::ncclCommWatchdog() + 0x14d (0x7f5c43387d7d in /usr/local/lib/python3.10/dist-packages/torch/lib/libtorch_cuda.so)
container-node08: frame #5: <unknown function> + 0xdc253 (0x7f5c41cb0253 in /lib/x86_64-linux-gnu/libstdc++.so.6)
container-node08: frame #6: <unknown function> + 0x94ac3 (0x7f5ca1ba9ac3 in /lib/x86_64-linux-gnu/libc.so.6)
container-node08: frame #7: clone + 0x44 (0x7f5ca1c3aa04 in /lib/x86_64-linux-gnu/libc.so.6)
container-node08: 
```

### Versions

I use ngc pytroch docker, 24.10-py3: 

PyTorch version: 2.5.0a0+e000cf0ad9.nv24.10
Is debug build: False
CUDA used to build PyTorch: 12.6
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.5 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.30.4
Libc version: glibc-2.35

Python version: 3.10.12 (main, Sep 11 2024, 15:47:36) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-5.15.0-88-generic-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration:
GPU 0: NVIDIA H100 80GB HBM3
GPU 1: NVIDIA H100 80GB HBM3
GPU 2: NVIDIA H100 80GB HBM3
GPU 3: NVIDIA H100 80GB HBM3
GPU 4: NVIDIA H100 80GB HBM3
GPU 5: NVIDIA H100 80GB HBM3
GPU 6: NVIDIA H100 80GB HBM3
GPU 7: NVIDIA H100 80GB HBM3

Nvidia driver version: 550.90.12
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.9.5.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv.so.9.5.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn.so.9.5.0
/usr/lib/x86_64-linux-gnu/libcudnn_engines_precompiled.so.9.5.0
/usr/lib/x86_64-linux-gnu/libcudnn_engines_runtime_compiled.so.9.5.0
/usr/lib/x86_64-linux-gnu/libcudnn_graph.so.9.5.0
/usr/lib/x86_64-linux-gnu/libcudnn_heuristic.so.9.5.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops.so.9.5.0
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                       x86_64
CPU op-mode(s):                     32-bit, 64-bit
Address sizes:                      46 bits physical, 57 bits virtual
Byte Order:                         Little Endian
CPU(s):                             224
On-line CPU(s) list:                0-223
Vendor ID:                          GenuineIntel
Model name:                         Intel(R) Xeon(R) Platinum 8480+
CPU family:                         6
Model:                              143
Thread(s) per core:                 2
Core(s) per socket:                 56
Socket(s):                          2
Stepping:                           8
Frequency boost:                    enabled
CPU max MHz:                        2001.0000
CPU min MHz:                        800.0000
BogoMIPS:                           4000.00
Flags:                              fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cat_l2 cdp_l3 invpcid_single intel_ppin cdp_l2 ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect avx_vnni avx512_bf16 wbnoinvd dtherm ida arat pln pts avx512vbmi umip pku ospke waitpkg avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid bus_lock_detect cldemote movdiri movdir64b enqcmd fsrm md_clear serialize tsxldtrk pconfig arch_lbr amx_bf16 avx512_fp16 amx_tile amx_int8 flush_l1d arch_capabilities
Virtualization:                     VT-x
L1d cache:                          5.3 MiB (112 instances)
L1i cache:                          3.5 MiB (112 instances)
L2 cache:                           224 MiB (112 instances)
L3 cache:                           210 MiB (2 instances)
NUMA node(s):                       2
NUMA node0 CPU(s):                  0-55,112-167
NUMA node1 CPU(s):                  56-111,168-223
Vulnerability Gather data sampling: Not affected
Vulnerability Itlb multihit:        Not affected
Vulnerability L1tf:                 Not affected
Vulnerability Mds:                  Not affected
Vulnerability Meltdown:             Not affected
Vulnerability Mmio stale data:      Not affected
Vulnerability Retbleed:             Not affected
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass:    Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:           Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:           Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected

Versions of relevant libraries:
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.24.4
[pip3] nvidia-cudnn-frontend==1.7.0
[pip3] nvidia-nccl-cu12==2.22.3
[pip3] nvtx==0.2.5
[pip3] onnx==1.16.2
[pip3] optree==0.13.0
[pip3] pynvjitlink==0.3.0
[pip3] pytorch-triton==3.0.0+dedb7bdf3
[pip3] torch==2.5.0a0+e000cf0ad9.nv24.10
[pip3] torch-fidelity==0.3.0
[pip3] torch_tensorrt==2.5.0a0
[pip3] torchmetrics==1.5.2
[pip3] torchprofile==0.0.4
[pip3] torchvision==0.20.0a0

cc @H-Huang @awgu @kwen2501 @wanchaol @fegin @fduwjj @wz337 @wconstab @d4l3k @c-p-i-o