pooling.h 12.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
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/tensor.h"
21
#include "paddle/fluid/operators/amp/fp16_type_traits.h"
Y
Yi Wang 已提交
22 23
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/hostdevice.h"
D
dzhwinter 已提交
24
#include "paddle/fluid/platform/macros.h"
25 26 27 28 29

namespace paddle {
namespace operators {
namespace math {

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

template <class T>
49
class AvgPool {
50 51 52
  using MT = typename details::MPTypeTrait<T>::Type;
  MT intermediate_res;

C
chengduoZH 已提交
53
 public:
54 55 56 57 58 59 60 61 62 63 64 65
  DEVICE inline T initial() {
    intermediate_res = static_cast<MT>(0.0f);
    return static_cast<T>(0);
  }

  DEVICE inline void compute(const T& x, T* y) {
    intermediate_res += static_cast<MT>(x);
  }

  DEVICE inline void finalize(const T& pool_field, T* y) {
    *y = static_cast<T>(intermediate_res / (static_cast<MT>(pool_field)));
  }
C
chengduoZH 已提交
66
};
C
chengduoZH 已提交
67

C
chengduoZH 已提交
68
template <class T>
69
class MaxPoolGrad {
C
chengduoZH 已提交
70
 public:
71 72 73
  static constexpr bool use_x = true;
  HOSTDEVICE inline void compute(const T& x, const T& y, const T& dy, T scale,
                                 T* dx) {
74
    *dx += dy * static_cast<T>(x == y);
75 76 77 78
  }
};

template <class T>
79
class AvgPoolGrad {
80
 public:
81 82 83
  static constexpr bool use_x = false;
  HOSTDEVICE inline void compute(const T& x, const T& y, const T& dy, T scale,
                                 T* dx) {
C
chengduo 已提交
84
    *dx += (scale * dy);
85 86 87
  }
};

D
dengkaipeng 已提交
88 89 90 91 92 93 94 95 96 97 98 99
/* 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 已提交
100 101 102
/*
 * \brief Getting pooling results, and calculating gradient.
 *
103 104 105 106 107
 * 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 已提交
108 109
 *
 * In max pooling, it is possible that the pooling region has multiple maximum
C
chengduoZH 已提交
110 111
 * elements. In this case, we should compute the gradient of the first maximum
 * element.
C
chengduoZH 已提交
112 113 114
 * This is different from average pooling. So we rewrite the max_pool_grad:
 * MaxPool2dGradFunctor, MaxPool3dGradFunctor.
 */
115
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
N
nhzlx 已提交
116 117 118 119 120 121 122
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,
123 124 125
                  const std::vector<int>& paddings, bool exclusive,
                  bool adaptive, T* output, gpuStream_t stream,
                  PoolProcess pool_compute);
N
nhzlx 已提交
126
};
N
nhzlx 已提交
127
#endif
N
nhzlx 已提交
128

Q
QI JUN 已提交
129
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
130
class Pool2dFunctor {
131
 public:
Q
QI JUN 已提交
132
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
133 134
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
135 136 137
                  const std::vector<int>& paddings, bool exclusive,
                  bool adaptive, framework::Tensor* output,
                  PoolProcess pool_compute);
138 139 140 141 142 143

  // 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,
144 145
                  const std::string data_format, bool exclusive, bool adaptive,
                  framework::Tensor* output, PoolProcess pool_compute);
146 147
};

Q
QI JUN 已提交
148
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
149
class Pool2dGradFunctor {
150
 public:
Q
QI JUN 已提交
151
  void operator()(const DeviceContext& context, const framework::Tensor& input,
152
                  const framework::Tensor& output,
C
chengduo 已提交
153 154 155
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
156 157 158
                  const std::vector<int>& paddings, bool exclusive,
                  bool adaptive, framework::Tensor* input_grad,
                  PoolProcess pool_compute);
159 160 161 162 163 164 165
  // 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,
166 167
                  const std::string data_format, bool exclusive, bool adaptive,
                  framework::Tensor* input_grad, PoolProcess pool_compute);
168 169
};

Q
QI JUN 已提交
170
template <typename DeviceContext, class T>
C
chengduoZH 已提交
171
class MaxPool2dGradFunctor {
172
 public:
Q
QI JUN 已提交
173
  void operator()(const DeviceContext& context, const framework::Tensor& input,
174
                  const framework::Tensor& output,
C
chengduo 已提交
175 176 177 178
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
C
chengduoZH 已提交
179
                  framework::Tensor* input_grad);
180 181 182 183 184 185 186 187
  // 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);
188 189
};

