minus_op.cc 3.4 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yu Yang 已提交
2

L
Luo Tao 已提交
3 4 5
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
Y
Yu Yang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Y
Yu Yang 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
Y
Yu Yang 已提交
14

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/minus_op.h"
16

17 18
#include <string>
#include <vector>
Y
Yu Yang 已提交
19 20 21 22 23 24

namespace paddle {
namespace operators {

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

30
  void InferShape(framework::InferShapeContext *ctx) const override {
Q
Qiao Longfei 已提交
31 32 33 34 35 36
    PADDLE_ENFORCE(ctx->HasInput("X"),
                   "Input(X) of MinusOp should not be null.");
    PADDLE_ENFORCE(ctx->HasInput("Y"),
                   "Input(Y) of MinusOp should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
                   "Output(Out) of MinusOp should not be null.");
37

Q
Qiao Longfei 已提交
38 39
    auto x_dims = ctx->GetInputDim("X");
    auto y_dims = ctx->GetInputDim("Y");
Y
Yu Yang 已提交
40 41

    PADDLE_ENFORCE_EQ(
Q
Qiao Longfei 已提交
42
        x_dims, y_dims,
Y
Yu Yang 已提交
43
        "Minus operator must take two tensor with same num of elements");
Q
Qiao Longfei 已提交
44 45
    ctx->SetOutputDim("Out", x_dims);
    ctx->ShareLoD("X", /*->*/ "Out");
Y
Yu Yang 已提交
46 47 48 49 50
  }
};

class MinusOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
51
  MinusOpMaker(OpProto *proto, OpAttrChecker *op_checker)
Y
Yu Yang 已提交
52
      : OpProtoAndCheckerMaker(proto, op_checker) {
53 54 55
    AddInput("X", "The left tensor of minus operator.");
    AddInput("Y", "The right tensor of minus operator.");
    AddOutput("Out", "The output tensor of minus operator.");
Y
Yu Yang 已提交
56

K
kexinzhao 已提交
57 58
    AddComment(R"DOC(
Minus Operator.
Y
Yu Yang 已提交
59

60 61
Equation:

K
kexinzhao 已提交
62
    $Out = X - Y$
63 64

Both the input `X` and `Y` can carry the LoD (Level of Details) information,
K
kexinzhao 已提交
65 66
or not. But the output only shares the LoD information with input `X`.

Y
Yu Yang 已提交
67 68 69
)DOC");
  }
};
70 71

class MinusGradMaker : public framework::GradOpDescMakerBase {
Y
Yu Yang 已提交
72
 public:
73 74
  using framework::GradOpDescMakerBase::GradOpDescMakerBase;

Y
Yu Yang 已提交
75 76
  std::vector<std::unique_ptr<framework::OpDesc>> operator()() const override {
    std::vector<std::unique_ptr<framework::OpDesc>> ops;
Y
Yu Yang 已提交
77 78
    auto x_g = InputGrad("X");
    if (!x_g.empty()) {
Y
Yu Yang 已提交
79
      auto *x_g_op = new framework::OpDesc();
Y
Yu Yang 已提交
80 81 82 83 84 85 86 87 88
      x_g_op->SetType("scale");
      x_g_op->SetInput("X", OutputGrad("Out"));
      x_g_op->SetOutput("Out", x_g);
      x_g_op->SetAttr("scale", 1.0f);
      ops.emplace_back(x_g_op);
    }

    auto y_g = InputGrad("Y");
    if (!y_g.empty()) {
Y
Yu Yang 已提交
89
      auto *y_g_op = new framework::OpDesc();
Y
Yu Yang 已提交
90 91 92 93 94 95 96
      y_g_op->SetType("scale");
      y_g_op->SetInput("X", OutputGrad("Out"));
      y_g_op->SetOutput("Out", y_g);
      y_g_op->SetAttr("scale", -1.0f);
      ops.emplace_back(y_g_op);
    }

97
    return ops;
Y
Yu Yang 已提交
98 99 100 101 102 103 104
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
105
REGISTER_OPERATOR(minus, ops::MinusOp, ops::MinusOpMaker, ops::MinusGradMaker);
Q
QI JUN 已提交
106 107
REGISTER_OP_CPU_KERNEL(
    minus, ops::MinusKernel<paddle::platform::CPUDeviceContext, float>);