nccl_op.cc 9.2 KB
Newer Older
D
Dong Zhihong 已提交
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
L
Luo Tao 已提交
2 3 4 5 6 7 8 9 10 11 12 13

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 已提交
14

Y
Yang Yang 已提交
15
#include <paddle/framework/framework.pb.h>
16 17
#include "paddle/framework/op_registry.h"
#include "paddle/operators/nccl/nccl_gpu_common.h"
D
dongzhihong 已提交
18 19 20 21

namespace paddle {
namespace operators {

Y
Yang Yang 已提交
22 23
static constexpr char kParallelScopes[] = "parallel_scopes";

D
Dong Zhihong 已提交
24
// NCCLinitOp
25
class NCCLInitOp : public framework::OperatorBase {
D
Dong Zhihong 已提交
26
 public:
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,
D
dzhwinter 已提交
33
           const platform::Place &place) const override {
Y
Yang Yang 已提交
34 35 36
    PADDLE_ENFORCE_NOT_NULL(scope.FindVar(Input(kParallelScopes)),
                            "Can not find variable '%s' in the scope.",
                            kParallelScopes);
37 38 39
    const auto &name = Output("Communicator");
    PADDLE_ENFORCE_NOT_NULL(scope.FindVar(name),
                            "Can not find variable '%s' in the scope.", name);
Y
Yang Yang 已提交
40 41 42 43 44 45 46 47 48
    // A parallel do may not use all the gpus. For example, the batch size is 7
    // in the last batch while we have 8 gpu. In this case, parallel_do will
    // create 7 parallel scopes, so should ncclInitOp create 7 gpu peers
    LOG(INFO) << "---------------";
    auto &parallel_scopes = scope.FindVar(Input(kParallelScopes))
                                ->Get<std::vector<framework::Scope *>>();
    LOG(INFO) << "---------------";
    std::vector<int> gpus(parallel_scopes.size());
    for (int i = 0; i < static_cast<int>(parallel_scopes.size()); ++i) {
Y
Yang Yang 已提交
49 50
      gpus[i] = i;
    }
Y
Yang Yang 已提交
51
    LOG(INFO) << "---------------";
Y
Yang Yang 已提交
52
    PADDLE_ENFORCE(!gpus.empty(), "NCCL init with 0 gpus.");
Y
Yang Yang 已提交
53
    LOG(INFO) << "---------------";
D
dzhwinter 已提交
54 55 56 57

    if (scope.FindVar(name) == nullptr) {
      PADDLE_THROW("Output(Communicator) is needed for ncclInit operator.");
    }
Y
Yang Yang 已提交
58
    LOG(INFO) << "---------------";
D
dzhwinter 已提交
59

60 61
    platform::Communicator *comm =
        scope.FindVar(name)->GetMutable<platform::Communicator>();
Y
Yang Yang 已提交
62
    LOG(INFO) << "---------------";
63
    comm->InitAll(gpus);
Y
Yang Yang 已提交
64
    LOG(INFO) << "---------------";
D
Dong Zhihong 已提交
65 66 67
  }
};

Y
Yang Yang 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
class NCCLInitOpVarTypeInference : public framework::VarTypeInference {
 public:
  void operator()(const framework::OpDesc &op_desc,
                  framework::BlockDesc *block) const override {
    auto out_var_name = op_desc.Output("Communicator").front();
    auto &out_var = block->FindRecursiveOrCreateVar(out_var_name);
    auto var_type = framework::proto::VarDesc::NCCL_COM;
    out_var.SetType(var_type);
  }
};

class NCCLInitOpShapeInference : public framework::InferShapeBase {
 public:
  void operator()(framework::InferShapeContext *ctx) const override {}
};

D
Dong Zhihong 已提交
84 85
class NCCLInitOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
86
  NCCLInitOpMaker(OpProto *proto, OpAttrChecker *op_checker)
D
Dong Zhihong 已提交
87
      : OpProtoAndCheckerMaker(proto, op_checker) {
Y
Yang Yang 已提交
88
    AddInput(kParallelScopes, "The working place of parallel do.");
D
Dong Zhihong 已提交
89 90 91
    AddOutput("Communicator",
              "Create Communicator for communicating between gpus");
    AddComment(R"DOC(
K
kexinzhao 已提交
92 93 94 95 96
NCCLInit Operator.

Create communicator.

)DOC");
D
Dong Zhihong 已提交
97 98 99 100
  }
};

