nccl_op.cc 6.8 KB
Newer Older
D
Dong Zhihong 已提交
1 2 3 4 5 6 7 8 9 10 11
/* 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
   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. */

D
Dong Zhihong 已提交
12
#include "paddle/operators/nccl_op.h"
D
dongzhihong 已提交
13 14 15 16

namespace paddle {
namespace operators {

D
Dong Zhihong 已提交
17 18 19 20 21 22 23
// NCCLinitOp
class NCCLInitOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  void InferShape(framework::InferShapeContext *ctx) const override {
D
Dong Zhihong 已提交
24 25 26
    PADDLE_ENFORCE(
        ctx->HasOutput("Communicator"),
        " Output(Communicator) of ncclInit op input should not be NULL");
D
Dong Zhihong 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  }
};

class NCCLInitOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  NCCLInitOpMaker(framework::OpProto *proto,
                  framework::OpAttrChecker *op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddAttr<std::vector<int>>("gpus", "gpu id lists");
    AddOutput("Communicator",
              "Create Communicator for communicating between gpus");
    AddComment(R"DOC(
               create communicator.
        )DOC");
  }
};

// AllReduceOp
D
dzhwinter 已提交
45
class NCCLAllReduceOp : public framework::OperatorWithKernel {
D
dongzhihong 已提交
46 47 48 49
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
D
Dong Zhihong 已提交
50 51 52
  void InferShape(framework::InferShapeContext *ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"),
                   " Input(X) of AllReduce op input should not be NULL");
D
Dong Zhihong 已提交
53 54 55
    PADDLE_ENFORCE(
        ctx->HasInput("Communicator"),
        " Input(Communicator) of AllReduce op input should not be NULL");
D
Dong Zhihong 已提交
56 57
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
                   " Input(X) of AllReduce op input should not be NULL");
D
dongzhihong 已提交
58

D
Dong Zhihong 已提交
59
    auto x_dims = ctx->GetInputsDim("X");
D
dongzhihong 已提交
60

D
Dong Zhihong 已提交
61 62 63 64
    std::string reduction = ctx->Attrs().Get<std::string>("reduction");
    PADDLE_ENFORCE((reduction == "ncclSum" || reduction == "ncclProd" ||
                    reduction == "ncclMin" || reduction == "ncclMax"),
                   "invalid reduction.");
D
dongzhihong 已提交
65

D
Dong Zhihong 已提交
66 67
    ctx->SetOutputsDim("Out", x_dims);
    ctx->ShareLoD("X", /*->*/ "Out");
D
dzhwinter 已提交
68 69 70
  }
};

D
Dong Zhihong 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
// ReduceOp
class NCCLReduceOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  void InferShape(framework::InferShapeContext *ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"),
                   " Input(X) of Reduce op input should not be NULL");
    PADDLE_ENFORCE(
        ctx->HasInput("Communicator"),
        " Input(Communicator) of Reduce op input should not be NULL");
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
                   " Input(X) of Reduce op input should not be NULL");
  }
};

// BcastSendOp
class NCCLBcastSendOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  void InferShape(framework::InferShapeContext *ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"),
                   " Input(X) of Bcast op input should not be NULL");
    PADDLE_ENFORCE(ctx->HasInput("Communicator"),
                   " Input(Communicator) of Bcast op input should not be NULL");
  }
};

// BcastRecvOp
class NCCLBcastRecvOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  void InferShape(framework::InferShapeContext *ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("Communicator"),
                   " Input(Communicator) of Bcast op input should not be NULL");
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
                   " Output(Out) of Bcast op output should not be NULL");
  }
};

