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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#pragma once

#include "common/log.h"
#include "framework/tensor.h"

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
朔-望 已提交
38 39 40
template <class T>
class MaxPool {
 public:
41
  inline T initial() { return static_cast<T>(-FLT_MAX); }
42

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

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

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

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

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

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

#endif