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

C
chengduoZH 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37
#define FLT_MAX \
  __FLT_MAX__  // It might need to be placed in another file, but I'm still
               // wondering where to put it

/*
 * \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 takes the average.
 *        MaxPoolGrad and AvgPoolGrad are gradient operations respectively.
 */
38
template <class T>
39
class MaxPool {
40
 public:
C
chengduoZH 已提交
41
  DEVICE inline T initial() { return static_cast<T>(-FLT_MAX); }
C
chengduoZH 已提交
42
  DEVICE inline void compute(T& y, const T& x) { y = y > x ? y : x; }
C
chengduoZH 已提交
43
  DEVICE inline void finalize(T& y, const T& pool_field) {}
C
chengduoZH 已提交
44 45 46
};

template <class T>
47
class AvgPool {
C
chengduoZH 已提交
48 49
 public:
  DEVICE inline T initial() { return static_cast<T>(0); }
C
chengduoZH 已提交
50
  DEVICE inline void compute(T& y, const T& x) { y += x; }
C
chengduoZH 已提交
51
  DEVICE inline void finalize(T& y, const T& pool_field) { y /= pool_field; }
C
chengduoZH 已提交
52
};
C
chengduoZH 已提交
53

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

template <class T>
64
class AvgPoolGrad {
65
 public:
C
chengduoZH 已提交
66 67
  DEVICE inline void compute(const T& x, const T& y, const T& dy, T& dx,
                             T scale) {
68 69 70 71
    dx += (scale * dy);
  }
};

C
chengduoZH 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85
/*
 * \brief Getting pooling results, and calculating gradient.
 *
 * In pool2d, all tensors are in NCHW format. In pool3d, all tensors are in
 * NCDHW format.
 *
 * In max pooling, it is possible that the pooling region has multiple maximum
 * elements.
 * In this case, we should compute the gradient of the first maximum element.
 * This is different from average pooling. So we rewrite the max_pool_grad:
 * MaxPool2dGradFunctor, MaxPool3dGradFunctor.
 *
 */

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

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

106
template <typename Place, class T>
C
chengduoZH 已提交
107
class MaxPool2dGradFunctor {
108 109 110 111 112 113 114 115
 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);
};

116
template <typename Place, typename PoolProcess, typename T>
C
chengduoZH 已提交
117
class Pool3dFunctor {
118
 public:
119 120
  void operator()(const platform::DeviceContext& context,
                  const framework::Tensor& input, framework::Tensor& output,
121
                  std::vector<int>& ksize, std::vector<int>& strides,
C
chengduoZH 已提交
122
                  std::vector<int>& paddings, PoolProcess pool_compute);
123 124 125
};

template <typename Place, typename PoolProcess, typename T>
C
chengduoZH 已提交
126
class Pool3dGradFunctor {
127
 public:
128 129
  void operator()(const platform::DeviceContext& context,
                  const framework::Tensor& input, framework::Tensor& input_grad,
130 131 132
                  const framework::Tensor& output,
                  const framework::Tensor& output_grad, std::vector<int>& ksize,
                  std::vector<int>& strides, std::vector<int>& paddings,
C
chengduoZH 已提交
133
                  PoolProcess pool_compute);
134 135
};

136
template <typename Place, class T>
C
chengduoZH 已提交
137
class MaxPool3dGradFunctor {
138 139 140 141 142 143 144 145
 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);
};

C
chengduoZH 已提交
146 147 148 149 150 151 152 153
/*
 * \brief Getting max pooling results and corresponding max index, and
 * calculating gradient.
 * In sub-sampling-pooling, it is necessary to know max element index.
 * In pool2d, all tensors are in NCHW format. In pool3d, all tensors are in
 * NCDHW format.
 *
 */
C
chengduoZH 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
template <typename Place, typename T>
class MaxPool2dWithIndexFunctor {
 public:
  void operator()(const platform::DeviceContext& context,
                  const framework::Tensor& input, framework::Tensor& output,
                  framework::Tensor& mask, std::vector<int>& ksize,
                  std::vector<int>& strides, std::vector<int>& paddings);
};

template <typename Place, typename T>
class MaxPool2dWithIndexGradFunctor {
 public:
  void operator()(const platform::DeviceContext& context,
                  framework::Tensor& input_grad,
                  const framework::Tensor& output_grad,
                  const framework::Tensor& mask, std::vector<int>& ksize,
                  std::vector<int>& strides, std::vector<int>& paddings);
};

template <typename Place, typename T>
class MaxPool3dWithIndexFunctor {
 public:
  void operator()(const platform::DeviceContext& context,
                  const framework::Tensor& input, framework::Tensor& output,
                  framework::Tensor& mask, std::vector<int>& ksize,
                  std::vector<int>& strides, std::vector<int>& paddings);
};

template <typename Place, typename T>
class MaxPool3dWithIndexGradFunctor {
 public:
  void operator()(const platform::DeviceContext& context,
                  framework::Tensor& input_grad,
                  const framework::Tensor& output_grad,
                  const framework::Tensor& mask, std::vector<int>& ksize,
                  std::vector<int>& strides, std::vector<int>& paddings);
};
191 192 193
}  // namespace math
}  // namespace operators
}  // namespace paddle