pool_op.h 5.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/* 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.
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

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

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;

template <typename Place, typename T>
class PoolKernel : public framework::OpKernel {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
C
chengduoZH 已提交
31
    const Tensor* in_x = context.Input<Tensor>("X");
32
    Tensor* out = context.Output<Tensor>("Out");
33

34
    bool global_pooling = context.Attr<bool>("globalPooling");
35
    std::string pooling_type = context.Attr<std::string>("poolingType");
36 37 38
    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
    if (global_pooling) {
40
      for (size_t i = 0; i < ksize.size(); ++i) {
C
chengduoZH 已提交
41
        ksize[i] = static_cast<int>(in_x->dims()[i + 2]);
42 43 44 45 46 47
      }
    }

    switch (ksize.size()) {
      case 2: {
        if (pooling_type == "max") {
C
chengduoZH 已提交
48 49
          paddle::operators::math::Pool2dFunctor<
              Place, paddle::operators::math::maxPool<T>, T>
50
              pool2d_forward;
C
chengduoZH 已提交
51 52
          paddle::operators::math::maxPool<T> pool_process;
          pool2d_forward(context.device_context(), *in_x, *out, ksize, strides,
53
                         paddings, pool_process);
54

C
chengduoZH 已提交
55
        } else if (pooling_type == "avg") {
C
chengduoZH 已提交
56 57
          paddle::operators::math::Pool2dFunctor<
              Place, paddle::operators::math::avgPool<T>, T>
58
              pool2d_forward;
C
chengduoZH 已提交
59 60
          paddle::operators::math::avgPool<T> pool_process;
          pool2d_forward(context.device_context(), *in_x, *out, ksize, strides,
61
                         paddings, pool_process);
62 63 64 65
        }
      } break;
      case 3: {
        if (pooling_type == "max") {
C
chengduoZH 已提交
66 67
          paddle::operators::math::Pool3dFunctor<
              Place, paddle::operators::math::maxPool<T>, T>
68
              pool3d_forward;
C
chengduoZH 已提交
69 70
          paddle::operators::math::maxPool<T> pool_process;
          pool3d_forward(context.device_context(), *in_x, *out, ksize, strides,
71
                         paddings, pool_process);
C
chengduoZH 已提交
72
        } else if (pooling_type == "avg") {
C
chengduoZH 已提交
73 74
          paddle::operators::math::Pool3dFunctor<
              Place, paddle::operators::math::avgPool<T>, T>
75
              pool3d_forward;
C
chengduoZH 已提交
76 77
          paddle::operators::math::avgPool<T> pool_process;
          pool3d_forward(context.device_context(), *in_x, *out, ksize, strides,
78
                         paddings, pool_process);
79 80 81 82 83 84 85 86 87 88
        }
      } break;
    }
  }
};

template <typename Place, typename T>
class PoolGradKernel : public framework::OpKernel {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
C
chengduoZH 已提交
89
    const Tensor* in_x = context.Input<Tensor>("X");
90 91 92
    const Tensor* out = context.Input<Tensor>("Out");
    const Tensor* out_grad =
        context.Input<Tensor>(framework::GradVarName("Out"));
C
chengduoZH 已提交
93
    Tensor* in_x_grad = context.Output<Tensor>(framework::GradVarName("X"));
94

95
    bool global_pooling = context.Attr<bool>("globalPooling");
96
    std::string pooling_type = context.Attr<std::string>("poolingType");
97 98 99 100
    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");

101
    if (global_pooling) {
C
chengduoZH 已提交
102 103
      for (size_t i = 0; i < ksize.size(); ++i)
        ksize[i] = static_cast<int>(in_x->dims()[i + 2]);
104 105
    }

C
chengduoZH 已提交
106 107 108
    if (in_x_grad) {
      in_x_grad->mutable_data<T>(context.GetPlace());
      auto temp = framework::EigenVector<T>::Flatten(*in_x_grad);
109 110 111 112 113 114
      temp.device(context.GetEigenDevice<Place>()) =
          temp.constant(static_cast<T>(0));

      switch (ksize.size()) {
        case 2: {
          if (pooling_type == "max") {
C
chengduoZH 已提交
115
            paddle::operators::math::MaxPool2dGradFunctor<Place, T>
116
                pool2d_backward;
C
chengduoZH 已提交
117
            pool2d_backward(context.device_context(), *in_x, *in_x_grad, *out,
C
chengduoZH 已提交
118
                            *out_grad, ksize, strides, paddings);
C
chengduoZH 已提交
119
          } else if (pooling_type == "avg") {
C
chengduoZH 已提交
120 121
            paddle::operators::math::Pool2dGradFunctor<
                Place, paddle::operators::math::avgPoolGrad<T>, T>
122
                pool2d_backward;
C
chengduoZH 已提交
123 124
            paddle::operators::math::avgPoolGrad<T> pool_process;
            pool2d_backward(context.device_context(), *in_x, *in_x_grad, *out,
125
                            *out_grad, ksize, strides, paddings, pool_process);
126 127 128 129
          }
        } break;
        case 3: {
          if (pooling_type == "max") {
C
chengduoZH 已提交
130
            paddle::operators::math::MaxPool3dGradFunctor<Place, T>
131
                pool3d_backward;
C
chengduoZH 已提交
132
            pool3d_backward(context.device_context(), *in_x, *in_x_grad, *out,
C
chengduoZH 已提交
133
                            *out_grad, ksize, strides, paddings);
C
chengduoZH 已提交
134
          } else if (pooling_type == "avg") {
C
chengduoZH 已提交
135 136
            paddle::operators::math::Pool3dGradFunctor<
                Place, paddle::operators::math::avgPoolGrad<T>, T>
137
                pool3d_backward;
C
chengduoZH 已提交
138 139
            paddle::operators::math::avgPoolGrad<T> pool_process;
            pool3d_backward(context.device_context(), *in_x, *in_x_grad, *out,
140
                            *out_grad, ksize, strides, paddings, pool_process);
141 142 143 144 145 146 147 148 149
          }
        } break;
      }
    }
  }
};

}  // namespace operators
}  // namespace paddle