nccl_op.cu.cc 6.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
D
Dong Zhihong 已提交
2 3 4
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
D
Dong Zhihong 已提交
5 6
http://www.apache.org/licenseshashernless required by applicable law or agreed
to in writing, software
D
Dong Zhihong 已提交
7 8 9 10 11
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

D
Dong Zhihong 已提交
12
#include <functional>
13
#include <unordered_map>
D
Dong Zhihong 已提交
14

Y
Yi Wang 已提交
15 16 17
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/nccl/nccl_gpu_common.h"
D
Dong Zhihong 已提交
18 19 20 21

namespace paddle {
namespace operators {

22 23
using framework::Tensor;
using platform::Communicator;
D
Dong Zhihong 已提交
24
using framework::LoDTensor;
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

template <typename Type>
class NCCLTypeWrapper;

template <>
class NCCLTypeWrapper<float> {
 public:
  static const ncclDataType_t type = ncclFloat;
};

template <>
class NCCLTypeWrapper<double> {
 public:
  static const ncclDataType_t type = ncclDouble;
};

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
static ncclRedOp_t str_to_nccl_red_type(std::string reduction) {
  static const std::unordered_map<std::string, ncclRedOp_t> str_to_type = {
      {"ncclSum", ncclSum},
      {"ncclMin", ncclMin},
      {"ncclMax", ncclMax},
      {"ncclProd", ncclProd},
  };
  auto it = str_to_type.find(reduction);
  PADDLE_ENFORCE_EQ(it != str_to_type.end(), true,
                    platform::errors::InvalidArgument(
                        "Invalid nccl reduction. Must be ncclMin | ncclMax | "
                        "ncclProd | ncclSum"));
  return it->second;
}

D
Dong Zhihong 已提交
56 57 58 59
template <typename T>
class NCCLAllReduceKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
60 61 62
    PADDLE_ENFORCE_EQ(platform::is_gpu_place(ctx.GetPlace()), true,
                      platform::errors::PreconditionNotMet(
                          "This kernel only runs on GPU device."));
D
dzhwinter 已提交
63 64 65
    auto* x = ctx.Input<LoDTensor>("X");
    auto* out = ctx.Output<LoDTensor>("Out");
    auto* comm = ctx.Input<Communicator>("Communicator");
D
Dong Zhihong 已提交
66 67
    std::string reduction = ctx.Attr<std::string>("reduction");

68 69
    auto reduction_op_ = str_to_nccl_red_type(reduction);

D
Dong Zhihong 已提交
70
    // device id
D
dzhwinter 已提交
71
    int gpu_id = boost::get<platform::CUDAPlace>(ctx.GetPlace()).GetDeviceId();
D
Dong Zhihong 已提交
72
    int idx = comm->GetCommId(gpu_id);
M
minqiyang 已提交
73 74 75
    VLOG(3) << "gpu : "
            << " invoke allreduce. send " << x->numel() << " recv "
            << out->numel();
76
    PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::ncclAllReduce(
D
dzhwinter 已提交
77 78 79
        x->data<T>(), out->mutable_data<T>(ctx.GetPlace()), out->numel(),
        NCCLTypeWrapper<T>::type, reduction_op_, comm->comms().at(idx),
        ctx.cuda_device_context().stream()));
M
minqiyang 已提交
80 81 82
    VLOG(3) << "gpu : "
            << " finished allreduce. send " << x->numel() << " recv "
            << out->numel();
D
Dong Zhihong 已提交
83 84 85
  }
};

D
Dong Zhihong 已提交
86 87 88 89
template <typename T>
class NCCLReduceKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
90 91 92
    PADDLE_ENFORCE_EQ(platform::is_gpu_place(ctx.GetPlace()), true,
                      platform::errors::InvalidArgument(
                          "This kernel only runs on GPU device."));
D
dzhwinter 已提交
93 94 95 96
    auto x = ctx.Input<LoDTensor>("X");  // x0, x1, x2
    auto out = ctx.Output<LoDTensor>("Out");
    auto* comm = ctx.Input<Communicator>("Communicator");
    int root = ctx.Attr<int>("root");