// AllReduceOp
D
dzhwinter 已提交
101
class NCCLAllReduceOp : public framework::OperatorWithKernel {
D
dongzhihong 已提交
102 103 104 105
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
D
Dong Zhihong 已提交
106 107 108
  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 已提交
109 110 111
    PADDLE_ENFORCE(
        ctx->HasInput("Communicator"),
        " Input(Communicator) of AllReduce op input should not be NULL");
D
Dong Zhihong 已提交
112
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
Y
Yang Yang 已提交
113
                   " Output(Out) of AllReduce op output should not be NULL");
D
dongzhihong 已提交
114

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

D
Dong Zhihong 已提交
117 118 119 120
    std::string reduction = ctx->Attrs().Get<std::string>("reduction");
    PADDLE_ENFORCE((reduction == "ncclSum" || reduction == "ncclProd" ||
                    reduction == "ncclMin" || reduction == "ncclMax"),
                   "invalid reduction.");
D
dongzhihong 已提交
121

D
Dong Zhihong 已提交
122 123
    ctx->SetOutputsDim("Out", x_dims);
    ctx->ShareLoD("X", /*->*/ "Out");
D
dzhwinter 已提交
124 125 126
  }
};

D
Dong Zhihong 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140
// 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 已提交
141

D
Dong Zhihong 已提交
142 143 144 145 146
    std::string reduction = ctx->Attrs().Get<std::string>("reduction");
    PADDLE_ENFORCE((reduction == "ncclSum" || reduction == "ncclProd" ||
                    reduction == "ncclMin" || reduction == "ncclMax"),
                   "invalid reduction.");

D
Dong Zhihong 已提交
147 148 149
    auto x_dims = ctx->GetInputsDim("X");
    ctx->SetOutputsDim("Out", x_dims);
    ctx->ShareLoD("X", /*->*/ "Out");
D
Dong Zhihong 已提交
150 151 152
  }
};

D
Dong Zhihong 已提交
153 154
// BcastOp
class NCCLBcastOp : public framework::OperatorWithKernel {
D
Dong Zhihong 已提交
155 156 157 158 159 160 161 162 163 164 165
 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 已提交
166

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

D
Dong Zhihong 已提交
170 171 172
    auto x_dims = ctx->GetInputsDim("X");
    ctx->SetOutputsDim("Out", x_dims);
    ctx->ShareLoD("X", /*->*/ "Out");
D
Dong Zhihong 已提交
173 174 175
  }
};

D
Dong Zhihong 已提交
176
// AllreduceOp
D
dzhwinter 已提交
177
class NCCLAllReduceOpMaker : public framework::OpProtoAndCheckerMaker {
D
Dong Zhihong 已提交
178
 public:
179
  NCCLAllReduceOpMaker(OpProto *proto, OpAttrChecker *op_checker)
D
Dong Zhihong 已提交
180
      : OpProtoAndCheckerMaker(proto, op_checker) {
D
dzhwinter 已提交
181
    AddInput("X", "The input of AllReduce op");
D
Dong Zhihong 已提交
182
    AddInput("Communicator", "Communicator for communicating between gpus");
D
dzhwinter 已提交
183
    AddOutput("Out", "The output of AllReduce op");
D
Dong Zhihong 已提交
184
    AddAttr<std::string>("reduction",
K
kexinzhao 已提交
185
                         "(string, default 'ncclSum') "
D
Dong Zhihong 已提交
186 187
                         "{'ncclMin', 'ncclMax', 'ncclProd', 'ncclSum'}.")
        .SetDefault("ncclSum");
D
dzhwinter 已提交
188
    AddComment(R"DOC(
K
kexinzhao 已提交
189 190 191 192 193
NCCLAllReduce Operator.

AllReduce the input tensors.

)DOC");
D
dzhwinter 已提交
194
  }
D
dongzhihong 已提交
195
};
D
dzhwinter 已提交
196

