Skip to content

Commit 4f5491b

Browse files
author
chengduo
authored
Merge pull request #4146 from chengduoZH/Add_pool_op
Add pool op
2 parents aa52fa1 + 6abcb74 commit 4f5491b

File tree

11 files changed

+1895
-4
lines changed

11 files changed

+1895
-4
lines changed

paddle/operators/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ function(op_library TARGET)
5555
set(pybind_flag 1)
5656
endif()
5757

58+
if ("${TARGET}" STREQUAL "pool_op")
59+
set(pybind_flag 1)
60+
# It's enough to just adding one operator to pybind
61+
file(APPEND ${pybind_file} "USE_OP(pool2d);\n")
62+
endif()
63+
5864
# activation_op contains several operators
5965
if ("${TARGET}" STREQUAL "activation_op")
6066
set(pybind_flag 1)

paddle/operators/math/CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
if(WITH_GPU)
2-
nv_library(math_function SRCS math_function.cc math_function.cu im2col.cc
3-
im2col.cu DEPS cblas device_context operator)
2+
nv_library(math_function SRCS math_function.cc math_function.cu im2col.cc im2col.cu pooling.cc pooling.cu DEPS cblas device_context operator)
43
nv_test(math_function_test SRCS math_function_test.cc DEPS math_function tensor)
54
nv_library(softmax SRCS softmax.cc softmax.cu DEPS operator)
65
nv_library(cross_entropy SRCS cross_entropy.cc cross_entropy.cu DEPS operator)
76
else()
8-
cc_library(math_function SRCS math_function.cc im2col.cc
9-
DEPS cblas device_context operator)
7+
cc_library(math_function SRCS math_function.cc im2col.cc pooling.cc DEPS cblas device_context operator)
108
cc_test(math_function_test SRCS math_function_test.cc DEPS math_function tensor)
119
cc_library(softmax SRCS softmax.cc DEPS operator)
1210
cc_library(cross_entropy SRCS cross_entropy.cc DEPS operator)

paddle/operators/math/pooling.cc

Lines changed: 463 additions & 0 deletions
Large diffs are not rendered by default.

paddle/operators/math/pooling.cu

Lines changed: 635 additions & 0 deletions
Large diffs are not rendered by default.