D
Dong Zhihong 已提交
116
// AllreduceOp
D
dzhwinter 已提交
117
class NCCLAllReduceOpMaker : public framework::OpProtoAndCheckerMaker {
D
Dong Zhihong 已提交
118
 public:
D
Dong Zhihong 已提交
119 120 121
  NCCLAllReduceOpMaker(framework::OpProto *proto,
                       framework::OpAttrChecker *op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
D
dzhwinter 已提交
122
    AddInput("X", "The input of AllReduce op");
D
Dong Zhihong 已提交
123
    AddInput("Communicator", "Communicator for communicating between gpus");
D
dzhwinter 已提交
124
    AddOutput("Out", "The output of AllReduce op");
D
Dong Zhihong 已提交
125 126
    AddAttr<std::string>("reduction",
                         "{'ncclmin', 'ncclmax', 'ncclprod', 'ncclsum'}.");
D
Dong Zhihong 已提交
127
    // AddAttr<std::vector<int>>("gpus", "gpu id lists");
D
dzhwinter 已提交
128 129 130 131
    AddComment(R"DOC(
            AllReduce the input tensors.
        )DOC");
  }
D
dongzhihong 已提交
132
};
D
dzhwinter 已提交
133

D
Dong Zhihong 已提交
134 135 136 137
// BcastSend should be in the root
// BcastSendOp
class NCCLBcastSendOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
D
Dong Zhihong 已提交
138 139
  NCCLBcastSendOpMaker(framework::OpProto *proto,
                       framework::OpAttrChecker *op_checker)
D
Dong Zhihong 已提交
140 141 142 143 144 145 146 147 148 149
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("X", "The input of BcastSend op");
    AddInput("Communicator", "Communicator for communicating between gpus");
    AddAttr<int>("root", "root gpu of Bcast");
    AddComment(R"DOC(
            Bcast the tensors.
        )DOC");
  }
};

D
Dong Zhihong 已提交
150
// BcastOp
D
Dong Zhihong 已提交
151
class NCCLBcastRecvOpMaker : public framework::OpProtoAndCheckerMaker {
D
Dong Zhihong 已提交
152
 public:
D
Dong Zhihong 已提交
153 154
  NCCLBcastRecvOpMaker(framework::OpProto *proto,
                       framework::OpAttrChecker *op_checker)
D
Dong Zhihong 已提交
155 156
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("Communicator", "Communicator for communicating between gpus");
D
Dong Zhihong 已提交
157 158
    AddAttr<int>("root", "root gpu of BcastRecv");
    AddOutput("Out", "The output of Bcast");
D
Dong Zhihong 已提交
159 160 161 162 163
    AddComment(R"DOC(
            Bcast the tensors.
        )DOC");
  }
};
D
dzhwinter 已提交
164

D
Dong Zhihong 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178
// BcastRecvOp
class NCCLReduceOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  NCCLReduceOpMaker(framework::OpProto *proto,
                    framework::OpAttrChecker *op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("X", "The input of Reduce op");
    AddInput("Communicator", "Communicator for communicating between gpus");
    AddOutput("Out", "The output of Reduce op");
    AddComment(R"DOC(
            Reduce the tensors.
        )DOC");
  }
};
D
dzhwinter 已提交
179

D
Dong Zhihong 已提交
180 181 182 183 184 185
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP_WITHOUT_GRADIENT(ncclAllReduce, ops::NCCLAllReduceOp,
                             ops::NCCLAllReduceOpMaker);
D
Dong Zhihong 已提交
186
REGISTER_OP_WITHOUT_GRADIENT(ncclInit, ops::NCCLInitOp, ops::NCCLInitOpMaker);
D
Dong Zhihong 已提交
187 188 189 190 191 192
REGISTER_OP_WITHOUT_GRADIENT(ncclBcastSend, ops::NCCLBcastSendOp,
                             ops::NCCLBcastSendOpMaker);
REGISTER_OP_WITHOUT_GRADIENT(ncclBcastRecv, ops::NCCLBcastRecvOp,
                             ops::NCCLBcastRecvOpMaker);
REGISTER_OP_WITHOUT_GRADIENT(ncclReduce, ops::NCCLReduceOp,
                             ops::NCCLReduceOpMaker);
D
Dong Zhihong 已提交
193
REGISTER_OP_CPU_KERNEL(ncclInit, ops::NCCLInitKernel<float>);