nccl_op.cu 4.9 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 12
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. */

#define EIGEN_USE_GPU
D
Dong Zhihong 已提交
13 14
#include <functional>

D
Dong Zhihong 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include "paddle/operators/nccl_op.h"

namespace paddle {
namespace operators {

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

    auto ins = ctx.MultiInput<Tensor>("X");
    auto outs = ctx.MultiOutput<Tensor>("Out");
    std::string reduction = ctx.Attr<std::string>("reduction");

    auto* comm = ctx.Input<Communicator>("Communicator");

    auto stream = reinterpret_cast<const platform::CUDADeviceContext&>(
                      ctx.device_context())
                      .stream();
    // device id
    int device_id =
        boost::get<platform::GPUPlace>(ctx.GetPlace()).GetDeviceId();
    int idx = comm->GetCommId(device_id);

    for (size_t i = 0; i < ins.size(); ++i) {
      PADDLE_ENFORCE(ncclAllReduce(
          ins[i]->data<T>(), outs[i]->mutable_data<T>(ctx.GetPlace()),
D
Dong Zhihong 已提交
44
          outs[i]->numel() * sizeof(T), NCCLTypeWrapper<T>::type, ncclSum,
D
Dong Zhihong 已提交
45 46 47 48 49 50
          comm->comms_[idx], stream));
      PADDLE_ENFORCE(cudaStreamSynchronize(stream));
    }
  }
};

D
Dong Zhihong 已提交
51 52 53 54 55 56 57
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 已提交
58
    auto ins = ctx.MultiInput<Tensor>("X");  // x0, x1, x2
D
Dong Zhihong 已提交
59 60 61 62 63 64 65 66 67 68 69 70
    auto outs = ctx.MultiOutput<Tensor>("Out");

    auto* comm = ctx.Input<Communicator>("Communicator");

    auto stream = reinterpret_cast<const platform::CUDADeviceContext&>(
                      ctx.device_context())
                      .stream();
    // device id
    int device_id =
        boost::get<platform::GPUPlace>(ctx.GetPlace()).GetDeviceId();
    int idx = comm->GetCommId(device_id);

D
Dong Zhihong 已提交
71 72
    auto ins_names = ctx.Inputs("X");
    std::hash<std::string> hasher;
D
Dong Zhihong 已提交
73
    for (size_t i = 0; i < ins.size(); ++i) {
D
Dong Zhihong 已提交
74
      int root = hasher(ins_names[i]) % comm->comms_.size();
D
Dong Zhihong 已提交
75 76 77 78 79
      T* recvbuffer = nullptr;
      if (root == device_id) {
        recvbuffer = outs[i]->mutable_data<T>(ctx.GetPlace());
      }
      PADDLE_ENFORCE(ncclReduce(ins[i]->data<T>(), recvbuffer, ins[i]->numel(),
D
Dong Zhihong 已提交
80
                                NCCLTypeWrapper<T>::type, ncclSum, root,
D
Dong Zhihong 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
                                comm->comms_[idx], stream));
      PADDLE_ENFORCE(cudaStreamSynchronize(stream));
    }
  }
};

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
    int device_id =
        boost::get<platform::GPUPlace>(ctx.GetPlace()).GetDeviceId();
    int idx = comm->GetCommId(device_id);
    if (idx == root) {
      auto ins = ctx.MultiInput<Tensor>("X");
      for (size_t i = 0; i < ins.size(); ++i) {
        PADDLE_ENFORCE(ncclBcast((void*)ins[i]->data<T>(), ins[i]->numel(),
                                 NCCLTypeWrapper<T>::type, root,
                                 comm->comms_[idx], stream));
        PADDLE_ENFORCE(cudaStreamSynchronize(stream));
      }
    } else {
      auto outs = ctx.MultiOutput<Tensor>("Out");
      for (size_t i = 0; i < outs.size(); ++i) {
D
Dong Zhihong 已提交
116
        PADDLE_ENFORCE(ncclBcast(outs[i]->mutable_data<T>(ctx.GetPlace()),
D
Dong Zhihong 已提交
117 118 119 120 121 122 123 124
                                 outs[i]->numel(), NCCLTypeWrapper<T>::type,
                                 root, comm->comms_[idx], stream));
        PADDLE_ENFORCE(cudaStreamSynchronize(stream));
      }
    }
  }
};

D
Dong Zhihong 已提交
125 126 127 128 129
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP_GPU_KERNEL(ncclAllReduce, ops::NCCLAllReduceKernel<float>);
D
Dong Zhihong 已提交
130 131 132
REGISTER_OP_GPU_KERNEL(ncclBcastSend, ops::NCCLBcastKernel<float>);
REGISTER_OP_GPU_KERNEL(ncclReduce, ops::NCCLReduceKernel<float>);
REGISTER_OP_GPU_KERNEL(ncclBcastRecv, ops::NCCLBcastKernel<float>);