Does torch support custom stream for nccl commucation now?

shadowshadow: <p>There are many discussion and posts saying that torch can’t use custom stream for nccl communcation. Since <code>ProcessGroupNCCL</code> maintains its internal stream pool for nccl.</p><aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/136187">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/136187" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue" data-github-private-repo="false">
	  <svg width="60" height="60" class="github-icon" viewBox="0 0 14 16" aria-hidden="true"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/136187" target="_blank" rel="noopener nofollow ugc">Unable to Specify CUDA Stream for Collective Operations Using with torch.cuda.stream() context</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2024-09-17" data-time="06:07:11" data-timezone="UTC">06:07AM - 17 Sep 24 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/nariaki3551" target="_blank" rel="noopener nofollow ugc">
          <img alt="" src="https://avatars.githubusercontent.com/u/21208556?v=4" class="onebox-avatar-inline" width="20" height="20">
          nariaki3551
        </a>
      </div>
    </div>

    <div class="labels">
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          oncall: distributed
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: cuda
        </span>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">### 🐛 Describe the bug

I found the example that specifying a CUDA stream usin<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">g a with-context does not work as expected.

```python
    with torch.cuda.stream(my_stream):
        dist.all_reduce(source)
```

The full test script is provided below. I executed it with `NCCL_DEBUG=INFO NCCL_DEBUG_SUBSYS=ALL OMP_NUM_THREADS=1 torchrun --nnodes 1 --nproc_per_node 2 test.py`.

```python
import os
import torch
import torch.distributed as dist

def run(rank, size):

    device = torch.device(f'cuda:{rank}')
    torch.cuda.set_device(device)

    # create cuda streams
    my_stream = torch.cuda.Stream(device)
    if rank == 0:
        print("[0] NCCL USER stream: ", my_stream)

    # source/output tensors
    source = torch.ones(10, dtype=torch.float64, device=device)

    # execute allreduce with my_stream
    with torch.cuda.stream(my_stream):
        dist.all_reduce(source)

    dist.destroy_process_group()

if __name__ == "__main__":
    world_size = int(os.environ['WORLD_SIZE'])
    rank = int(os.environ['LOCAL_RANK'])

    dist.init_process_group("nccl", rank=rank, world_size=world_size)
    run(rank, world_size)
```

I expected that the allgather operation would be executed on my_stream, but it seems that it was actually performed on a different CUDA stream. Below is a snippet of the output:

&gt; [0] NCCL USER stream:  &lt;torch.cuda.Stream device=cuda:0 cuda_stream=**0x55f31fee0fd0**&gt;
snail01:2307143:2307143 [0] NCCL INFO AllReduce: opCount 0 sendbuff 0x7f6c97600000 recvbuff 0x7f6c97600000 count 10 datatype 8 op 0 root 0 comm 0x55f32103c600 [nranks=2] stream **0x55f31fe42810**

My question is:
Q: Is there any way to explicitly specify the CUDA stream for collective operations?

Thank you in advance!


### Versions

```
Collecting environment information...
PyTorch version: 2.4.0a0
Is debug build: False
CUDA used to build PyTorch: 12.4
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.6 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
Clang version: Could not collect
CMake version: version 3.22.3
Libc version: glibc-2.31

Python version: 3.10.8 (main, Nov 15 2022, 23:41:17) [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-5.4.0-193-generic-x86_64-with-glibc2.31
Is CUDA available: True
CUDA runtime version: 12.4.131
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA A100-PCIE-40GB
GPU 1: NVIDIA A100-PCIE-40GB
GPU 2: Tesla V100-PCIE-16GB
GPU 3: Tesla V100S-PCIE-32GB

Nvidia driver version: 560.35.03
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
Byte Order:                         Little Endian
Address sizes:                      46 bits physical, 48 bits virtual
CPU(s):                             56
On-line CPU(s) list:                0-55
Thread(s) per core:                 2
Core(s) per socket:                 14
Socket(s):                          2
NUMA node(s):                       2
Vendor ID:                          GenuineIntel
CPU family:                         6
Model:                              79
Model name:                         Intel(R) Xeon(R) CPU E5-2660 v4 @ 2.00GHz
Stepping:                           1
CPU MHz:                            1201.536
CPU max MHz:                        3200.0000
CPU min MHz:                        1200.0000
BogoMIPS:                           3999.71
Virtualization:                     VT-x
L1d cache:                          896 KiB
L1i cache:                          896 KiB
L2 cache:                           7 MiB
L3 cache:                           70 MiB
NUMA node0 CPU(s):                  0-13,28-41
NUMA node1 CPU(s):                  14-27,42-55
Vulnerability Gather data sampling: Not affected
Vulnerability Itlb multihit:        KVM: Mitigation: Split huge pages
Vulnerability L1tf:                 Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Vulnerability Mds:                  Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown:             Mitigation; PTI
Vulnerability Mmio stale data:      Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed:             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; Retpolines; IBPB conditional; IBRS_FW; STIBP conditional; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Mitigation; Clear CPU buffers; SMT vulnerable
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 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 pti intel_ppin ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap intel_pt xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts md_clear flush_l1d

Versions of relevant libraries:
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.26.4
[pip3] torch==2.4.0a0+gitunknown
[pip3] torchvision==0.15.0a0+0dceac0
[pip3] triton==3.0.0
[conda] Could not collect
```

