nccl_op.cu.cc 7.0 KB
Newer Older
D
Dong Zhihong 已提交
1 2 3 4
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
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>

D
Dong Zhihong 已提交
14
#include "paddle/framework/lod_tensor.h"
15 16
#include "paddle/framework/op_registry.h"
#include "paddle/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 46
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
Dong Zhihong 已提交
47 48
    auto ins = ctx.MultiInput<LoDTensor>("X");
    auto outs = ctx.MultiOutput<LoDTensor>("Out");
D
Dong Zhihong 已提交
49

D
Dong Zhihong 已提交
50 51 52 53 54 55 56 57 58 59 60 61
    std::string reduction = ctx.Attr<std::string>("reduction");
    ncclRedOp_t reduction_op_ = ncclSum;

    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 已提交
62
      PADDLE_THROW("Invalid reduction. default ncclSum.");
D
Dong Zhihong 已提交
63 64
    }

D
Dong Zhihong 已提交
65 66
    auto* comm = ctx.Input<Communicator>("Communicator");

T
typhoonzero 已提交
67
    auto stream = ctx.cuda_device_context().stream();
D
Dong Zhihong 已提交
68

D
Dong Zhihong 已提交
69
    // device id
D
Dong Zhihong 已提交
70 71
    int gpu_id = boost::get<platform::GPUPlace>(ctx.GetPlace()).GetDeviceId();
    int idx = comm->GetCommId(gpu_id);
D
Dong Zhihong 已提交
72 73

    for (size_t i = 0; i < ins.size(); ++i) {
D
Dong Zhihong 已提交
74 75
      VLOG(1) << "gpu : "
              << " invoke allreduce. send " << ins[i]->numel() << " recv "
D
Dong Zhihong 已提交
76 77
              << outs[i]->numel();

D
Dong Zhihong 已提交
78
      PADDLE_ENFORCE(platform::dynload::ncclAllReduce(
D
Dong Zhihong 已提交
79
          ins[i]->data<T>(), outs[i]->mutable_data<T>(ctx.GetPlace()),
D
Dong Zhihong 已提交
80
          outs[i]->numel(), NCCLTypeWrapper<T>::type, reduction_op_,
D
Dong Zhihong 已提交
81 82
          comm->comms_[idx], stream));
      PADDLE_ENFORCE(cudaStreamSynchronize(stream));
D
Dong Zhihong 已提交
83

D
Dong Zhihong 已提交
84 85
      VLOG(1) << "gpu : "
              << " finished allreduce. send " << ins[i]->numel() << " recv "
D
Dong Zhihong 已提交
86
              << outs[i]->numel();
D
Dong Zhihong 已提交
87 88 89 90
    }
  }
};

D
Dong Zhihong 已提交
91 92 93 94 95 96 97
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
Dong Zhihong 已提交
98 99
    auto ins = ctx.MultiInput<LoDTensor>("X");  // x0, x1, x2
    auto outs = ctx.MultiOutput<LoDTensor>("Out");
D
Dong Zhihong 已提交
100

D
Dong Zhihong 已提交
101 102 103 104 105 106 107 108 109 110 111 112
    std::string reduction = ctx.Attr<std::string>("reduction");
    ncclRedOp_t reduction_op_ = ncclSum;

    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 已提交
113
      PADDLE_THROW("Invalid reduction. default ncclSum.");
D
Dong Zhihong 已提交
114 115 116
    }

    int root = ctx.Attr<int>("root");
D
Dong Zhihong 已提交
117 118 119 120 121 122
    auto* comm = ctx.Input<Communicator>("Communicator");

    auto stream = reinterpret_cast<const platform::CUDADeviceContext&>(
                      ctx.device_context())
                      .stream();
    // device id
D
Dong Zhihong 已提交
123 124
    int gpu_id = boost::get<platform::GPUPlace>(ctx.GetPlace()).GetDeviceId();
    int idx = comm->GetCommId(gpu_id);
D
Dong Zhihong 已提交
125

D
Dong Zhihong 已提交
126 127
    auto ins_names = ctx.Inputs("X");
    std::hash<std::string> hasher;
