|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 2 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + you may not use this file except in compliance with the License. |
| 4 | + You may obtain a copy of the License at |
| 5 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + Unless required by applicable law or agreed to in writing, software |
| 7 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | + See the License for the specific language governing permissions and |
| 10 | + limitations under the License. */ |
| 11 | + |
| 12 | +#include "paddle/framework/op_registry.h" |
| 13 | +#include "paddle/operators/nccl/nccl_gpu_common.h" |
| 14 | + |
| 15 | +namespace paddle { |
| 16 | +namespace operators { |
| 17 | + |
| 18 | +// NCCLinitOp |
| 19 | +class NCCLInitOp : public framework::OperatorBase { |
| 20 | + public: |
| 21 | + NCCLInitOp(const std::string &type, const framework::VariableNameMap &inputs, |
| 22 | + const framework::VariableNameMap &outputs, |
| 23 | + const framework::AttributeMap &attrs) |
| 24 | + : OperatorBase(type, inputs, outputs, attrs) {} |
| 25 | + |
| 26 | + void Run(const framework::Scope &scope, |
| 27 | + const platform::DeviceContext &dev_ctx) const override { |
| 28 | + const auto &name = Output("Communicator"); |
| 29 | + PADDLE_ENFORCE_NOT_NULL(scope.FindVar(name), |
| 30 | + "Can not find variable '%s' in the scope.", name); |
| 31 | + std::vector<int> gpus = Attr<std::vector<int>>("gpus"); |
| 32 | + PADDLE_ENFORCE(!gpus.empty(), "Attr(gpus) should not be empty."); |
| 33 | + |
| 34 | + if (scope.FindVar(name) == nullptr) { |
| 35 | + PADDLE_THROW("Output(Communicator) is needed for ncclInit operator."); |
| 36 | + } |
| 37 | + |
| 38 | + platform::Communicator *comm = |
| 39 | + scope.FindVar(name)->GetMutable<platform::Communicator>(); |
| 40 | + comm->InitAll(gpus); |
| 41 | + } |
| 42 | +}; |
| 43 | + |
| 44 | +class NCCLInitOpMaker : public framework::OpProtoAndCheckerMaker { |
| 45 | + public: |
| 46 | + NCCLInitOpMaker(framework::OpProto *proto, |
| 47 | + framework::OpAttrChecker *op_checker) |
| 48 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 49 | + AddOutput("Communicator", |
| 50 | + "Create Communicator for communicating between gpus"); |
| 51 | + AddAttr<std::vector<int>>("gpus", "gpu id lists"); |
| 52 | + AddAttr<int>("data_type", "output data type") |
| 53 | + .SetDefault(framework::DataType::FP32); |
| 54 | + AddComment(R"DOC( |
| 55 | + create communicator. |
| 56 | + )DOC"); |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +// AllReduceOp |
| 61 | +class NCCLAllReduceOp : public framework::OperatorWithKernel { |
| 62 | + public: |
| 63 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 64 | + |
| 65 | + protected: |
| 66 | + void InferShape(framework::InferShapeContext *ctx) const override { |
| 67 | + PADDLE_ENFORCE(ctx->HasInput("X"), |
| 68 | + " Input(X) of AllReduce op input should not be NULL"); |
| 69 | + PADDLE_ENFORCE( |
| 70 | + ctx->HasInput("Communicator"), |
| 71 | + " Input(Communicator) of AllReduce op input should not be NULL"); |
| 72 | + PADDLE_ENFORCE(ctx->HasOutput("Out"), |
| 73 | + " Input(X) of AllReduce op input should not be NULL"); |
| 74 | + |
| 75 | + auto x_dims = ctx->GetInputsDim("X"); |
| 76 | + |
| 77 | + std::string reduction = ctx->Attrs().Get<std::string>("reduction"); |
| 78 | + PADDLE_ENFORCE((reduction == "ncclSum" || reduction == "ncclProd" || |
| 79 | + reduction == "ncclMin" || reduction == "ncclMax"), |
| 80 | + "invalid reduction."); |
| 81 | + |
| 82 | + ctx->SetOutputsDim("Out", x_dims); |
| 83 | + ctx->ShareLoD("X", /*->*/ "Out"); |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +// ReduceOp |
| 88 | +class NCCLReduceOp : public framework::OperatorWithKernel { |
| 89 | + public: |
| 90 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 91 | + |
| 92 | + protected: |
| 93 | + void InferShape(framework::InferShapeContext *ctx) const override { |
| 94 | + PADDLE_ENFORCE(ctx->HasInput("X"), |
| 95 | + " Input(X) of Reduce op input should not be NULL"); |
| 96 | + PADDLE_ENFORCE( |
| 97 | + ctx->HasInput("Communicator"), |
| 98 | + " Input(Communicator) of Reduce op input should not be NULL"); |
| 99 | + PADDLE_ENFORCE(ctx->HasOutput("Out"), |
| 100 | + " Input(X) of Reduce op input should not be NULL"); |
| 101 | + |
| 102 | + std::string reduction = ctx->Attrs().Get<std::string>("reduction"); |
| 103 | + PADDLE_ENFORCE((reduction == "ncclSum" || reduction == "ncclProd" || |
| 104 | + reduction == "ncclMin" || reduction == "ncclMax"), |
| 105 | + "invalid reduction."); |
| 106 | + |
| 107 | + auto x_dims = ctx->GetInputsDim("X"); |
| 108 | + ctx->SetOutputsDim("Out", x_dims); |
| 109 | + ctx->ShareLoD("X", /*->*/ "Out"); |
| 110 | + } |
| 111 | +}; |
| 112 | + |
| 113 | +// BcastOp |
| 114 | +class NCCLBcastOp : public framework::OperatorWithKernel { |
| 115 | + public: |
| 116 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 117 | + |
| 118 | + protected: |
| 119 | + void InferShape(framework::InferShapeContext *ctx) const override { |
| 120 | + PADDLE_ENFORCE(ctx->HasInput("X"), |
| 121 | + " Input(X) of Bcast op input should not be NULL"); |
| 122 | + PADDLE_ENFORCE(ctx->HasInput("Communicator"), |
| 123 | + " Input(Communicator) of Bcast op input should not be NULL"); |
| 124 | + PADDLE_ENFORCE(ctx->HasOutput("Out"), |
| 125 | + " Output(Out) of Bcast op output should not be NULL"); |
| 126 | + |
| 127 | + int root = ctx->Attrs().Get<int>("root"); |
| 128 | + PADDLE_ENFORCE(root != platform::kInvalidGPUId, "Bcast root must be set."); |
| 129 | + |
| 130 | + auto x_dims = ctx->GetInputsDim("X"); |
| 131 | + ctx->SetOutputsDim("Out", x_dims); |
| 132 | + ctx->ShareLoD("X", /*->*/ "Out"); |
| 133 | + } |
| 134 | +}; |
| 135 | + |
| 136 | +// AllreduceOp |
| 137 | +class NCCLAllReduceOpMaker : public framework::OpProtoAndCheckerMaker { |
| 138 | + public: |
| 139 | + NCCLAllReduceOpMaker(framework::OpProto *proto, |
| 140 | + framework::OpAttrChecker *op_checker) |
| 141 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 142 | + AddInput("X", "The input of AllReduce op"); |
| 143 | + AddInput("Communicator", "Communicator for communicating between gpus"); |
| 144 | + AddOutput("Out", "The output of AllReduce op"); |
| 145 | + AddAttr<std::string>("reduction", |
| 146 | + "{'ncclMin', 'ncclMax', 'ncclProd', 'ncclSum'}.") |
| 147 | + .SetDefault("ncclSum"); |
| 148 | + AddComment(R"DOC( |
| 149 | + AllReduce the input tensors. |
| 150 | + )DOC"); |
| 151 | + } |
| 152 | +}; |
| 153 | + |
| 154 | +// ReduceOp |
| 155 | +class NCCLReduceOpMaker : public framework::OpProtoAndCheckerMaker { |
| 156 | + public: |
| 157 | + NCCLReduceOpMaker(framework::OpProto *proto, |
| 158 | + framework::OpAttrChecker *op_checker) |
| 159 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 160 | + AddInput("X", "The input of Reduce op"); |
| 161 | + AddInput("Communicator", "Communicator for communicating between gpus"); |
| 162 | + AddOutput("Out", "The output of Reduce op"); |
| 163 | + AddAttr<std::string>("reduction", |
| 164 | + "{'ncclMin', 'ncclMax', 'ncclProd', 'ncclSum'}.") |
| 165 | + .SetDefault("ncclSum"); |
| 166 | + AddAttr<int>("root", |
| 167 | + "root gpu of the parameter. if not " |
| 168 | + "set(platform::kInvalidGPUId). hashed by name.") |
| 169 | + .SetDefault(platform::kInvalidGPUId); |
| 170 | + AddComment(R"DOC( |
| 171 | + Reduce the tensors)DOC"); |
| 172 | + } |
| 173 | +}; |
| 174 | + |
| 175 | +// BcastOp |
| 176 | +class NCCLBcastOpMaker : public framework::OpProtoAndCheckerMaker { |
| 177 | + public: |
| 178 | + NCCLBcastOpMaker(framework::OpProto *proto, |
| 179 | + framework::OpAttrChecker *op_checker) |
| 180 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 181 | + AddInput("X", "The input of BcastSend op"); |
| 182 | + AddInput("Communicator", "Communicator for communicating between gpus"); |
| 183 | + AddOutput("Out", "The output of Bcast"); |
| 184 | + AddAttr<int>("root", |
| 185 | + "root gpu of the parameter. if not " |
| 186 | + "set(platform::kInvalidGPUId). hashed by name.") |
| 187 | + .SetDefault(platform::kInvalidGPUId); |
| 188 | + AddComment(R"DOC( |
| 189 | + Bcast the tensors. |
| 190 | + )DOC"); |
| 191 | + } |
| 192 | +}; |
| 193 | + |
| 194 | +} // namespace operators |
| 195 | +} // namespace paddle |
| 196 | + |
| 197 | +namespace ops = paddle::operators; |
| 198 | +REGISTER_OPERATOR(ncclInit, ops::NCCLInitOp, |
| 199 | + paddle::framework::EmptyGradOpMaker, ops::NCCLInitOpMaker); |
| 200 | + |
| 201 | +REGISTER_OP_WITHOUT_GRADIENT(ncclAllReduce, ops::NCCLAllReduceOp, |
| 202 | + ops::NCCLAllReduceOpMaker); |
| 203 | +REGISTER_OP_WITHOUT_GRADIENT(ncclBcast, ops::NCCLBcastOp, |
| 204 | + ops::NCCLBcastOpMaker); |
| 205 | +REGISTER_OP_WITHOUT_GRADIENT(ncclReduce, ops::NCCLReduceOp, |
| 206 | + ops::NCCLReduceOpMaker); |
0 commit comments