[Bug] _NcclGroup.recv() synchronizes _recv_stream before comm.recv() and omits post-recv synchronization, returning stale buffer contents
Summary
In python/ray/experimental/channel/nccl_group.py (commit e4462656e827790245c66d69e800cdfd8df0630a, lines 208-240), when use_communication_streams=True, _NcclGroup.recv() calls self._recv_stream.synchronize() before enqueuing self._comm.recv(buf, peer_rank, stream=self._recv_stream) and returns buf without synchronizing or inserting wait_stream(self._recv_stream) on the caller's stream after recv:
# python/ray/experimental/channel/nccl_group.py:208-240
if self._use_communication_streams:
self._recv_stream.synchronize()
self._comm.recv(...)
# missing post-recv wait_stream / synchronize before returning buf
Because self._comm.recv runs asynchronously on self._recv_stream, returning buf without waiting for self._recv_stream allows any default-stream kernel or clone executed by the caller to read stale zeros/previous contents from buf—directly violating _NcclGroup.recv()'s guarantee that buf is safe to consume on return.
Affected Location
- Repository:
ray-project/ray(e4462656e827790245c66d69e800cdfd8df0630a) - File & lines:
python/ray/experimental/channel/nccl_group.py:208-240(_NcclGroup.recv)
Minimal Standalone Reproduction
Executed across 2x NVIDIA A800-SXM4-80GB ranks with NCCL 2.27.5 (use_communication_streams pattern, 16,777,216 float32 elements, expected value 33.0):
- Rank 0 queues GPU work then sends
33.0on a communication stream. - Rank 1 pre-syncs
comm_stream, issuesdist.recv(buf, src=0)insidewith torch.cuda.stream(comm_stream):, and immediately clonesbufon the default stream withoutwait_stream(comm_stream).
Observed behavior vs. waited-stream control:
- Without post-recv
wait_stream: Rank 1 observed 8/8 stale-read runs with 16,777,216 / 16,777,216 elements reading0.0instead of33.0. - With
torch.cuda.current_stream(device).wait_stream(comm_stream)afterrecv: 0/4 stale runs.
Suggested Fix
- Call
torch.cuda.current_stream().wait_stream(self._recv_stream)(or record/wait a CUDA event orself._recv_stream.synchronize(), matching theuse_communication_streams=Falseguarantee) afterself._comm.recv(...)before returningbuf. - Add a 2-GPU NCCL regression test checking immediate default-stream reads after
_NcclGroup.recv().
Hi, I'd like to work on this issue. I'll first investigate the existing NCCL communication-stream handling and regression-test structure, then work on a minimal fix that preserves the expected synchronization guarantees. I'll share my findings and proposed changes before opening a PR.