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

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
16
#include <string>
C
chengduo 已提交
17
#include <vector>
18

Y
Yi Wang 已提交
19 20 21 22
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/hostdevice.h"
D
dzhwinter 已提交
23
#include "paddle/fluid/platform/macros.h"
24 25 26 27 28

namespace paddle {
namespace operators {
namespace math {

C
chengduoZH 已提交
29 30
/*
 * \brief Extracting simple operations from pooling.
C
chengduoZH 已提交
31 32
 *        Both MaxPool and AvgPool need "initial", "compute" and "finalize"
 * operation.
C
chengduoZH 已提交
33 34 35
 *        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
C
chengduoZH 已提交
36
 * in pool pooling, and finally takes the average.
C
chengduoZH 已提交
37 38
 *        MaxPoolGrad and AvgPoolGrad are gradient operations respectively.
 */
39
template <class T>
40
class MaxPool {
41
 public:
C
chengduoZH 已提交
42
  DEVICE inline T initial() { return static_cast<T>(-FLT_MAX); }
C
chengduo 已提交
43 44
  DEVICE inline void compute(const T& x, T* y) { *y = *y > x ? *y : x; }
  DEVICE inline void finalize(const T& pool_field, T* y) {}
C
chengduoZH 已提交
45 46 47
};

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

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

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

D
dengkaipeng 已提交
73 74 75 76 77 78 79 80 81 82 83 84
/* used for adaptive pool to calculate start and end index of each divided grid
 */
HOSTDEVICE inline int AdaptStartIndex(int ph, int input_size, int output_size) {
  return static_cast<int>(
      floor(static_cast<double>(ph * input_size) / output_size));
}

HOSTDEVICE inline int AdaptEndIndex(int ph, int input_size, int output_size) {
  return static_cast<int>(
      ceil(static_cast<double>((ph + 1) * input_size) / output_size));
}

C
chengduoZH 已提交
85 86 87
/*
 * \brief Getting pooling results, and calculating gradient.
 *
88 89 90 91 92
 * In pool2d, all Tensors are in NCHW or NHWC format. Where N is batch size, C
 * is the number of channels, H and W is the height and width of feature.
 * In pool3d, all Tensors are in NCDHW or NDHWC format. Where N is batch size, C
 * is the number of channels, D, H and W is the depth, height and width of
 * feature.
C
chengduoZH 已提交
93 94
 *
 * In max pooling, it is possible that the pooling region has multiple maximum
C
chengduoZH 已提交
95 96
 * elements. In this case, we should compute the gradient of the first maximum
 * element.
C
chengduoZH 已提交
97 98 99
 * This is different from average pooling. So we rewrite the max_pool_grad:
 * MaxPool2dGradFunctor, MaxPool3dGradFunctor.
 */
N
nhzlx 已提交
100
#ifdef PADDLE_WITH_CUDA
N
nhzlx 已提交
101 102 103 104 105 106 107 108
template <typename PoolProcess, typename T>
class Pool2dDirectCUDAFunctor {
 public:
  void operator()(const T* input, const std::vector<int>& input_shape,
                  const std::vector<int>& output_shape,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
109 110
                  bool exclusive, bool adaptive, T* output,
                  cudaStream_t stream);
N
nhzlx 已提交
111
};
N
nhzlx 已提交
112
#endif
N
nhzlx 已提交
113

Q
QI JUN 已提交
114
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
115
class Pool2dFunctor {
116
 public:
Q
QI JUN 已提交
117
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
118 119 120
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
121
                  bool exclusive, bool adaptive, framework::Tensor* output);
122 123 124 125 126 127 128 129

