unpool_op.h 3.1 KB
Newer Older
S
sweetsky0901 已提交
1 2 3 4
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
5
Indicesou may obtain a copy of the License at
S
sweetsky0901 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

    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. */

#pragma once

#include "paddle/framework/op_registry.h"
#include "paddle/operators/math/math_function.h"
#include "paddle/operators/math/unpooling.h"

namespace paddle {
namespace operators {
S
sweetsky0901 已提交
23
template <typename Place, typename T>
S
sweetsky0901 已提交
24 25 26
class UnpoolKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
S
sweetsky0901 已提交
27
    const framework::Tensor* in_x = context.Input<framework::Tensor>("X");
28
    const framework::Tensor* in_y = context.Input<framework::Tensor>("Indices");
S
sweetsky0901 已提交
29
    auto* out = context.Output<framework::Tensor>("Out");
S
sweetsky0901 已提交
30
    std::string unpooling_type = context.Attr<std::string>("unpooling_type");
S
sweetsky0901 已提交
31 32 33
    std::vector<int> ksize = context.Attr<std::vector<int>>("ksize");
    std::vector<int> strides = context.Attr<std::vector<int>>("strides");
    std::vector<int> paddings = context.Attr<std::vector<int>>("paddings");
S
sweetsky0901 已提交
34 35 36 37 38
    T* output_data = out->mutable_data<T>(context.GetPlace());
    if (output_data) {
      math::SetConstant<Place, T> set_zero;
      set_zero(context.device_context(), out, static_cast<T>(0));
    }
S
sweetsky0901 已提交
39
    math::Unpool2dMaxFunctor<Place, T> unpool2d_max_forward;
S
sweetsky0901 已提交
40
    unpool2d_max_forward(context.device_context(), *in_x, *in_y, out);
S
sweetsky0901 已提交
41 42
  }
};
S
sweetsky0901 已提交
43
template <typename Place, typename T>
S
sweetsky0901 已提交
44 45 46
class UnpoolGradKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
S
sweetsky0901 已提交
47
    const framework::Tensor* in_x = context.Input<framework::Tensor>("X");
48
    const framework::Tensor* in_y = context.Input<framework::Tensor>("Indices");
S
sweetsky0901 已提交
49 50 51 52
    const framework::Tensor* out = context.Input<framework::Tensor>("Out");
    const framework::Tensor* out_grad =
        context.Input<framework::Tensor>(framework::GradVarName("Out"));
    framework::Tensor* in_x_grad =
S
sweetsky0901 已提交
53
        context.Output<framework::Tensor>(framework::GradVarName("X"));
S
sweetsky0901 已提交
54
    std::string unpooling_type = context.Attr<std::string>("unpooling_type");
S
sweetsky0901 已提交
55 56 57 58 59 60 61 62
    std::vector<int> ksize = context.Attr<std::vector<int>>("ksize");
    std::vector<int> strides = context.Attr<std::vector<int>>("strides");
    std::vector<int> paddings = context.Attr<std::vector<int>>("paddings");

    auto& device_ctx = context.device_context();
    math::SetConstant<Place, T> zero;
    if (in_x_grad) {
      in_x_grad->mutable_data<T>(context.GetPlace());
S
sweetsky0901 已提交
63
      zero(device_ctx, in_x_grad, static_cast<T>(0));
S
sweetsky0901 已提交
64
    }
S
sweetsky0901 已提交
65
    math::Unpool2dMaxGradFunctor<Place, T> unpool2d_max_backward;
S
sweetsky0901 已提交
66 67
    unpool2d_max_backward(context.device_context(), *in_x, *in_y, *out,
                          *out_grad, in_x_grad);
S
sweetsky0901 已提交
68 69 70 71
  }
};
}  // namespace operators
}  // namespace paddle