pool_with_index_op.h 4.4 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");
39
    bool adaptive = context.Attr<bool>("adaptive");
Q
QI JUN 已提交
40 41

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

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

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

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

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