pooling.h 7.7 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"
21 22 23 24 25

namespace paddle {
namespace operators {
namespace math {

C
chengduoZH 已提交
26
#define FLT_MAX \
C
chengduo 已提交
27 28
  __FLT_MAX__  // TODO(zcd) :It might need to be placed in another file, but I'm
               // still wondering where to put it.
C
chengduoZH 已提交
29 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 {
C
chengduoZH 已提交
50 51
 public:
  DEVICE inline T initial() { return static_cast<T>(0); }
C
chengduo 已提交
52 53
  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 已提交
54
};
C
chengduoZH 已提交
55

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

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

C
chengduoZH 已提交
74 75 76
/*
 * \brief Getting pooling results, and calculating gradient.
 *
C
chengduoZH 已提交
77 78 79 80
 * 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 已提交
81 82
 *
 * In max pooling, it is possible that the pooling region has multiple maximum
C
chengduoZH 已提交
83 84
 * elements. In this case, we should compute the gradient of the first maximum
 * element.
C
chengduoZH 已提交
85 86 87
 * This is different from average pooling. So we rewrite the max_pool_grad:
 * MaxPool2dGradFunctor, MaxPool3dGradFunctor.
 */
Q
QI JUN 已提交
88
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
89
class Pool2dFunctor {
90
 public:
Q
QI JUN 已提交
91
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
92 93 94
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
Q
QI JUN 已提交
95
                  framework::Tensor* output);
96 97
};

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

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

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

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

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

C
chengduoZH 已提交
156 157 158
/*
 * \brief Getting max pooling results and corresponding max index, and
 * calculating gradient.
C
chengduoZH 已提交
159
 * In up-sampling-pooling, it is necessary to know max element index.
C
chengduoZH 已提交
160 161 162
 * In pool2d, all tensors are in NCHW format. In pool3d, all tensors are in
 * NCDHW format.
 */
Q
QI JUN 已提交
163
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
164 165
class MaxPool2dWithIndexFunctor {
 public:
Q
QI JUN 已提交
166
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
167 168 169
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, framework::Tensor* output,
Q
QI JUN 已提交
170
                  framework::Tensor* mask);
C
chengduoZH 已提交
171 172
};

Q
QI JUN 已提交
173
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
174 175
class MaxPool2dWithIndexGradFunctor {
 public:
Q
QI JUN 已提交
176
  void operator()(const DeviceContext& context,
C
chengduoZH 已提交
177
                  const framework::Tensor& output_grad,
C
chengduo 已提交
178 179 180
                  const framework::Tensor& mask, const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
C
chengduoZH 已提交
181
                  framework::Tensor* input_grad);
C
chengduoZH 已提交
182 183
};

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

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

205 206 207
}  // namespace math
}  // namespace operators
}  // namespace paddle