paddle/operators/math/pooling.h

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#pragma once
16+
#include "paddle/framework/eigen.h"
17+
#include "paddle/framework/tensor.h"
18+
#include "paddle/platform/device_context.h"
19+
#include "paddle/platform/hostdevice.h"
20+
21+
namespace paddle {
22+
namespace operators {
23+
namespace math {
24+
//////////////////////
25+
#define FLT_MAX __FLT_MAX__ //
26+
27+
template <class T>
28+
class MaxPool {
29+
public:
30+
DEVICE inline T initial() { return static_cast<T>(-FLT_MAX); }
31+
DEVICE inline void compute(T& y, const T& x) { y = y > x ? y : x; }
32+
DEVICE inline void finalize(T& y, const T& poo_size) {}
33+
};
34+
35+
template <class T>
36+
class AvgPool {
37+
public:
38+
DEVICE inline T initial() { return static_cast<T>(0); }
39+
DEVICE inline void compute(T& y, const T& x) { y += x; }
40+
DEVICE inline void finalize(T& y, const T& poo_size) { y /= poo_size; }
41+
};
42+
template <class T>
43+
class MaxPoolGrad {
44+
public:
45+
DEVICE inline void compute(const T& x, const T& y, const T& dy, T& dx,
46+
T scale) {
47+
dx += dy * (x == y);
48+
}
49+
};
50+
51+
template <class T>
52+
class AvgPoolGrad {
53+
public:
54+
DEVICE inline void compute(const T& x, const T& y, const T& dy, T& dx,
55+
T scale) {
56+
dx += (scale * dy);
57+
}
58+
};
59+
60+
template <typename Place, typename PoolProcess, typename T>
61+
class Pool2dFunctor {
62+
public:
63+
void operator()(const platform::DeviceContext& context,
64+
const framework::Tensor& input, framework::Tensor& output,
65+
std::vector<int>& ksize, std::vector<int>& strides,
66+
std::vector<int>& paddings, PoolProcess pool_compute);
67+
};
68+
69+
template <typename Place, typename PoolProcess, typename T>
70+
class Pool2dGradFunctor {
71+
public:
72+
void operator()(const platform::DeviceContext& context,
73+
const framework::Tensor& input, framework::Tensor& input_grad,
74+
const framework::Tensor& output,
75+
const framework::Tensor& output_grad, std::vector<int>& ksize,
76+
std::vector<int>& strides, std::vector<int>& paddings,
77+
PoolProcess pool_compute);
78+
};
79+
80+
template <typename Place, class T>
81+
class MaxPool2dGradFunctor {
82+
public:
83+
void operator()(const platform::DeviceContext& context,
84+
const framework::Tensor& input, framework::Tensor& input_grad,
85+
const framework::Tensor& output,
86+
const framework::Tensor& output_grad, std::vector<int>& ksize,
87+
std::vector<int>& strides, std::vector<int>& paddings);
88+
};
89+
90+
template <typename Place, typename PoolProcess, typename T>
91+
class Pool3dFunctor {
92+
public:
93+
void operator()(const platform::DeviceContext& context,
94+
const framework::Tensor& input, framework::Tensor& output,
95+
std::vector<int>& ksize, std::vector<int>& strides,
96+
std::vector<int>& paddings, PoolProcess pool_compute);
97+
};
98+
99+
template <typename Place, typename PoolProcess, typename T>
100+
class Pool3dGradFunctor {
101+
public:
102+
void operator()(const platform::DeviceContext& context,
103+
const framework::Tensor& input, framework::Tensor& input_grad,
104+
const framework::Tensor& output,
105+
const framework::Tensor& output_grad, std::vector<int>& ksize,
106+
std::vector<int>& strides, std::vector<int>& paddings,
107+
PoolProcess pool_compute);
108+
};
109+
110+
template <typename Place, class T>
111+
class MaxPool3dGradFunctor {
112+
public:
113+
void operator()(const platform::DeviceContext& context,
114+
const framework::Tensor& input, framework::Tensor& input_grad,
115+
const framework::Tensor& output,
116+
const framework::Tensor& output_grad, std::vector<int>& ksize,
117+
std::vector<int>& strides, std::vector<int>& paddings);
118+
};
119+
120+
} // namespace math
121+
} // namespace operators
122+
} // namespace paddle

