increment_op.cc 3.4 KB
Newer Older
F
fengjiayi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//   Copyright (c) 2018 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/increment_op.h"
H
hong 已提交
16
#include <memory>
A
Abhinav Arora 已提交
17
#include <string>
A
Abhinav Arora 已提交
18 19 20 21

namespace paddle {
namespace operators {

F
fengjiayi 已提交
22
class IncrementOp : public framework::OperatorWithKernel {
A
Abhinav Arora 已提交
23
 public:
F
fengjiayi 已提交
24 25 26 27 28 29
  IncrementOp(const std::string &type, const framework::VariableNameMap &inputs,
              const framework::VariableNameMap &outputs,
              const framework::AttributeMap &attrs)
      : OperatorWithKernel(type, inputs, outputs, attrs) {}

  void InferShape(framework::InferShapeContext *ctx) const override {
A
Abhinav Arora 已提交
30 31 32 33
    PADDLE_ENFORCE(ctx->HasInput("X"),
                   "Input(X) of IncrementOp should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
                   "Output(Out) of IncrementOp should not be null.");
Y
Yang Yu 已提交
34
    PADDLE_ENFORCE_EQ(1, framework::product(ctx->GetInputDim("X")));
A
Abhinav Arora 已提交
35
    ctx->SetOutputDim("Out", ctx->GetInputDim("X"));
F
fengjiayi 已提交
36
    ctx->ShareLoD("X", "Out");
A
Abhinav Arora 已提交
37
  }
38 39 40 41 42 43 44 45 46

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext &ctx) const override {
    framework::OpKernelType kt = OperatorWithKernel::GetExpectedKernelType(ctx);
    // IncrementOp kernel's device type is decided by input tensor place
    kt.place_ = ctx.Input<framework::LoDTensor>("X")->place();
    return kt;
  }
A
Abhinav Arora 已提交
47 48 49 50
};

class IncrementOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
51
  void Make() override {
A
Abhinav Arora 已提交
52 53
    AddInput("X", "(Tensor) The input tensor of increment operator");
    AddOutput("Out", "(Tensor) The output tensor of increment operator.");
Y
Yu Yang 已提交
54 55 56 57
    AddAttr<float>("step",
                   "(float, default 1.0) "
                   "The step size by which the "
                   "input tensor will be incremented.")
A
Abhinav Arora 已提交
58
        .SetDefault(1.0);
K
kexinzhao 已提交
59 60 61 62 63 64 65
    AddComment(R"DOC(
Increment Operator.

The equation is: 
$$Out = X + step$$

)DOC");
A
Abhinav Arora 已提交
66 67 68
  }
};

H
hong 已提交
69 70
template <typename T>
class IncrementGradOpMaker : public framework::SingleGradOpMaker<T> {
A
Abhinav Arora 已提交
71
 public:
H
hong 已提交
72
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
A
Abhinav Arora 已提交
73

74
  void Apply(GradOpPtr<T> grad_op) const override {
Y
Yang Yu 已提交
75
    grad_op->SetType("increment");
H
hong 已提交
76 77 78
    grad_op->SetInput("X", this->Output("Out"));
    grad_op->SetOutput("Out", this->Input("X"));
    grad_op->SetAttr("step", -boost::get<float>(this->GetAttr("step")));
A
Abhinav Arora 已提交
79 80 81 82 83 84 85
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
F
fengjiayi 已提交
86
REGISTER_OPERATOR(increment, ops::IncrementOp, ops::IncrementOpMaker,
H
hong 已提交
87 88
                  ops::IncrementGradOpMaker<paddle::framework::OpDesc>,
                  ops::IncrementGradOpMaker<paddle::imperative::OpBase>);
F
fengjiayi 已提交
89 90 91 92
REGISTER_OP_CPU_KERNEL(
    increment, ops::IncrementKernel<paddle::platform::CPUDeviceContext, float>,
    ops::IncrementKernel<paddle::platform::CPUDeviceContext, double>,
    ops::IncrementKernel<paddle::platform::CPUDeviceContext, int>,
93
    ops::IncrementKernel<paddle::platform::CPUDeviceContext, int64_t>);