pool_op.h 6.2 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 55 56 57 58 59

        } 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;
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);
72 73 74 75 76
        } 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;
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 114 115 116 117
      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;
118 119
            pool2d_backward(context.device_context(), *in_X, *in_X_grad, *out,
                            *out_grad, ksize, strides, paddings, pool_process);
120 121 122 123 124
          } 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;
125 126
            pool2d_backward(context.device_context(), *in_X, *in_X_grad, *out,
                            *out_grad, ksize, strides, paddings, pool_process);
127 128 129 130 131 132 133 134
          }
        } 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;
135 136
            pool3d_backward(context.device_context(), *in_X, *in_X_grad, *out,
                            *out_grad, ksize, strides, paddings, pool_process);
137 138 139 140 141
          } 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;
142 143
            pool3d_backward(context.device_context(), *in_X, *in_X_grad, *out,
                            *out_grad, ksize, strides, paddings, pool_process);
144 145 146 147 148 149 150 151 152
          }
        } break;
      }
    }
  }
};

}  // namespace operators
}  // namespace paddle