D
Dong Zhihong 已提交
97 98
    std::string reduction = ctx.Attr<std::string>("reduction");

99 100
    auto reduction_op_ = str_to_nccl_red_type(reduction);

D
Dong Zhihong 已提交
101
    // device id
D
dzhwinter 已提交
102
    int gpu_id = boost::get<platform::CUDAPlace>(ctx.GetPlace()).GetDeviceId();
D
Dong Zhihong 已提交
103
    int idx = comm->GetCommId(gpu_id);
D
dzhwinter 已提交
104 105 106
    T* recvbuffer = nullptr;
    if (root == gpu_id) {
      recvbuffer = out->mutable_data<T>(ctx.GetPlace());
C
chengduoZH 已提交
107 108
    } else {
      out->Resize(framework::make_ddim({0}));
D
Dong Zhihong 已提交
109
    }
M
minqiyang 已提交
110 111
    VLOG(3) << "gpu : " << gpu_id << " invoke reduce. send " << x->numel()
            << " recv " << out->numel();
112
    PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::ncclReduce(
D
dzhwinter 已提交
113 114 115
        x->data<T>(), recvbuffer, x->numel(), NCCLTypeWrapper<T>::type,
        reduction_op_, root, comm->comms().at(idx),
        ctx.cuda_device_context().stream()));
M
minqiyang 已提交
116 117
    VLOG(3) << "gpu : " << gpu_id << " finished reduce. send " << x->numel()
            << " recv " << out->numel();
D
Dong Zhihong 已提交
118 119 120 121 122 123 124
  }
};

template <typename T>
class NCCLBcastKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
125 126 127
    PADDLE_ENFORCE_EQ(platform::is_gpu_place(ctx.GetPlace()), true,
                      platform::errors::InvalidArgument(
                          "This kernel only runs on GPU device."));
D
Dong Zhihong 已提交
128 129 130
    int root = ctx.Attr<int>("root");
    auto* comm = ctx.Input<Communicator>("Communicator");
    // device id
D
dzhwinter 已提交
131
    int gpu_id = boost::get<platform::CUDAPlace>(ctx.GetPlace()).GetDeviceId();
D
Dong Zhihong 已提交
132
    int idx = comm->GetCommId(gpu_id);
D
Dong Zhihong 已提交
133
    if (idx == root) {
D
dzhwinter 已提交
134
      auto* x = ctx.Input<LoDTensor>("X");
M
minqiyang 已提交
135
      VLOG(3) << "gpu : " << gpu_id << " invoke Bcast. send " << x->numel();
136
      PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::ncclBcast(
137 138 139
          reinterpret_cast<void*>(const_cast<T*>(x->data<T>())), x->numel(),
          NCCLTypeWrapper<T>::type, root, comm->comms().at(idx),
          ctx.cuda_device_context().stream()));
M
minqiyang 已提交
140
      VLOG(3) << "gpu : " << gpu_id << " finished Bcast.";
D
Dong Zhihong 已提交
141
    } else {
D
dzhwinter 已提交
142
      auto* out = ctx.Output<LoDTensor>("Out");
M
minqiyang 已提交
143 144
      VLOG(3) << "gpu : " << gpu_id << " invoke Bcast. recv buffer "
              << framework::product(out->dims());
145
      PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::ncclBcast(
D
dzhwinter 已提交
146 147 148
          out->mutable_data<T>(ctx.GetPlace()), out->numel(),
          NCCLTypeWrapper<T>::type, root, comm->comms().at(idx),
          ctx.cuda_device_context().stream()));
M
minqiyang 已提交
149
      VLOG(3) << "gpu : " << gpu_id << " finished Bcast. recv " << out->numel();
D
Dong Zhihong 已提交
150 151 152 153
    }
  }
};

D
Dong Zhihong 已提交
154 155 156 157
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
Q
QI JUN 已提交
158 159 160
REGISTER_OP_CUDA_KERNEL(ncclAllReduce, ops::NCCLAllReduceKernel<float>);
REGISTER_OP_CUDA_KERNEL(ncclBcast, ops::NCCLBcastKernel<float>);
REGISTER_OP_CUDA_KERNEL(ncclReduce, ops::NCCLReduceKernel<float>);