pooling.h 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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. */

#pragma once
#include "paddle/framework/eigen.h"
#include "paddle/framework/tensor.h"
#include "paddle/platform/device_context.h"
C
chengduoZH 已提交
19
#include "paddle/platform/hostdevice.h"
20 21 22 23 24

namespace paddle {
namespace operators {
namespace math {
//////////////////////
25
#define FLT_MAX __FLT_MAX__  //
26 27

template <class T>
28
class MaxPool {
29
 public:
C
chengduoZH 已提交
30
  DEVICE inline T initial() { return static_cast<T>(-FLT_MAX); }
C
chengduoZH 已提交
31
  DEVICE inline void compute(T& y, const T& x) { y = y > x ? y : x; }
C
chengduoZH 已提交
32
  DEVICE inline void finalize(T& y, const T& poo_size) {}
C
chengduoZH 已提交
33 34 35
};

template <class T>
36
class AvgPool {
C
chengduoZH 已提交
37 38
 public:
  DEVICE inline T initial() { return static_cast<T>(0); }
C
chengduoZH 已提交
39
  DEVICE inline void compute(T& y, const T& x) { y += x; }
C
chengduoZH 已提交
40 41 42
  DEVICE inline void finalize(T& y, const T& poo_size) { y /= poo_size; }
};
template <class T>
43
class MaxPoolGrad {
C
chengduoZH 已提交
44
 public:
C
chengduoZH 已提交
45 46
  DEVICE inline void compute(const T& x, const T& y, const T& dy, T& dx,
                             T scale) {
47 48 49 50 51
    dx += dy * (x == y);
  }
};

template <class T>
52
class AvgPoolGrad {
53
 public:
C
chengduoZH 已提交
54 55
  DEVICE inline void compute(const T& x, const T& y, const T& dy, T& dx,
                             T scale) {
56 57 58 59 60
    dx += (scale * dy);
  }
};

template <typename Place, typename PoolProcess, typename T>
C
chengduoZH 已提交
61
class Pool2dFunctor {
62
 public:
63 64
  void operator()(const platform::DeviceContext& context,
                  const framework::Tensor& input, framework::Tensor& output,
65
                  std::vector<int>& ksize, std::vector<int>& strides,
C
chengduoZH 已提交
66
                  std::vector<int>& paddings, PoolProcess pool_compute);
67 68 69
};

template <typename Place, typename PoolProcess, typename T>
C
chengduoZH 已提交
70
class Pool2dGradFunctor {
71
 public:
72 73
  void operator()(const platform::DeviceContext& context,
                  const framework::Tensor& input, framework::Tensor& input_grad,
74 75 76
                  const framework::Tensor& output,
                  const framework::Tensor& output_grad, std::vector<int>& ksize,
                  std::vector<int>& strides, std::vector<int>& paddings,
C
chengduoZH 已提交
77
                  PoolProcess pool_compute);
78 79
};

80
template <typename Place, class T>
C
chengduoZH 已提交
81
class MaxPool2dGradFunctor {
82 83 84 85 86 87 88 89
 public:
  void operator()(const platform::DeviceContext& context,
                  const framework::Tensor& input, framework::Tensor& input_grad,
                  const framework::Tensor& output,
                  const framework::Tensor& output_grad, std::vector<int>& ksize,
                  std::vector<int>& strides, std::vector<int>& paddings);
};

90
template <typename Place, typename PoolProcess, typename T>
C
chengduoZH 已提交
91
class Pool3dFunctor {
92
 public:
93 94
  void operator()(const platform::DeviceContext& context,
                  const framework::Tensor& input, framework::Tensor& output,
95
                  std::vector<int>& ksize, std::vector<int>& strides,
C
chengduoZH 已提交
96
                  std::vector<int>& paddings, PoolProcess pool_compute);
97 98 99
};

template <typename Place, typename PoolProcess, typename T>
C
chengduoZH 已提交
100
class Pool3dGradFunctor {
101
 public:
102 103
  void operator()(const platform::DeviceContext& context,
                  const framework::Tensor& input, framework::Tensor& input_grad,
104 105 106
                  const framework::Tensor& output,
                  const framework::Tensor& output_grad, std::vector<int>& ksize,
                  std::vector<int>& strides, std::vector<int>& paddings,
C
chengduoZH 已提交
107
                  PoolProcess pool_compute);
108 109
};

110
template <typename Place, class T>
C
chengduoZH 已提交
111
class MaxPool3dGradFunctor {
112 113 114 115 116 117 118 119
 public:
  void operator()(const platform::DeviceContext& context,
                  const framework::Tensor& input, framework::Tensor& input_grad,
                  const framework::Tensor& output,
                  const framework::Tensor& output_grad, std::vector<int>& ksize,
                  std::vector<int>& strides, std::vector<int>& paddings);
};

120 121 122
}  // namespace math
}  // namespace operators
}  // namespace paddle