pooling.h 12.6 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:
C
chengduo 已提交
71 72
  DEVICE inline void compute(const T& x, const T& y, const T& dy, T scale,
                             T* dx) {
73
    *dx += dy * static_cast<T>(x == y);
74 75 76 77
  }
};

template <class T>
78
class AvgPoolGrad {
79
 public:
C
chengduo 已提交
80 81 82
  DEVICE inline void compute(const T& x, const T& y, const T& dy, T scale,
                             T* dx) {
    *dx += (scale * dy);
83 84 85
  }
};

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

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

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

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

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

F
feng_shuai 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201
#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 已提交
202
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
203
class Pool3dFunctor {
204
 public:
Q
QI JUN 已提交
205
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
206 207
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
208 209 210
                  const std::vector<int>& paddings, bool exclusive,
                  bool adaptive, framework::Tensor* output,
                  PoolProcess pool_compute);
211 212 213 214 215
  // 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,
216 217
                  const std::string data_format, bool exclusive, bool adaptive,
                  framework::Tensor* output, PoolProcess pool_compute);
218 219
};

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

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

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

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

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

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

311 312 313
}  // namespace math
}  // namespace operators
}  // namespace paddle