paddle/operators/pool_op.cc

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "paddle/operators/pool_op.h"
16+
17+
namespace paddle {
18+
namespace operators {
19+
20+
int OutputSizePool(int input_size, int filter_size, int padding, int stride) {
21+
int output_size = (input_size - filter_size + 2 * padding) / stride + 1;
22+
return output_size;
23+
}
24+
25+
class PoolOp : public framework::OperatorWithKernel {
26+
public:
27+
using framework::OperatorWithKernel::OperatorWithKernel;
28+
29+
protected:
30+
void InferShape(framework::InferShapeContextBase *ctx) const override {
31+
PADDLE_ENFORCE(ctx->HasInput("X"),
32+
"X(Input) of Pooling should not be null.");
33+
PADDLE_ENFORCE(ctx->HasOutput("Out"),
34+
"Out(Output) of Pooling should not be null.");
35+
36+
auto in_x_dims = ctx->GetInputDim("X");
37+
38+
std::string pooling_type = ctx->Attrs().Get<std::string>("poolingType");
39+
std::vector<int> ksize = ctx->Attrs().Get<std::vector<int>>("ksize");
40+
std::vector<int> strides = ctx->Attrs().Get<std::vector<int>>("strides");
41+
std::vector<int> paddings = ctx->Attrs().Get<std::vector<int>>("paddings");
42+
43+
PADDLE_ENFORCE(pooling_type == "max" || pooling_type == "avg",
44+
"pooling_type should be 'max' or 'avg'");
45+
PADDLE_ENFORCE(in_x_dims.size() == 4 || in_x_dims.size() == 5,
46+
"Pooling intput should be 4-D or 5-D");
47+
48+
if (ctx->Attrs().Get<bool>("globalPooling")) {
49+
ksize.resize(static_cast<size_t>(in_x_dims.size()) - 2);
50+
for (size_t i = 0; i < ksize.size(); ++i)
51+
ksize[i] = static_cast<int>(in_x_dims[i + 2]);
52+
}
53+
54+
PADDLE_ENFORCE(in_x_dims.size() - ksize.size() == 2U,
55+
"Input size and Pooling size should be consistent.");
56+
PADDLE_ENFORCE(ksize.size() == 2 || ksize.size() == 3,
57+
"Pooling size should be 2 elements. or 3 elements.");
58+
PADDLE_ENFORCE_EQ(ksize.size(), strides.size(),
59+
"strides size and pooling size should be the same.");
60+
PADDLE_ENFORCE_EQ(ksize.size(), paddings.size(),
61+
"paddings size and pooling size should be the same.");
62+
63+
std::vector<int64_t> output_shape({in_x_dims[0], in_x_dims[1]});
64+
for (size_t i = 0; i < ksize.size(); ++i) {
65+
output_shape.push_back(
66+
OutputSizePool(in_x_dims[i + 2], ksize[i], paddings[i], strides[i]));
67+
}
68+
ctx->SetOutputDim("Out", framework::make_ddim(output_shape));
69+
}
70+
};
71+
72+
class PoolOpGrad : public framework::OperatorWithKernel {
73+
public:
74+
using framework::OperatorWithKernel::OperatorWithKernel;
75+
76+
protected:
77+
void InferShape(framework::InferShapeContextBase *ctx) const override {
78+
PADDLE_ENFORCE(ctx->HasInput("X"),
79+
"X(Input) of Pooling should not be null.");
80+
PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")),
81+
"Input@Grad of Pooling should not be null.");
82+
ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
83+
}
84+
};
85+
86+
class Pool2dOpMaker : public framework::OpProtoAndCheckerMaker {
87+
public:
88+
Pool2dOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
89+
: OpProtoAndCheckerMaker(proto, op_checker) {
90+
AddInput(
91+
"X",
92+
"The input tensor of pooling operator. "
93+
"The format of input tensor is NCHW. Where N is batch size, C is the "
94+
"number of channels, H and W is the height and width of feature.");
95+
AddOutput("Out",
96+
"The output tensor of pooling operator."
97+
"The format of output tensor is also NCHW.");
98+
99+
AddAttr<std::string>("poolingType",
100+
"PoolingType of pooling operator."
101+
"Str constant equal to 'max' or 'avg'.")
102+
.InEnum({"max", "avg"});
103+
AddAttr<std::vector<int>>(
104+
"ksize",
105+
"Pooling size(depth, height, width) of pooling operator."
106+
"If globalPooling = true, ksize is ignored and need not be "
107+
"specified."); // TODO(Add checker)
108+
AddAttr<bool>(
109+
"globalPooling",
110+
"Whether to use the globalPooling."
111+
"Bool constant equal to false or true."
112+
"Default false."
113+
"If globalPooling = true, ksize is ignored and need not be specified.")
114+
.SetDefault(false);
115+
AddAttr<std::vector<int>>("strides",
116+
"Strides(height, width) of pooling operator."
117+
"Default {1,1}")
118+
.SetDefault({1, 1}); // TODO(Add checker)
119+
AddAttr<std::vector<int>>("paddings",
120+
"Paddings(height, width) of pooling operator."
121+
"Default {0,0}.")
122+
.SetDefault({0, 0}); // TODO(Add checker)
123+
AddComment(R"DOC(
124+
The pooling2d operation calculates the output based on
125+
the input, poolingType and ksize, strides, paddings parameters.
126+
)DOC");
127+
}
128+
};
129+
130+
class Pool3dOpMaker : public framework::OpProtoAndCheckerMaker {
131+
public:
132+
Pool3dOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
133+
: OpProtoAndCheckerMaker(proto, op_checker) {
134+
AddInput("X",
135+
"The input tensor of pooling operator. "
136+
"The format of input tensor is NCDHW. Where N is batch size, C is "
137+
"the "
138+
"number of channels, D, H and W is the depth, height and width of "
139+
"feature.");
140+
AddOutput("Out",
141+
"The output tensor of pooling operator."
142+
"The format of output tensor is also NCDHW.");
143+
144+
AddAttr<std::string>("poolingType",
145+
"PoolingType of pooling operator."
146+
"str constant equal to 'max' or 'avg'.")
147+
.InEnum({"max", "avg"});
148+
AddAttr<std::vector<int>>(
149+
"ksize",
150+
"Pooling size(depth, height, width) of pooling operator."
151+
"If globalPooling = true, ksize is ignored and need not be "
152+
"specified."); // TODO(Add checker)
153+
AddAttr<bool>(
154+
"globalPooling",
155+
"Whether to use the globalPooling."
156+
"Bool constant equal to false or true."
157+
"Default false."
158+
"If globalPooling = true, ksize is ignored and need not be specified.")
159+
.SetDefault(false);
160+
AddAttr<std::vector<int>>(
161+
"strides",
162+
"Strides(depth, height, width) of pooling operator."
163+
"Default {1,1,1}.")
164+
.SetDefault({1, 1, 1}); // TODO(Add checker)
165+
AddAttr<std::vector<int>>(
166+
"paddings",
167+
"Paddings(depth, height, width) of pooling operator."
168+
"Default {0,0,0}.")
169+
.SetDefault({0, 0, 0}); // TODO(Add checker)
170+
AddComment(R"DOC(
171+
The pooling3d operation calculates the output based on
172+
the input, poolingType and ksize, strides, paddings parameters.
173+
)DOC");
174+
}
175+
};
176+
} // namespace operators
177+
} // namespace paddle
178+
179+
namespace ops = paddle::operators;
180+
181+
REGISTER_OP(pool2d, ops::PoolOp, ops::Pool2dOpMaker, pool2d_grad,
182+
ops::PoolOpGrad);
183+
184+
REGISTER_OP_CPU_KERNEL(pool2d,
185+
ops::PoolKernel<paddle::platform::CPUPlace, float>);
186+
REGISTER_OP_CPU_KERNEL(pool2d_grad,
187+
ops::PoolGradKernel<paddle::platform::CPUPlace, float>)
188+
189+
REGISTER_OP(pool3d, ops::PoolOp, ops::Pool3dOpMaker, pool3d_grad,
190+
ops::PoolOpGrad);
191+
192+
REGISTER_OP_CPU_KERNEL(pool3d,
193+
ops::PoolKernel<paddle::platform::CPUPlace, float>);
194+
REGISTER_OP_CPU_KERNEL(pool3d_grad,
195+
ops::PoolGradKernel<paddle::platform::CPUPlace, float>);

paddle/operators/pool_op.cu

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "paddle/operators/pool_op.h"
16+
17+
namespace ops = paddle::operators;
18+
19+
REGISTER_OP_GPU_KERNEL(pool2d,
20+
ops::PoolKernel<paddle::platform::GPUPlace, float>);
21+
REGISTER_OP_GPU_KERNEL(pool2d_grad,
22+
ops::PoolGradKernel<paddle::platform::GPUPlace, float>);
23+
24+
REGISTER_OP_GPU_KERNEL(pool3d,
25+
ops::PoolKernel<paddle::platform::GPUPlace, float>);
26+
REGISTER_OP_GPU_KERNEL(pool3d_grad,
27+
ops::PoolGradKernel<paddle::platform::GPUPlace, float>);

0 commit comments

Comments
 (0)