Skip to content

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 from 1cb037f to 8a97324 Compare September 28, 2017 03:33
@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 from 2149e40 to 6326c40 Compare September 28, 2017 09:31
@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 chengduoZH force-pushed the Add_maxpool_withIdx_only branch from d907eba to 5b606f3 Compare September 30, 2017 06:05
@chengduoZH chengduoZH force-pushed the Add_maxpool_withIdx_only branch from 5b606f3 to bee95fc Compare September 30, 2017 07:06
@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.

@chengduoZH chengduoZH force-pushed the Add_maxpool_withIdx_only branch from 59f20d5 to f5e625a Compare September 30, 2017 07:48
@chengduoZH chengduoZH requested review from NHZlX and luotao1 September 30, 2017 07:59
@chengduoZH chengduoZH force-pushed the Add_maxpool_withIdx_only branch from 42b9a34 to bb33c2b Compare September 30, 2017 09:00

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

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.

@chengduoZH chengduoZH force-pushed the Add_maxpool_withIdx_only branch from 232077d to 811cf0b Compare October 9, 2017 11:32
@chengduoZH chengduoZH force-pushed the Add_maxpool_withIdx_only branch from 811cf0b to fcfce48 Compare October 9, 2017 11:36
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

@chengduoZH chengduoZH Oct 10, 2017

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 from c93063a to 6c6474c Compare October 10, 2017 03:26
@chengduoZH chengduoZH force-pushed the Add_maxpool_withIdx_only branch 2 times, most recently from 5308f84 to 68b0508 Compare October 10, 2017 08:29
@chengduoZH chengduoZH force-pushed the Add_maxpool_withIdx_only branch from 68b0508 to 36da825 Compare October 10, 2017 10:55
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add maxpool with index op

3 participants