提交 6c6474cb 编写于 作者: C chengduoZH

follow coments

上级 fcfce484
...@@ -55,12 +55,20 @@ function(op_library TARGET) ...@@ -55,12 +55,20 @@ function(op_library TARGET)
set(pybind_flag 1) set(pybind_flag 1)
endif() endif()
# pool_op contains several operators
if ("${TARGET}" STREQUAL "pool_op") if ("${TARGET}" STREQUAL "pool_op")
set(pybind_flag 1) set(pybind_flag 1)
# It's enough to just adding one operator to pybind # It's enough to just adding one operator to pybind
file(APPEND ${pybind_file} "USE_OP(pool2d);\n") file(APPEND ${pybind_file} "USE_OP(pool2d);\n")
endif() endif()
# pool_with_index_op contains several operators
if ("${TARGET}" STREQUAL "pool_with_index_op")
set(pybind_flag 1)
# It's enough to just adding one operator to pybind
file(APPEND ${pybind_file} "USE_OP(max_pool2d_with_index);\n")
endif()
# activation_op contains several operators # activation_op contains several operators
if ("${TARGET}" STREQUAL "activation_op") if ("${TARGET}" STREQUAL "activation_op")
set(pybind_flag 1) set(pybind_flag 1)
...@@ -75,13 +83,6 @@ function(op_library TARGET) ...@@ -75,13 +83,6 @@ function(op_library TARGET)
file(APPEND ${pybind_file} "USE_OP(reduce_sum);\n") file(APPEND ${pybind_file} "USE_OP(reduce_sum);\n")
endif() endif()
# pool_with_index_op contains several operators
if ("${TARGET}" STREQUAL "pool_with_index_op")
set(pybind_flag 1)
# It's enough to just adding one operator to pybind
file(APPEND ${pybind_file} "USE_OP(max_pool2d_with_index);\n")
endif()
# pybind USE_NO_KERNEL_OP # pybind USE_NO_KERNEL_OP
file(READ ${TARGET}.cc TARGET_CONTENT) file(READ ${TARGET}.cc TARGET_CONTENT)
string(REGEX MATCH "OperatorWithKernel" regex_result "${TARGET_CONTENT}") string(REGEX MATCH "OperatorWithKernel" regex_result "${TARGET_CONTENT}")
......
...@@ -24,15 +24,16 @@ namespace math { ...@@ -24,15 +24,16 @@ namespace math {
#define FLT_MAX \ #define FLT_MAX \
__FLT_MAX__ // It might need to be placed in another file, but I'm still __FLT_MAX__ // It might need to be placed in another file, but I'm still
// wondering where to put it // wondering where to put it.
/* /*
* \brief Extracting simple operations from pooling. * \brief Extracting simple operations from pooling.
* Both MaxPool and AvgPool need initial, compute and finalize operation. * Both MaxPool and AvgPool need "initial", "compute" and "finalize"
* operation.
* MaxPool initializes temp variable to the negative maximum to find the * MaxPool initializes temp variable to the negative maximum to find the
* maximum value in the pooling field. * maximum value in the pooling field.
* AvgPool initializes temp variable to the zero to accumulate all values * AvgPool initializes temp variable to the zero to accumulate all values
* in pool pooling, and takes the average. * in pool pooling, and finally takes the average.
* MaxPoolGrad and AvgPoolGrad are gradient operations respectively. * MaxPoolGrad and AvgPoolGrad are gradient operations respectively.
*/ */
template <class T> template <class T>
...@@ -72,17 +73,17 @@ class AvgPoolGrad { ...@@ -72,17 +73,17 @@ class AvgPoolGrad {
/* /*
* \brief Getting pooling results, and calculating gradient. * \brief Getting pooling results, and calculating gradient.
* *
* In pool2d, all tensors are in NCHW format. In pool3d, all tensors are in * In pool2d, all tensors are in NCHW format. Where N is batch size, C is the
* NCDHW format. * 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.
* *
* In max pooling, it is possible that the pooling region has multiple maximum * In max pooling, it is possible that the pooling region has multiple maximum
* elements. * elements. In this case, we should compute the gradient of the first maximum
* In this case, we should compute the gradient of the first maximum element. * element.
* This is different from average pooling. So we rewrite the max_pool_grad: * This is different from average pooling. So we rewrite the max_pool_grad:
* MaxPool2dGradFunctor, MaxPool3dGradFunctor. * MaxPool2dGradFunctor, MaxPool3dGradFunctor.
*
*/ */
template <typename Place, typename PoolProcess, typename T> template <typename Place, typename PoolProcess, typename T>
class Pool2dFunctor { class Pool2dFunctor {
public: public:
...@@ -146,10 +147,9 @@ class MaxPool3dGradFunctor { ...@@ -146,10 +147,9 @@ class MaxPool3dGradFunctor {
/* /*
* \brief Getting max pooling results and corresponding max index, and * \brief Getting max pooling results and corresponding max index, and
* calculating gradient. * calculating gradient.
* In sub-sampling-pooling, it is necessary to know max element index. * In up-sampling-pooling, it is necessary to know max element index.
* In pool2d, all tensors are in NCHW format. In pool3d, all tensors are in * In pool2d, all tensors are in NCHW format. In pool3d, all tensors are in
* NCDHW format. * NCDHW format.
*
*/ */
template <typename Place, typename T> template <typename Place, typename T>
class MaxPool2dWithIndexFunctor { class MaxPool2dWithIndexFunctor {
...@@ -188,6 +188,7 @@ class MaxPool3dWithIndexGradFunctor { ...@@ -188,6 +188,7 @@ class MaxPool3dWithIndexGradFunctor {
const framework::Tensor& mask, std::vector<int>& ksize, const framework::Tensor& mask, std::vector<int>& ksize,
std::vector<int>& strides, std::vector<int>& paddings); std::vector<int>& strides, std::vector<int>& paddings);
}; };
} // namespace math } // namespace math
} // namespace operators } // namespace operators
} // namespace paddle } // namespace paddle
...@@ -34,7 +34,7 @@ class MaxPoolWithIndexOp : public framework::OperatorWithKernel { ...@@ -34,7 +34,7 @@ class MaxPoolWithIndexOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE(ctx->HasOutput("Out"), PADDLE_ENFORCE(ctx->HasOutput("Out"),
"Out(Output) of Pooling should not be null."); "Out(Output) of Pooling should not be null.");
PADDLE_ENFORCE(ctx->HasOutput("Mask"), PADDLE_ENFORCE(ctx->HasOutput("Mask"),
"Out(Output) of Pooling should not be null."); "Mask(Output) of Pooling should not be null.");
auto in_x_dims = ctx->GetInputDim("X"); auto in_x_dims = ctx->GetInputDim("X");
...@@ -52,13 +52,11 @@ class MaxPoolWithIndexOp : public framework::OperatorWithKernel { ...@@ -52,13 +52,11 @@ class MaxPoolWithIndexOp : public framework::OperatorWithKernel {
} }
PADDLE_ENFORCE(in_x_dims.size() - ksize.size() == 2U, PADDLE_ENFORCE(in_x_dims.size() - ksize.size() == 2U,
"Pooling intput size and pooling size should be consistent"); "Intput size and pooling size should be consistent.");
PADDLE_ENFORCE(ksize.size() == 2 || ksize.size() == 3,
"Pooling size size should be 2 elements. or 3 elements.");
PADDLE_ENFORCE_EQ(ksize.size(), strides.size(), PADDLE_ENFORCE_EQ(ksize.size(), strides.size(),
"strides size and pooling size should be the same."); "Strides size and pooling size should be the same.");
PADDLE_ENFORCE_EQ(ksize.size(), paddings.size(), PADDLE_ENFORCE_EQ(ksize.size(), paddings.size(),
"paddings size and pooling size should be the same."); "Paddings size and pooling size should be the same.");
std::vector<int64_t> output_shape({in_x_dims[0], in_x_dims[1]}); std::vector<int64_t> output_shape({in_x_dims[0], in_x_dims[1]});
for (size_t i = 0; i < ksize.size(); ++i) { for (size_t i = 0; i < ksize.size(); ++i) {
...@@ -76,11 +74,9 @@ class MaxPoolWithIndexOpGrad : public framework::OperatorWithKernel { ...@@ -76,11 +74,9 @@ class MaxPoolWithIndexOpGrad : public framework::OperatorWithKernel {
protected: protected:
void InferShape(framework::InferShapeContextBase *ctx) const override { void InferShape(framework::InferShapeContextBase *ctx) const override {
PADDLE_ENFORCE(ctx->HasInput("X"), PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) must not be null.");
"X(Input) of Pooling should not be null."); PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")),
PADDLE_ENFORCE( "Input(X@GRAD) should not be null.");
ctx->HasOutput(framework::GradVarName("X")),
"X@GRAD(Input@GRAD) of MaxPoolWithIndexOpGrad should not be null.");
ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X")); ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
} }
}; };
...@@ -110,9 +106,10 @@ class MaxPool2dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -110,9 +106,10 @@ class MaxPool2dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker {
AddAttr<std::vector<int>>( AddAttr<std::vector<int>>(
"ksize", "ksize",
"Pooling size(height, width) of pooling operator." "The pooling size(height, width) of pooling operator."
"If globalPooling = true, ksize is ignored and need not be " "If globalPooling = true, ksize is ignored and need not be "
"specified."); // TODO(Add checker) "specified."); // TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
AddAttr<bool>( AddAttr<bool>(
"globalPooling", "globalPooling",
"Whether to use the globalPooling." "Whether to use the globalPooling."
...@@ -123,15 +120,21 @@ class MaxPool2dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -123,15 +120,21 @@ class MaxPool2dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker {
AddAttr<std::vector<int>>("strides", AddAttr<std::vector<int>>("strides",
"Strides(height, width) of pooling operator." "Strides(height, width) of pooling operator."
"Default {1,1}.") "Default {1,1}.")
.SetDefault({1, 1}); // TODO(Add checker) .SetDefault({1, 1}); // TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
AddAttr<std::vector<int>>("paddings", AddAttr<std::vector<int>>("paddings",
"Paddings(height, width) of pooling operator." "Paddings(height, width) of pooling operator."
"Default {0,0}.") "Default {0,0}.")
.SetDefault({0, 0}); // TODO(Add checker) .SetDefault({0, 0}); // TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
AddComment(R"DOC( AddComment(R"DOC(
The maxPooling2d with index operation calculates the output and the mask based on The maxPooling2d with index operation calculates the output and the mask
the input and ksize, strides, paddings parameters. based on the input and ksize, strides, paddings parameters. Input(X) and
output(Out, Mask) 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.
Parameters(ksize, strides, paddings) are two elements.
These two elements represent height and width, respectively.
)DOC"); )DOC");
} }
}; };
...@@ -162,9 +165,10 @@ class MaxPool3dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -162,9 +165,10 @@ class MaxPool3dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker {
AddAttr<std::vector<int>>( AddAttr<std::vector<int>>(
"ksize", "ksize",
"Pooling size(depth, height, width) of pooling operator." "The pooling size(depth, height, width) of pooling operator."
"If globalPooling = true, ksize is ignored and need not be " "If globalPooling = true, ksize is ignored and need not be "
"specified."); // TODO(Add checker) "specified."); // TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
AddAttr<bool>( AddAttr<bool>(
"globalPooling", "globalPooling",
"Whether to use the globalPooling." "Whether to use the globalPooling."
...@@ -176,19 +180,26 @@ class MaxPool3dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -176,19 +180,26 @@ class MaxPool3dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker {
"strides", "strides",
"Strides(depth, height, width) of pooling operator." "Strides(depth, height, width) of pooling operator."
"Default {1,1,1}.") "Default {1,1,1}.")
.SetDefault({1, 1, 1}); // TODO(Add checker) .SetDefault({1, 1, 1}); // TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
AddAttr<std::vector<int>>( AddAttr<std::vector<int>>(
"paddings", "paddings",
"Paddings(depth, height, width) of pooling operator." "Paddings(depth, height, width) of pooling operator."
"Default {0,0,0}.") "Default {0,0,0}.")
.SetDefault({0, 0, 0}); // TODO(Add checker) .SetDefault({0, 0, 0}); // TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
AddComment(R"DOC( AddComment(R"DOC(
The maxpooling3d with index operation calculates the output and the mask based on The maxpooling3d with index operation calculates the output and the mask
the input and ksize, strides, paddings parameters. based on the input and ksize, strides, paddings parameters.
Input(X) and output(Out, Mask) 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. Parameters(ksize, strides, paddings) are three elements.
These three elements represent depth, height and width, respectively.
)DOC"); )DOC");
} }
}; };
} // namespace operators } // namespace operators
} // namespace paddle } // namespace paddle
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册