From e742e63f10f033c498a4a63a9536c1049aa5a298 Mon Sep 17 00:00:00 2001 From: Junru Shao Date: Tue, 14 May 2019 14:43:18 -0700 Subject: [PATCH 1/4] Initial commit --- python/mxnet/ndarray/ndarray.py | 1 + tests/python/unittest/test_ndarray.py | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/python/mxnet/ndarray/ndarray.py b/python/mxnet/ndarray/ndarray.py index 1c182731c78e..df8bc379562a 100644 --- a/python/mxnet/ndarray/ndarray.py +++ b/python/mxnet/ndarray/ndarray.py @@ -4261,6 +4261,7 @@ def _make_dl_managed_tensor(array): if not ndarray.flags['C_CONTIGUOUS']: raise ValueError("Only c-contiguous arrays are supported for zero-copy") + ndarray.flags['WRITEABLE'] = False c_obj = _make_dl_managed_tensor(ndarray) address = ctypes.addressof(c_obj) address = ctypes.cast(address, ctypes.c_void_p) diff --git a/tests/python/unittest/test_ndarray.py b/tests/python/unittest/test_ndarray.py index c62bd19453d9..55daca47711a 100644 --- a/tests/python/unittest/test_ndarray.py +++ b/tests/python/unittest/test_ndarray.py @@ -1687,8 +1687,10 @@ def test_zero_from_numpy(): mx.test_utils.assert_almost_equal(np_array, mx_array.asnumpy()) np_array = arrays[0] mx_array = mx.nd.from_numpy(np_array) - np_array[2, 1] = 0 - mx.test_utils.assert_almost_equal(np_array, mx_array.asnumpy()) + try: + np_array[2, 1] = 0 + except ValueError: + pass mx_array[2, 1] = 100 mx.test_utils.assert_almost_equal(np_array, mx_array.asnumpy()) np_array = np.array([[1, 2], [3, 4], [5, 6]]).transpose() From e93291d017a4f2e6a72f19765704ddfaf107baf0 Mon Sep 17 00:00:00 2001 From: Junru Shao Date: Sat, 18 May 2019 23:05:13 -0700 Subject: [PATCH 2/4] update --- python/mxnet/ndarray/ndarray.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/python/mxnet/ndarray/ndarray.py b/python/mxnet/ndarray/ndarray.py index df8bc379562a..232589063ff0 100644 --- a/python/mxnet/ndarray/ndarray.py +++ b/python/mxnet/ndarray/ndarray.py @@ -4212,7 +4212,12 @@ def dl_managed_tensor_deleter(dl_managed_tensor_handle): def from_numpy(ndarray, zero_copy=True): - """Returns an MXNet's NDArray backed by Numpy's ndarray. + """Returns an MXNet's ndarray backed by numpy's ndarray. + When `zero_copy` is set to be true, + this API consumes numpy's ndarray and produces MXNet's ndarray + without having to copy the content. In this case, we disallow + users to modify the given numpy ndarray, and it is suggested + not to read the numpy ndarray as well for internal correctness. Parameters ---------- From 3de87dbe1ac7592708e8b4af807c940c6100a08e Mon Sep 17 00:00:00 2001 From: Sheng Zha Date: Sun, 19 May 2019 16:51:41 -0700 Subject: [PATCH 3/4] Update test_ndarray.py --- tests/python/unittest/test_ndarray.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/python/unittest/test_ndarray.py b/tests/python/unittest/test_ndarray.py index 55daca47711a..df505436fa0c 100644 --- a/tests/python/unittest/test_ndarray.py +++ b/tests/python/unittest/test_ndarray.py @@ -1687,10 +1687,8 @@ def test_zero_from_numpy(): mx.test_utils.assert_almost_equal(np_array, mx_array.asnumpy()) np_array = arrays[0] mx_array = mx.nd.from_numpy(np_array) - try: - np_array[2, 1] = 0 - except ValueError: - pass + assertRaises(ValueError, np_array.__setitem__, (2, 1), 0) + mx_array[2, 1] = 100 mx.test_utils.assert_almost_equal(np_array, mx_array.asnumpy()) np_array = np.array([[1, 2], [3, 4], [5, 6]]).transpose() From 4587797522e6d3117cf98b631d681b3c20c9ee82 Mon Sep 17 00:00:00 2001 From: Junru Shao Date: Sun, 19 May 2019 20:07:19 -0700 Subject: [PATCH 4/4] Retrigger