Repository: pytorch
Issue: torchrun: elastic training not restarted on missing keep-alive heartbeat/scale-down event
Labels: oncall: distributed

### 🐛 Describe the bug

When running elastic training with C10d backend and multiple nodes, the workers need to be restarted in case of a down-scale event. If this does not happen, as right now, the remaining workers get stuck in NCCL operations and wait by default 30 minutes until they finish with a timeout error.

*Expected Behaviour*
If a worker misses its heartbeat/leaves the rendevous, a new rendevous should happen.

*Minimal Example*

There are two scripts for the master worker and a faulty worker

**master**
```python
import torch.distributed.run
import logging
logging.getLogger("torch.distributed.elastic.rendezvous.dynamic_rendezvous").level=10
torch.distributed.run.main(["--nnodes=1:4","--rdzv-backend=c10d",
                            "--rdzv-endpoint=localhost:9999",
                            "--rdzv-conf=last_call_timeout=10",
                            "--no-python",
                            "bash","-c",f"echo start; sleep 600; echo done"])
```

**faulty**
```python
import torch.distributed.run
import logging
logging.getLogger("torch.distributed.elastic.rendezvous.dynamic_rendezvous").level=10
torch.distributed.run.main(["--nnodes=1:4","--rdzv-backend=c10d",
                            "--local-addr=faulty",
                            "--rdzv-endpoint=localhost:9999",
                            "--rdzv-conf=last_call_timeout=10",
                            "--no-python",
                            "bash","-c",f"echo start; sleep 10; kill -9 $$PPID"])
```
The minimal example enables debug logging to highlight the problematic area.
It does the following:
**master** runs normally, while **faulty** simulates a forceful termination after 10 seconds.
Both ranks print `start` when a new generation is started.

Run first master and then faulty script.

The output 
```
[2023-10-20 11:55:30,978] torch.distributed.run: [WARNING] master_addr is only used for static rdzv_backend and when rdzv_endpoint is not specified.
[2023-10-20 11:55:30,983] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [INFO] The node 'georg-AERO-15-YC_945987_0' attempts to join the next round of the rendezvous 'none'.
[2023-10-20 11:55:31,071] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' added itself to the participants of round 0 of the rendezvous 'none'. Pending sync.
[2023-10-20 11:55:31,077] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' updated its keep-alive heartbeat time for the rendezvous 'none'. Pending sync.
[2023-10-20 11:55:31,080] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' has successfully synced its local changes with other nodes in the rendezvous 'none'.
[2023-10-20 11:55:36,088] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' updated its keep-alive heartbeat time for the rendezvous 'none'. Pending sync.
[2023-10-20 11:55:36,093] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' has successfully synced its local changes with other nodes in the rendezvous 'none'.
[2023-10-20 11:55:41,102] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' marked round 0 of the rendezvous 'none' as complete. Pending sync.
[2023-10-20 11:55:41,107] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' has successfully synced its local changes with other nodes in the rendezvous 'none'.
[2023-10-20 11:55:41,110] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [INFO] The node 'georg-AERO-15-YC_945987_0' has joined round 0 of the rendezvous 'none' as rank 0 in a world of size 2.
start
[2023-10-20 11:55:46,112] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' updated its keep-alive heartbeat time for the rendezvous 'none'. Pending sync.
[2023-10-20 11:55:46,115] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' has successfully synced its local changes with other nodes in the rendezvous 'none'.
[2023-10-20 11:55:46,117] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' has sent a keep-alive heartbeat to the rendezvous 'none'.
[2023-10-20 11:55:51,121] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' updated its keep-alive heartbeat time for the rendezvous 'none'. Pending sync.
[2023-10-20 11:55:51,124] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' has successfully synced its local changes with other nodes in the rendezvous 'none'.
[2023-10-20 11:55:51,126] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' has sent a keep-alive heartbeat to the rendezvous 'none'.
[2023-10-20 11:55:56,129] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' updated its keep-alive heartbeat time for the rendezvous 'none'. Pending sync.
[2023-10-20 11:55:56,132] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' has successfully synced its local changes with other nodes in the rendezvous 'none'.
[2023-10-20 11:55:56,134] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' has sent a keep-alive heartbeat to the rendezvous 'none'.
[2023-10-20 11:56:01,138] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' updated its keep-alive heartbeat time for the rendezvous 'none'. Pending sync.
[2023-10-20 11:56:01,140] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' has successfully synced its local changes with other nodes in the rendezvous 'none'.
[2023-10-20 11:56:01,141] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' has sent a keep-alive heartbeat to the rendezvous 'none'.
[2023-10-20 11:56:06,144] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' updated its keep-alive heartbeat time for the rendezvous 'none'. Pending sync.
[2023-10-20 11:56:06,147] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' has successfully synced its local changes with other nodes in the rendezvous 'none'.
[2023-10-20 11:56:06,150] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' has sent a keep-alive heartbeat to the rendezvous 'none'.
[2023-10-20 11:56:11,153] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' updated its keep-alive heartbeat time for the rendezvous 'none'. Pending sync.
[2023-10-20 11:56:11,156] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] As part of the sync operation the node(s) 'faulty_945990_0' have been removed from the rendezvous 'none' since they had no heartbeat.
[2023-10-20 11:56:11,156] torch.distributed.elastic.rendezvous.dynamic_rendezvous: [DEBUG] The node 'georg-AERO-15-YC_945987_0' has successfully synced its local changes with other nodes in the rendezvous 'none'.
```

