pooling.h 5.2 KB
Newer Older
W
wangliu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* 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. */
14

L
liuruilong 已提交
15 16
#ifdef POOL_OP

17 18
#pragma once

H
hjchen2 已提交
19
#include <algorithm>
Z
ZhenWang 已提交
20
#include <cmath>
H
hjchen2 已提交
21 22 23
#include <limits>
#include <vector>
#include "common/types.h"
24
#include "framework/tensor.h"
H
hjchen2 已提交
25 26 27
#if defined(__ARM_NEON) || defined(__ARM_NEON__)
#include <arm_neon.h>
#endif
28 29 30 31 32

namespace paddle_mobile {
namespace operators {
namespace math {

33
template <PoolingType P = MAX>
H
hjchen2 已提交
34 35 36
struct PoolingVal {
  float val;
  int count;
H
hjchen2 已提交
37
  PoolingVal() : count(0) { val = -std::numeric_limits<float>::max(); }
H
hjchen2 已提交
38 39
  inline PoolingVal<P> &operator+=(const float &x) {
    val = std::max(val, x);
H
hjchen2 已提交
40
    ++count;
H
hjchen2 已提交
41 42
    return *this;
  }
H
hjchen2 已提交
43
  inline float Value() { return (count > 0) ? val : 0.f; }
44 45
};

H
hjchen2 已提交
46
template <>
47
struct PoolingVal<AVG> {
H
hjchen2 已提交
48 49
  float val;
  int count;
H
hjchen2 已提交
50
  PoolingVal() : val(0.f), count(0) {}
51
  inline PoolingVal<AVG> &operator+=(const float &x) {
H
hjchen2 已提交
52
    val += x;
H
hjchen2 已提交
53
    ++count;
H
hjchen2 已提交
54 55
    return *this;
  }
56
  inline float Value() { return (count > 0) ? val * (1.f / count) : 0.f; }
57 58
};

H
hjchen2 已提交
59
#if defined(__ARM_NEON) || defined(__ARM_NEON__)
60
template <PoolingType P = MAX>
H
hjchen2 已提交
61 62 63 64 65
inline float32x4_t vPoolInitq_f32() {
  return vdupq_n_f32(-std::numeric_limits<float>::max());
}

template <>
66
inline float32x4_t vPoolInitq_f32<AVG>() {
H
hjchen2 已提交
67 68 69
  return vdupq_n_f32(0.f);
}

70 71 72 73 74 75 76 77 78 79
template <PoolingType P = MAX>
inline float32x2_t vPoolInit_f32() {
  return vdup_n_f32(-std::numeric_limits<float>::max());
}

template <>
inline float32x2_t vPoolInit_f32<AVG>() {
  return vdup_n_f32(0.f);
}

80
template <PoolingType P = MAX>
H
hjchen2 已提交
81 82 83 84 85
inline float32x4_t vPoolPreq_f32(const float32x4_t &x1, const float32x4_t &x2) {
  return vmaxq_f32(x1, x2);
}

template <>
86
inline float32x4_t vPoolPreq_f32<AVG>(const float32x4_t &x1,
H
hjchen2 已提交
87 88 89 90
                                      const float32x4_t &x2) {
  return vaddq_f32(x1, x2);
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
template <PoolingType P = MAX>
inline float32x2_t vPoolPre_f32(const float32x2_t &x1, const float32x2_t &x2) {
  return vmax_f32(x1, x2);
}

template <>
inline float32x2_t vPoolPre_f32<AVG>(const float32x2_t &x1,
                                     const float32x2_t &x2) {
  return vadd_f32(x1, x2);
}

template <PoolingType P = MAX>
inline float32x2_t vpPoolPre_f32(const float32x2_t &x1, const float32x2_t &x2) {
  return vpmax_f32(x1, x2);
}

template <>
inline float32x2_t vpPoolPre_f32<AVG>(const float32x2_t &x1,
                                      const float32x2_t &x2) {
  return vpadd_f32(x1, x2);
}

113
template <PoolingType P = MAX>
H
hjchen2 已提交
114 115
inline float32x4_t vPoolPostq_f32(const float32x4_t &x,
                                  const float32x4_t &post) {
H
hjchen2 已提交
116 117 118 119
  return x;
}

template <>
120
inline float32x4_t vPoolPostq_f32<AVG>(const float32x4_t &x,
H
hjchen2 已提交
121 122
                                       const float32x4_t &post) {
  return vmulq_f32(x, post);
H
hjchen2 已提交
123
}
124 125 126 127 128 129 130 131 132 133 134 135

template <PoolingType P = MAX>
inline float32x2_t vPoolPost_f32(const float32x2_t &x,
                                 const float32x2_t &post) {
  return x;
}

template <>
inline float32x2_t vPoolPost_f32<AVG>(const float32x2_t &x,
                                      const float32x2_t &post) {
  return vmul_f32(x, post);
}
H
hjchen2 已提交
136 137
#endif  // __ARM_NEON__

138
template <PoolingType P = MAX>
H
hjchen2 已提交
139 140 141 142 143
inline float PoolPre(const float &x1, const float &x2) {
  return std::max(x1, x2);
}

template <>
144
inline float PoolPre<AVG>(const float &x1, const float &x2) {
H
hjchen2 已提交
145 146 147
  return x1 + x2;
}

148
template <PoolingType P = MAX>
H
hjchen2 已提交
149
inline float PoolPost(const float &x, const float &post) {
H
hjchen2 已提交
150 151 152 153
  return x;
}

template <>
154
inline float PoolPost<AVG>(const float &x, const float &post) {
H
hjchen2 已提交
155
  return x * post;
H
hjchen2 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
}

template <PoolingType P>
struct Pooling {
  inline void operator()(const framework::Tensor &input,
                         const std::vector<int> &kernel_size,
                         const std::vector<int> &strides,
                         const std::vector<int> &paddings,
                         framework::Tensor *output);
};

template <PoolingType P, int Stride>
struct Pooling2x2 {
  inline void operator()(const framework::Tensor &input,
                         const std::vector<int> &paddings,
                         framework::Tensor *output);
};

template <PoolingType P, int Stride>
struct Pooling3x3 {
  inline void operator()(const framework::Tensor &input,
                         const std::vector<int> &paddings,
                         framework::Tensor *output);
};

template <PoolingType P, int Stride>
struct Pooling5x5 {
  inline void operator()(const framework::Tensor &input,
                         const std::vector<int> &paddings,
                         framework::Tensor *output);
};

template <PoolingType P, int Stride>
struct Pooling7x7 {
  inline void operator()(const framework::Tensor &input,
                         const std::vector<int> &paddings,
                         framework::Tensor *output);
193
};
H
hjchen2 已提交
194

D
dolphin8 已提交
195
}  // namespace math
朔-望's avatar
朔-望 已提交
196 197
}  // namespace operators
}  // namespace paddle_mobile
L
liuruilong 已提交
198 199

#endif