pool_op.h 6.0 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 {
31 32
    const Tensor* in_X = context.Input<Tensor>("X");
    Tensor* out = context.Output<Tensor>("Out");
33

34 35
    int global_pooling = context.Attr<int>("globalPooling");
    std::string pooling_type = context.Attr<std::string>("poolingType");
36 37 38 39 40
    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");
    if (global_pooling == 1) {
      for (size_t i = 0; i < ksize.size(); ++i) {
41
        ksize[i] = in_X->dims()[i + 2];
42 43 44 45 46 47 48 49 50 51
      }
    }

    switch (ksize.size()) {
      case 2: {
        if (pooling_type == "max") {
          paddle::operators::math::Pool2dForwardFunctor<
              Place, paddle::operators::math::pool::maxPool<T>, T>
              pool2d_forward;
          paddle::operators::math::pool::maxPool<T> pool_process;
52 53
          pool2d_forward(context.device_context(), *in_X, *out, ksize, strides,
                         paddings, pool_process);
54

C
chengduoZH 已提交
55
        } else if (pooling_type == "avg") {
56
          paddle::operators::math::Pool2dForwardFunctor<
C
chengduoZH 已提交
57
              Place, paddle::operators::math::pool::avgPool<T>, T>
58
              pool2d_forward;
C
chengduoZH 已提交
59
          paddle::operators::math::pool::avgPool<T> pool_process;
60 61
          pool2d_forward(context.device_context(), *in_X, *out, ksize, strides,
                         paddings, pool_process);
62 63 64 65 66 67 68 69
        }
      } break;
      case 3: {
        if (pooling_type == "max") {
          paddle::operators::math::Pool3dForwardFunctor<
              Place, paddle::operators::math::pool::maxPool<T>, T>
              pool3d_forward;
          paddle::operators::math::pool::maxPool<T> pool_process;
70 71
          pool3d_forward(context.device_context(), *in_X, *out, ksize, strides,
                         paddings, pool_process);
C
chengduoZH 已提交
72
        } else if (pooling_type == "avg") {
73
          paddle::operators::math::Pool3dForwardFunctor<
C
chengduoZH 已提交
74
              Place, paddle::operators::math::pool::avgPool<T>, T>
75
              pool3d_forward;
C
chengduoZH 已提交
76
          paddle::operators::math::pool::avgPool<T> pool_process;
77 78
          pool3d_forward(context.device_context(), *in_X, *out, ksize, strides,
                         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 {
89 90 91 92
    const Tensor* in_X = context.Input<Tensor>("X");
    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 96

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

    if (global_pooling == 1) {
102
      for (size_t i = 0; i < ksize.size(); ++i) ksize[i] = in_X->dims()[i + 2];
103 104
    }

105 106 107
    if (in_X_grad) {
      in_X_grad->mutable_data<T>(context.GetPlace());
      auto temp = framework::EigenVector<T>::Flatten(*in_X_grad);
108 109 110 111 112 113
      temp.device(context.GetEigenDevice<Place>()) =
          temp.constant(static_cast<T>(0));

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

}  // namespace operators
}  // namespace paddle