pool_with_index_op.h 4.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
C
chengduoZH 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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

#pragma once

17
#include <vector>
Y
Yi Wang 已提交
18 19 20 21
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/operators/math/pooling.h"
C
chengduoZH 已提交
22 23 24 25 26 27

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;

Q
QI JUN 已提交
28
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
29
class MaxPoolWithIndexKernel : public framework::OpKernel<T1> {
C
chengduoZH 已提交
30 31 32 33 34 35 36 37 38
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    const Tensor* in_x = context.Input<Tensor>("X");
    Tensor* out = context.Output<Tensor>("Out");
    Tensor* mask = context.Output<Tensor>("Mask");

    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");
Q
QI JUN 已提交
39 40

    auto& dev_ctx = context.template device_context<DeviceContext>();
C
chengduoZH 已提交
41
    if (context.Attr<bool>("global_pooling")) {
C
chengduoZH 已提交
42
      for (size_t i = 0; i < ksize.size(); ++i) {
C
fix bug  
chengduoZH 已提交
43
        paddings[i] = 0;
C
chengduoZH 已提交
44 45 46 47 48 49
        ksize[i] = static_cast<int>(in_x->dims()[i + 2]);
      }
    }

    switch (ksize.size()) {
      case 2: {
Q
QI JUN 已提交
50 51
        paddle::operators::math::MaxPool2dWithIndexFunctor<DeviceContext, T1,
                                                           T2>
C
chengduoZH 已提交
52
            pool2d_forward;
Q
QI JUN 已提交
53
        pool2d_forward(dev_ctx, *in_x, ksize, strides, paddings, out, mask);
C
chengduoZH 已提交
54 55
      } break;
      case 3: {
Q
QI JUN 已提交
56 57
        paddle::operators::math::MaxPool3dWithIndexFunctor<DeviceContext, T1,
                                                           T2>
C
chengduoZH 已提交
58
            pool3d_forward;
Q
QI JUN 已提交
59
        pool3d_forward(dev_ctx, *in_x, ksize, strides, paddings, out, mask);
C
chengduoZH 已提交
60
      } break;
C
fix bug  
chengduoZH 已提交
61
      default: { PADDLE_THROW("Pool op only supports 2D and 3D input."); }
C
chengduoZH 已提交
62 63 64 65
    }
  }
};

Q
QI JUN 已提交
66
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
67
class MaxPoolWithIndexGradKernel : public framework::OpKernel<T1> {
C
chengduoZH 已提交
68 69
 public:
  void Compute(const framework::ExecutionContext& context) const override {
C
chengduoZH 已提交
70
    const Tensor* mask = context.Input<Tensor>("Mask");
C
chengduoZH 已提交
71 72 73 74 75 76 77
    const Tensor* out_grad =
        context.Input<Tensor>(framework::GradVarName("Out"));
    Tensor* in_x_grad = context.Output<Tensor>(framework::GradVarName("X"));

    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");
C
chengduoZH 已提交
78
    if (context.Attr<bool>("global_pooling")) {
C
chengduoZH 已提交
79
      for (size_t i = 0; i < ksize.size(); ++i) {
C
fix bug  
chengduoZH 已提交
80
        paddings[i] = 0;
C
chengduoZH 已提交
81 82 83
        ksize[i] = static_cast<int>(in_x_grad->dims()[i + 2]);
      }
    }
C
chengduoZH 已提交
84 85

    if (in_x_grad) {
C
chengduoZH 已提交
86
      in_x_grad->mutable_data<T1>(context.GetPlace());
Q
QI JUN 已提交
87
      auto& device_ctx = context.template device_context<DeviceContext>();
88
      math::set_constant(device_ctx, in_x_grad, 0);
C
chengduoZH 已提交
89 90 91

      switch (ksize.size()) {
        case 2: {
Q
QI JUN 已提交
92 93
          paddle::operators::math::MaxPool2dWithIndexGradFunctor<DeviceContext,
                                                                 T1, T2>
C
chengduoZH 已提交
94
              pool2d_backward;
D
dangqingqing 已提交
95 96
          pool2d_backward(device_ctx, *out_grad, *mask, ksize, strides,
                          paddings, in_x_grad);
C
chengduoZH 已提交
97 98
        } break;
        case 3: {
Q
QI JUN 已提交
99 100
          paddle::operators::math::MaxPool3dWithIndexGradFunctor<DeviceContext,
                                                                 T1, T2>
C
chengduoZH 已提交
101
              pool3d_backward;
D
dangqingqing 已提交
102 103
          pool3d_backward(device_ctx, *out_grad, *mask, ksize, strides,
                          paddings, in_x_grad);
C
chengduoZH 已提交
104
        } break;
C
fix bug  
chengduoZH 已提交
105
        default: { PADDLE_THROW("Pool op only supports 2D and 3D input."); }
C
chengduoZH 已提交
106 107 108 109 110 111
      }
    }
  }
};
}  // namespace operators
}  // namespace paddle