nccl_op.cc 8.3 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 {

D
Dong Zhihong 已提交
22
// NCCLinitOp
23
class NCCLInitOp : public framework::OperatorBase {
D
Dong Zhihong 已提交
24
 public:
25 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) {}

  void Run(const framework::Scope &scope,
D
dzhwinter 已提交
31
           const platform::Place &place) const override {
32 33 34
    const auto &name = Output("Communicator");
    PADDLE_ENFORCE_NOT_NULL(scope.FindVar(name),
                            "Can not find variable '%s' in the scope.", name);
Y
Yang Yang 已提交
35 36 37 38 39 40 41

    int count = platform::GetCUDADeviceCount();
    std::vector<int> gpus(count);
    for (int i = 0; i < count; ++i) {
      gpus[i] = i;
    }
    PADDLE_ENFORCE(!gpus.empty(), "NCCL init with 0 gpus.");
D
dzhwinter 已提交
42 43 44 45 46

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

47 48 49
    platform::Communicator *comm =
        scope.FindVar(name)->GetMutable<platform::Communicator>();
    comm->InitAll(gpus);
D
Dong Zhihong 已提交
50 51 52
  }
};

Y
Yang Yang 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
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 已提交
69 70
class NCCLInitOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
71
  NCCLInitOpMaker(OpProto *proto, OpAttrChecker *op_checker)
D
Dong Zhihong 已提交
72 73 74 75
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddOutput("Communicator",
              "Create Communicator for communicating between gpus");
    AddComment(R"DOC(
K
kexinzhao 已提交
76 77 78 79 80
NCCLInit Operator.

Create communicator.

)DOC");
D
Dong Zhihong 已提交
81 82 83 84
  }
};

// AllReduceOp
D
dzhwinter 已提交
85
class NCCLAllReduceOp : public framework::OperatorWithKernel {
D
dongzhihong 已提交
86 87 88 89
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
D
Dong Zhihong 已提交
90 91 92
  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 已提交
93 94 95
    PADDLE_ENFORCE(
        ctx->HasInput("Communicator"),
        " Input(Communicator) of AllReduce op input should not be NULL");
D
Dong Zhihong 已提交
96
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
Y
Yang Yang 已提交
97
                   " Output(Out) of AllReduce op output should not be NULL");
D
dongzhihong 已提交
98

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

D
Dong Zhihong 已提交
101 102 103 104
    std::string reduction = ctx->Attrs().Get<std::string>("reduction");
    PADDLE_ENFORCE((reduction == "ncclSum" || reduction == "ncclProd" ||
                    reduction == "ncclMin" || reduction == "ncclMax"),
                   "invalid reduction.");
D
dongzhihong 已提交
105

D
Dong Zhihong 已提交
106 107
    ctx->SetOutputsDim("Out", x_dims);
    ctx->ShareLoD("X", /*->*/ "Out");
D
dzhwinter 已提交
108 109 110
  }
};

D
Dong Zhihong 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124
// 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 已提交
125

D
Dong Zhihong 已提交
126 127 128 129 130
    std::string reduction = ctx->Attrs().Get<std::string>("reduction");
    PADDLE_ENFORCE((reduction == "ncclSum" || reduction == "ncclProd" ||
                    reduction == "ncclMin" || reduction == "ncclMax"),
                   "invalid reduction.");

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

D
Dong Zhihong 已提交
137 138
// BcastOp
class NCCLBcastOp : public framework::OperatorWithKernel {
D
Dong Zhihong 已提交
139 140 141 142 143 144 145 146 147 148 149
 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 已提交
150

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

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

D
Dong Zhihong 已提交
160
// AllreduceOp
D
dzhwinter 已提交
161
class NCCLAllReduceOpMaker : public framework::OpProtoAndCheckerMaker {
D
Dong Zhihong 已提交
162
 public:
163
  NCCLAllReduceOpMaker(OpProto *proto, OpAttrChecker *op_checker)
D
Dong Zhihong 已提交
164
      : OpProtoAndCheckerMaker(proto, op_checker) {
D
dzhwinter 已提交
165
    AddInput("X", "The input of AllReduce op");
D
Dong Zhihong 已提交
166
    AddInput("Communicator", "Communicator for communicating between gpus");
D
dzhwinter 已提交
167
    AddOutput("Out", "The output of AllReduce op");
D
Dong Zhihong 已提交
168
    AddAttr<std::string>("reduction",
K
kexinzhao 已提交
169
                         "(string, default 'ncclSum') "
D
Dong Zhihong 已提交
170 171
                         "{'ncclMin', 'ncclMax', 'ncclProd', 'ncclSum'}.")
        .SetDefault("ncclSum");
D
dzhwinter 已提交
172
    AddComment(R"DOC(
K
kexinzhao 已提交
173 174 175 176 177
NCCLAllReduce Operator.

AllReduce the input tensors.

)DOC");
D
dzhwinter 已提交
178
  }
D
dongzhihong 已提交
179
};
D
dzhwinter 已提交
180

D
Dong Zhihong 已提交
181 182
// ReduceOp
class NCCLReduceOpMaker : public framework::OpProtoAndCheckerMaker {
D
Dong Zhihong 已提交
183
 public:
184
  NCCLReduceOpMaker(OpProto *proto, OpAttrChecker *op_checker)
D
Dong Zhihong 已提交
185
      : OpProtoAndCheckerMaker(proto, op_checker) {
D
Dong Zhihong 已提交
186
    AddInput("X", "The input of Reduce op");
D
Dong Zhihong 已提交
187
    AddInput("Communicator", "Communicator for communicating between gpus");
D
Dong Zhihong 已提交
188
    AddOutput("Out", "The output of Reduce op");
D
Dong Zhihong 已提交
189
    AddAttr<std::string>("reduction",
K
kexinzhao 已提交
190
                         "(string, default 'ncclSum') "
D
Dong Zhihong 已提交
191 192
                         "{'ncclMin', 'ncclMax', 'ncclProd', 'ncclSum'}.")
        .SetDefault("ncclSum");
D
Dong Zhihong 已提交
193
    AddAttr<int>("root",
K
kexinzhao 已提交
194 195 196
                 "(int, default kInvalidGPUId) "
                 "Root gpu of the parameter. If not, "
                 "set(platform::kInvalidGPUId). Hashed by name.")
D
Dong Zhihong 已提交
197
        .SetDefault(platform::kInvalidGPUId);
D
Dong Zhihong 已提交
198
    AddComment(R"DOC(
K
kexinzhao 已提交
199 200 201 202 203
NCCLReduce Operator.

Reduce the tensors.

)DOC");
D
Dong Zhihong 已提交
204 205 206
  }
};

D
Dong Zhihong 已提交
207
// BcastOp
D
Dong Zhihong 已提交
208
class NCCLBcastOpMaker : public framework::OpProtoAndCheckerMaker {
D
Dong Zhihong 已提交
209
 public:
210
  NCCLBcastOpMaker(OpProto *proto, OpAttrChecker *op_checker)
D
Dong Zhihong 已提交
211
      : OpProtoAndCheckerMaker(proto, op_checker) {
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;
233
REGISTER_OPERATOR(ncclInit, ops::NCCLInitOp,
Y
Yang Yang 已提交
234 235 236
                  paddle::framework::EmptyGradOpMaker, ops::NCCLInitOpMaker,
                  ops::NCCLInitOpVarTypeInference,
                  ops::NCCLInitOpShapeInference);
237

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