c_comm_init_op.cc 4.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* 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. */
14
#if defined(PADDLE_WITH_NCCL)
15 16
#include <nccl.h>
#endif
17 18 19
#if defined(PADDLE_WITH_RCCL)
#include <rccl.h>
#endif
20 21 22
#if defined(PADDLE_WITH_XPU_BKCL)
#include "xpu/bkcl.h"
#endif
23 24 25
#if defined(PADDLE_WITH_CNCL)
#include <cncl.h>
#endif
26 27 28
#include <string>

#include "paddle/fluid/framework/op_registry.h"
W
wanghuancoder 已提交
29

30
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL) || \
31
    defined(PADDLE_WITH_XPU_BKCL) || defined(PADDLE_WITH_CNCL)
32 33 34
#include "paddle/fluid/platform/collective_helper.h"
#endif

W
wanghuancoder 已提交
35 36 37 38 39
namespace paddle {
namespace framework {
class Scope;
}  // namespace framework
}  // namespace paddle
40

41 42 43 44 45 46 47 48 49 50 51 52
namespace paddle {
namespace operators {

class CCommInitOp : public framework::OperatorBase {
 public:
  CCommInitOp(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 {
53 54 55 56 57 58 59 60 61
// TODO(wangxi): Put this in the unified header file
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
    using UniqueId = ncclUniqueId;
    using Place = platform::CUDAPlace;
    using CommContext = platform::NCCLCommContext;
#elif defined(PADDLE_WITH_XPU_BKCL)
    using UniqueId = BKCLUniqueId;
    using Place = platform::XPUPlace;
    using CommContext = platform::BKCLCommContext;
62 63 64 65
#elif defined(PADDLE_WITH_CNCL)
    using UniqueId = cnclCliqueId;
    using Place = platform::MLUPlace;
    using CommContext = platform::CNCLCommContext;
66 67
#else
    PADDLE_THROW(platform::errors::PreconditionNotMet(
68
        "PaddlePaddle should be compiled with GPU or XPU or MLU."));
69 70
#endif

71
    PADDLE_ENFORCE_EQ(
72 73 74 75
        platform::is_gpu_place(place) || platform::is_xpu_place(place) ||
            platform::is_mlu_place(place),
        true, platform::errors::PreconditionNotMet(
                  "CCommInitOp can run on gpu or xpu or mlu place only."));
76

77
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL) || \
78
    defined(PADDLE_WITH_XPU_BKCL) || defined(PADDLE_WITH_CNCL)
79
    auto var = scope.FindVar(Input("X"));
M
MRXLT 已提交
80 81
    PADDLE_ENFORCE_NOT_NULL(
        var, platform::errors::InvalidArgument("Input con not be empty."));
82 83 84 85 86 87

    UniqueId* comm_id = var->GetMutable<UniqueId>();

    int nranks = Attr<int>("nranks");
    int rid = Attr<int>("ring_id");

88
#if defined(PADDLE_WITH_XPU_BKCL)
89 90 91 92 93
    PADDLE_ENFORCE_EQ(
        rid, 0,
        platform::errors::OutOfRange(
            "Ring id must equal 0 in multi Kunlun cards training, but got %d",
            rid));
94
#endif
95

96
    int device_id = place.device;
97 98
    if (Attr<int>("device_id") >= 0) {
      device_id = Attr<int>("device_id");
99
    }
F
Fan Zhang 已提交
100 101 102 103 104 105 106 107

#if defined(PADDLE_WITH_XPU_BKCL) && defined(PADDLE_WITH_HETERPS) && \
    defined(PADDLE_WITH_PSLIB)
    // XPUPS rank_id only equals 0, so replace rank_id with device_id
    CommContext::Instance().CreateComm(comm_id, nranks, device_id, device_id,
                                       rid);
#else
    int rank_id = Attr<int>("rank");
108 109
    CommContext::Instance().CreateComm(comm_id, nranks, rank_id, device_id,
                                       rid);
F
Fan Zhang 已提交
110 111
#endif

112
#endif
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  }
};

class CCommInitOpMaker : 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>("nranks", "(int) The number of ranks of distributed trainers");
    AddAttr<int>("rank",
                 "(int) The rank of the trainer in distributed training.");
128 129 130 131 132
    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);
133 134 135 136 137 138 139 140 141 142 143
    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, ops::CCommInitOp, ops::CCommInitOpMaker);