top_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 TopPoolOp : 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
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
W
wangguanzhong 已提交
33
    return framework::OpKernelType(ctx.Input<Tensor>("X")->type(),
W
wangguanzhong 已提交
34 35 36 37 38
                                   ctx.GetPlace());
  }
};

class TopPoolOpMaker : 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 top pooling output based on the input.
Scan the input from bottom to top for the vertical max-pooling.
The output has the same shape with input.
        )Doc");
  }
};

class TopPoolOpGrad : 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
  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");
W
wangguanzhong 已提交
63

W
wangguanzhong 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    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 TopPoolGradDescMaker : public framework::SingleGradOpMaker<T> {
 public:
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;

 protected:
  void Apply(GradOpPtr<T> op) const override {
    op->SetType("top_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(top_pool,
                  ops::TopPoolOp,
                  ops::TopPoolOpMaker,
                  ops::TopPoolGradDescMaker<paddle::framework::OpDesc>,
                  ops::TopPoolGradDescMaker<paddle::imperative::OpBase>);
REGISTER_OPERATOR(top_pool_grad, ops::TopPoolOpGrad);