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

namespace paddle {
namespace operators {
namespace math {

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

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

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

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

C
chengduoZH 已提交
71 72 73
/*
 * \brief Getting pooling results, and calculating gradient.
 *
C
chengduoZH 已提交
74 75 76 77
 * In pool2d, all tensors are in NCHW 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 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 已提交
78 79
 *
 * In max pooling, it is possible that the pooling region has multiple maximum
C
chengduoZH 已提交
80 81
 * elements. In this case, we should compute the gradient of the first maximum
 * element.
C
chengduoZH 已提交
82 83 84
 * This is different from average pooling. So we rewrite the max_pool_grad:
 * MaxPool2dGradFunctor, MaxPool3dGradFunctor.
 */
N
nhzlx 已提交
85 86 87 88 89 90 91 92 93 94 95 96

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,
                  bool exclusive, T* output, cudaStream_t stream);
};

Q
QI JUN 已提交
97
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
98
class Pool2dFunctor {
99
 public:
Q
QI JUN 已提交
100
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
101 102 103
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
104
                  bool exclusive, framework::Tensor* output);
105 106
};

Q
QI JUN 已提交
107
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
108
class Pool2dGradFunctor {
109
 public:
Q
QI JUN 已提交
110
  void operator()(const DeviceContext& context, const framework::Tensor& input,
111
                  const framework::Tensor& output,
C
chengduo 已提交
112 113 114 115
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
116
                  bool exclusive, framework::Tensor* input_grad);
117 118
};

Q
QI JUN 已提交
119
template <typename DeviceContext, class T>
C
chengduoZH 已提交
120
class MaxPool2dGradFunctor {
121
 public:
Q
QI JUN 已提交
122
  void operator()(const DeviceContext& context, const framework::Tensor& input,
123
                  const framework::Tensor& output,
C
chengduo 已提交
124 125 126 127
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
C
chengduoZH 已提交
128
                  framework::Tensor* input_grad);
129 130
};

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

Q
QI JUN 已提交
141
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
142
class Pool3dGradFunctor {
143
 public:
Q
QI JUN 已提交
144
  void operator()(const DeviceContext& context, const framework::Tensor& input,
145
                  const framework::Tensor& output,
C
chengduo 已提交
146 147 148 149
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
150
                  bool exclusive, framework::Tensor* input_grad);
151 152
};

Q
QI JUN 已提交
153
template <typename DeviceContext, class T>
C
chengduoZH 已提交
154
class MaxPool3dGradFunctor {
155
 public:
Q
QI JUN 已提交
156
  void operator()(const DeviceContext& context, const framework::Tensor& input,
157
                  const framework::Tensor& output,
C
chengduo 已提交
158 159 160 161
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
C
chengduoZH 已提交
162
                  framework::Tensor* input_grad);
163 164
};

C
chengduoZH 已提交
165 166 167
/*
 * \brief Getting max pooling results and corresponding max index, and
 * calculating gradient.
C
chengduoZH 已提交
168
 * In up-sampling-pooling, it is necessary to know max element index.
C
chengduoZH 已提交
169 170 171
 * In pool2d, all tensors are in NCHW format. In pool3d, all tensors are in
 * NCDHW format.
 */
Q
QI JUN 已提交
172
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
173 174
class MaxPool2dWithIndexFunctor {
 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, framework::Tensor* output,
Q
QI JUN 已提交
179
                  framework::Tensor* mask);
C
chengduoZH 已提交
180 181
};

Q
QI JUN 已提交
182
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
183 184
class MaxPool2dWithIndexGradFunctor {
 public:
Q
QI JUN 已提交
185
  void operator()(const DeviceContext& context,
C
chengduoZH 已提交
186
                  const framework::Tensor& output_grad,
C
chengduo 已提交
187 188 189
                  const framework::Tensor& mask, const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
C
chengduoZH 已提交
190
                  framework::Tensor* input_grad);
C
chengduoZH 已提交
191 192
};

Q
QI JUN 已提交
193
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
194 195
class MaxPool3dWithIndexFunctor {
 public:
Q
QI JUN 已提交
196
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
197 198 199
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, framework::Tensor* output,
Q
QI JUN 已提交
200
                  framework::Tensor* mask);
C
chengduoZH 已提交
201 202
};

Q
QI JUN 已提交
203
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
204 205
class MaxPool3dWithIndexGradFunctor {
 public:
Q
QI JUN 已提交
206
  void operator()(const DeviceContext& context,
C
chengduoZH 已提交
207
                  const framework::Tensor& output_grad,
C
chengduo 已提交
208 209 210
                  const framework::Tensor& mask, const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
C
chengduoZH 已提交
211
                  framework::Tensor* input_grad);
C
chengduoZH 已提交
212
};
C
chengduoZH 已提交
213

214 215 216
}  // namespace math
}  // namespace operators
}  // namespace paddle