nccl_op.cc 7.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. */

12 13
#include "paddle/framework/op_registry.h"
#include "paddle/operators/nccl/nccl_gpu_common.h"
D
dongzhihong 已提交
14 15 16 17

namespace paddle {
namespace operators {

D
Dong Zhihong 已提交
18
// NCCLinitOp
19
class NCCLInitOp : public framework::OperatorBase {
D
Dong Zhihong 已提交
20
 public:
21 22 23 24 25 26 27 28 29 30 31 32
  NCCLInitOp(const std::string &type, const framework::VariableNameMap &inputs,
             const framework::VariableNameMap &outputs,
             const framework::AttributeMap &attrs)
      : OperatorBase(type, inputs, outputs, attrs) {}

  void Run(const framework::Scope &scope,
           const platform::DeviceContext &dev_ctx) const override {
    const auto &name = Output("Communicator");
    PADDLE_ENFORCE_NOT_NULL(scope.FindVar(name),
                            "Can not find variable '%s' in the scope.", name);
    std::vector<int> gpus = Attr<std::vector<int>>("gpus");
    PADDLE_ENFORCE(!gpus.empty(), "Attr(gpus) should not be empty.");
D
dzhwinter 已提交
33 34 35 36 37

    if (scope.FindVar(name) == nullptr) {
      PADDLE_THROW("Output(Communicator) is needed for ncclInit operator.");
    }

38 39 40
    platform::Communicator *comm =
        scope.FindVar(name)->GetMutable<platform::Communicator>();
    comm->InitAll(gpus);
D
Dong Zhihong 已提交
41 42 43 44 45
  }
};

class NCCLInitOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
46
  NCCLInitOpMaker(OpProto *proto, OpAttrChecker *op_checker)
D
Dong Zhihong 已提交
47 48 49
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddOutput("Communicator",
              "Create Communicator for communicating between gpus");
K
kexinzhao 已提交
50
    AddAttr<std::vector<int>>("gpus", "(vector<int>) GPU id lists");
F
fengjiayi 已提交
51
    AddAttr<int>("dtype",
K
kexinzhao 已提交
52 53
                 "(int, default 5 (FP32)) "
                 "Output data type")
54
        .SetDefault(framework::proto::DataType::FP32);
D
Dong Zhihong 已提交
55
    AddComment(R"DOC(
K
kexinzhao 已提交
56 57 58 59 60
NCCLInit Operator.

Create communicator.

)DOC");
D
Dong Zhihong 已提交
61 62 63 64
  }
};

// AllReduceOp
D
dzhwinter 已提交
65
class NCCLAllReduceOp : public framework::OperatorWithKernel {
D
dongzhihong 已提交
66 67 68 69
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
D
Dong Zhihong 已提交
70 71 72
  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 已提交
73 74 75
    PADDLE_ENFORCE(
        ctx->HasInput("Communicator"),
        " Input(Communicator) of AllReduce op input should not be NULL");
D
Dong Zhihong 已提交
76 77
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
                   " Input(X) of AllReduce op input should not be NULL");
D
dongzhihong 已提交
78

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

D
Dong Zhihong 已提交
81 82 83 84
    std::string reduction = ctx->Attrs().Get<std::string>("reduction");
    PADDLE_ENFORCE((reduction == "ncclSum" || reduction == "ncclProd" ||
                    reduction == "ncclMin" || reduction == "ncclMax"),
                   "invalid reduction.");
D
dongzhihong 已提交
85

D
Dong Zhihong 已提交
86 87
    ctx->SetOutputsDim("Out", x_dims);
    ctx->ShareLoD("X", /*->*/ "Out");
D
dzhwinter 已提交
88 89 90
  }
};

D
Dong Zhihong 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104
// 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");
D
Dong Zhihong 已提交
105

D
Dong Zhihong 已提交
106 107 108 109 110
    std::string reduction = ctx->Attrs().Get<std::string>("reduction");
    PADDLE_ENFORCE((reduction == "ncclSum" || reduction == "ncclProd" ||
                    reduction == "ncclMin" || reduction == "ncclMax"),
                   "invalid reduction.");

D
Dong Zhihong 已提交
111 112 113
    auto x_dims = ctx->GetInputsDim("X");
    ctx->SetOutputsDim("Out", x_dims);
    ctx->ShareLoD("X", /*->*/ "Out");
D
Dong Zhihong 已提交
114 115 116
  }
};

D
Dong Zhihong 已提交
117 118
// BcastOp
class NCCLBcastOp : public framework::OperatorWithKernel {
D
Dong Zhihong 已提交
119 120 121 122 123 124 125 126 127 128 129
 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");
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
                   " Output(Out) of Bcast op output should not be NULL");
D
Dong Zhihong 已提交
130

D
Dong Zhihong 已提交
131
    int root = ctx->Attrs().Get<int>("root");
D
Dong Zhihong 已提交
132
    PADDLE_ENFORCE(root != platform::kInvalidGPUId, "Bcast root must be set.");
D
Dong Zhihong 已提交
133

D
Dong Zhihong 已提交
134 135 136
    auto x_dims = ctx->GetInputsDim("X");
    ctx->SetOutputsDim("Out", x_dims);
    ctx->ShareLoD("X", /*->*/ "Out");
D
Dong Zhihong 已提交
137 138 139
  }
};

