nccl_op.cu.cc 5.9 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 13
#include <functional>

Y
Yi Wang 已提交
14 15 16
#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 已提交
17 18 19 20

namespace paddle {
namespace operators {

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

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;
};

D
Dong Zhihong 已提交
40 41 42 43 44 45
template <typename T>
class NCCLAllReduceKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    PADDLE_ENFORCE(platform::is_gpu_place(ctx.GetPlace()),
                   "This kernel only runs on GPU device.");
D
dzhwinter 已提交
46 47 48
    auto* x = ctx.Input<LoDTensor>("X");
    auto* out = ctx.Output<LoDTensor>("Out");
    auto* comm = ctx.Input<Communicator>("Communicator");
D
Dong Zhihong 已提交
49 50
    std::string reduction = ctx.Attr<std::string>("reduction");

D
dzhwinter 已提交
51
    ncclRedOp_t reduction_op_ = ncclSum;
D
Dong Zhihong 已提交
52 53 54 55 56 57 58 59 60
    if (reduction == "ncclMin") {
      reduction_op_ = ncclMin;
    } else if (reduction == "ncclMax") {
      reduction_op_ = ncclMax;
    } else if (reduction == "ncclSum") {
      reduction_op_ = ncclSum;
    } else if (reduction == "ncclProd") {
      reduction_op_ = ncclProd;
    } else {
D
dzhwinter 已提交
61
      PADDLE_THROW("Invalid reduction. default ncclSum.");
D
Dong Zhihong 已提交
62
    }
D
Dong Zhihong 已提交
63
    // device id
D
dzhwinter 已提交
64
    int gpu_id = boost::get<platform::CUDAPlace>(ctx.GetPlace()).GetDeviceId();
D
Dong Zhihong 已提交
65
    int idx = comm->GetCommId(gpu_id);
D
dzhwinter 已提交
66 67 68 69 70 71 72 73 74 75
    VLOG(3) << "gpu : "
            << " invoke allreduce. send " << x->numel() << " recv "
            << out->numel();
    PADDLE_ENFORCE(platform::dynload::ncclAllReduce(
        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()));
    VLOG(3) << "gpu : "
            << " finished allreduce. send " << x->numel() << " recv "
            << out->numel();
D
Dong Zhihong 已提交
76 77 78
  }
};

D
Dong Zhihong 已提交
79 80 81 82 83 84
template <typename T>
class NCCLReduceKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    PADDLE_ENFORCE(platform::is_gpu_place(ctx.GetPlace()),
                   "This kernel only runs on GPU device.");
D
dzhwinter 已提交
85 86 87 88
    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 已提交
89 90
    std::string reduction = ctx.Attr<std::string>("reduction");

D
dzhwinter 已提交
91
    ncclRedOp_t reduction_op_ = ncclSum;
D
Dong Zhihong 已提交
92 93 94 95 96 97 98 99 100
    if (reduction == "ncclMin") {
      reduction_op_ = ncclMin;
    } else if (reduction == "ncclMax") {
      reduction_op_ = ncclMax;
    } else if (reduction == "ncclSum") {
      reduction_op_ = ncclSum;
    } else if (reduction == "ncclProd") {
      reduction_op_ = ncclProd;
    } else {
D
dzhwinter 已提交
101
      PADDLE_THROW("Invalid reduction. default ncclSum.");
D
Dong Zhihong 已提交
102
    }
D
Dong Zhihong 已提交
103
    // device id
D
dzhwinter 已提交
104
    int gpu_id = boost::get<platform::CUDAPlace>(ctx.GetPlace()).GetDeviceId();
D
Dong Zhihong 已提交
105
    int idx = comm->GetCommId(gpu_id);
D
dzhwinter 已提交
106 107 108
    T* recvbuffer = nullptr;
    if (root == gpu_id) {
      recvbuffer = out->mutable_data<T>(ctx.GetPlace());
C
chengduoZH 已提交
109 110
    } else {
      out->Resize(framework::make_ddim({0}));
D
Dong Zhihong 已提交
111
    }
D
dzhwinter 已提交
112 113 114 115 116 117 118 119
    VLOG(3) << "gpu : " << gpu_id << " invoke reduce. send " << x->numel()
            << " recv " << out->numel();
    PADDLE_ENFORCE(platform::dynload::ncclReduce(
        x->data<T>(), recvbuffer, x->numel(), NCCLTypeWrapper<T>::type,
        reduction_op_, root, comm->comms().at(idx),
        ctx.cuda_device_context().stream()));
    VLOG(3) << "gpu : " << gpu_id << " finished reduce. send " << x->numel()
            << " recv " << out->numel();
D
Dong Zhihong 已提交
120 121 122 123 124 125 126 127 128 129 130 131
  }
};

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