nccl_op.cc 8.3 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
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
Yi Wang 已提交
15 16
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/nccl/nccl_gpu_common.h"
D
dongzhihong 已提交
17 18 19 20

namespace paddle {
namespace operators {

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

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

31 32 33
 private:
  void RunImpl(const framework::Scope &scope,
               const platform::Place &place) const override {
34 35 36 37
    PADDLE_ENFORCE_NOT_NULL(
        scope.FindVar(Input(kParallelScopes)),
        platform::errors::NotFound("Can not find variable '%s' in the scope.",
                                   kParallelScopes));
38
    const auto &name = Output("Communicator");
39 40 41 42
    PADDLE_ENFORCE_NOT_NULL(
        scope.FindVar(name),
        platform::errors::NotFound(
            "Output(%s) is needed for ncclInit operator.", name));
Y
Yang Yang 已提交
43 44 45 46 47 48 49
    // 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
    auto &parallel_scopes = scope.FindVar(Input(kParallelScopes))
                                ->Get<std::vector<framework::Scope *>>();
    std::vector<int> gpus(parallel_scopes.size());
    for (int i = 0; i < static_cast<int>(parallel_scopes.size()); ++i) {
Y
Yang Yang 已提交
50 51
      gpus[i] = i;
    }
52 53 54
    PADDLE_ENFORCE_EQ(!gpus.empty(), true,
                      platform::errors::PreconditionNotMet(
                          "gpus is empty, NCCL must init with gpus"));
D
dzhwinter 已提交
55

56 57 58
    platform::Communicator *comm =
        scope.FindVar(name)->GetMutable<platform::Communicator>();
    comm->InitAll(gpus);
D
Dong Zhihong 已提交
59 60 61
  }
};

Y
Yang Yang 已提交
62 63
class NCCLInitOpVarTypeInference : public framework::VarTypeInference {
 public:
M
minqiyang 已提交
64
  void operator()(framework::InferVarTypeContext *ctx) const override {
65
    ctx->SetOutputType("Communicator", framework::proto::VarType::RAW);
Y
Yang Yang 已提交
66 67 68 69 70 71 72 73
  }
};

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

D
Dong Zhihong 已提交
74 75
class NCCLInitOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
76
  void Make() override {
Y
Yang Yang 已提交
77
    AddInput(kParallelScopes, "The working place of parallel do.");
D
Dong Zhihong 已提交
78 79 80
    AddOutput("Communicator",
              "Create Communicator for communicating between gpus");
    AddComment(R"DOC(
K
kexinzhao 已提交
81 82 83 84 85
NCCLInit Operator.

Create communicator.

)DOC");
D
Dong Zhihong 已提交
86 87 88 89
  }
};

// AllReduceOp
D
dzhwinter 已提交
90
class NCCLAllReduceOp : public framework::OperatorWithKernel {
D
dongzhihong 已提交
91 92 93 94
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
D
Dong Zhihong 已提交
95
  void InferShape(framework::InferShapeContext *ctx) const override {
96 97 98 99 100 101
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "NCCLAllReduce");
    OP_INOUT_CHECK(ctx->HasInput("Communicator"), "Input", "Communicator",
                   "NCCLAllReduce");

    OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "NCCLAllReduce");

D
Dong Zhihong 已提交
102
    std::string reduction = ctx->Attrs().Get<std::string>("reduction");
103 104 105 106
    PADDLE_ENFORCE_EQ(
        (reduction == "ncclSum" || reduction == "ncclProd" ||
         reduction == "ncclMin" || reduction == "ncclMax"),
        true, platform::errors::InvalidArgument("invalid nccl reduction."));
D
dongzhihong 已提交
107

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

D
dzhwinter 已提交
114 115 116
// AllReduceOp
class NCCLAllReduceOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
117
  void Make() override {
D
dzhwinter 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    AddInput("X", "The input of AllReduce op");
    AddInput("Communicator", "Communicator for communicating between gpus");
    AddOutput("Out", "The output of AllReduce op");
    AddAttr<std::string>("reduction",
                         "(string, default 'ncclSum') "
                         "{'ncclMin', 'ncclMax', 'ncclProd', 'ncclSum'}.")
        .SetDefault("ncclSum");
    AddComment(R"DOC(
NCCLAllReduce Operator.

AllReduce the input tensors.

)DOC");
  }
};

