Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add max pool op (with index) #4461

Merged
merged 10 commits into from
Oct 10, 2017

Conversation

chengduoZH
Copy link
Contributor

@chengduoZH chengduoZH commented Sep 28, 2017

fix #4327

@chengduoZH chengduoZH changed the title Add max pool op (with index) [WIP] Add max pool op (with index) Sep 28, 2017
@chengduoZH chengduoZH force-pushed the Add_maxpool_withIdx_only branch 4 times, most recently from e4e6964 to d987c44 Compare September 28, 2017 08:48
@chengduoZH chengduoZH force-pushed the Add_maxpool_withIdx_only branch 3 times, most recently from 2d72558 to d907eba Compare September 29, 2017 12:24
@chengduoZH chengduoZH changed the title [WIP] Add max pool op (with index) Add max pool op (with index) Sep 29, 2017
@chengduoZH
Copy link
Contributor Author

chengduoZH commented Sep 30, 2017

I've commented out the check_grad in max pooling because the current method of testing can not effectively test the gradient of max pooling. But it ensures that the gradients computed by GPU and CPU are equal.

@@ -75,6 +75,12 @@ function(op_library TARGET)
file(APPEND ${pybind_file} "USE_OP(reduce_sum);\n")
endif()

if ("${TARGET}" STREQUAL "pool_with_index_op")
set(pybind_flag 1)
# It's enough to just adding one operator to pybind
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment does not make clear why need add another name to this operator. See above comments, like:

# reduce_op contains several operators

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -458,6 +458,233 @@ template class Pool3dGradFunctor<
platform::CPUPlace, paddle::operators::math::MaxPoolGrad<double>, double>;
template class Pool3dGradFunctor<
platform::CPUPlace, paddle::operators::math::AvgPoolGrad<double>, double>;

template <typename T>
class MaxPool2dWithIndexFunctor<platform::CPUPlace, T> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add some comments about these functors, like "Input order NCHW or NHWC"

Please take a glance at https://google.github.io/styleguide/cppguide.html#Function_Comments

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thx !!!

namespace paddle {
namespace operators {

int OutputSizeMaxPool(int input_size, int filter_size, int padding,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function can be a inline. And, this function can be of more general use for conv and pool operators.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.
This function is frequently called in conv2/3d, conv2/3d_cudnn, pool2/3d and pool2/3_cudnn.
I think we should convert this function to functor, and move to namespace math.


namespace ops = paddle::operators;

REGISTER_OP(maxPool2dWithIndex, ops::MaxPoolWithIndexOp,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems operator names are commonly xxx_xxx_op but not "camal case".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

@typhoonzero typhoonzero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor comments.

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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also need to add max_pool3d_with_index

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just adding one operator of pool_with_index_op is ok. And unit test(max_pool2d_with_index, max_pool3d_with_index) passed.

"X(Input) of Pooling should not be null.");
PADDLE_ENFORCE(
ctx->HasOutput(framework::GradVarName("X")),
"X@GRAD(Input@GRAD) of MaxPoolWithIndexOpGrad should not be null.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GRAD suffix is for internal use, may confuse users if this message came out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve with you. If X@GRAD is null, it must be an internet error. But I think it is helpful to developer.

"ksize",
"Pooling size(height, width) of pooling operator."
"If globalPooling = true, ksize is ignored and need not be "
"specified."); // TODO(Add checker)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems you need to configure pre-commit on your local environment before running git commit, so the style check can automatically prints some style errors.

In this case, "TODO" comments should be like: https://google.github.io/styleguide/cppguide.html#TODO_Comments

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this style of writing is really nonstandard.
I have configured pre-commit on my local environment, but it also passed.

"image.");
AddOutput("Out",
"The output tensor of pooling operator."
"The format of output tensor is also NCDHW."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"The format of output tensor is also NCDHW."
"Where N is batch size, C is "
"the number of channels, D, H and W is the depth, height and "
"width of image."

Can put these explains to AddComment, so here can be more simpler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@chengduoZH chengduoZH force-pushed the Add_maxpool_withIdx_only branch 2 times, most recently from 5308f84 to 68b0508 Compare October 10, 2017 08:29
Copy link
Contributor

@typhoonzero typhoonzero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM++

@chengduoZH chengduoZH merged commit 0f1d3af into PaddlePaddle:develop Oct 10, 2017
@chengduoZH chengduoZH added this to Done in Port Operators Oct 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

Add maxpool with index op
3 participants