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

Y
Yi Wang 已提交
17 18 19 20
#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 已提交
21 22 23 24 25 26

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;

Q
QI JUN 已提交
27
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
28
class MaxPoolWithIndexKernel : public framework::OpKernel<T1> {
C
chengduoZH 已提交
29 30 31 32 33 34 35 36 37
 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 已提交
38 39

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

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

Q
QI JUN 已提交
65
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
66
class MaxPoolWithIndexGradKernel : public framework::OpKernel<T1> {
C
chengduoZH 已提交
67 68
 public:
  void Compute(const framework::ExecutionContext& context) const override {
C
chengduoZH 已提交
69
    const Tensor* mask = context.Input<Tensor>("Mask");
C
chengduoZH 已提交
70 71 72 73 74 75 76
    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 已提交
77
    if (context.Attr<bool>("global_pooling")) {
C
chengduoZH 已提交
78
      for (size_t i = 0; i < ksize.size(); ++i) {
C
fix bug  
chengduoZH 已提交
79
        paddings[i] = 0;
C
chengduoZH 已提交
80 81 82
        ksize[i] = static_cast<int>(in_x_grad->dims()[i + 2]);
      }
    }
C
chengduoZH 已提交
83 84

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

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