F
feng_shuai 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
template <typename PoolProcess, typename T>
class Pool3dDirectCUDAFunctor {
 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, bool exclusive,
                  bool adaptive, T* output, gpuStream_t stream,
                  PoolProcess pool_compute);
};
#endif

Q
QI JUN 已提交
204
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
205
class Pool3dFunctor {
206
 public:
Q
QI JUN 已提交
207
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
208 209
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
210 211 212
                  const std::vector<int>& paddings, bool exclusive,
                  bool adaptive, framework::Tensor* output,
                  PoolProcess pool_compute);
213 214 215 216 217
  // 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,
218 219
                  const std::string data_format, bool exclusive, bool adaptive,
                  framework::Tensor* output, PoolProcess pool_compute);
220 221
};

Q
QI JUN 已提交
222
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
223
class Pool3dGradFunctor {
224
 public:
Q
QI JUN 已提交
225
  void operator()(const DeviceContext& context, const framework::Tensor& input,
226
                  const framework::Tensor& output,
C
chengduo 已提交
227 228 229
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
230 231 232
                  const std::vector<int>& paddings, bool exclusive,
                  bool adaptive, framework::Tensor* input_grad,
                  PoolProcess pool_compute);
233 234 235 236 237 238 239
  // 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,
240 241
                  const std::string data_format, bool exclusive, bool adaptive,
                  framework::Tensor* input_grad, PoolProcess pool_compute);
242 243
};

Q
QI JUN 已提交
244
template <typename DeviceContext, class T>
C
chengduoZH 已提交
245
class MaxPool3dGradFunctor {
246
 public:
Q
QI JUN 已提交
247
  void operator()(const DeviceContext& context, const framework::Tensor& input,
248
                  const framework::Tensor& output,
C
chengduo 已提交
249 250 251 252
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
C
chengduoZH 已提交
253
                  framework::Tensor* input_grad);
254 255 256 257 258 259 260 261
  // 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);
262 263
};

C
chengduoZH 已提交
264 265 266
/*
 * \brief Getting max pooling results and corresponding max index, and
 * calculating gradient.
C
chengduoZH 已提交
267
 * In up-sampling-pooling, it is necessary to know max element index.
C
chengduoZH 已提交
268 269 270
 * In pool2d, all tensors are in NCHW format. In pool3d, all tensors are in
 * NCDHW format.
 */
Q
QI JUN 已提交
271
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
272 273
class MaxPool2dWithIndexFunctor {
 public:
Q
QI JUN 已提交
274
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
275 276
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
277 278
                  const std::vector<int>& paddings, bool adaptive,
                  framework::Tensor* output, framework::Tensor* mask);
C
chengduoZH 已提交
279 280
};

Q
QI JUN 已提交
281
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
282 283
class MaxPool2dWithIndexGradFunctor {
 public:
Q
QI JUN 已提交
284
  void operator()(const DeviceContext& context,
C
chengduoZH 已提交
285
                  const framework::Tensor& output_grad,
C
chengduo 已提交
286 287
                  const framework::Tensor& mask, const std::vector<int>& ksize,
                  const std::vector<int>& strides,
288
                  const std::vector<int>& paddings, bool adaptive,
C
chengduoZH 已提交
289
                  framework::Tensor* input_grad);
C
chengduoZH 已提交
290 291
};

Q
QI JUN 已提交
292
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
293 294
class MaxPool3dWithIndexFunctor {
 public:
Q
QI JUN 已提交
295
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
296 297
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
298 299
                  const std::vector<int>& paddings, bool adaptive,
                  framework::Tensor* output, framework::Tensor* mask);
C
chengduoZH 已提交
300 301
};

Q
QI JUN 已提交
302
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
303 304
class MaxPool3dWithIndexGradFunctor {
 public:
Q
QI JUN 已提交
305
  void operator()(const DeviceContext& context,
C
chengduoZH 已提交
306
                  const framework::Tensor& output_grad,
C
chengduo 已提交
307 308
                  const framework::Tensor& mask, const std::vector<int>& ksize,
                  const std::vector<int>& strides,
309
                  const std::vector<int>& paddings, bool adaptive,
C
chengduoZH 已提交
310
                  framework::Tensor* input_grad);
C
chengduoZH 已提交
311
};
C
chengduoZH 已提交
312

313 314 315
}  // namespace math
}  // namespace operators
}  // namespace paddle