This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
[MXNET-80] Fix average pooling kernel size assignment error #10000
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,15 +46,14 @@ static void PoolingParamParser(nnvm::NodeAttrs *attrs) { | |
if (param.stride.ndim() == 0) param.stride = Shape2(1, 1); | ||
if (param.pad.ndim() == 0) param.pad = Shape2(0, 0); | ||
} else { | ||
CHECK_EQ(param.kernel.ndim(), 3U) << param.kernel.ndim() | ||
<< "D pooling not supported"; | ||
// ignore kernel size only if global_pool not assigned false | ||
if (param.global_pool == false) { | ||
CHECK_EQ(param.kernel.ndim(), 3U) << param.kernel.ndim() | ||
<< "D pooling not supported"; | ||
} | ||
if (param.stride.ndim() == 0) param.stride = Shape3(1, 1, 1); | ||
if (param.pad.ndim() == 0) param.pad = Shape3(0, 0, 0); | ||
} | ||
CHECK_EQ(param.stride.ndim(), param.kernel.ndim()) | ||
<< "stride and kernel should have the same length"; | ||
CHECK_EQ(param.pad.ndim(), param.kernel.ndim()) | ||
<< "pad and kernel should have the same length"; | ||
attrs->parsed = std::move(param); | ||
} | ||
|
||
|
@@ -98,28 +97,37 @@ static bool PoolingShape(const nnvm::NodeAttrs &attrs, | |
<< "Pooling: Input data should be 3D in (batch, channel, x)" | ||
<< " Or 4D in (batch, channel, y, x) " | ||
<< " Or 5D in (batch, channel, d, y, x)"; | ||
CHECK_LE(dshape.ndim(), 5U) | ||
<< "Pooling: Input data should be 3D in (batch, channel, x)" | ||
<< " Or 4D in (batch, channel, y, x) " | ||
<< " Or 5D in (batch, channel, d, y, x)"; | ||
TShape oshape = dshape; | ||
if (dshape.ndim() == 0) return false; | ||
if (param.kernel.ndim() == 1) { | ||
if (param.global_pool) { | ||
for (size_t i{2}; i < dshape.ndim(); i++) | ||
oshape[i] = 1; | ||
out_shape->clear(); | ||
out_shape->push_back(oshape); // save output shape | ||
#if MXNET_USE_MKLDNN == 1 | ||
if (MKLDNNRequireWorkspace(param) && SupportMKLDNNPooling(param)) | ||
out_shape->push_back(oshape); // for workspace | ||
#endif | ||
} else if (param.kernel.ndim() == 1) { | ||
CHECK_EQ(dshape.ndim(), 3U) | ||
<< "Pooling: Input data should be 3D in (batch, channel, x)"; | ||
if (param.global_pool) { | ||
oshape[2] = 1; | ||
CHECK(param.kernel[0] <= dshape[2] + 2 * param.pad[0]) | ||
<< "kernel size (" << param.kernel[0] << ") exceeds input (" | ||
<< dshape[2] << " padded to " << (dshape[2] + 2 * param.pad[0]) | ||
<< ")"; | ||
if (param.pooling_convention == pool_enum::kValid) { | ||
oshape[2] = 1 + | ||
(dshape[2] + 2 * param.pad[0] - param.kernel[0]) / | ||
param.stride[0]; | ||
} else { | ||
CHECK(param.kernel[0] <= dshape[2] + 2 * param.pad[0]) | ||
<< "kernel size (" << param.kernel[0] << ") exceeds input (" | ||
<< dshape[2] << " padded to " << (dshape[2] + 2 * param.pad[0]) | ||
<< ")"; | ||
if (param.pooling_convention == pool_enum::kValid) { | ||
oshape[2] = 1 + | ||
(dshape[2] + 2 * param.pad[0] - param.kernel[0]) / | ||
param.stride[0]; | ||
} else { | ||
oshape[2] = 1 + static_cast<int>(ceil( | ||
static_cast<float>(dshape[2] + 2 * param.pad[0] - | ||
param.kernel[0]) / | ||
param.stride[0])); | ||
} | ||
oshape[2] = 1 + static_cast<int>(ceil( | ||
static_cast<float>(dshape[2] + 2 * param.pad[0] - | ||
param.kernel[0]) / | ||
param.stride[0])); | ||
} | ||
out_shape->clear(); | ||
out_shape->push_back(oshape); // save output shape | ||
|
@@ -130,35 +138,30 @@ static bool PoolingShape(const nnvm::NodeAttrs &attrs, | |
} else if (param.kernel.ndim() == 2) { | ||
CHECK_EQ(dshape.ndim(), 4U) | ||
<< "Pooling: Input data should be 4D in (batch, channel, y, x)"; | ||
if (param.global_pool) { | ||
oshape[2] = 1; | ||
oshape[3] = 1; | ||
CHECK(param.kernel[0] <= dshape[2] + 2 * param.pad[0]) | ||
<< "kernel size (" << param.kernel[0] << ") exceeds input (" | ||
<< dshape[2] << " padded to " << (dshape[2] + 2 * param.pad[0]) | ||
<< ")"; | ||
CHECK(param.kernel[1] <= dshape[3] + 2 * param.pad[1]) | ||
<< "kernel size (" << param.kernel[1] << ") exceeds input (" | ||
<< dshape[3] << " padded to " << (dshape[3] + 2 * param.pad[1]) | ||
<< ")"; | ||
if (param.pooling_convention == pool_enum::kValid) { | ||
oshape[2] = 1 + | ||
(dshape[2] + 2 * param.pad[0] - param.kernel[0]) / | ||
param.stride[0]; | ||
oshape[3] = 1 + | ||
(dshape[3] + 2 * param.pad[1] - param.kernel[1]) / | ||
param.stride[1]; | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ignore this comment. I've misinterpreted the code. |
||
CHECK(param.kernel[0] <= dshape[2] + 2 * param.pad[0]) | ||
<< "kernel size (" << param.kernel[0] << ") exceeds input (" | ||
<< dshape[2] << " padded to " << (dshape[2] + 2 * param.pad[0]) | ||
<< ")"; | ||
CHECK(param.kernel[1] <= dshape[3] + 2 * param.pad[1]) | ||
<< "kernel size (" << param.kernel[1] << ") exceeds input (" | ||
<< dshape[3] << " padded to " << (dshape[3] + 2 * param.pad[1]) | ||
<< ")"; | ||
if (param.pooling_convention == pool_enum::kValid) { | ||
oshape[2] = 1 + | ||
(dshape[2] + 2 * param.pad[0] - param.kernel[0]) / | ||
param.stride[0]; | ||
oshape[3] = 1 + | ||
(dshape[3] + 2 * param.pad[1] - param.kernel[1]) / | ||
param.stride[1]; | ||
} else { | ||
oshape[2] = 1 + static_cast<int>(ceil( | ||
static_cast<float>(dshape[2] + 2 * param.pad[0] - | ||
param.kernel[0]) / | ||
param.stride[0])); | ||
oshape[3] = 1 + static_cast<int>(ceil( | ||
static_cast<float>(dshape[3] + 2 * param.pad[1] - | ||
param.kernel[1]) / | ||
param.stride[1])); | ||
} | ||
oshape[2] = 1 + static_cast<int>(ceil( | ||
static_cast<float>(dshape[2] + 2 * param.pad[0] - | ||
param.kernel[0]) / | ||
param.stride[0])); | ||
oshape[3] = 1 + static_cast<int>(ceil( | ||
static_cast<float>(dshape[3] + 2 * param.pad[1] - | ||
param.kernel[1]) / | ||
param.stride[1])); | ||
} | ||
out_shape->clear(); | ||
out_shape->push_back(oshape); // save output shape | ||
|
@@ -175,35 +178,29 @@ static bool PoolingShape(const nnvm::NodeAttrs &attrs, | |
<< "kernel size exceeds input"; | ||
CHECK_LE(param.kernel[2], dshape[4] + 2 * param.pad[2]) | ||
<< "kernel size exceeds input"; | ||
if (param.global_pool) { | ||
oshape[2] = 1; | ||
oshape[3] = 1; | ||
oshape[4] = 1; | ||
if (param.pooling_convention == pool_enum::kValid) { | ||
oshape[2] = 1 + | ||
(dshape[2] + 2 * param.pad[0] - param.kernel[0]) / | ||
param.stride[0]; | ||
oshape[3] = 1 + | ||
(dshape[3] + 2 * param.pad[1] - param.kernel[1]) / | ||
param.stride[1]; | ||
oshape[4] = 1 + | ||
(dshape[4] + 2 * param.pad[2] - param.kernel[2]) / | ||
param.stride[2]; | ||
} else { | ||
if (param.pooling_convention == pool_enum::kValid) { | ||
oshape[2] = 1 + | ||
(dshape[2] + 2 * param.pad[0] - param.kernel[0]) / | ||
param.stride[0]; | ||
oshape[3] = 1 + | ||
(dshape[3] + 2 * param.pad[1] - param.kernel[1]) / | ||
param.stride[1]; | ||
oshape[4] = 1 + | ||
(dshape[4] + 2 * param.pad[2] - param.kernel[2]) / | ||
param.stride[2]; | ||
} else { | ||
oshape[2] = 1 + static_cast<int>(ceil( | ||
static_cast<float>(dshape[2] + 2 * param.pad[0] - | ||
param.kernel[0]) / | ||
param.stride[0])); | ||
oshape[3] = 1 + static_cast<int>(ceil( | ||
static_cast<float>(dshape[3] + 2 * param.pad[1] - | ||
param.kernel[1]) / | ||
param.stride[1])); | ||
oshape[4] = 1 + static_cast<int>(ceil( | ||
static_cast<float>(dshape[4] + 2 * param.pad[2] - | ||
param.kernel[2]) / | ||
param.stride[2])); | ||
} | ||
oshape[2] = 1 + static_cast<int>(ceil( | ||
static_cast<float>(dshape[2] + 2 * param.pad[0] - | ||
param.kernel[0]) / | ||
param.stride[0])); | ||
oshape[3] = 1 + static_cast<int>(ceil( | ||
static_cast<float>(dshape[3] + 2 * param.pad[1] - | ||
param.kernel[1]) / | ||
param.stride[1])); | ||
oshape[4] = 1 + static_cast<int>(ceil( | ||
static_cast<float>(dshape[4] + 2 * param.pad[2] - | ||
param.kernel[2]) / | ||
param.stride[2])); | ||
} | ||
|
||
out_shape->clear(); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realized that we need to change the order of the DMLC_DECLARE_FIELD here. In the original version, the parameters that do not have default values will be set first and then goes the params with default values. So the order will be
kernel
,pool_type
,global_pool
,cudnn_off
, ... After we add default values tokernel
andpool_type
, the order becomesglobal_pool
,cudnn_off
,kernel
,pool_type
, ... Thus, the way to solve the problem is to change the order:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I see in the original version that the order is: global_pool(false), cudnn_off(false), kernel(no default value), pool_type(no default value), ....
In the original version, "kernel" and "pool_type" do not have default value, but they go after "global_pool" and "cudnn_off" which are with default values.
https://github.com/apache/incubator-mxnet/blob/94f68fc8fd21611b7f5c148cb0e5d134efe58f87/src/operator/nn/pooling-inl.h#L59
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, maybe I have misunderstood what you said, I will have a try.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried a few times, and it failed at this position:
https://github.com/apache/incubator-mxnet/blob/94f68fc8fd21611b7f5c148cb0e5d134efe58f87/src/operator/nn/pooling.cc#L55
But I do not understand why it requires stride and kernel have the same length.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no need to check this if global_pool is turned on.