minus_op.cc 3.7 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* 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. */

#include "paddle/operators/minus_op.h"
#include "paddle/operators/net_op.h"

namespace paddle {
namespace operators {

class MinusOp : public framework::OperatorWithKernel {
 public:
23 24 25
  MinusOp(const std::string &type, const framework::VariableNameMap &inputs,
          const framework::VariableNameMap &outputs,
          const framework::AttributeMap &attrs)
Y
Yu Yang 已提交
26 27 28 29
      : OperatorWithKernel(type, inputs, outputs, attrs) {}

 protected:
  void InferShape(const framework::InferShapeContext &ctx) const override {
30 31 32 33 34 35 36
    PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("X"),
                            "Input(X) of MinusOp should not be null.");
    PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Y"),
                            "Input(Y) of MinusOp should not be null.");
    PADDLE_ENFORCE_NOT_NULL(ctx.OutputVar("Out"),
                            "Output(Out) of MinusOp should not be null.");

Y
Yu Yang 已提交
37 38 39 40
    auto *left_tensor = ctx.Input<framework::Tensor>("X");
    auto *right_tensor = ctx.Input<framework::Tensor>("Y");

    PADDLE_ENFORCE_EQ(
41
        left_tensor->numel(), right_tensor->numel(),
Y
Yu Yang 已提交
42
        "Minus operator must take two tensor with same num of elements");
43 44
    ctx.Output<framework::Tensor>("Out")->Resize(left_tensor->dims());
    ctx.ShareLoD("X", /*->*/ "Out");
Y
Yu Yang 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57
  }
};

class MinusOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  MinusOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("X", "The left tensor of minus operator.").NotInGradient();
    AddInput("Y", "The right tensor of minus operator.").NotInGradient();
    AddOutput("Out", "The output tensor of minus operator.").NotInGradient();

    AddComment(R"DOC(Minus Operator

58 59 60 61 62 63
Equation:

    Out = X - Y

Both the input `X` and `Y` can carry the LoD (Level of Details) information,
or not. But the output only shares the LoD with input `X`.
Y
Yu Yang 已提交
64 65 66 67 68 69
)DOC");
  }
};
template <typename AttrType>
class MinusGradOp : public NetOp {
 public:
70 71 72
  MinusGradOp(const std::string &type, const framework::VariableNameMap &inputs,
              const framework::VariableNameMap &outputs,
              const framework::AttributeMap &attrs)
Y
Yu Yang 已提交
73 74 75 76 77 78
      : NetOp(type, inputs, outputs, attrs) {
    auto out_grad = Input(framework::GradVarName("Out"));
    auto x_grad = Output(framework::GradVarName("X"));
    auto y_grad = Output(framework::GradVarName("Y"));

    // x_grad = out_grad
79
    AppendOp(framework::OpRegistry::CreateOp("identity", {{"X", {out_grad}}},
80
                                             {{"Y", {x_grad}}}, {}));
Y
Yu Yang 已提交
81 82 83

    framework::AttributeMap scale_attr;
    scale_attr["scale"] = static_cast<AttrType>(-1);
84 85 86
    AppendOp(framework::OpRegistry::CreateOp("scale", {{"X", {out_grad}}},
                                             {{"Out", {y_grad}}}, scale_attr));
    CompleteAddOp(false);
Y
Yu Yang 已提交
87 88 89 90 91 92 93
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
94 95
REGISTER_OP(minus, ops::MinusOp, ops::MinusOpMaker, minus_grad,
            ops::MinusGradOp<float>);
Y
Yu Yang 已提交
96 97
REGISTER_OP_CPU_KERNEL(minus,
                       ops::MinusKernel<paddle::platform::CPUPlace, float>);