Repository: pytorch
Issue: Async NCCL communication blocks CUDA kernel in the first run
Labels: oncall: distributed

### 🐛 Describe the bug

Async NCCL comminucations from `torch.distributed` should run in parallel with CUDA computing kernels, but traces from `torch.profiler` shows it is not true for the first run. However, the asynchronicity works from the second run.

Reproduction:

```python
import torch

torch.distributed.init_process_group(backend='nccl')
rank = torch.distributed.get_rank()

torch.cuda.set_device(rank)

torch.distributed.barrier()

with torch.profiler.profile(
    activities=[
        torch.profiler.ProfilerActivity.CPU,
        torch.profiler.ProfilerActivity.CUDA,
    ]
) as p:

    if rank == 0:
        x = torch.empty(1000000000, device='cuda')
        x.fill_(2.3)
        work = torch.distributed.isend(x, 1)
        work.wait()
    else:
        y = torch.empty(1000000000, device='cuda')
        work = torch.distributed.irecv(y, 0)
        a = torch.ones((10000, 10000), dtype=torch.float32, device='cuda')
        b = torch.ones((10000, 10000), dtype=torch.float32, device='cuda')
        c = a @ b
        work.wait()

    if rank == 0:
        x = torch.empty(1000000000, device='cuda')
        x.fill_(2.3)
        work = torch.distributed.isend(x, 1)
        work.wait()
    else:
        y = torch.empty(1000000000, device='cuda')
        work = torch.distributed.irecv(y, 0)
        a = torch.ones((10000, 10000), dtype=torch.float32, device='cuda')
        b = torch.ones((10000, 10000), dtype=torch.float32, device='cuda')
        c = a @ b
        work.wait()

p.export_chrome_trace(f"rank{rank}.json")
```

The trace from Rank 1 (the receiving rank) is like this:

![image](https://github.com/user-attachments/assets/8f598556-0e5f-4a47-9238-2ba2e41dee49)


The two pairs of `nccl:recv` and `volta_sgemm_128x64tn` should both overlap, but only one of them overlaps.



### Versions

```
Collecting environment information...
PyTorch version: 2.3.0+cu118
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A

OS: Debian GNU/Linux 12 (bookworm) (x86_64)
GCC version: (Spack GCC) 11.3.0
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.36

Python version: 3.9.12 (main, Nov  2 2022, 21:57:59)  [GCC 10.2.1 20210110] (64-bit runtime)
Python platform: Linux-6.1.0-22-amd64-x86_64-with-glibc2.36
Is CUDA available: False
CUDA runtime version: 11.8.89
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
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:                        46 bits physical, 48 bits virtual
Byte Order:                           Little Endian
CPU(s):                               96
On-line CPU(s) list:                  0-95
Vendor ID:                            GenuineIntel
Model name:                           Intel(R) Xeon(R) Gold 6252 CPU @ 2.10GHz
CPU family:                           6
Model:                                85
Thread(s) per core:                   2
Core(s) per socket:                   24
Socket(s):                            2
Stepping:                             7
CPU(s) scaling MHz:                   37%
CPU max MHz:                          3700.0000
CPU min MHz:                          1000.0000
BogoMIPS:                             4200.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 pni pclmulqdq dtes64 monitor 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 cdp_l3 invpcid_single intel_ppin 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 mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts pku ospke avx512_vnni md_clear flush_l1d arch_capabilities
Virtualization:                       VT-x
L1d cache:                            1.5 MiB (48 instances)
L1i cache:                            1.5 MiB (48 instances)
L2 cache:                             48 MiB (48 instances)
L3 cache:                             71.5 MiB (2 instances)
NUMA node(s):                         2
NUMA node0 CPU(s):                    0-23,48-71
NUMA node1 CPU(s):                    24-47,72-95
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 Reg file data sampling: Not affected
Vulnerability Retbleed:               Mitigation; Enhanced IBRS
Vulnerability Spec rstack overflow:   Not affected
Vulnerability Spec store bypass:      Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:             Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:             Mitigation; Enhanced / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS SW sequence; BHI SW loop, KVM SW loop
Vulnerability Srbds:                  Not affected
Vulnerability Tsx async abort:        Mitigation; TSX disabled

Versions of relevant libraries:
[pip3] numpy==1.26.3
[pip3] torch==2.3.0+cu118
[pip3] triton==2.3.0
[conda] Could not collect
```

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