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

[MXNET-599] Partial shape infer for Slice #11406

Merged
merged 11 commits into from
Jul 23, 2018
Merged

Conversation

rahul003
Copy link
Member

@rahul003 rahul003 commented Jun 26, 2018

Description

Fix for #11349
Allow partial shape inference of slice operator

Checklist

Essentials

Please feel free to remove inapplicable items for your PR.

  • The PR title starts with [MXNET-$JIRA_ID], where $JIRA_ID refers to the relevant JIRA issue created (except PRs with tiny changes)
  • 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

  • Compute shape based on known info

@sandeep-krishnamurthy @reminisce Please review, thanks!

@@ -674,13 +675,23 @@ inline void SetSliceOpOutputDimSize(const index_t i, const int b,
const int e, const int s,
TShape* oshape) {
if (s > 0) {
CHECK_LT(b, e) << "slicing with begin=[" << i << "]=" << b << ", end[" << i << "]="
CHECK_LE(b, e) << "slicing with begin=[" << i << "]=" << b << ", end[" << i << "]="
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the case b = e required?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, now when shape of a dim is not known, b==e==0

// for partial shape infer
(*oshape)[i] = 0;
} else {
(*oshape)[i] = (e - b - 1) / s + 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should only fill in output shape dim size when the corresponding input shape dim size is non-zero.
That is if input shape is (0, 20), begin=(0, 0), end=(5, 10), it should return (0, 10), instead of (5, 10) as inferred output shape.
@sandeep-krishnamurthy Could you confirm?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes that is correct. Even if one of the shape is unknown (0) we cannot/should not infer output shape.

@@ -690,9 +701,10 @@ inline bool SliceOpShape(const nnvm::NodeAttrs& attrs,
CHECK_EQ(in_attrs->size(), 1U);
CHECK_EQ(out_attrs->size(), 1U);
const TShape& dshape = (*in_attrs)[0];
if (dshape.ndim() == 0 || dshape.Size() == 0) return false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we are removing one of the checks, please add

return !shape_is_none(dshape) && !shape_is_none(oshape)

at the end of this function.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added

@sandeep-krishnamurthy
Copy link
Contributor

sandeep-krishnamurthy commented Jun 28, 2018

Thanks a lot @rahul003 - This is very useful fix for all Keras-MXNet users using slice operation. Overall looks good to me, except 1 change that Jun mentioned about unknown dimension should be unknown.

Copy link
Contributor

@sandeep-krishnamurthy sandeep-krishnamurthy left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you. Verified with Keras-MXNet also.
@reminisce - Requesting you to kindly review the updates :-)

@rahul003
Copy link
Member Author

rahul003 commented Jul 3, 2018

Updated the PR with suggested changes to set as 0 when axis is unknown.

Summary of changes to help review:
If length known, previous logic, else new logic to set them to 0.

@@ -5740,6 +5740,30 @@ def test_slice_forward_backward(a, index):
slice_sym = mx.sym.slice(data, begin=[0, None], end=[1, None], step=[2, -1])
check_numeric_gradient(slice_sym, [in_data])

def test_slice_partial_infer():
var1 = mx.sym.var(name="data", shape=(0,20))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you modulize the test code? For example:

def check_slice_partial_shape_infer(data, begin, end, step, expected_out_shape):
    out = mx.sym.slice(data, begin, end, step)
    assert out.infer_shape_partial()[1][0] == expected_out_shape

check_slice_partial_shape_infer(var1, (None, None), (None, 10), None, (0, 10))
...

@rahul003
Copy link
Member Author

@reminisce Made the requested changes and added a couple more tests. Please check. Thanks!

@reminisce reminisce merged commit 38282e9 into apache:master Jul 23, 2018
@sandeep-krishnamurthy
Copy link
Contributor

Thank you very much @rahul003 and @reminisce

XinYao1994 pushed a commit to XinYao1994/incubator-mxnet that referenced this pull request Aug 29, 2018
* slice change

* add tests

* add slice axis test

* update check

* Whitespace

* bring back a check for ndim

* trigger ci

* update case when axis can't be inferred

* test failure

* refactor and add more tests

* fix lint
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

3 participants