D
Dong Zhihong 已提交
128
    for (size_t i = 0; i < ins.size(); ++i) {
D
Dong Zhihong 已提交
129
      if (root == platform::kInvalidGPUId) {
D
Dong Zhihong 已提交
130 131
        root = hasher(ins_names[i]) % comm->comms_.size();
      }
D
Dong Zhihong 已提交
132
      T* recvbuffer = nullptr;
D
Dong Zhihong 已提交
133
      if (root == gpu_id) {
D
Dong Zhihong 已提交
134 135
        recvbuffer = outs[i]->mutable_data<T>(ctx.GetPlace());
      }
D
Dong Zhihong 已提交
136

D
Dong Zhihong 已提交
137 138
      VLOG(1) << "gpu : " << gpu_id << " invoke reduce. send "
              << ins[i]->numel() << " recv " << outs[i]->numel();
D
Dong Zhihong 已提交
139

D
Dong Zhihong 已提交
140 141
      PADDLE_ENFORCE(platform::dynload::ncclReduce(
          ins[i]->data<T>(), recvbuffer, ins[i]->numel(),
D
Dong Zhihong 已提交
142 143
          NCCLTypeWrapper<T>::type, reduction_op_, root, comm->comms_[idx],
          stream));
D
Dong Zhihong 已提交
144
      PADDLE_ENFORCE(cudaStreamSynchronize(stream));
D
Dong Zhihong 已提交
145

D
Dong Zhihong 已提交
146 147
      VLOG(1) << "gpu : " << gpu_id << " finished reduce. send "
              << ins[i]->numel() << " recv " << outs[i]->numel();
D
Dong Zhihong 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    }
  }
};

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");

    auto stream = reinterpret_cast<const platform::CUDADeviceContext&>(
                      ctx.device_context())
                      .stream();
    // device id
D
Dong Zhihong 已提交
167 168
    int gpu_id = boost::get<platform::GPUPlace>(ctx.GetPlace()).GetDeviceId();
    int idx = comm->GetCommId(gpu_id);
D
Dong Zhihong 已提交
169

D
Dong Zhihong 已提交
170
    if (idx == root) {
D
Dong Zhihong 已提交
171
      auto ins = ctx.MultiInput<LoDTensor>("X");
D
Dong Zhihong 已提交
172
      for (size_t i = 0; i < ins.size(); ++i) {
D
Dong Zhihong 已提交
173 174
        VLOG(1) << "gpu : " << gpu_id << " invoke Bcast. send "
                << ins[i]->numel();
D
Dong Zhihong 已提交
175

D
Dong Zhihong 已提交
176
        VLOG(1) << " before ncclBcast";
D
Dong Zhihong 已提交
177 178 179
        PADDLE_ENFORCE(platform::dynload::ncclBcast(
            (void*)ins[i]->data<T>(), ins[i]->numel(), NCCLTypeWrapper<T>::type,
            root, comm->comms_[idx], stream));
D
Dong Zhihong 已提交
180
        VLOG(1) << " after ncclBcast";
D
Dong Zhihong 已提交
181
        PADDLE_ENFORCE(cudaStreamSynchronize(stream));
D
Dong Zhihong 已提交
182

D
Dong Zhihong 已提交
183
        VLOG(1) << "gpu : " << gpu_id << " finished Bcast.";
D
Dong Zhihong 已提交
184 185
      }
    } else {
D
Dong Zhihong 已提交
186
      auto outs = ctx.MultiOutput<LoDTensor>("Out");
D
Dong Zhihong 已提交
187
      for (size_t i = 0; i < outs.size(); ++i) {
D
Dong Zhihong 已提交
188 189
        VLOG(1) << "gpu : " << gpu_id << " invoke Bcast. recv buffer "
                << framework::product(outs[i]->dims());
D
Dong Zhihong 已提交
190

D
Dong Zhihong 已提交
191 192 193
        PADDLE_ENFORCE(platform::dynload::ncclBcast(
            outs[i]->mutable_data<T>(ctx.GetPlace()), outs[i]->numel(),
            NCCLTypeWrapper<T>::type, root, comm->comms_[idx], stream));
D
Dong Zhihong 已提交
194
        PADDLE_ENFORCE(cudaStreamSynchronize(stream));
D
Dong Zhihong 已提交
195

D
Dong Zhihong 已提交
196 197
        VLOG(1) << "gpu : " << gpu_id << " finished Bcast. recv "
                << outs[i]->numel();
D
Dong Zhihong 已提交
198 199 200 201 202
      }
    }
  }
};

D
Dong Zhihong 已提交
203 204 205 206
}  // namespace operators
}  // namespace paddle

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