pool_arm_func.h 3.3 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
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

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. */

#ifdef POOL_OP
#pragma once

#include <string>
#include <vector>
#include "operators/math/pooling.h"

namespace paddle_mobile {
namespace operators {
using framework::Tensor;

inline void PoolBasic(std::string pooling_type, std::vector<int> ksize,
                      std::vector<int> strides, std::vector<int> paddings,
                      const Tensor *in_x, Tensor *out) {
  if (pooling_type == "max") {
    math::PoolFunctor<CPU, math::MaxPool<float>, float> pool2d_forward;
    math::MaxPool<float> pool_process;
    pool2d_forward(*in_x, ksize, strides, paddings, pool_process, out);

  } else if (pooling_type == "avg") {
    math::PoolFunctor<CPU, math::AvgPool<float>, float> pool2d_forward;
    math::AvgPool<float> pool_process;
    pool2d_forward(*in_x, ksize, strides, paddings, pool_process, out);
  }
}
template <typename P>
N
nhzlx 已提交
41
void PoolCompute(const PoolParam<CPU> &param) {
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  const Tensor *in_x = param.Input();
  Tensor *out = param.Output();
  std::string pooling_type = param.PoolingType();

  std::vector<int> ksize = param.Ksize();

  std::vector<int> strides = param.Strides();

  std::vector<int> paddings = param.Paddings();
  if (ksize.size() != 2) {
    LOG(paddle_mobile::LogLevel::kLOG_ERROR)
        << "Pool op only supports 2D and 3D input.";
  }

  if (param.isGlobalPooling()) {
    for (size_t i = 0; i < ksize.size(); ++i) {
      paddings[i] = 0;
      ksize[i] = static_cast<int>(in_x->dims()[i + 2]);
    }
N
nhzlx 已提交
61 62
  }
  if (ksize[0] == 3 && ksize[0] == ksize[1]) {
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    if (pooling_type == "max") {
      if (strides[0] == strides[1] && strides[0] == 1 &&
          paddings[0] == paddings[1] && paddings[1] == 1) {
        math::Pool3x3Maxs1p1(in_x, out);
      } else {
        math::Pool3x3Max(strides, paddings, in_x, out);
      }
    } else if (pooling_type == "avg") {
      if (strides[0] == strides[1] && strides[0] == 1 &&
          paddings[0] == paddings[1] && paddings[1] == 1) {
        math::Pool3x3Avgs1p1(in_x, out);
      } else {
        math::Pool3x3Avg(strides, paddings, in_x, out);
      }
    }

E
eclipsess 已提交
79
  } else if (ksize[0] == 2 && ksize[0] == ksize[1] && strides[0] == 2 &&
80 81
             strides[0] == strides[1] && paddings[0] == paddings[1] &&
             paddings[1] == 0) {
82
#if __ARM_NEON
L
liuruilong 已提交
83 84 85
#if __aarch64__
    PoolBasic(pooling_type, ksize, strides, paddings, in_x, out);
#else
E
eclipsess 已提交
86
    /// todo: fix bug in Pool2x2
87
    if (pooling_type == "max") {
88
      math::Pool2x2Maxs2p0(strides, paddings, in_x, out);
89
    } else if (pooling_type == "avg") {
90
      math::Pool2x2Avgs2p0(strides, paddings, in_x, out);
91
    }
L
liuruilong 已提交
92
#endif
93 94
#else
    PoolBasic(pooling_type, ksize, strides, paddings, in_x, out);
95 96
#endif  // __ARM_NEON

97 98 99 100 101 102 103 104
  } else {
    PoolBasic(pooling_type, ksize, strides, paddings, in_x, out);
  }
}

}  // namespace operators
}  // namespace paddle_mobile
#endif