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

Commit

Permalink
[fix issue#9976] The assignment problem in NDArray (#9981)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
wkcn authored and piiswrong committed Mar 6, 2018
1 parent 7d08810 commit e2b1a56
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions include/mxnet/ndarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ class NDArray {
inline Engine::VarHandle var() const {
return ptr_->var;
}
/*! \return byte offset in chunk of the ndarray*/
inline size_t byte_offset() const {
return byte_offset_;
}
/*!
* \brief save the content into binary stream
* \param strm the output stream
Expand Down
2 changes: 1 addition & 1 deletion src/ndarray/ndarray.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
// skip to copy to itself
return;
}
Expand Down
29 changes: 29 additions & 0 deletions tests/python/unittest/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,35 @@ def test_assign_float_value_to_ndarray():
b[0] = a[0]
assert same(a, b.asnumpy())

@with_seed()
def test_assign_a_row_to_ndarray():
"""Test case from https://github.com/apache/incubator-mxnet/issues/9976"""
H, W = 10, 10
dtype = np.float32
a_np = np.random.random((H, W)).astype(dtype)
a_nd = mx.nd.array(a_np)

# assign directly
a_np[0] = a_np[1]
a_nd[0] = a_nd[1]
assert same(a_np, a_nd.asnumpy())

# assign a list
v = np.random.random(W).astype(dtype).tolist()
a_np[1] = v
a_nd[1] = v
assert same(a_np, a_nd.asnumpy())

# assign a np.ndarray
v = np.random.random(W).astype(dtype)
a_np[2] = v
a_nd[2] = v
assert same(a_np, a_nd.asnumpy())

# assign by slice
a_np[0, :] = a_np[1]
a_nd[0, :] = a_nd[1]
assert same(a_np, a_nd.asnumpy())

if __name__ == '__main__':
import nose
Expand Down

0 comments on commit e2b1a56

Please sign in to comment.