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

[fix issue#9976] The assignment problem in NDArray #9981

Merged

Conversation

wkcn
Copy link
Member

@wkcn wkcn commented Mar 3, 2018

Description

Fix #9976 (#9976)

>>> a = mx.nd.array(np.arange(12).reshape((3,4)))
>>> a

[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]]
<NDArray 3x4 @cpu(0)>
>>> a[0]=a[1]     #   HERE
>>> a

[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]]
<NDArray 3x4 @cpu(0)>

Checklist

Essentials

  • Passed code style checking (make lint)
  • 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
  • To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

Changes

Comments

  • Is it necessary to add byte_offset() function for NDArray?
  • Is it necessary to add is_itself() function for NDArray?

@wkcn wkcn requested a review from cjolivier01 as a code owner March 3, 2018 08:38
@marcoabreu
Copy link
Contributor

Hello, could you please add a test for this case?

@wkcn
Copy link
Member Author

wkcn commented Mar 3, 2018

@marcoabreu Yes, I will add it soon :)

@marcoabreu
Copy link
Contributor

@piiswrong

Copy link
Contributor

@reminisce reminisce left a comment

Choose a reason for hiding this comment

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

The last three test cases are not affected by the bug. But there is nothing wrong to add them here.

# assign directly
a_np[0] = a_np[1]
a_nd[0] = a_nd[1]
assert np.allclose(a_np, a_nd.asnumpy())
Copy link
Contributor

Choose a reason for hiding this comment

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

Use assert same(a_np, a_nd.asnumpy()). Same for all others.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you! I will modify it.

a_np[0] = a_np[1]
a_nd[0] = a_nd[1]
assert np.allclose(a_np, a_nd.asnumpy())
assert id(a_nd) == a_nd_id
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the purpose of this check? This seems always true.

Copy link
Member Author

Choose a reason for hiding this comment

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

My mistake. I want to check the memory address of NDArray didn't change.
And assert np.allclose(a_np, a_nd.asnumpy()) means checking issue #9976.

@@ -1099,6 +1099,39 @@ def test_assign_float_value_to_ndarray():
b[0] = a[0]
assert same(a, b.asnumpy())

@with_seed()
def test_ndarray_assignment():
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Add issue link as code comment.
  2. Change the function name to a special one since this is only for testing special cases. General cases have been covered by test_ndarray_indexing.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay. Thanks!

@@ -1128,7 +1128,7 @@ void CopyFromToImpl(const NDArray& from, const NDArray& to,
}

void CopyFromTo(const NDArray& from, const NDArray& to, int priority) {
if (from.var() == to.var()) {
if (from.var() == to.var() && from.byte_offset() == to.byte_offset()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

should we also check for size? @reminisce

Copy link
Contributor

Choose a reason for hiding this comment

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

@piiswrong There is a shape equality check after this. For CopyFromTo, I think the added condition is sufficient. Agree we should also check sizes if it is for a function of checking if two ndarrays are the same one.

@piiswrong
Copy link
Contributor

@marcoabreu retrigger test?

@wkcn
Copy link
Member Author

wkcn commented Mar 5, 2018

@piiswrong Thank you! I will change the code and then trigger test.

@marcoabreu
Copy link
Contributor

@piiswrong you can always retrigger a test by logging in at http://jenkins.mxnet-ci.amazon-ml.com/ (committers only)

@piiswrong piiswrong merged commit e2b1a56 into apache:master Mar 6, 2018
jinhuang415 pushed a commit to jinhuang415/incubator-mxnet that referenced this pull request Mar 30, 2018
* fix the assignment problem in NDArray

* add ndarray assignment test

* fix test_ndarray_assignment()

* fix comparison precision for test_ndarray_assignment

* rename test_ndarray_assignment to test_assign_a_row_to_ndarray and fix the dtype in the test
rahul003 pushed a commit to rahul003/mxnet that referenced this pull request Jun 4, 2018
* fix the assignment problem in NDArray

* add ndarray assignment test

* fix test_ndarray_assignment()

* fix comparison precision for test_ndarray_assignment

* rename test_ndarray_assignment to test_assign_a_row_to_ndarray and fix the dtype in the test
zheng-da pushed a commit to zheng-da/incubator-mxnet that referenced this pull request Jun 28, 2018
* fix the assignment problem in NDArray

* add ndarray assignment test

* fix test_ndarray_assignment()

* fix comparison precision for test_ndarray_assignment

* rename test_ndarray_assignment to test_assign_a_row_to_ndarray and fix the dtype in the test
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.

The assignment problem about NDArray
4 participants