pooling.h 12.0 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
};

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

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

Q
QI JUN 已提交
228
template <typename DeviceContext, class T>
C
chengduoZH 已提交
229
class MaxPool3dGradFunctor {
230
 public:
Q
QI JUN 已提交
231
  void operator()(const DeviceContext& context, const framework::Tensor& input,
232
                  const framework::Tensor& output,
C
chengduo 已提交
233 234 235 236
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
C
chengduoZH 已提交
237
                  framework::Tensor* input_grad);
238 239 240 241 242 243 244 245
  // 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);
246 247
};

C
chengduoZH 已提交
248 249 250
/*
 * \brief Getting max pooling results and corresponding max index, and
 * calculating gradient.
C
chengduoZH 已提交
251
 * In up-sampling-pooling, it is necessary to know max element index.
C
chengduoZH 已提交
252 253 254
 * In pool2d, all tensors are in NCHW format. In pool3d, all tensors are in
 * NCDHW format.
 */
Q
QI JUN 已提交
255
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
256 257
class MaxPool2dWithIndexFunctor {
 public:
Q
QI JUN 已提交
258
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
259 260
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
261 262
                  const std::vector<int>& paddings, bool adaptive,
                  framework::Tensor* output, framework::Tensor* mask);
C
chengduoZH 已提交
263 264
};

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

Q
QI JUN 已提交
276
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
277 278
class MaxPool3dWithIndexFunctor {
 public:
Q
QI JUN 已提交
279
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
280 281
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
282 283
                  const std::vector<int>& paddings, bool adaptive,
                  framework::Tensor* output, framework::Tensor* mask);
C
chengduoZH 已提交
284 285
};

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

297 298 299
}  // namespace math
}  // namespace operators
}  // namespace paddle