D
Dong Zhihong 已提交
140
// AllreduceOp
D
dzhwinter 已提交
141
class NCCLAllReduceOpMaker : public framework::OpProtoAndCheckerMaker {
D
Dong Zhihong 已提交
142
 public:
143
  NCCLAllReduceOpMaker(OpProto *proto, OpAttrChecker *op_checker)
D
Dong Zhihong 已提交
144
      : OpProtoAndCheckerMaker(proto, op_checker) {
D
dzhwinter 已提交
145
    AddInput("X", "The input of AllReduce op");
D
Dong Zhihong 已提交
146
    AddInput("Communicator", "Communicator for communicating between gpus");
D
dzhwinter 已提交
147
    AddOutput("Out", "The output of AllReduce op");
D
Dong Zhihong 已提交
148
    AddAttr<std::string>("reduction",
K
kexinzhao 已提交
149
                         "(string, default 'ncclSum') "
D
Dong Zhihong 已提交
150 151
                         "{'ncclMin', 'ncclMax', 'ncclProd', 'ncclSum'}.")
        .SetDefault("ncclSum");
D
dzhwinter 已提交
152
    AddComment(R"DOC(
K
kexinzhao 已提交
153 154 155 156 157
NCCLAllReduce Operator.

AllReduce the input tensors.

)DOC");
D
dzhwinter 已提交
158
  }
D
dongzhihong 已提交
159
};
D
dzhwinter 已提交
160

D
Dong Zhihong 已提交
161 162
// ReduceOp
class NCCLReduceOpMaker : public framework::OpProtoAndCheckerMaker {
D
Dong Zhihong 已提交
163
 public:
164
  NCCLReduceOpMaker(OpProto *proto, OpAttrChecker *op_checker)
D
Dong Zhihong 已提交
165
      : OpProtoAndCheckerMaker(proto, op_checker) {
D
Dong Zhihong 已提交
166
    AddInput("X", "The input of Reduce op");
D
Dong Zhihong 已提交
167
    AddInput("Communicator", "Communicator for communicating between gpus");
D
Dong Zhihong 已提交
168
    AddOutput("Out", "The output of Reduce op");
D
Dong Zhihong 已提交
169
    AddAttr<std::string>("reduction",
K
kexinzhao 已提交
170
                         "(string, default 'ncclSum') "
D
Dong Zhihong 已提交
171 172
                         "{'ncclMin', 'ncclMax', 'ncclProd', 'ncclSum'}.")
        .SetDefault("ncclSum");
D
Dong Zhihong 已提交
173
    AddAttr<int>("root",
K
kexinzhao 已提交
174 175 176
                 "(int, default kInvalidGPUId) "
                 "Root gpu of the parameter. If not, "
                 "set(platform::kInvalidGPUId). Hashed by name.")
D
Dong Zhihong 已提交
177
        .SetDefault(platform::kInvalidGPUId);
D
Dong Zhihong 已提交
178
    AddComment(R"DOC(
K
kexinzhao 已提交
179 180 181 182 183
NCCLReduce Operator.

Reduce the tensors.

)DOC");
D
Dong Zhihong 已提交
184 185 186
  }
};

D
Dong Zhihong 已提交
187
// BcastOp
D
Dong Zhihong 已提交
188
class NCCLBcastOpMaker : public framework::OpProtoAndCheckerMaker {
D
Dong Zhihong 已提交
189
 public:
190
  NCCLBcastOpMaker(OpProto *proto, OpAttrChecker *op_checker)
D
Dong Zhihong 已提交
191
      : OpProtoAndCheckerMaker(proto, op_checker) {
D
Dong Zhihong 已提交
192
    AddInput("X", "The input of BcastSend op");
D
Dong Zhihong 已提交
193
    AddInput("Communicator", "Communicator for communicating between gpus");
D
Dong Zhihong 已提交
194
    AddOutput("Out", "The output of Bcast");
D
Dong Zhihong 已提交
195
    AddAttr<int>("root",
K
kexinzhao 已提交
196 197 198
                 "(int, default kInvalidGPUId) "
                 "Root gpu of the parameter. If not, "
                 "set(platform::kInvalidGPUId). Hashed by name.")
D
Dong Zhihong 已提交
199
        .SetDefault(platform::kInvalidGPUId);
D
Dong Zhihong 已提交
200
    AddComment(R"DOC(
K
kexinzhao 已提交
201 202 203 204 205
NCCLBcast Operator.

Bcast the tensors.

)DOC");
D
Dong Zhihong 已提交
206 207
  }
};
D
dzhwinter 已提交
208

D
Dong Zhihong 已提交
209 210 211 212
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
213 214 215
REGISTER_OPERATOR(ncclInit, ops::NCCLInitOp,
                  paddle::framework::EmptyGradOpMaker, ops::NCCLInitOpMaker);

D
Dong Zhihong 已提交
216 217
REGISTER_OP_WITHOUT_GRADIENT(ncclAllReduce, ops::NCCLAllReduceOp,
                             ops::NCCLAllReduceOpMaker);
D
Dong Zhihong 已提交
218 219
REGISTER_OP_WITHOUT_GRADIENT(ncclBcast, ops::NCCLBcastOp,
                             ops::NCCLBcastOpMaker);
D
Dong Zhihong 已提交
220 221
REGISTER_OP_WITHOUT_GRADIENT(ncclReduce, ops::NCCLReduceOp,
                             ops::NCCLReduceOpMaker);