c_broadcast_op.cu.cc 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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 "paddle/fluid/operators/collective/c_broadcast_op.h"
16 17
#include "paddle/phi/core/distributed/comm_context_manager.h"
#include "paddle/phi/core/distributed/nccl_comm_context.h"
18

19
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
20
#include "paddle/fluid/platform/collective_helper.h"
21
#include "paddle/fluid/platform/device/gpu/nccl_helper.h"
22
#endif
L
lilong12 已提交
23
#include "paddle/phi/api/include/tensor.h"
24 25 26 27

namespace paddle {
namespace operators {

28
template <typename T, typename DeviceContext>
29 30 31
class CBroadcastOpCUDAKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
32
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
33 34
    auto x = ctx.Input<phi::DenseTensor>("X");
    auto out = ctx.Output<phi::DenseTensor>("Out");
35 36

    int rid = ctx.Attr<int>("ring_id");
37 38
    const auto& place = ctx.GetPlace();
    ctx.device_context().Alloc<T>(out);
39 40 41

    int root = ctx.Attr<int>("root");

42 43 44 45 46 47 48 49
    gpuStream_t stream = ctx.cuda_device_context().stream();
    const auto& comm_context_manager =
        phi::distributed::CommContextManager::GetInstance();
    if (comm_context_manager.Has(rid)) {
      auto* comm_context = static_cast<phi::distributed::NCCLCommContext*>(
          comm_context_manager.Get(rid));

      comm_context->Broadcast(out, *x, root, stream);
50
    } else {
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
      // NOTE(liyurui): This will be removed after moving this operator to phi.
      int numel = x->numel();
      ncclDataType_t dtype =
          platform::ToNCCLDataType(framework::TransToProtoVarType(x->dtype()));
      auto comm = platform::NCCLCommContext::Instance().Get(rid, place);
      if (root == comm->rank()) {
        PADDLE_ENFORCE_GPU_SUCCESS(platform::dynload::ncclBcast(
            reinterpret_cast<void*>(const_cast<T*>(x->data<T>())),
            numel,
            dtype,
            root,
            comm->comm(),
            stream));
        VLOG(3) << "rank " << comm->rank() << " invoke Bcast. sent "
                << x->numel();
        if (out != x) {
          framework::TensorCopy(
              *static_cast<const phi::DenseTensor*>(x),
              place,
              *platform::DeviceContextPool::Instance().Get(place),
              static_cast<phi::DenseTensor*>(out));
        }
      } else {
        PADDLE_ENFORCE_GPU_SUCCESS(platform::dynload::ncclBcast(
            out->data<T>(), numel, dtype, root, comm->comm(), stream));
        VLOG(3) << "rank " << comm->rank() << " invoke Bcast. received "
                << phi::product(out->dims());
      }
79 80 81 82
    }

    out->set_lod(x->lod());
#else
M
MRXLT 已提交
83 84
    PADDLE_THROW(platform::errors::PreconditionNotMet(
        "PaddlePaddle should compile with GPU."));
85 86 87 88 89 90 91
#endif
  }
};

}  // namespace operators
}  // namespace paddle

92 93 94
namespace ops = paddle::operators;
namespace plat = paddle::platform;

95 96 97 98 99 100 101 102
PD_REGISTER_STRUCT_KERNEL(c_broadcast,
                          GPU,
                          ALL_LAYOUT,
                          ops::CBroadcastOpCUDAKernel,
                          int,
                          int64_t,
                          float,
                          double,
L
LiYuRio 已提交
103
#if NCCL_VERSION_CODE >= 21000 && CUDA_VERSION >= 11000
104
                          plat::bfloat16,
105
#endif
106 107
                          plat::float16) {
}