pooling.h 11.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>
Y
Yi Wang 已提交
18 19 20 21
#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 已提交
22
#include "paddle/fluid/platform/macros.h"
23 24 25 26 27

namespace paddle {
namespace operators {
namespace math {

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

template <class T>
47
class AvgPool {
C
chengduoZH 已提交
48 49
 public:
  DEVICE inline T initial() { return static_cast<T>(0); }
C
chengduo 已提交
50 51
  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 已提交
52
};
C
chengduoZH 已提交
53

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

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

D
dengkaipeng 已提交
72 73 74 75 76 77 78 79 80 81 82 83
/* 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 已提交
84 85 86
/*
 * \brief Getting pooling results, and calculating gradient.
 *
87 88 89 90 91
 * 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 已提交
92 93
 *
 * In max pooling, it is possible that the pooling region has multiple maximum
C
chengduoZH 已提交
94 95
 * elements. In this case, we should compute the gradient of the first maximum
 * element.
C
chengduoZH 已提交
96 97 98
 * This is different from average pooling. So we rewrite the max_pool_grad:
 * MaxPool2dGradFunctor, MaxPool3dGradFunctor.
 */
N
nhzlx 已提交
99
#ifdef PADDLE_WITH_CUDA
N
nhzlx 已提交
100 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,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
108 109
                  bool exclusive, bool adaptive, T* output,
                  cudaStream_t stream);
N
nhzlx 已提交
110
};
N
nhzlx 已提交
111
#endif
N
nhzlx 已提交
112

Q
QI JUN 已提交
113
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
114
class Pool2dFunctor {
115
 public:
Q
QI JUN 已提交
116
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
117 118 119
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
120
                  bool exclusive, bool adaptive, framework::Tensor* output);
121 122 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,
                  const std::string data_format, PoolProcess pool_compute,
                  bool exclusive, bool adaptive, framework::Tensor* output);
129 130
};

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

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

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

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

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

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

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

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

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

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