cc @XilunWu @H-Huang @awgu @kwen2501 @wanchaol @fegin @fduwjj @wz337 @wconstab @d4l3k @c-p-i-o @ptrblck @msaroufim</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
<aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/137390">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/137390" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue" data-github-private-repo="false">
	  <svg width="60" height="60" class="github-icon" viewBox="0 0 14 16" aria-hidden="true"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/137390" target="_blank" rel="noopener nofollow ugc">Allow passing CUDA stream to the NCCL collectives (specially the functional collectives)</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2024-10-05" data-time="04:23:40" data-timezone="UTC">04:23AM - 05 Oct 24 UTC</span>
      </div>

        <div class="date">
          closed <span class="discourse-local-date" data-format="ll" data-date="2024-11-01" data-time="23:02:07" data-timezone="UTC">11:02PM - 01 Nov 24 UTC</span>
        </div>

      <div class="user">
        <a href="https://github.com/mayank31398" target="_blank" rel="noopener nofollow ugc">
          <img alt="" src="https://avatars.githubusercontent.com/u/32954280?v=4" class="onebox-avatar-inline" width="20" height="20">
          mayank31398
        </a>
      </div>
    </div>

    <div class="labels">
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          oncall: distributed
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: nccl
        </span>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">### 🚀 The feature, motivation and pitch

Currently, I dont see any way to run a <span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">NCCL collective on a custom stream.
This is a big limitation and leading frameworks like vLLM have completely ditched PyTorch distributed API and use their own NCCL wrappers like here: https://github.com/vllm-project/vllm/blob/a95354a36ee65523a499b3eb42f70a4a0ea4322d/vllm/distributed/device_communicators/pynccl.py#L104

Is it possible to run the NCCL collectives on the stream that is set by the user context in PyTorch?
This would be a great addition.

### Alternatives

_No response_

### Additional context

_No response_

cc @XilunWu @H-Huang @awgu @kwen2501 @wanchaol @fegin @fduwjj @wz337 @wconstab @d4l3k @c-p-i-o</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<pre data-code-wrap="cpp"><code class="lang-cpp">  // The CUDA streams used by NCCL kernels
  std::unordered_map&lt;std::string, at::cuda::CUDAStream&gt; ncclStreams_;
</code></pre>
<p>when we write code like below, it actually doesn’t really use <code>comm_stream</code> but as a <code>dummy compute stream</code> since nccl will make commucation wait for current stream to ensure input data ready.</p>
<pre data-code-wrap="python"><code class="lang-python">a = some_compute()
with torch.cuda.stream(comm_stream):
  dist.allgather(output, a)
</code></pre>
<p>But when I dive into ProcessGroupNCCL. I find torch will choose nccl stream based on whether it’s an async op. If it is, torch will use internal stream from <code>ncclStreams_</code> which is a map from device to cuda stream. otherwise, it will simply use current cuda stream</p>
<pre data-code-wrap="cpp"><code class="lang-cpp">template &lt;typename Fn, typename PreProcess, typename PostProcess&gt;
c10::intrusive_ptr&lt;Work&gt; ProcessGroupNCCL::collective(
    std::vector&lt;at::Tensor&gt;&amp; inputs,
    std::vector&lt;at::Tensor&gt;&amp; outputs,
    Fn fn,
    PreProcess pre,
    PostProcess post,
    OpType opType,
    bool asyncOp,
    const char* profilingTitle,
    bool nanCheck) {

  // ....

  // in asyncOp=false [default] mode, we use currentStream as ncclStream
  // otherwise, we use separate ncclStream and let it sync on currentStream
  auto ncclStream = asyncOp ? ncclStreams_.at(key)
                            : at::cuda::getCurrentCUDAStream(device.index());
</code></pre>
<p>The two statements above seem somewhat contradictory, and I’m very curious which one is correct. Can torch actually customize the stream that NCCL will use?</p>

H-Huang: <p>The ProcessGroupNCCL code is what is correct, when you do:</p>
<pre data-code-wrap="python"><code class="lang-python">with torch.cuda.stream(comm_stream):
  dist.allgather(output, a)
</code></pre>
<p>The allgather will happen in the <code>comm_stream</code>. If no stream is specified and it is async, then it will use the internal stream.</p>

shadowshadow: <p>Thanks,so torch has already supported custom NCCL streams to some extent.</p>

shadowshadow: <p>I find only latest torch version can use user specified nccl stream</p>
<p>In torch 2.7, <code>ProcessGroupNCCL</code> just use its internal stream</p>
<pre><code class="lang-auto">// Used many times below, so we stash the unordered_map lookup
auto ncclStream = ncclStreams_.at(key);
</code></pre>

cokespace2: <p>In which version torch has supported custom communication stream?</p>

shadowshadow: <p>torch 2.8 does support.</p>