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 46
  inline float ExclusiveSum(int total) {
    return ((count > 0) ? val : 0.f) * total;
  }
47 48
};

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

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

template <>
70
inline float32x4_t vPoolInitq_f32<AVG>() {
H
hjchen2 已提交
71 72 73
  return vdupq_n_f32(0.f);
}

74 75 76 77 78 79 80 81 82 83
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);
}

84
template <PoolingType P = MAX>
H
hjchen2 已提交
85 86 87 88 89
inline float32x4_t vPoolPreq_f32(const float32x4_t &x1, const float32x4_t &x2) {
  return vmaxq_f32(x1, x2);
}

template <>
90
inline float32x4_t vPoolPreq_f32<AVG>(const float32x4_t &x1,
H
hjchen2 已提交
91 92 93 94
                                      const float32x4_t &x2) {
  return vaddq_f32(x1, x2);
}

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
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);
}

117
template <PoolingType P = MAX>
H
hjchen2 已提交
118 119
inline float32x4_t vPoolPostq_f32(const float32x4_t &x,
                                  const float32x4_t &post) {
H
hjchen2 已提交
120 121 122 123
  return x;
}

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

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 已提交
140 141
#endif  // __ARM_NEON__

142
template <PoolingType P = MAX>
H
hjchen2 已提交
143 144 145 146 147
inline float PoolPre(const float &x1, const float &x2) {
  return std::max(x1, x2);
}

template <>
148
inline float PoolPre<AVG>(const float &x1, const float &x2) {
H
hjchen2 已提交
149 150 151
  return x1 + x2;
}

152
template <PoolingType P = MAX>
H
hjchen2 已提交
153
inline float PoolPost(const float &x, const float &post) {
H
hjchen2 已提交
154 155 156 157
  return x;
}

template <>
158
inline float PoolPost<AVG>(const float &x, const float &post) {
H
hjchen2 已提交
159
  return x * post;
H
hjchen2 已提交
160 161 162 163
}

template <PoolingType P>
struct Pooling {
164 165 166 167
  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);
H
hjchen2 已提交
168 169 170 171
};

template <PoolingType P, int Stride>
struct Pooling2x2 {
172 173
  void operator()(const framework::Tensor &input,
                  const std::vector<int> &paddings, framework::Tensor *output);
H
hjchen2 已提交
174 175 176 177
};

template <PoolingType P, int Stride>
struct Pooling3x3 {
178
  void operator()(const framework::Tensor &input,
179 180
                  const std::vector<int> &paddings, const bool exclusive,
                  framework::Tensor *output);
H
hjchen2 已提交
181 182 183 184
};

template <PoolingType P, int Stride>
struct Pooling5x5 {
185 186
  void operator()(const framework::Tensor &input,
                  const std::vector<int> &paddings, framework::Tensor *output);
H
hjchen2 已提交
187 188 189 190
};

template <PoolingType P, int Stride>
struct Pooling7x7 {
191 192
  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