pooling.h 11.8 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.
 */
100
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
N
nhzlx 已提交
101 102 103 104 105 106 107
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,
108 109 110
                  const std::vector<int>& paddings, bool exclusive,
                  bool adaptive, T* output, gpuStream_t stream,
                  PoolProcess pool_compute);
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
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
120 121 122
                  const std::vector<int>& paddings, bool exclusive,
                  bool adaptive, framework::Tensor* output,
                  PoolProcess pool_compute);
123 124 125 126 127 128

  // 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,
129 130
                  const std::string data_format, bool exclusive, bool adaptive,
                  framework::Tensor* output, PoolProcess pool_compute);
131 132
};

Q
QI JUN 已提交
133
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
134
class Pool2dGradFunctor {
135
 public:
Q
QI JUN 已提交
136
  void operator()(const DeviceContext& context, const framework::Tensor& input,
137
                  const framework::Tensor& output,
C
chengduo 已提交
138 139 140
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
141 142 143
                  const std::vector<int>& paddings, bool exclusive,
                  bool adaptive, framework::Tensor* input_grad,
                  PoolProcess pool_compute);
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,
151 152
                  const std::string data_format, bool exclusive, bool adaptive,
                  framework::Tensor* input_grad, PoolProcess pool_compute);
153 154
};

Q
QI JUN 已提交
155
template <typename DeviceContext, class T>
C
chengduoZH 已提交
156
class MaxPool2dGradFunctor {
157
 public:
Q
QI JUN 已提交
158
  void operator()(const DeviceContext& context, const framework::Tensor& input,
159
                  const framework::Tensor& output,
C
chengduo 已提交
160 161 162 163
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
C
chengduoZH 已提交
164
                  framework::Tensor* input_grad);
165 166 167 168 169 170 171 172
  // 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);
173 174
};

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

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

Q
QI JUN 已提交
215
template <typename DeviceContext, class T>
C
chengduoZH 已提交
216
class MaxPool3dGradFunctor {
217
 public:
Q
QI JUN 已提交
218
  void operator()(const DeviceContext& context, const framework::Tensor& input,
219
                  const framework::Tensor& output,
C
chengduo 已提交
220 221 222 223
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
C
chengduoZH 已提交
224
                  framework::Tensor* input_grad);
225 226 227 228 229 230 231 232
  // 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);
233 234
};

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

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

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

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

284 285 286
}  // namespace math
}  // namespace operators
}  // namespace paddle