increment_op.cc 3.8 KB
Newer Older
A
Abhinav Arora 已提交
1 2
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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
A
Abhinav Arora 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
A
Abhinav Arora 已提交
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. */
A
Abhinav Arora 已提交
14

Y
Yi Wang 已提交
15
#include "paddle/fluid/framework/op_registry.h"
A
Abhinav Arora 已提交
16 17 18 19

namespace paddle {
namespace operators {

Y
Yang Yu 已提交
20
class IncrementInferShape : public framework::InferShapeBase {
A
Abhinav Arora 已提交
21
 public:
Y
Yang Yu 已提交
22
  void operator()(framework::InferShapeContext *ctx) const override {
A
Abhinav Arora 已提交
23 24 25 26
    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 已提交
27
    PADDLE_ENFORCE_EQ(1, framework::product(ctx->GetInputDim("X")));
A
Abhinav Arora 已提交
28
    ctx->SetOutputDim("Out", ctx->GetInputDim("X"));
Y
Yang Yu 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  }
};

struct IncrementFunctor {
  IncrementFunctor(const framework::LoDTensor &x, framework::LoDTensor *out,
                   float value)
      : x_(x), out_(out), value_(value) {}

  template <typename T>
  void operator()() const {
    *out_->data<T>() = *x_.data<T>() + static_cast<T>(value_);
  }

  const framework::LoDTensor &x_;
  framework::LoDTensor *out_;
  float value_;
};

class IncrementOp : public framework::OperatorBase {
 public:
  IncrementOp(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 已提交
55
           const platform::Place &place) const override {
Y
Yang Yu 已提交
56 57 58 59 60 61 62 63
    auto &x = scope.FindVar(Input("X"))->Get<framework::LoDTensor>();
    auto &out =
        *scope.FindVar(Output("Out"))->GetMutable<framework::LoDTensor>();

    PADDLE_ENFORCE(platform::is_cpu_place(x.place()));
    out.Resize(x.dims());
    out.mutable_data(x.place(), x.type());
    float value = Attr<float>("step");
64 65
    VLOG(10) << Output("Out") << " increase " << Input("X") << " with "
             << value;
Y
Yang Yu 已提交
66 67
    framework::VisitDataType(framework::ToDataType(out.type()),
                             IncrementFunctor(x, &out, value));
A
Abhinav Arora 已提交
68 69 70 71 72
  }
};

class IncrementOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
73
  IncrementOpMaker(OpProto *proto, OpAttrChecker *op_checker)
A
Abhinav Arora 已提交
74 75 76
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("X", "(Tensor) The input tensor of increment operator");
    AddOutput("Out", "(Tensor) The output tensor of increment operator.");
Y
Yu Yang 已提交
77 78 79 80
    AddAttr<float>("step",
                   "(float, default 1.0) "
                   "The step size by which the "
                   "input tensor will be incremented.")
A
Abhinav Arora 已提交
81
        .SetDefault(1.0);
K
kexinzhao 已提交
82 83 84 85 86 87 88
    AddComment(R"DOC(
Increment Operator.

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

)DOC");
A
Abhinav Arora 已提交
89 90 91 92 93 94 95
  }
};

class IncrementGradOpMaker : public framework::SingleGradOpDescMaker {
 public:
  using framework::SingleGradOpDescMaker::SingleGradOpDescMaker;

Y
Yu Yang 已提交
96 97
  std::unique_ptr<framework::OpDesc> Apply() const override {
    auto *grad_op = new framework::OpDesc();
Y
Yang Yu 已提交
98 99 100 101
    grad_op->SetType("increment");
    grad_op->SetInput("X", Output("Out"));
    grad_op->SetOutput("Out", Input("X"));
    grad_op->SetAttr("step", -boost::get<float>(GetAttr("step")));
Y
Yu Yang 已提交
102
    return std::unique_ptr<framework::OpDesc>(grad_op);
A
Abhinav Arora 已提交
103 104 105 106 107 108 109
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
Y
Yang Yu 已提交
110 111
REGISTER_OPERATOR(increment, ops::IncrementOp, ops::IncrementInferShape,
                  ops::IncrementOpMaker, ops::IncrementGradOpMaker);