diff --git a/include/mxnet/ndarray.h b/include/mxnet/ndarray.h index 67d2a278916b..e6e7468a1f46 100644 --- a/include/mxnet/ndarray.h +++ b/include/mxnet/ndarray.h @@ -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 diff --git a/src/ndarray/ndarray.cc b/src/ndarray/ndarray.cc index 84328ea1cf8a..d4a6583254bb 100644 --- a/src/ndarray/ndarray.cc +++ b/src/ndarray/ndarray.cc @@ -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; } diff --git a/tests/python/unittest/test_ndarray.py b/tests/python/unittest/test_ndarray.py index 0daf74a88799..e96fb2f272df 100644 --- a/tests/python/unittest/test_ndarray.py +++ b/tests/python/unittest/test_ndarray.py @@ -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