left_pool_op.cc 3.6 KB
Newer Older
W
wangguanzhong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* 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/framework/op_registry.h"
namespace paddle {
namespace operators {

using Tensor = framework::Tensor;

class LeftPoolOp : public framework::OperatorWithKernel {
W
wangguanzhong 已提交
21
 public:
W
wangguanzhong 已提交
22 23 24 25 26 27 28 29
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null");
    ctx->ShareDim("X", /*->*/ "MaxMap");
    ctx->ShareDim("X", /*->*/ "Output");
  }

W
wangguanzhong 已提交
30
 protected:
W
wangguanzhong 已提交
31 32 33 34 35 36 37 38
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(ctx.Input<Tensor>("X")->type(),
                                   ctx.GetPlace());
  }
};

class LeftPoolOpMaker : public framework::OpProtoAndCheckerMaker {
W
wangguanzhong 已提交
39
 public:
W
wangguanzhong 已提交
40
  void Make() override {
W
wangguanzhong 已提交
41
    AddInput("X", "Input with shape (batch, C, H, W)");
W
wangguanzhong 已提交
42 43 44 45 46 47 48 49 50 51 52 53
    AddOutput("MaxMap", "Max map with index of maximum value of input");
    AddOutput("Output", "output with same shape as input(X)");
    AddComment(
        R"Doc(
This operatio calculates the left pooling output based on the input.
Scan the input from right to left for the horizontal max-pooling.
The output has the same shape with input.
        )Doc");
  }
};

class LeftPoolOpGrad : public framework::OperatorWithKernel {
W
wangguanzhong 已提交
54
 public:
W
wangguanzhong 已提交
55 56
  using framework::OperatorWithKernel::OperatorWithKernel;

W
wangguanzhong 已提交
57
 protected:
W
wangguanzhong 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null");
    PADDLE_ENFORCE(ctx->HasInput("MaxMap"), "Input(MaxMap) should not be null");
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Output")),
                   "Input(Output@GRAD) should not be null");
    auto out_grad_name = framework::GradVarName("Output");
    ctx->ShareDim(out_grad_name, framework::GradVarName("X"));
  }

  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(
        ctx.Input<Tensor>(framework::GradVarName("Output"))->type(),
        ctx.GetPlace());
  }
};

template <typename T>
class LeftPoolGradDescMaker : public framework::SingleGradOpMaker<T> {
W
wangguanzhong 已提交
77
 public:
W
wangguanzhong 已提交
78 79
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;

W
wangguanzhong 已提交
80
 protected:
W
wangguanzhong 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  void Apply(GradOpPtr<T> op) const override {
    op->SetType("left_pool_grad");
    op->SetInput("X", this->Input("X"));
    op->SetInput(framework::GradVarName("Output"), this->OutputGrad("Output"));
    op->SetInput("MaxMap", this->Output("MaxMap"));
    op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
    op->SetAttrMap(this->Attrs());
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OPERATOR(left_pool,
                  ops::LeftPoolOp,
                  ops::LeftPoolOpMaker,
                  ops::LeftPoolGradDescMaker<paddle::framework::OpDesc>,
                  ops::LeftPoolGradDescMaker<paddle::imperative::OpBase>);
REGISTER_OPERATOR(left_pool_grad, ops::LeftPoolOpGrad);