Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[MXNET-380] count_include_pad argument for Avg Pooling #11021

Merged
merged 6 commits into from
Jun 18, 2018

Conversation

haojin2
Copy link
Contributor

@haojin2 haojin2 commented May 22, 2018

Description

As title.

Checklist

Essentials

  • Changes are complete (i.e. I finished coding on this PR)
  • All changes have test coverage:
  • Unit tests are added for small changes to verify correctness (e.g. adding a new operator)
  • Nightly tests are added for complicated/long-running ones (e.g. changing distributed kvstore)
  • Build tests will be added for build configuration changes (e.g. adding a new build option with NCCL)
  • Code is well-documented:
  • For user-facing API changes, API doc string has been updated.
  • For new C++ functions in header files, their functionalities and arguments are documented.
  • For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
  • Check the API doc at http:https://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
  • To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

Changes

  • Add count_include_pad argument for AvgPool
  • Unit tests

Comments

#10194

@TaoLv
Copy link
Member

TaoLv commented May 22, 2018

Also need fix it in mkldnn_pooling: https://github.com/apache/incubator-mxnet/blob/master/src/operator/nn/mkldnn/mkldnn_pooling.cc#L124
if count_include_pad=False, should use pooling_avg_exclude_padding here.

@@ -50,6 +50,7 @@ struct PoolingParam : public dmlc::Parameter<PoolingParam> {
bool global_pool;
bool cudnn_off;
dmlc::optional<int> p_value;
dmlc::optional<bool> count_include_pad;
Copy link
Contributor

Choose a reason for hiding this comment

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

doesn't need to be optional. Just set to the previous default

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We want to ensure forward compatibility here, if this is not an optional field, json file generated for the symbol will have an extra field, which we think may cause confusions for users of earlier versions.

.describe("Value of p for Lp pooling, can be 1 or 2, required for Lp Pooling.");

DMLC_DECLARE_FIELD(count_include_pad).set_default(dmlc::optional<bool>())
.describe("Whether to count padding elements for average calculation.");
Copy link
Contributor

Choose a reason for hiding this comment

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

Be more descriptive.
Only used for average pooling. Whether to count padded elements for normalization at edge and corners.
For example, blah blah

@piiswrong
Copy link
Contributor

Please add tests

.describe("Value of p for Lp pooling, can be 1 or 2, required for Lp Pooling");
.describe("Value of p for Lp pooling, can be 1 or 2, required for Lp Pooling.");

DMLC_DECLARE_FIELD(count_include_pad).set_default(dmlc::optional<bool>())
Copy link
Contributor

Choose a reason for hiding this comment

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

Argument name sounds weird

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

would it be better to take an ignore_pad_value here instead of assuming the padding value?

@piiswrong
Copy link
Contributor

Please add this option to Gluon's avgpool

@haojin2 haojin2 force-pushed the count_include_pad branch 2 times, most recently from f2d2e6e to 845b944 Compare May 22, 2018 17:41
@zhanghang1989
Copy link
Contributor

FYI @hetong007

@haojin2
Copy link
Contributor Author

haojin2 commented Jun 2, 2018

@piiswrong @reminisce @szha Please give this a review if you have time, thanks!
@hetong007 @zhanghang1989 this should be ready soon.

@haojin2 haojin2 changed the title [MXNET-380] [WIP] count_include_pad argument for Avg Pooling [MXNET-380] count_include_pad argument for Avg Pooling Jun 2, 2018
@@ -311,7 +322,9 @@ __global__ void pool_sum_3d_gpu_kernel(const int nthreads, const DType* in_data,
}
}
}
out_data[index] = a_root_p<DType, p>::Map(sum);
out_data[index] = (pool_size == 0) ?
DType(0.0f / pool_size) :
Copy link
Member

Choose a reason for hiding this comment

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

division by 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We want to create an artificial NaN here to ensure consistency with cudnn.

Copy link
Member

Choose a reason for hiding this comment

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

would it be better to use nan constants in math/limits? performing this division produces nan through interrupt which causes slow down.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You cannot use the std thing in gpu kernels, as they are host functions while this kernel here is a global function.

Copy link
Member

Choose a reason for hiding this comment

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

how about nan and nanf?

Copy link
Contributor

Choose a reason for hiding this comment

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

Why would pool_size be 0? Shouldn't that be invalid?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When your pad is wider than kernel size, you could get that pool_size when count_include_pad is False.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For example in the 3d test cases, there's one with (20, 20, 20) input, (4,5,3) kernel, and (2,3,3) pad. So on the last dim you could get 0 valid width and your whole pool_size will be 0.

