You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by jx...@apache.org on 2018/03/06 19:13:20 UTC
[incubator-mxnet] branch master updated: [fix issue#9976] The
assignment problem in NDArray (#9981)
This is an automated email from the ASF dual-hosted git repository.
jxie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git
The following commit(s) were added to refs/heads/master by this push:
new e2b1a56 [fix issue#9976] The assignment problem in NDArray (#9981)
e2b1a56 is described below
commit e2b1a563e3a46ec54420c887d3c36e04c11af311
Author: JackieWu <wk...@live.cn>
AuthorDate: Wed Mar 7 03:13:15 2018 +0800
[fix issue#9976] The assignment problem in NDArray (#9981)
* 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
---
include/mxnet/ndarray.h | 4 ++++
src/ndarray/ndarray.cc | 2 +-
tests/python/unittest/test_ndarray.py | 29 +++++++++++++++++++++++++++++
3 files changed, 34 insertions(+), 1 deletion(-)
diff --git a/include/mxnet/ndarray.h b/include/mxnet/ndarray.h
index 67d2a27..e6e7468 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 84328ea..d4a6583 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 0daf74a..e96fb2f 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
--
To stop receiving notification emails like this one, please contact
jxie@apache.org.