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

fix add_n bug: when input mem overlap with output mem, results is wrong #14889

Merged
merged 3 commits into from
May 7, 2019
Merged

fix add_n bug: when input mem overlap with output mem, results is wrong #14889

merged 3 commits into from
May 7, 2019

Conversation

rongzha1
Copy link
Contributor

@rongzha1 rongzha1 commented May 6, 2019

Description

Fix add_n forword bug
Due to "FInplaceOption" is active, output memory should not set_zero before actually do "add_n" which will make inputs memory set zero too when overlap.
Fix issue for #14858

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

  • Feature1, tests, (and when applicable, API doc)
  • Feature2, tests, (and when applicable, API doc)

Comments

  • If this change is a backward incompatible change, why must this change be made.
  • Interesting edge cases to note here
    @pengzhao-intel @TaoLv

Copy link
Member

@eric-haibin-lin eric-haibin-lin left a comment

Choose a reason for hiding this comment

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

Nice catch! Would you mind adding an unit test to test_ndarray.py? e.g.

x = mx.nd.ones((2,2))
mx.nd.ElemwiseSum(x,x,out=x)

@rongzha1
Copy link
Contributor Author

rongzha1 commented May 6, 2019

Nice catch! Would you mind adding an unit test to test_ndarray.py? e.g.

x = mx.nd.ones((2,2))
mx.nd.ElemwiseSum(x,x,out=x)

Thanks for your advice. Testcase has been added.

Copy link
Contributor

@pengzhao-intel pengzhao-intel left a comment

Choose a reason for hiding this comment

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

LGTM

@sandeep-krishnamurthy please take a review too.

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.

Thanks

@anirudhacharya
Copy link
Member

@mxnet-label-bot add [pr-awaiting-merge]

@marcoabreu marcoabreu added the pr-awaiting-merge Review and CI is complete. Ready to Merge label May 6, 2019
Copy link
Member

@yuxihu yuxihu left a comment

Choose a reason for hiding this comment

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

Nice catch! LGTM!

Copy link
Member

@TaoLv TaoLv left a comment

Choose a reason for hiding this comment

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

Minor comments.

@@ -207,7 +207,10 @@ void ElementwiseSumContainsDnsImpl(mshadow::Stream<cpu>* s,
using namespace mxnet::op::mxnet_op;
const TBlob& out_data = out->data();
MSHADOW_TYPE_SWITCH(out->dtype(), DType, { // data type
Kernel<set_zero, cpu>::Launch(s, out_data.Size(), out_data.dptr<DType>());
// Do not set_zero if output mem inplace with input mem: elemwise_sum.cc FInplaceOption
Copy link
Member

Choose a reason for hiding this comment

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

Make this comment easy to understand. Add comment that output can be in-placed with the first input.

@@ -8284,6 +8284,18 @@ def check_concat(shape1, shape2, axis):
check_concat((8, 0, 0), (8, 0, 0), 2)


@with_seed()
def test_elemwise_sum_add_n():
Copy link
Member

Choose a reason for hiding this comment

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

test_add_n() ?

rslt = mx.nd.zeros(shape=data_shape)
for i in range(input_num):
rslt += data[i]
add_n_rslt = mx.nd.add_n(*data,out=data[0])
Copy link
Member

Choose a reason for hiding this comment

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

add a space before out=data[0].

@pengzhao-intel pengzhao-intel merged commit 5bda980 into apache:master May 7, 2019
access2rohit pushed a commit to access2rohit/incubator-mxnet that referenced this pull request May 14, 2019
…ng (apache#14889)

* fix add_n bug: when input mem overlap with output mem, results is wrong

* add testcase for bugfix verification

* add more comments for modification and change testcase name to test_add_n
haohuanw pushed a commit to haohuanw/incubator-mxnet that referenced this pull request Jun 23, 2019
…ng (apache#14889)

* fix add_n bug: when input mem overlap with output mem, results is wrong

* add testcase for bugfix verification

* add more comments for modification and change testcase name to test_add_n
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pr-awaiting-merge Review and CI is complete. Ready to Merge
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

8 participants