Copy link
Member

Choose a reason for hiding this comment

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

DType cast should have already handled it for half_t and half2_t, or did I miss something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@szha okay then I'll just use those functions to generate NaN.

@haojin2 haojin2 force-pushed the count_include_pad branch 2 times, most recently from ce912fa to 4115730 Compare June 5, 2018 04:19
@haojin2
Copy link
Contributor Author

haojin2 commented Jun 5, 2018

@piiswrong Addressed all the reviews so far, please take another look when you’ve got time, thanks!

@haojin2
Copy link
Contributor Author

haojin2 commented Jun 5, 2018

@szha

@eric-haibin-lin eric-haibin-lin self-assigned this Jun 8, 2018
@@ -879,13 +881,13 @@ class AvgPool1D(_Pooling):
equation.
Copy link
Member

Choose a reason for hiding this comment

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

Pls add documentation for count_include_pad for gluon blocks

@@ -926,13 +928,13 @@ class AvgPool2D(_Pooling):
equation.
Copy link
Member

Choose a reason for hiding this comment

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

Also here and and for AvgPool3D

.describe("Value of p for Lp pooling, can be 1 or 2, required for Lp Pooling.");

DMLC_DECLARE_FIELD(count_include_pad).set_default(dmlc::optional<bool>())
.describe("Only used for AvgPool, specify whether to count padding elements for average"
Copy link
Member

Choose a reason for hiding this comment

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

The argument is optional. Is the default value True or False?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The default behavior is True, which was the behavior before the change

Copy link
Member

Choose a reason for hiding this comment

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

pls include this in the doc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

@@ -580,7 +607,8 @@ __global__ void unpool_sum_3d_gpu_kernel(const int nthreads, const DType* out_gr
const int kernel_d, const int kernel_h,
const int kernel_w, const int stride_d, const int stride_h,
const int stride_w, const int pad_d, const int pad_h,
const int pad_w, DType* in_grad, const bool isAvg = false) {
const int pad_w, DType* in_grad, const bool isAvg = false,
Copy link
Member

Choose a reason for hiding this comment

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

when was isAvg introduced? Should be is_avg

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was here a long time ago. see #5519

@haojin2 haojin2 force-pushed the count_include_pad branch 4 times, most recently from e380c21 to 11e9604 Compare June 11, 2018 05:59
@eric-haibin-lin eric-haibin-lin removed their assignment Jun 11, 2018
@haojin2
Copy link
Contributor Author

haojin2 commented Jun 12, 2018

@piiswrong Is this PR good to be merged?

@TaoLv
Copy link
Member

TaoLv commented Jun 17, 2018

I feel like adding new tests for this change would be better, compared with changing the existing ones.

@haojin2
Copy link
Contributor Author

haojin2 commented Jun 17, 2018

@TaoLv All previous tests were still performed even with those changes. If I do separate the tests, wouldn't it be a bit confusing if the tests for count_include_pad=True and count_include_pad=False are in 2 different unit tests?

@eric-haibin-lin eric-haibin-lin merged commit f34dafe into apache:master Jun 18, 2018
@haojin2 haojin2 deleted the count_include_pad branch June 18, 2018 18:13
zheng-da pushed a commit to zheng-da/incubator-mxnet that referenced this pull request Jun 28, 2018
* add count_include_pad argument

* add cound_include_pad in cudnn pooling and corresponding tests

* add gluon support for the new option

* switch to built-in functions for artificial NaN

* add doc for gluon

* change isAvg/getAvg to is_avg/get_avg
@marcoabreu
Copy link
Contributor

Hi @haojin2 , I think your PR has made test_pooling_versions flaky. See details at #11517

@haojin2
Copy link
Contributor Author

haojin2 commented Jul 1, 2018

test_2d_pooling('max') the test is failing at max pooling, which does not even hit my code paths.

@marcoabreu
Copy link
Contributor

marcoabreu commented Jul 1, 2018

Ah sorry, I just thought it might be this PR since it was the last one that modified the overall test. I've created an issue to track further investigation.

XinYao1994 pushed a commit to XinYao1994/incubator-mxnet that referenced this pull request Aug 29, 2018
* add count_include_pad argument

* add cound_include_pad in cudnn pooling and corresponding tests

* add gluon support for the new option

* switch to built-in functions for artificial NaN

* add doc for gluon

* change isAvg/getAvg to is_avg/get_avg
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants