pooling.h 3.9 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 {

H
hjchen2 已提交
33 34 35 36 37
template <PoolingType P = Max>
struct PoolingVal {
  float val;
  int count;
  PoolingVal() {
H
hjchen2 已提交
38
    val = -std::numeric_limits<float>::max();
H
hjchen2 已提交
39 40 41 42 43 44 45 46 47 48
    count = 0;
  }
  inline PoolingVal<P> &operator+=(const float &x) {
    val = std::max(val, x);
    count += 1;
    return *this;
  }
  float Value() const {
    if (count > 0) {
      return val;
Z
ZhenWang 已提交
49
    }
H
hjchen2 已提交
50
    return 0.f;
Z
ZhenWang 已提交
51
  }
52 53
};

H
hjchen2 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
template <>
struct PoolingVal<Avg> {
  float val;
  int count;
  PoolingVal() {
    val = 0.f;
    count = 0;
  }
  inline PoolingVal<Avg> &operator+=(const float &x) {
    val += x;
    count += 1;
    return *this;
  }
  float Value() const {
    if (count > 0) {
      return val / count;
Z
ZhenWang 已提交
70
    }
H
hjchen2 已提交
71
    return 0.f;
Z
ZhenWang 已提交
72
  }
73 74
};

H
hjchen2 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 149 150 151 152 153
#if defined(__ARM_NEON) || defined(__ARM_NEON__)
template <PoolingType P = Max>
inline float32x4_t vPoolPreq_f32(const float32x4_t &x1, const float32x4_t &x2) {
  return vmaxq_f32(x1, x2);
}

template <>
inline float32x4_t vPoolPreq_f32<Avg>(const float32x4_t &x1,
                                      const float32x4_t &x2) {
  return vaddq_f32(x1, x2);
}

template <PoolingType P = Max>
inline float32x4_t vPoolPostq_f32(const float32x4_t &x) {
  return x;
}

template <>
inline float32x4_t vPoolPostq_f32<Avg>(const float32x4_t &x) {
  float32x4_t avg = vdupq_n_f32(1.f / 9);
  return vmulq_f32(avg, x);
}
#endif  // __ARM_NEON__

template <PoolingType P = Max>
inline float PoolPre(const float &x1, const float &x2) {
  return std::max(x1, x2);
}

template <>
inline float PoolPre<Avg>(const float &x1, const float &x2) {
  return x1 + x2;
}

template <PoolingType P = Max>
inline float PoolPost(const float &x) {
  return x;
}

template <>
inline float PoolPost<Avg>(const float &x) {
  return 1.f / 9 * x;
}

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

D
dolphin8 已提交
156
}  // namespace math
朔-望's avatar
朔-望 已提交
157 158
}  // namespace operators
}  // namespace paddle_mobile
L
liuruilong 已提交
159 160

#endif