pooling.h 4.1 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 / 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
template <PoolingType P = MAX>
H
hjchen2 已提交
71 72 73 74 75
inline float32x4_t vPoolPreq_f32(const float32x4_t &x1, const float32x4_t &x2) {
  return vmaxq_f32(x1, x2);
}

template <>
76
inline float32x4_t vPoolPreq_f32<AVG>(const float32x4_t &x1,
H
hjchen2 已提交
77 78 79 80
                                      const float32x4_t &x2) {
  return vaddq_f32(x1, x2);
}

81
template <PoolingType P = MAX>
H
hjchen2 已提交
82 83
inline float32x4_t vPoolPostq_f32(const float32x4_t &x,
                                  const float32x4_t &post) {
H
hjchen2 已提交
84 85 86 87
  return x;
}

template <>
88
inline float32x4_t vPoolPostq_f32<AVG>(const float32x4_t &x,
H
hjchen2 已提交
89 90
                                       const float32x4_t &post) {
  return vmulq_f32(x, post);
H
hjchen2 已提交
91 92 93
}
#endif  // __ARM_NEON__

94
template <PoolingType P = MAX>
H
hjchen2 已提交
95 96 97 98 99
inline float PoolPre(const float &x1, const float &x2) {
  return std::max(x1, x2);
}

template <>
100
inline float PoolPre<AVG>(const float &x1, const float &x2) {
H
hjchen2 已提交
101 102 103
  return x1 + x2;
}

104
template <PoolingType P = MAX>
H
hjchen2 已提交
105
inline float PoolPost(const float &x, const float &post) {
H
hjchen2 已提交
106 107 108 109
  return x;
}

template <>
110
inline float PoolPost<AVG>(const float &x, const float &post) {
H
hjchen2 已提交
111
  return x * post;
H
hjchen2 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
}

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);
149
};
H
hjchen2 已提交
150

D
dolphin8 已提交
151
}  // namespace math
朔-望's avatar
朔-望 已提交
152 153
}  // namespace operators
}  // namespace paddle_mobile
L
liuruilong 已提交
154 155

#endif