pooling.h 2.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 19 20
#pragma once

#include "common/log.h"
#include "framework/tensor.h"
W
wangliu 已提交
21 22
#include "pool_2x2.h"
#include "pool_3x3.h"
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

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.
 */
朔-望's avatar
朔-望 已提交
40 41 42
template <class T>
class MaxPool {
 public:
43
  inline T initial() { return static_cast<T>(-FLT_MAX); }
44

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

47
  inline void finalize(const T &pool_field, T *y) {}
48 49
};

朔-望's avatar
朔-望 已提交
50 51 52
template <class T>
class AvgPool {
 public:
53
  inline T initial() { return static_cast<T>(0); }
54

55
  inline void compute(const T &x, T *y) { *y += x; }
56

57
  inline void finalize(const T &pool_field, T *y) { *y /= pool_field; }
58 59 60 61
};

template <typename DeviceType, typename PoolProcess, typename T>
class PoolFunctor {
朔-望's avatar
朔-望 已提交
62
 public:
63 64 65 66
  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);
67
};
D
dolphin8 已提交
68
}  // namespace math
朔-望's avatar
朔-望 已提交
69 70
}  // namespace operators
}  // namespace paddle_mobile
L
liuruilong 已提交
71 72

#endif