D
Dong Zhihong 已提交
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 {
141 142 143 144 145
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "NCCLReduce");
    OP_INOUT_CHECK(ctx->HasInput("Communicator"), "Input", "Communicator",
                   "NCCLReduce");

    OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "NCCLReduce");
D
Dong Zhihong 已提交
146

D
Dong Zhihong 已提交
147
    std::string reduction = ctx->Attrs().Get<std::string>("reduction");
148 149 150 151
    PADDLE_ENFORCE_EQ(
        (reduction == "ncclSum" || reduction == "ncclProd" ||
         reduction == "ncclMin" || reduction == "ncclMax"),
        true, platform::errors::InvalidArgument("invalid nccl reduction."));
D
Dong Zhihong 已提交
152

D
Dong Zhihong 已提交
153 154 155
    auto x_dims = ctx->GetInputsDim("X");
    ctx->SetOutputsDim("Out", x_dims);
    ctx->ShareLoD("X", /*->*/ "Out");
D
Dong Zhihong 已提交
156 157 158
  }
};

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

Reduce the tensors.

)DOC");
D
Dong Zhihong 已提交
181 182 183
  }
};

D
dzhwinter 已提交
184 185 186 187 188 189 190
// BcastOp
class NCCLBcastOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  void InferShape(framework::InferShapeContext *ctx) const override {
191 192 193 194 195
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "NCCLBcast");
    OP_INOUT_CHECK(ctx->HasInput("Communicator"), "Input", "Communicator",
                   "NCCLBcast");

    OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "NCCLBcast");
D
dzhwinter 已提交
196 197

    int root = ctx->Attrs().Get<int>("root");
198 199 200
    PADDLE_ENFORCE_EQ(
        root != platform::kInvalidGPUId, true,
        platform::errors::InvalidArgument("Bcast root must be set."));
D
dzhwinter 已提交
201 202 203 204 205 206 207

    auto x_dims = ctx->GetInputsDim("X");
    ctx->SetOutputsDim("Out", x_dims);
    ctx->ShareLoD("X", /*->*/ "Out");
  }
};

D
Dong Zhihong 已提交
208
// BcastOp
D
Dong Zhihong 已提交
209
class NCCLBcastOpMaker : public framework::OpProtoAndCheckerMaker {
D
Dong Zhihong 已提交
210
 public:
Y
Yu Yang 已提交
211
  void Make() override {
D
Dong Zhihong 已提交
212
    AddInput("X", "The input of BcastSend op");
D
Dong Zhihong 已提交
213
    AddInput("Communicator", "Communicator for communicating between gpus");
D
Dong Zhihong 已提交
214
    AddOutput("Out", "The output of Bcast");
D
Dong Zhihong 已提交
215
    AddAttr<int>("root",
K
kexinzhao 已提交
216 217 218
                 "(int, default kInvalidGPUId) "
                 "Root gpu of the parameter. If not, "
                 "set(platform::kInvalidGPUId). Hashed by name.")
D
Dong Zhihong 已提交
219
        .SetDefault(platform::kInvalidGPUId);
D
Dong Zhihong 已提交
220
    AddComment(R"DOC(
K
kexinzhao 已提交
221 222 223 224 225
NCCLBcast Operator.

Bcast the tensors.

)DOC");
D
Dong Zhihong 已提交
226 227
  }
};
D
dzhwinter 已提交
228

D
Dong Zhihong 已提交
229 230 231 232
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
H
hong 已提交
233 234 235 236 237 238
REGISTER_OPERATOR(
    ncclInit, ops::NCCLInitOp,
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
    ops::NCCLInitOpMaker, ops::NCCLInitOpVarTypeInference,
    ops::NCCLInitOpShapeInference);
239

D
Dong Zhihong 已提交
240 241
REGISTER_OP_WITHOUT_GRADIENT(ncclAllReduce, ops::NCCLAllReduceOp,
                             ops::NCCLAllReduceOpMaker);
D
Dong Zhihong 已提交
242 243
REGISTER_OP_WITHOUT_GRADIENT(ncclBcast, ops::NCCLBcastOp,
                             ops::NCCLBcastOpMaker);
D
Dong Zhihong 已提交
244 245
REGISTER_OP_WITHOUT_GRADIENT(ncclReduce, ops::NCCLReduceOp,
                             ops::NCCLReduceOpMaker);