broadcast_op.cu.cc 3.2 KB
Newer Older
C
chengduo 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.

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

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
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. */

#include <algorithm>
#include <utility>
#include <vector>

#include "paddle/fluid/framework/data_type.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/op_registry.h"

23
#if defined(PADDLE_WITH_NCCL)
C
chengduo 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36
#include "paddle/fluid/platform/nccl_helper.h"
#endif

namespace ops = paddle::operators;
namespace plat = paddle::platform;

namespace paddle {
namespace operators {

template <typename T>
class NCCLBroadcastOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
37 38 39 40
    PADDLE_ENFORCE_EQ(
        platform::is_gpu_place(ctx.GetPlace()), true,
        platform::errors::PreconditionNotMet(
            "The place of ExecutionContext should be CUDAPlace."));
C
chengduo 已提交
41

42
#if defined(PADDLE_WITH_NCCL)
43
    int dev_id = BOOST_GET_CONST(platform::CUDAPlace, ctx.GetPlace()).device;
C
chengduo 已提交
44 45 46 47
    int root_dev_id = ctx.Attr<int>("root");

    auto in = ctx.Input<framework::Tensor>("X");
    auto out = ctx.Output<framework::Tensor>("Out");
48 49 50 51 52
    PADDLE_ENFORCE_EQ(
        out->IsInitialized(), true,
        platform::errors::PreconditionNotMet(
            "Currently, the output of broadcast op must be initialized,"
            "because this op can only be an In-Place operation."));
C
chengduo 已提交
53 54 55
    void* send_recv_buffer = out->mutable_data<T>(ctx.GetPlace());
    PADDLE_ENFORCE_EQ(
        send_recv_buffer, in->data<void>(),
56 57
        platform::errors::PreconditionNotMet("Currently, the broadcast op can "
                                             "only be an In-Place operation."));
C
chengduo 已提交
58 59 60 61 62

    auto& dev_ctx = ctx.template device_context<platform::CUDADeviceContext>();
    auto comm = dev_ctx.nccl_comm();
    auto stream = dev_ctx.stream();

63
    PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::ncclBcast(
C
chengduo 已提交
64 65 66
        send_recv_buffer, static_cast<size_t>(in->numel()),
        platform::ToNCCLDataType(in->type()), root_dev_id, comm, stream));

H
hong 已提交
67
    VLOG(3) << "Bcast " << ctx.InputNames("X")[0] << ", (" << in->numel() << ")"
C
chengduo 已提交
68 69 70
            << " From " << root_dev_id << " to " << dev_id;

    if (ctx.Attr<bool>("sync_mode")) {
71
      PADDLE_ENFORCE_CUDA_SUCCESS(cudaStreamSynchronize(stream));
C
chengduo 已提交
72 73
    }
#else
74 75
    PADDLE_THROW(platform::errors::PreconditionNotMet(
        "PaddlePaddle should compile with GPU."));
C
chengduo 已提交
76 77 78 79 80 81 82 83 84 85 86 87
#endif
  }
};

}  // namespace operators
}  // namespace paddle

REGISTER_OP_CUDA_KERNEL(broadcast, ops::NCCLBroadcastOpKernel<float>,
                        ops::NCCLBroadcastOpKernel<double>,
                        ops::NCCLBroadcastOpKernel<int>,
                        ops::NCCLBroadcastOpKernel<int64_t>,
                        ops::NCCLBroadcastOpKernel<plat::float16>);