Expected would now be that after the missing heartbeat and removal of faulty the rendezvous process should be started again, as is also documented in [rdzv_doc](https://github.com/pytorch/pytorch/blob/619ae87a1d1ae086f59a64d3b71dbfe4af8b804a/docs/source/elastic/etcd_rdzv_diagram.png) also the [__init__.py](https://github.com/pytorch/pytorch/blob/619ae87a1d1ae086f59a64d3b71dbfe4af8b804a/torch/distributed/elastic/rendezvous/__init__.py#L71) mention a restart in such a case.


### Versions

```
Collecting environment information...
PyTorch version: 2.1.0
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.3 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.26.4
Libc version: glibc-2.35

Python version: 3.11.5 (main, Sep 11 2023, 13:54:46) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.15.0-86-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 GeForce RTX 3080 Laptop GPU
Nvidia driver version: 535.104.12
cuDNN version: Could not collect
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:                      39 bits physical, 48 bits virtual
Byte Order:                         Little Endian
CPU(s):                             16
On-line CPU(s) list:                0-15
Vendor ID:                          GenuineIntel
Model name:                         Intel(R) Core(TM) i9-10980HK CPU @ 2.40GHz
CPU family:                         6
Model:                              165
Thread(s) per core:                 2
Core(s) per socket:                 8
Socket(s):                          1
Stepping:                           2
CPU max MHz:                        5300,0000
CPU min MHz:                        800,0000
BogoMIPS:                           6199.99
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 pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp pku ospke md_clear flush_l1d arch_capabilities
Virtualization:                     VT-x
L1d cache:                          256 KiB (8 instances)
L1i cache:                          256 KiB (8 instances)
L2 cache:                           2 MiB (8 instances)
L3 cache:                           16 MiB (1 instance)
NUMA node(s):                       1
NUMA node0 CPU(s):                  0-15
Vulnerability Gather data sampling: Mitigation; Microcode
Vulnerability Itlb multihit:        KVM: Mitigation: VMX disabled
Vulnerability L1tf:                 Not affected
Vulnerability Mds:                  Not affected
Vulnerability Meltdown:             Not affected
Vulnerability Mmio stale data:      Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed:             Mitigation; Enhanced IBRS
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:                Mitigation; Microcode
Vulnerability Tsx async abort:      Not affected

Versions of relevant libraries:
[pip3] mypy==1.6.0
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.0
[pip3] optree==0.9.1
[pip3] torch==2.1.0
[pip3] torchaudio==2.1.0
[pip3] torchvision==0.16.0
[pip3] triton==2.1.0
[conda] blas                      1.0                         mkl  
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] libjpeg-turbo             2.0.0                h9bf148f_0    pytorch
[conda] mkl                       2023.1.0         h213fc3f_46343  
[conda] mkl-include               2023.1.0         h06a4308_46343  
[conda] mkl-service               2.4.0           py311h5eee18b_1  
[conda] mkl_fft                   1.3.8           py311h5eee18b_0  
[conda] mkl_random                1.2.4           py311hdb19cb5_0  
[conda] numpy                     1.26.0          py311h08b1b3b_0  
[conda] numpy-base                1.26.0          py311hf175353_0  
[conda] optree                    0.9.1                    pypi_0    pypi
[conda] pytorch                   2.1.0           py3.11_cuda11.8_cudnn8.7.0_0    pytorch
[conda] pytorch-cuda              11.8                 h7e8668a_5    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torchaudio                2.1.0               py311_cu118    pytorch
[conda] torchtriton               2.1.0                     py311    pytorch
[conda] torchvision               0.16.0              py311_cu118    pytorch
```


cc @mrshenli @pritamdamania87 @zhaojuanmao @satgera @rohan-varma @gqchen @aazzolini @osalpekar @jiayisuse @H-Huang @kwen2501 @awgu @penguinwu