Fixes #179908
Summary
torch.cuda.nccl.broadcast(tensors, root=N) silently ignores root and always broadcasts from tensors[0], while nccl.reduce honors root — full root-cause chain in my issue comment:
torch/cuda/nccl.py::broadcastpassesroottotorch._C._nccl_broadcastcorrectly.THCPModule_nccl_broadcast(torch/csrc/cuda/python_nccl.cpp) parses and validatesroot, then callstorch::cuda::nccl::broadcast(inputs, streams, user_comms)without it.torch::cuda::nccl::broadcast(torch/csrc/cuda/nccl.cpp) has no root parameter and hardcodes0in thencclBcastcall.
The bug is old: v0.3.0 passed root through to ncclBcast; commit de5f7b7251f ("Base for pure C++ NCCL interface", Dec 2017) dropped it when the C++ helper was extracted, so the binding has been wrong since v0.4.0 (~8.5 years).
Fix
torch/csrc/cuda/nccl.h/nccl.cpp: addint32_t root = 0totorch::cuda::nccl::broadcast, mirroring the existingreducesignature (defaulted, so the internal caller intorch/csrc/cuda/comm.cpp::broadcast_coalescedis unaffected); validate it in-range exactly likereducedoes; pass it toncclBcast.torch/csrc/cuda/python_nccl.cpp: forward the already-parsedroot.test/distributed/test_nccl.py: extendtest_broadcastwith a non-zero-root regression block (root = nGPUs - 1; the root device holds auniform_()tensor, all other devices hold zeros; assert every device ends up with the root's values). Before this fix the broadcast originates from the zeros at index 0, so the new assertions fail; after the fix they pass. It runs under the existingTEST_MULTIGPUskip and@dtypes(*broadcast_dtypes)(incl. float8) conventions.
Test plan
- New regression assertions in
test/distributed/test_nccl.py::TestNCCL::test_broadcast(multi-GPU). lintrunner -aon the four touched files: no lint issues (CLANGTIDY skipped locally — requires a build dir).- Compile-verified the touched translation units (
nccl.cpp,python_nccl.cpp) plus the unmodified internal caller (comm.cpp) withg++ -fsyntax-onlyagainst the modified header: all clean. - Validation gap, stated honestly: my box has a single GPU, so I could not run the multi-GPU broadcast path locally;
python test/distributed/test_nccl.py -k broadcastcollects and skip-passes here (OK (skipped=2)). The behavioral coverage relies on PyTorch's multi-GPU CI exercising the extendedtest_broadcast.
🤖 Generated with Claude Code
Pull Request resolved: #187216
Approved by: https://github.com/d4l3k
SOCIAL SHARE CARD GENERATOR