pool_op.h 6.5 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/* 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 {
    const Tensor* input = context.Input<Tensor>("Input");
    Tensor* output = context.Output<Tensor>("Output");

    int global_pooling = context.Attr<int>("global_pooling");
    std::string pooling_type = context.Attr<std::string>("pooling_type");
    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) {
        ksize[i] = input->dims()[i + 2];
      }
    }

    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;
          pool2d_forward(*input, *output, ksize, strides, paddings,
C
chengduoZH 已提交
53
                         pool_process, context.device_context());
54 55 56 57 58 59 60

        } else if (pooling_type == "ave") {
          paddle::operators::math::Pool2dForwardFunctor<
              Place, paddle::operators::math::pool::avePool<T>, T>
              pool2d_forward;
          paddle::operators::math::pool::avePool<T> pool_process;
          pool2d_forward(*input, *output, ksize, strides, paddings,
C
chengduoZH 已提交
61
                         pool_process, (context.device_context()));
62 63 64 65 66 67 68 69 70
        }
      } 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;
          pool3d_forward(*input, *output, ksize, strides, paddings,
C
chengduoZH 已提交
71
                         pool_process, context.device_context());
72 73 74 75 76 77
        } else if (pooling_type == "ave") {
          paddle::operators::math::Pool3dForwardFunctor<
              Place, paddle::operators::math::pool::avePool<T>, T>
              pool3d_forward;
          paddle::operators::math::pool::avePool<T> pool_process;
          pool3d_forward(*input, *output, ksize, strides, paddings,
C
chengduoZH 已提交
78
                         pool_process, context.device_context());
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        }
      } break;
    }
  }
};

template <typename Place, typename T>
class PoolGradKernel : public framework::OpKernel {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    const Tensor* input = context.Input<Tensor>("Input");
    const Tensor* output = context.Input<Tensor>("Output");
    const Tensor* output_grad =
        context.Input<Tensor>(framework::GradVarName("Output"));
    Tensor* input_grad =
        context.Output<framework::LoDTensor>(framework::GradVarName("Input"));

    int global_pooling = context.Attr<int>("global_pooling");
    std::string pooling_type = context.Attr<std::string>("pooling_type");
    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) ksize[i] = input->dims()[i + 2];
    }

    if (input_grad) {
      input_grad->mutable_data<T>(context.GetPlace());
      auto temp = framework::EigenVector<T>::Flatten(*input_grad);
      temp.device(context.GetEigenDevice<Place>()) =
          temp.constant(static_cast<T>(0));

      switch (ksize.size()) {
        case 2: {
          if (pooling_type == "max") {
            paddle::operators::math::Pool2dBackwardFunctor<
                Place, paddle::operators::math::pool::maxPool<T>, T>
                pool2d_backward;
            paddle::operators::math::pool::maxPool<T> pool_process;
            pool2d_backward(*input, *input_grad, *output, *output_grad, ksize,
C
chengduoZH 已提交
120 121
                            strides, paddings, pool_process,
                            context.device_context());
122 123 124 125 126 127
          } else if (pooling_type == "ave") {
            paddle::operators::math::Pool2dBackwardFunctor<
                Place, paddle::operators::math::pool::avePool<T>, T>
                pool2d_backward;
            paddle::operators::math::pool::avePool<T> pool_process;
            pool2d_backward(*input, *input_grad, *output, *output_grad, ksize,
C
chengduoZH 已提交
128 129
                            strides, paddings, pool_process,
                            context.device_context());
130 131 132 133 134 135 136 137 138
          }
        } break;
        case 3: {
          if (pooling_type == "max") {
            paddle::operators::math::Pool3dBackwardFunctor<
                Place, paddle::operators::math::pool::maxPool<T>, T>
                pool3d_backward;
            paddle::operators::math::pool::maxPool<T> pool_process;
            pool3d_backward(*input, *input_grad, *output, *output_grad, ksize,
C
chengduoZH 已提交
139 140
                            strides, paddings, pool_process,
                            context.device_context());
141 142 143 144 145 146
          } else if (pooling_type == "ave") {
            paddle::operators::math::Pool3dBackwardFunctor<
                Place, paddle::operators::math::pool::avePool<T>, T>
                pool3d_backward;
            paddle::operators::math::pool::avePool<T> pool_process;
            pool3d_backward(*input, *input_grad, *output, *output_grad, ksize,
C
chengduoZH 已提交
147 148
                            strides, paddings, pool_process,
                            context.device_context());
149 150 151 152 153 154 155 156 157
          }
        } break;
      }
    }
  }
};

}  // namespace operators
}  // namespace paddle