c_broadcast_op.cc 2.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.

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

#include "paddle/fluid/operators/collective/c_broadcast_op.h"

namespace paddle {
namespace operators {

class CBroadcastOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    ctx->SetOutputDim("Out", ctx->GetInputDim("X"));
  }

 protected:
29
  phi::KernelKey GetExpectedKernelType(
30
      const framework::ExecutionContext& ctx) const override {
31 32
    return phi::KernelKey(OperatorWithKernel::IndicateVarDataType(ctx, "X"),
                          ctx.GetPlace());
33 34 35 36 37
  }
};

class CBroadcastOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
38
  void Make() override {
39 40 41 42 43 44
    AddInput("X", "(Tensor) tensor to be broadcasted.");
    AddOutput("Out", "(Tensor) the result of broadcast.");
    AddAttr<int>("ring_id", "(int default 0) nccl communication ring id.")
        .SetDefault(0);
    AddAttr<int>("root", "(int default 0) root id for broadcasting.")
        .SetDefault(0);
张春乔 已提交
45

46 47 48 49 50
    AddAttr<bool>(
        "use_calc_stream",
        "(bool default false) eject CUDA operations to calculation stream.")
        .SetDefault(false);
    AddComment(R"DOC(
51
CBroadcast Operator
52

53
Reference: https://docs.nvidia.com/deeplearning/sdk/nccl-developer-guide/docs/usage/operations.html#broadcast
54 55 56 57 58 59 60 61 62 63
)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
namespace plat = paddle::platform;

64 65
REGISTER_OP_WITHOUT_GRADIENT(c_broadcast,
                             ops::CBroadcastOp,
66 67
                             ops::CBroadcastOpMaker);

68 69 70 71 72 73 74 75 76
PD_REGISTER_STRUCT_KERNEL(c_broadcast,
                          CPU,
                          ALL_LAYOUT,
                          ops::CBroadcastOpCPUKernel,
                          float,
                          double,
                          int,
                          int64_t,
                          plat::float16) {}