pool_with_index_op.h 4.5 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;
64 65 66 67
      default: {
        PADDLE_THROW(platform::errors::InvalidArgument(
            "Pool op only supports 2D and 3D input."));
      }
C
chengduoZH 已提交
68 69 70 71
    }
  }
};

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

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

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