Skip to content

[C10d][Gloo] Enable complex datatype support in ProcessGroupGloo #156633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build_variables.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ libtorch_distributed_base_sources = [
"torch/csrc/distributed/c10d/TCPStore.cpp",
"torch/csrc/distributed/c10d/TCPStoreBackend.cpp",
"torch/csrc/distributed/c10d/TCPStoreLibUvBackend.cpp",
"torch/csrc/distributed/c10d/Types.cpp",
"torch/csrc/distributed/c10d/Utils.cpp",
"torch/csrc/distributed/c10d/Work.cpp",
"torch/csrc/distributed/c10d/comm.cpp",
Expand Down
16 changes: 14 additions & 2 deletions torch/csrc/distributed/c10d/ProcessGroupGloo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,9 @@ class AsyncBroadcastWork : public ProcessGroupGloo::AsyncWork {
const uint32_t tag;

void broadcast(at::Tensor& tensor) {
if (tensor.is_complex()) {
tensor = at::view_as_real(tensor);
}
const auto& scalarType = tensor.scalar_type();
gloo::BroadcastOptions opts(context_);
opts.setRoot(rootRank);
Expand Down Expand Up @@ -1061,12 +1064,21 @@ class AsyncReduceWork : public ProcessGroupGloo::AsyncWork {
const uint32_t tag;

void reduce(std::vector<at::Tensor>& tensors) {
const auto& scalarType = tensors[0].scalar_type();
auto tensor = tensors[0];
if (tensor.is_complex()) {
TORCH_CHECK(
c10d::complexViewAsRealAllowed(reduceOp),
"reduce does not support",
reduceOp,
"on complex tensors");
tensor = at::view_as_real(tensor);
}
gloo::ReduceOptions opts(context_);
const auto& scalarType = tensor.scalar_type();
opts.setRoot(rootRank);
opts.setTag(tag);
opts.setReduceFunction(getFunction(scalarType, reduceOp));
GENERATE_ALL_TYPES(scalarType, setOutput, opts, tensors[0]);
GENERATE_ALL_TYPES(scalarType, setOutput, opts, tensor);
gloo::reduce(opts);

// Gloo doesn't support AVG so we use SUM + division.
Expand Down
11 changes: 10 additions & 1 deletion torch/csrc/distributed/c10d/ProcessGroupGlooDetail.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,17 @@ class AsyncAllreduceWork : public ProcessGroupGloo::AsyncWork {
const uint32_t tag;

void allreduce(std::vector<at::Tensor>& tensors) {
const auto& scalarType = tensors[0].scalar_type();
auto tensor = tensors[0];
if (tensor.is_complex()) {
TORCH_CHECK(
c10d::complexViewAsRealAllowed(reduceOp),
"all_reduce does not support",
reduceOp,
"on complex tensors");
tensor = at::view_as_real(tensor);
}
gloo::AllreduceOptions opts(context_);
const auto& scalarType = tensor.scalar_type();
opts.setReduceFunction(getFunction(scalarType, reduceOp));
opts.setTag(tag);
GENERATE_ALL_TYPES(scalarType, setOutputs, opts, tensors);
Expand Down
21 changes: 2 additions & 19 deletions torch/csrc/distributed/c10d/ProcessGroupNCCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,6 @@ inline bool isUnsupportedFloat8(at::ScalarType t) {
);
}

bool complexViewAsRealAllowed(const ReduceOp& reduceOp) {
switch (reduceOp) {
// NOLINTNEXTLINE(bugprone-branch-clone)
case ReduceOp::SUM:
return true;
case ReduceOp::AVG:
return true;
case ReduceOp::PREMUL_SUM:
return true;
case ReduceOp::UNUSED:
return true;
default:
return false;
}
return false;
}

#ifdef ENABLE_NCCL_PREMUL_SUM_SUPPORT
template <typename T, ncclDataType_t dataType>
ncclRedOpRAII unpackPreMulSum(
Expand Down Expand Up @@ -4392,7 +4375,7 @@ c10::intrusive_ptr<Work> ProcessGroupNCCL::allreduce(
auto tensor = tensors.back();
if (tensor.is_complex()) {
TORCH_CHECK(
complexViewAsRealAllowed(opts.reduceOp),
c10d::complexViewAsRealAllowed(opts.reduceOp),
"all_reduce does not support",
opts.reduceOp,
"on complex tensors");
Expand Down Expand Up @@ -4586,7 +4569,7 @@ c10::intrusive_ptr<Work> ProcessGroupNCCL::reduce(
auto tensor = tensors.back();
if (tensor.is_complex()) {
TORCH_CHECK(
complexViewAsRealAllowed(opts.reduceOp),
c10d::complexViewAsRealAllowed(opts.reduceOp),
"reduce does not support",
opts.reduceOp,
"on complex tensors");
Expand Down
22 changes: 22 additions & 0 deletions torch/csrc/distributed/c10d/Types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <torch/csrc/distributed/c10d/Types.hpp>

namespace c10d {

bool complexViewAsRealAllowed(const ReduceOp& reduceOp) {
switch (reduceOp) {
// NOLINTNEXTLINE(bugprone-branch-clone)
case ReduceOp::SUM:
return true;
case ReduceOp::AVG:
return true;
case ReduceOp::PREMUL_SUM:
return true;
case ReduceOp::UNUSED:
return true;
default:
return false;
}
return false;
}

} // namespace c10d
2 changes: 2 additions & 0 deletions torch/csrc/distributed/c10d/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ ReduceOp makeNCCLPreMulSum(const T& factor) {
return rop;
}

TORCH_API bool complexViewAsRealAllowed(const ReduceOp& reduceOp);

constexpr auto kUnsetTimeout = std::chrono::milliseconds(-1);

struct BroadcastOptions {
Expand Down