c_comm_init_hccl_op.cc 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* 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 <string>

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

namespace paddle {
namespace framework {
class Scope;
}  // namespace framework
}  // namespace paddle
#if defined(PADDLE_WITH_ASCEND_CL)
25 26 27
#include "acl/acl.h"
#include "hccl/hccl.h"
#include "hccl/hccl_types.h"
28
#include "paddle/fluid/platform/collective_helper.h"
29
#include "paddle/fluid/platform/hccl_helper.h"
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#endif

namespace paddle {
namespace operators {

class CCommInitOpAscend : public framework::OperatorBase {
 public:
  CCommInitOpAscend(const std::string& type,
                    const framework::VariableNameMap& inputs,
                    const framework::VariableNameMap& outputs,
                    const framework::AttributeMap& attrs)
      : OperatorBase(type, inputs, outputs, attrs) {}

  void RunImpl(const framework::Scope& scope,
               const platform::Place& place) const override {
    PADDLE_ENFORCE_EQ(is_npu_place(place), true,
                      platform::errors::PreconditionNotMet(
                          "CCommInitOpAscend can run on npu place only."));

    auto var = scope.FindVar(Input("X"));
    PADDLE_ENFORCE_NOT_NULL(
        var, platform::errors::InvalidArgument("Input con not be empty."));
#if defined(PADDLE_WITH_ASCEND_CL)
    HcclRootInfo* hccl_id = var->GetMutable<HcclRootInfo>();

    int rank_ids = Attr<int>("rank_ids");
    int rank_id = Attr<int>("rank");
    int rid = Attr<int>("ring_id");
    int device_id = BOOST_GET_CONST(platform::NPUPlace, place).device;
    if (Attr<int>("device_id") >= 0) {
      device_id = Attr<int>("device_id");
    }
    platform::HCCLCommContext::Instance().CreateHCCLComm(
        hccl_id, rank_ids, rank_id, device_id, rid);
64 65 66 67 68 69 70 71

    //  Build comm
    float* buff;
    int32_t size = 20;
    std::vector<float> input(size, 0);
    for (int32_t idx = 0; idx < size; idx++) {
      input[idx] = 1.0;
    }
B
Baibaifan 已提交
72 73 74 75 76 77
    PADDLE_ENFORCE_NPU_SUCCESS(aclrtMalloc(reinterpret_cast<void**>(&buff),
                                           size * sizeof(float),
                                           ACL_MEM_MALLOC_HUGE_FIRST));
    PADDLE_ENFORCE_NPU_SUCCESS(aclrtMemcpy(
        reinterpret_cast<void*>(buff), size * sizeof(float), input.data(),
        size * sizeof(float), ACL_MEMCPY_HOST_TO_DEVICE));
78 79 80 81 82 83 84 85 86 87
    VLOG(3) << "Build buff data successful.";

    aclrtStream stream = nullptr;
    auto comm = paddle::platform::HCCLCommContext::Instance().Get(rid, place);
    if (rank_id == 0) {
      stream = comm->stream();
    } else {
      auto dev_ctx = platform::DeviceContextPool::Instance().Get(place);
      stream = static_cast<platform::NPUDeviceContext*>(dev_ctx)->stream();
    }
B
Baibaifan 已提交
88 89
    PADDLE_ENFORCE_NPU_SUCCESS(platform::dynload::HcclBroadcast(
        buff, size, HCCL_DATA_TYPE_FP32, 0, comm->comm(), stream));
B
Baibaifan 已提交
90 91
    // Synchronize stream to find hccl error in time.
    PADDLE_ENFORCE_NPU_SUCCESS(aclrtSynchronizeStream(stream));
92
    VLOG(3) << "Build connection successful.";
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#else
    PADDLE_THROW(platform::errors::PreconditionNotMet(
        "PaddlePaddle should compile with NPU."));
#endif
  }
};

class CCommInitOpAscendMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "Raw variable contains a NCCL UniqueId instaces.");
    AddComment(R"DOC(
CCommInit operator

Initialize collective communicatoin context within this trainer
)DOC");
    AddAttr<int>("rank_ids",
                 "(int) The number of ranks of distributed trainers");
    AddAttr<int>("rank",
                 "(int) The rank of the trainer in distributed training.");
    AddAttr<int>("device_id",
                 "(int) The deivce_id on which to initialize the communicator."
                 "Now, you only have to set this attr manually for pipeline "
                 "training. Otherwise, make it as default.")
        .SetDefault(-1);
    AddAttr<int>("ring_id", "(int default 0) user specified ring id")
        .SetDefault(0);
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

REGISTER_OPERATOR(c_comm_init_hccl, ops::CCommInitOpAscend,
                  ops::CCommInitOpAscendMaker);