D
Dong Zhihong 已提交
197 198
// ReduceOp
class NCCLReduceOpMaker : public framework::OpProtoAndCheckerMaker {
D
Dong Zhihong 已提交
199
 public:
200
  NCCLReduceOpMaker(OpProto *proto, OpAttrChecker *op_checker)
D
Dong Zhihong 已提交
201
      : OpProtoAndCheckerMaker(proto, op_checker) {
D
Dong Zhihong 已提交
202
    AddInput("X", "The input of Reduce op");
D
Dong Zhihong 已提交
203
    AddInput("Communicator", "Communicator for communicating between gpus");
D
Dong Zhihong 已提交
204
    AddOutput("Out", "The output of Reduce op");
D
Dong Zhihong 已提交
205
    AddAttr<std::string>("reduction",
K
kexinzhao 已提交
206
                         "(string, default 'ncclSum') "
D
Dong Zhihong 已提交
207 208
                         "{'ncclMin', 'ncclMax', 'ncclProd', 'ncclSum'}.")
        .SetDefault("ncclSum");
D
Dong Zhihong 已提交
209
    AddAttr<int>("root",
K
kexinzhao 已提交
210 211 212
                 "(int, default kInvalidGPUId) "
                 "Root gpu of the parameter. If not, "
                 "set(platform::kInvalidGPUId). Hashed by name.")
D
Dong Zhihong 已提交
213
        .SetDefault(platform::kInvalidGPUId);
D
Dong Zhihong 已提交
214
    AddComment(R"DOC(
K
kexinzhao 已提交
215 216 217 218 219
NCCLReduce Operator.

Reduce the tensors.

)DOC");
D
Dong Zhihong 已提交
220 221 222
  }
};

D
Dong Zhihong 已提交
223
// BcastOp
D
Dong Zhihong 已提交
224
class NCCLBcastOpMaker : public framework::OpProtoAndCheckerMaker {
D
Dong Zhihong 已提交
225
 public:
226
  NCCLBcastOpMaker(OpProto *proto, OpAttrChecker *op_checker)
D
Dong Zhihong 已提交
227
      : OpProtoAndCheckerMaker(proto, op_checker) {
D
Dong Zhihong 已提交
228
    AddInput("X", "The input of BcastSend op");
D
Dong Zhihong 已提交
229
    AddInput("Communicator", "Communicator for communicating between gpus");
D
Dong Zhihong 已提交
230
    AddOutput("Out", "The output of Bcast");
D
Dong Zhihong 已提交
231
    AddAttr<int>("root",
K
kexinzhao 已提交
232 233 234
                 "(int, default kInvalidGPUId) "
                 "Root gpu of the parameter. If not, "
                 "set(platform::kInvalidGPUId). Hashed by name.")
D
Dong Zhihong 已提交
235
        .SetDefault(platform::kInvalidGPUId);
D
Dong Zhihong 已提交
236
    AddComment(R"DOC(
K
kexinzhao 已提交
237 238 239 240 241
NCCLBcast Operator.

Bcast the tensors.

)DOC");
D
Dong Zhihong 已提交
242 243
  }
};
D
dzhwinter 已提交
244

D
Dong Zhihong 已提交
245 246 247 248
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
249
REGISTER_OPERATOR(ncclInit, ops::NCCLInitOp,
Y
Yang Yang 已提交
250 251 252
                  paddle::framework::EmptyGradOpMaker, ops::NCCLInitOpMaker,
                  ops::NCCLInitOpVarTypeInference,
                  ops::NCCLInitOpShapeInference);
253

D
Dong Zhihong 已提交
254 255
REGISTER_OP_WITHOUT_GRADIENT(ncclAllReduce, ops::NCCLAllReduceOp,
                             ops::NCCLAllReduceOpMaker);
D
Dong Zhihong 已提交
256 257
REGISTER_OP_WITHOUT_GRADIENT(ncclBcast, ops::NCCLBcastOp,
                             ops::NCCLBcastOpMaker);
D
Dong Zhihong 已提交
258 259
REGISTER_OP_WITHOUT_GRADIENT(ncclReduce, ops::NCCLReduceOp,
                             ops::NCCLReduceOpMaker);