pooling.h 2.6 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

Z
ZhenWang 已提交
19 20
#include <climits>
#include <cmath>
21 22
#include "common/log.h"
#include "framework/tensor.h"
W
wangliu 已提交
23 24
#include "pool_2x2.h"
#include "pool_3x3.h"
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

namespace paddle_mobile {
namespace operators {
namespace math {

#define FLT_MAX __FLT_MAX__

/*
 * \brief Extracting simple operations from pooling.
 *        Both MaxPool and AvgPool need "initial", "compute" and "finalize"
 * operation.
 *        MaxPool initializes temp variable to the negative maximum to find the
 * maximum value in the pooling field.
 *        AvgPool initializes temp variable to the zero to accumulate all values
 * in pool pooling, and finally takes the average.
 *        MaxPoolGrad and AvgPoolGrad are gradient operations respectively.
 */
Z
ZhenWang 已提交
42
template <typename T>
朔-望's avatar
朔-望 已提交
43 44
class MaxPool {
 public:
Z
ZhenWang 已提交
45 46 47 48 49 50
  inline T initial() {
    if (typeid(T) == typeid(int8_t)) {
      return static_cast<T>(-SCHAR_MAX);
    }
    return static_cast<T>(-FLT_MAX);
  }
51

52
  inline void compute(const T &x, T *y) { *y = *y > x ? *y : x; }
53

54
  inline void finalize(const T &pool_field, T *y) {}
55 56
};

Z
ZhenWang 已提交
57
template <typename Itype, typename Otype>
朔-望's avatar
朔-望 已提交
58 59
class AvgPool {
 public:
Z
ZhenWang 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  inline Otype initial() { return static_cast<Otype>(0); }

  inline void compute(const Itype &x, Otype *y) { *y += x; }

  inline void finalize(const float &pool_field, Otype *y) {
    if (typeid(Itype) == typeid(int8_t)) {
      float tmp = *y / pool_field;
      if (tmp > SCHAR_MAX) {
        *y = SCHAR_MAX;
      } else if (tmp < -SCHAR_MAX) {
        *y = -SCHAR_MAX;
      } else {
        *y = static_cast<Otype>(std::round(tmp));
      }
    } else {
      *y /= pool_field;
    }
  }
78 79 80 81
};

template <typename DeviceType, typename PoolProcess, typename T>
class PoolFunctor {
朔-望's avatar
朔-望 已提交
82
 public:
83 84 85 86
  void operator()(const framework::Tensor &input, const std::vector<int> &ksize,
                  const std::vector<int> &strides,
                  const std::vector<int> &paddings, PoolProcess pool_compute,
                  framework::Tensor *output);
87
};
D
dolphin8 已提交
88
}  // namespace math
朔-望's avatar
朔-望 已提交
89 90
}  // namespace operators
}  // namespace paddle_mobile
L
liuruilong 已提交
91 92

#endif