nccl_ops.cc 3.1 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
dzhwinter 已提交
12
#include "paddle/operators/nccl/nccl_ops.h"
D
dongzhihong 已提交
13 14 15 16 17

namespace paddle {
namespace operators {

// AllreduceOp
D
dzhwinter 已提交
18
class NCCLAllReduceOp : public framework::OperatorWithKernel {
D
dongzhihong 已提交
19 20 21 22
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
D
Dong Zhihong 已提交
23 24 25 26 27
  void InferShape(framework::InferShapeContext *ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"),
                   " Input(X) of AllReduce op input should not be NULL");
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
                   " Input(X) of AllReduce op input should not be NULL");
D
dongzhihong 已提交
28

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

D
Dong Zhihong 已提交
31 32 33 34
    std::string reduction = ctx->Attrs().Get<std::string>("reduction");
    PADDLE_ENFORCE((reduction == "ncclSum" || reduction == "ncclProd" ||
                    reduction == "ncclMin" || reduction == "ncclMax"),
                   "invalid reduction.");
D
dongzhihong 已提交
35

D
Dong Zhihong 已提交
36 37
    ctx->SetOutputsDim("Out", x_dims);
    ctx->ShareLoD("X", /*->*/ "Out");
D
dzhwinter 已提交
38 39 40
  }
};

D
Dong Zhihong 已提交
41
// AllreduceOp
D
dzhwinter 已提交
42
class NCCLAllReduceOpMaker : public framework::OpProtoAndCheckerMaker {
D
Dong Zhihong 已提交
43
 public:
D
Dong Zhihong 已提交
44 45 46
  NCCLAllReduceOpMaker(framework::OpProto *proto,
                       framework::OpAttrChecker *op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
D
dzhwinter 已提交
47 48
    AddInput("X", "The input of AllReduce op");
    AddOutput("Out", "The output of AllReduce op");
D
Dong Zhihong 已提交
49 50 51
    AddAttr<std::string>("reduction",
                         "{'ncclmin', 'ncclmax', 'ncclprod', 'ncclsum'}.");
    AddAttr<std::vector<int>>("gpus", "gpu id lists");
D
dzhwinter 已提交
52 53 54 55
    AddComment(R"DOC(
            AllReduce the input tensors.
        )DOC");
  }
D
dongzhihong 已提交
56
};
D
dzhwinter 已提交
57

D
Dong Zhihong 已提交
58
// BcastSendOp
D
dzhwinter 已提交
59
class NCCLBcastSendOpMaker : public framework::OpProtoAndCheckerMaker {
D
Dong Zhihong 已提交
60
 public:
D
Dong Zhihong 已提交
61 62 63
  NCCLAllReduceOpMaker(framework::OpProto *proto,
                       framework::OpAttrChecker *op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
D
dzhwinter 已提交
64 65 66 67 68 69 70
    AddInput("X", "The input of BcastSend op");
    AddComment(R"DOC(
            BcastSend the tensors.
        )DOC");
  }
};

D
Dong Zhihong 已提交
71
// BcastRecvOp
D
dzhwinter 已提交
72
class NCCLBcastRecvOpMaker : public framework::OpProtoAndCheckerMaker {
D
Dong Zhihong 已提交
73
 public:
D
Dong Zhihong 已提交
74 75 76
  NCCLAllReduceOpMaker(framework::OpProto *proto,
                       framework::OpAttrChecker *op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
D
dzhwinter 已提交
77 78 79 80 81 82 83
    AddOutput("Out", "The output of BcastRecv op");
    AddComment(R"DOC(
            BcastRecv the tensors.
        )DOC");
  }
};

D
Dong Zhihong 已提交
84 85 86 87 88 89
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP_WITHOUT_GRADIENT(ncclAllReduce, ops::NCCLAllReduceOp,
                             ops::NCCLAllReduceOpMaker);