PyTorch refines nccl2 backend with cleaner C++ and better error handling
New commit addresses stack review feedback for PyTorch's distributed training backend...
PyTorch's latest trunk commit addresses review comments on the nccl2 stack (PRs #188582-#188588) for the ProcessGroupNCCL backend used in distributed training. The changes focus on code quality: getErrorString now returns std::string_view instead of const char*, with the one caller (NCCLException) switching to fmt::format since std::string + std::string_view requires C++26. Unused-parameter suppressions are collapsed from multiple (void)x; statements into a single std::ignore = std::tie(...). Shared_ptr and intrusive_ptr constructor arguments in NCCLBootstrap are now std::move'd for efficiency.
The naming conventions are also updated: the 'TC' suffix is removed, the backend lives in the c10d::nccl2 namespace, and the Python binding uses ProcessGroupNCCL2 to avoid pybind name clashes with the built-in ProcessGroupNCCL. A distinct 'nccl2' backend is registered for CUDA without overriding the existing 'nccl' registration. The commit was authored with assistance from Claude and passes tests via pip install -e . and python test/distributed/test_c10d_nccl2.py.
- Error strings now use std::string_view instead of const char*, with fmt::format for concatenation
- Raw std::runtime_error calls replaced with TORCH_CHECK for consistent error handling
- Backend renamed to c10d::nccl2 namespace and exposed as ProcessGroupNCCL2 in Python to avoid clash
Why It Matters
Cleaner, more modern C++ in PyTorch's distributed backend means better maintainability and fewer bugs for multi-GPU training.