nccl_op.cu 6.2 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
#include "paddle/framework/lod_tensor.h"
16 17
#include "paddle/framework/op_registry.h"
#include "paddle/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;
};

D
Dong Zhihong 已提交
41 42 43 44 45 46 47
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 已提交
48 49
    auto ins = ctx.MultiInput<LoDTensor>("X");
    auto outs = ctx.MultiOutput<LoDTensor>("Out");
D
Dong Zhihong 已提交
50 51 52 53 54 55 56

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

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

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

D
Dong Zhihong 已提交
65
      PADDLE_ENFORCE(platform::dynload::ncclAllReduce(
D
Dong Zhihong 已提交
66
          ins[i]->data<T>(), outs[i]->mutable_data<T>(ctx.GetPlace()),
D
Dong Zhihong 已提交
67
          outs[i]->numel(), NCCLTypeWrapper<T>::type, ncclSum,
D
Dong Zhihong 已提交
68 69
          comm->comms_[idx], stream));
      PADDLE_ENFORCE(cudaStreamSynchronize(stream));
D
Dong Zhihong 已提交
70

D
Dong Zhihong 已提交
71 72
      VLOG(1) << "gpu : "
              << " finished allreduce. send " << ins[i]->numel() << " recv "
D
Dong Zhihong 已提交
73
              << outs[i]->numel();
D
Dong Zhihong 已提交
74 75 76 77
    }
  }
};

D
Dong Zhihong 已提交
78 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
Dong Zhihong 已提交
85 86
    auto ins = ctx.MultiInput<LoDTensor>("X");  // x0, x1, x2
    auto outs = ctx.MultiOutput<LoDTensor>("Out");
D
Dong Zhihong 已提交
87
    int root = ctx.Attr<int>("root");
D
Dong Zhihong 已提交
88 89 90 91 92 93 94

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

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

D
Dong Zhihong 已提交
98 99
    auto ins_names = ctx.Inputs("X");
    std::hash<std::string> hasher;
D
Dong Zhihong 已提交
100
    for (size_t i = 0; i < ins.size(); ++i) {
D
Dong Zhihong 已提交
101 102 103
      if (root == -1) {
        root = hasher(ins_names[i]) % comm->comms_.size();
      }
D
Dong Zhihong 已提交
104
      T* recvbuffer = nullptr;
D
Dong Zhihong 已提交
105
      if (root == gpu_id) {
D
Dong Zhihong 已提交
106 107
        recvbuffer = outs[i]->mutable_data<T>(ctx.GetPlace());
      }
D
Dong Zhihong 已提交
108

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

D
Dong Zhihong 已提交
112 113 114
      PADDLE_ENFORCE(platform::dynload::ncclReduce(
          ins[i]->data<T>(), recvbuffer, ins[i]->numel(),
          NCCLTypeWrapper<T>::type, ncclSum, root, comm->comms_[idx], stream));
D
Dong Zhihong 已提交
115
      PADDLE_ENFORCE(cudaStreamSynchronize(stream));
D
Dong Zhihong 已提交
116

D
Dong Zhihong 已提交
117 118
      VLOG(1) << "gpu : " << gpu_id << " finished reduce. send "
              << ins[i]->numel() << " recv " << outs[i]->numel();
D
Dong Zhihong 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    }
  }
};

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 已提交
138 139
    int gpu_id = boost::get<platform::GPUPlace>(ctx.GetPlace()).GetDeviceId();
    int idx = comm->GetCommId(gpu_id);
D
Dong Zhihong 已提交
140

D
Dong Zhihong 已提交
141
    if (idx == root) {
D
Dong Zhihong 已提交
142
      auto ins = ctx.MultiInput<LoDTensor>("X");
D
Dong Zhihong 已提交
143
      for (size_t i = 0; i < ins.size(); ++i) {
D
Dong Zhihong 已提交
144 145
        VLOG(1) << "gpu : " << gpu_id << " invoke Bcast. send "
                << ins[i]->numel();
D
Dong Zhihong 已提交
146

D
Dong Zhihong 已提交
147
        VLOG(1) << " before ncclBcast";
D
Dong Zhihong 已提交
148 149 150
        PADDLE_ENFORCE(platform::dynload::ncclBcast(
            (void*)ins[i]->data<T>(), ins[i]->numel(), NCCLTypeWrapper<T>::type,
            root, comm->comms_[idx], stream));
D
Dong Zhihong 已提交
151
        VLOG(1) << " after ncclBcast";
D
Dong Zhihong 已提交
152
        PADDLE_ENFORCE(cudaStreamSynchronize(stream));
D
Dong Zhihong 已提交
153

D
Dong Zhihong 已提交
154
        VLOG(1) << "gpu : " << gpu_id << " finished Bcast.";
D
Dong Zhihong 已提交
155 156
      }
    } else {
D
Dong Zhihong 已提交
157
      auto outs = ctx.MultiOutput<LoDTensor>("Out");
D
Dong Zhihong 已提交
158
      for (size_t i = 0; i < outs.size(); ++i) {
D
Dong Zhihong 已提交
159 160
        VLOG(1) << "gpu : " << gpu_id << " invoke Bcast. recv buffer "
                << framework::product(outs[i]->dims());
D
Dong Zhihong 已提交
161

D
Dong Zhihong 已提交
162 163 164
        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 已提交
165
        PADDLE_ENFORCE(cudaStreamSynchronize(stream));
D
Dong Zhihong 已提交
166

D
Dong Zhihong 已提交
167 168
        VLOG(1) << "gpu : " << gpu_id << " finished Bcast. recv "
                << outs[i]->numel();
D
Dong Zhihong 已提交
169 170 171 172 173
      }
    }
  }
};

D
Dong Zhihong 已提交
174 175 176 177 178
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP_GPU_KERNEL(ncclAllReduce, ops::NCCLAllReduceKernel<float>);
D
Dong Zhihong 已提交
179
REGISTER_OP_GPU_KERNEL(ncclBcast, ops::NCCLBcastKernel<float>);
D
Dong Zhihong 已提交
180
REGISTER_OP_GPU_KERNEL(ncclReduce, ops::NCCLReduceKernel<float>);