  // overload operator() to support argument data_format
  void operator()(const DeviceContext& context, const framework::Tensor& input,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
                  const std::string data_format, PoolProcess pool_compute,
                  bool exclusive, bool adaptive, framework::Tensor* output);
130 131
};

Q
QI JUN 已提交
132
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
133
class Pool2dGradFunctor {
134
 public:
Q
QI JUN 已提交
135
  void operator()(const DeviceContext& context, const framework::Tensor& input,
136
                  const framework::Tensor& output,
C
chengduo 已提交
137 138 139 140
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
141
                  bool exclusive, bool adaptive, framework::Tensor* input_grad);
142 143 144 145 146 147 148 149 150
  // overload operator() to support argument data_format
  void operator()(const DeviceContext& context, const framework::Tensor& input,
                  const framework::Tensor& output,
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
                  const std::string data_format, PoolProcess pool_compute,
                  bool exclusive, bool adaptive, framework::Tensor* input_grad);
151 152
};

Q
QI JUN 已提交
153
template <typename DeviceContext, class T>
C
chengduoZH 已提交
154
class MaxPool2dGradFunctor {
155
 public:
Q
QI JUN 已提交
156
  void operator()(const DeviceContext& context, const framework::Tensor& input,
157
                  const framework::Tensor& output,
C
chengduo 已提交
158 159 160 161
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
C
chengduoZH 已提交
162
                  framework::Tensor* input_grad);
163 164 165 166 167 168 169 170
  // overload operator() to support argument data_format
  void operator()(const DeviceContext& context, const framework::Tensor& input,
                  const framework::Tensor& output,
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
                  const std::string data_format, framework::Tensor* input_grad);
171 172
};

Q
QI JUN 已提交
173
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
174
class Pool3dFunctor {
175
 public:
Q
QI JUN 已提交
176
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
177 178 179
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
180
                  bool exclusive, bool adaptive, framework::Tensor* output);
181 182 183 184 185 186 187
  // overload operator() to support argument data_format
  void operator()(const DeviceContext& context, const framework::Tensor& input,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
                  const std::string data_format, PoolProcess pool_compute,
                  bool exclusive, bool adaptive, framework::Tensor* output);
188 189
};

Q
QI JUN 已提交
190
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
191
class Pool3dGradFunctor {
192
 public:
Q
QI JUN 已提交
193
  void operator()(const DeviceContext& context, const framework::Tensor& input,
194
                  const framework::Tensor& output,
C
chengduo 已提交
195 196 197 198
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
199
                  bool exclusive, bool adaptive, framework::Tensor* input_grad);
200 201 202 203 204 205 206 207 208
  // overload operator() to support argument data_format
  void operator()(const DeviceContext& context, const framework::Tensor& input,
                  const framework::Tensor& output,
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
                  const std::string data_format, PoolProcess pool_compute,
                  bool exclusive, bool adaptive, framework::Tensor* input_grad);
209 210
};

Q
QI JUN 已提交
211
template <typename DeviceContext, class T>
C
chengduoZH 已提交
212
class MaxPool3dGradFunctor {
213
 public:
Q
QI JUN 已提交
214
  void operator()(const DeviceContext& context, const framework::Tensor& input,
215
                  const framework::Tensor& output,
C
chengduo 已提交
216 217 218 219
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
C
chengduoZH 已提交
220
                  framework::Tensor* input_grad);
221 222 223 224 225 226 227 228
  // overload operator() to support argument data_format
  void operator()(const DeviceContext& context, const framework::Tensor& input,
                  const framework::Tensor& output,
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
                  const std::string data_format, framework::Tensor* input_grad);
229 230
};

C
chengduoZH 已提交
231 232 233
/*
 * \brief Getting max pooling results and corresponding max index, and
 * calculating gradient.
C
chengduoZH 已提交
234
 * In up-sampling-pooling, it is necessary to know max element index.
C
chengduoZH 已提交
235 236 237
 * In pool2d, all tensors are in NCHW format. In pool3d, all tensors are in
 * NCDHW format.
 */
Q
QI JUN 已提交
238
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
239 240
class MaxPool2dWithIndexFunctor {
 public:
Q
QI JUN 已提交
241
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
242 243
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
244 245
                  const std::vector<int>& paddings, bool adaptive,
                  framework::Tensor* output, framework::Tensor* mask);
C
chengduoZH 已提交
246 247
};

Q
QI JUN 已提交
248
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
249 250
class MaxPool2dWithIndexGradFunctor {
 public:
Q
QI JUN 已提交
251
  void operator()(const DeviceContext& context,
C
chengduoZH 已提交
252
                  const framework::Tensor& output_grad,
C
chengduo 已提交
253 254
                  const framework::Tensor& mask, const std::vector<int>& ksize,
                  const std::vector<int>& strides,
255
                  const std::vector<int>& paddings, bool adaptive,
C
chengduoZH 已提交
256
                  framework::Tensor* input_grad);
C
chengduoZH 已提交
257 258
};

Q
QI JUN 已提交
259
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
260 261
class MaxPool3dWithIndexFunctor {
 public:
Q
QI JUN 已提交
262
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
263 264
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
265 266
                  const std::vector<int>& paddings, bool adaptive,
                  framework::Tensor* output, framework::Tensor* mask);
C
chengduoZH 已提交
267 268
};

Q
QI JUN 已提交
269
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
270 271
class MaxPool3dWithIndexGradFunctor {
 public:
Q
QI JUN 已提交
272
  void operator()(const DeviceContext& context,
C
chengduoZH 已提交
273
                  const framework::Tensor& output_grad,
C
chengduo 已提交
274 275
                  const framework::Tensor& mask, const std::vector<int>& ksize,
                  const std::vector<int>& strides,
276
                  const std::vector<int>& paddings, bool adaptive,
C
chengduoZH 已提交
277
                  framework::Tensor* input_grad);
C
chengduoZH 已提交
278
};
C
chengduoZH 已提交
279

280 281 282
}  // namespace math
}  // namespace operators
}  // namespace paddle