zoeygxy commented on a change in pull request #15942: Refines NDArray indexing 
and adds numpy ndarray indexing [READY FOR REVIEW]
URL: https://github.com/apache/incubator-mxnet/pull/15942#discussion_r317962266
 
 

 ##########
 File path: tests/python/unittest/test_numpy_ndarray.py
 ##########
 @@ -421,208 +468,217 @@ def assert_same(np_array, np_index, mx_array, 
mx_index, mx_value, np_value=None)
                     np_index.append(idx)
             np_index = tuple(np_index)
 
-        mx_array = np.array(np_array, dtype=np_array.dtype)
-        np_array = mx_array.asnumpy()
-        indexed_array_shape = np_array[np_index].shape
-        np_indexed_array = _np.random.randint(low=-10000, high=0, 
size=indexed_array_shape)
-        # test value is a numpy array without broadcast
-        assert_same(np_array, np_index, mx_array, index, np_indexed_array)
-        # test value is an numeric_type
-        assert_same(np_array, np_index, mx_array, index, 
_np.random.randint(low=-10000, high=0))
-        if len(indexed_array_shape) > 1:
-            # test ndarray with broadcast
-            assert_same(np_array, np_index, mx_array, index,
-                        _np.random.uniform(low=-10000, high=0, 
size=(indexed_array_shape[-1],)))
-            # test numpy array with broadcast
-            assert_same(np_array, np_index, mx_array, index,
-                        _np.random.randint(low=-10000, high=0, 
size=(indexed_array_shape[-1],)))
-            # test list with broadcast
-            assert_same(np_array, np_index, mx_array, index,
-                        [_np.random.randint(low=-10000, high=0)] * 
indexed_array_shape[-1])
+        mx_array = np.array(np_array, dtype=np_array.dtype)  # mxnet.np.ndarray
+        np_array = mx_array.asnumpy()  # native numpy array
+        if is_scalar:
+            # test value is a numeric type
+            assert_same(np_array, np_index, mx_array, index, 
_np.random.randint(low=-10000, high=0))
+            value_nd = [_np.random.randint(low=-10000, high=0)]
+            assert_same(np_array, np_index, mx_array, index, value_nd, 
value_nd[0])
+        else:
+            indexed_array_shape = np_array[np_index].shape
+            np_indexed_array = _np.random.randint(low=-10000, high=0, 
size=indexed_array_shape)
+            # test value is a native numpy array without broadcast
+            assert_same(np_array, np_index, mx_array, index, np_indexed_array)
+            # test value is a mxnet numpy array without broadcast
+            assert_same(np_array, np_index, mx_array, index, 
np.array(np_indexed_array))
+            # test value is an numeric_type
+            assert_same(np_array, np_index, mx_array, index, 
_np.random.randint(low=-10000, high=0))
+            if len(indexed_array_shape) > 1:
+                np_value = _np.random.randint(low=-10000, high=0, 
size=(indexed_array_shape[-1],))
+                # test mxnet ndarray with broadcast
+                assert_same(np_array, np_index, mx_array, index, 
np.array(np_value))
+                # test native numpy array with broadcast
+                assert_same(np_array, np_index, mx_array, index, np_value)
+                # test list with broadcast
+                assert_same(np_array, np_index, mx_array, index,
+                            [_np.random.randint(low=-10000, high=0)] * 
indexed_array_shape[-1])
 
     def test_getitem_autograd(np_array, index):
+        """
+        np_array: native numpy array.
+        """
         x = np.array(np_array, dtype=np_array.dtype)
         x.attach_grad()
-        with autograd.record():
+        with mx.autograd.record():
             y = x[index]
+        if not isinstance(y, np.ndarray):
+            return
         y.backward()
         value = np.ones_like(y)
         x_grad = np.zeros_like(x)
         x_grad[index] = value
         assert same(x_grad.asnumpy(), x.grad.asnumpy())
 
     def test_setitem_autograd(np_array, index):
+        """
+        np_array: native numpy array.
+        """
         x = np.array(np_array, dtype=np_array.dtype)
+        if not isinstance(x[index], np.ndarray):
+            return  # x[index] is scalar
         out_shape = x[index].shape
         y = np.array(_np.random.uniform(size=out_shape))
         y.attach_grad()
         try:
-            with autograd.record():
+            with mx.autograd.record():
                 x[index] = y
-                assert False  # should not reach here
+                x.backward()
+                y_grad = np.ones_like(y)
+                assert same(y_grad.asnumpy(), y.grad.asnumpy())
         except mx.base.MXNetError as err:
             assert str(err).find('Inplace operations (+=, -=, x[:]=, etc) are 
not supported when recording with') != -1
 
-    def np_int(index, int_type=_np.int32):
-        def convert(num):
-            if num is None:
-                return num
-            else:
-                return int_type(num)
-
-        if isinstance(index, slice):
-            return slice(convert(index.start), convert(index.stop), 
convert(index.step))
-        elif isinstance(index, tuple):  # tuple of slices and integers
-            ret = []
-            for elem in index:
-                if isinstance(elem, slice):
-                    ret.append(slice(convert(elem.start), convert(elem.stop), 
convert(elem.step)))
-                else:
-                    ret.append(convert(elem))
-            return tuple(ret)
-        else:
-            assert False
-
     shape = (8, 16, 9, 9)
-    np_array = _np.arange(_np.prod(shape), dtype='int32').reshape(shape)
+    np_array = _np.arange(_np.prod(_np.array(shape)), 
dtype='int32').reshape(shape)  # native np array
     index_list = [
-        (),
-        0,
-        _np.int32(0),
-        _np.int64(0),
-        5,
-        _np.int32(5),
-        _np.int64(5),
-        -1,
-        _np.int32(-1),
-        _np.int64(-1),
-        slice(5),
-        np_int(slice(5), _np.int32),
-        np_int(slice(5), _np.int64),
-        slice(1, 5),
-        np_int(slice(1, 5), _np.int32),
-        np_int(slice(1, 5), _np.int64),
-        slice(1, 5, 2),
-        np_int(slice(1, 5, 2), _np.int32),
-        np_int(slice(1, 5, 2), _np.int64),
-        slice(7, 0, -1),
-        np_int(slice(7, 0, -1)),
-        np_int(slice(7, 0, -1), _np.int64),
-        slice(None, 6),
-        np_int(slice(None, 6)),
-        np_int(slice(None, 6), _np.int64),
-        slice(None, 6, 3),
-        np_int(slice(None, 6, 3)),
-        np_int(slice(None, 6, 3), _np.int64),
-        slice(1, None),
-        np_int(slice(1, None)),
-        np_int(slice(1, None), _np.int64),
-        slice(1, None, 3),
-        np_int(slice(1, None, 3)),
-        np_int(slice(1, None, 3), _np.int64),
-        slice(None, None, 2),
-        np_int(slice(None, None, 2)),
-        np_int(slice(None, None, 2), _np.int64),
-        slice(None, None, -1),
-        np_int(slice(None, None, -1)),
-        np_int(slice(None, None, -1), _np.int64),
-        slice(None, None, -2),
-        np_int(slice(None, None, -2), _np.int32),
-        np_int(slice(None, None, -2), _np.int64),
-        (slice(None), slice(None), 1, 8),
-        (slice(None), slice(None), -1, 8),
-        (slice(None), slice(None), 1, -8),
-        (slice(None), slice(None), -1, -8),
-        np_int((slice(None), slice(None), 1, 8)),
-        np_int((slice(None), slice(None), 1, 8), _np.int64),
-        (slice(None), slice(None), 1, 8),
-        np_int((slice(None), slice(None), -1, -8)),
-        np_int((slice(None), slice(None), -1, -8), _np.int64),
-        (slice(None), 2, slice(1, 5), 1),
-        np_int((slice(None), 2, slice(1, 5), 1)),
-        np_int((slice(None), 2, slice(1, 5), 1), _np.int64),
-        (1, 2, 3),
-        np_int((1, 2, 3)),
-        np_int((1, 2, 3), _np.int64),
-        (-1, -2, -3),
-        np_int((-1, -2, -3)),
-        np_int((-1, -2, -3), _np.int64),
-        (1, 2, 3, 4),
-        np_int((1, 2, 3, 4)),
-        np_int((1, 2, 3, 4), _np.int64),
-        (-4, -3, -2, -1),
-        np_int((-4, -3, -2, -1)),
-        np_int((-4, -3, -2, -1), _np.int64),
-        (slice(None, None, -1), 2, slice(1, 5), 1),
-        np_int((slice(None, None, -1), 2, slice(1, 5), 1)),
-        np_int((slice(None, None, -1), 2, slice(1, 5), 1), _np.int64),
-        (slice(None, None, -1), 2, slice(1, 7, 2), 1),
-        np_int((slice(None, None, -1), 2, slice(1, 7, 2), 1)),
-        np_int((slice(None, None, -1), 2, slice(1, 7, 2), 1), _np.int64),
-        (slice(1, 8, 2), slice(14, 2, -2), slice(3, 8), slice(0, 7, 3)),
-        np_int((slice(1, 8, 2), slice(14, 2, -2), slice(3, 8), slice(0, 7, 
3))),
-        np_int((slice(1, 8, 2), slice(14, 2, -2), slice(3, 8), slice(0, 7, 
3)), _np.int64),
-        (slice(1, 8, 2), 1, slice(3, 8), 2),
-        np_int((slice(1, 8, 2), 1, slice(3, 8), 2)),
-        np_int((slice(1, 8, 2), 1, slice(3, 8), 2), _np.int64),
-        [1],
-        [1, 2],
-        [2, 1, 3],
-        [7, 5, 0, 3, 6, 2, 1],
-        _np.array([6, 3], dtype=_np.int32),
-        _np.array([[3, 4], [0, 6]], dtype=_np.int32),
-        _np.array([[7, 3], [2, 6], [0, 5], [4, 1]], dtype=_np.int32),
-        _np.array([[7, 3], [2, 6], [0, 5], [4, 1]], dtype=_np.int64),
-        _np.array([[2], [0], [1]], dtype=_np.int32),
-        _np.array([[2], [0], [1]], dtype=_np.int64),
-        np.array([4, 7], dtype=_np.int32),
-        np.array([4, 7], dtype=_np.int64),
-        np.array([[3, 6], [2, 1]], dtype=_np.int32),
-        np.array([[3, 6], [2, 1]], dtype=_np.int64),
-        np.array([[7, 3], [2, 6], [0, 5], [4, 1]], dtype=_np.int32),
-        np.array([[7, 3], [2, 6], [0, 5], [4, 1]], dtype=_np.int64),
-        (1, [2, 3]),
-        (1, [2, 3], _np.array([[3], [0]], dtype=_np.int32)),
-        (1, [2, 3]),
-        (1, [2, 3], _np.array([[3], [0]], dtype=_np.int64)),
-        (1, [2], _np.array([[5], [3]], dtype=_np.int32), slice(None)),
-        (1, [2], _np.array([[5], [3]], dtype=_np.int64), slice(None)),
-        (1, [2, 3], _np.array([[6], [0]], dtype=_np.int32), slice(2, 5)),
-        (1, [2, 3], _np.array([[6], [0]], dtype=_np.int64), slice(2, 5)),
-        (1, [2, 3], _np.array([[4], [7]], dtype=_np.int32), slice(2, 5, 2)),
-        (1, [2, 3], _np.array([[4], [7]], dtype=_np.int64), slice(2, 5, 2)),
-        (1, [2], _np.array([[3]], dtype=_np.int32), slice(None, None, -1)),
-        (1, [2], _np.array([[3]], dtype=_np.int64), slice(None, None, -1)),
-        (1, [2], _np.array([[3]], dtype=_np.int32), np.array([[5, 7], [2, 4]], 
dtype=_np.int64)),
-        (1, [2], np.array([[4]], dtype=_np.int32), np.array([[1, 3], [5, 7]], 
dtype='int64')),
-        [0],
-        [0, 1],
-        [1, 2, 3],
-        [2, 0, 5, 6],
-        ([1, 1], [2, 3]),
-        ([1], [4], [5]),
-        ([1], [4], [5], [6]),
-        ([[1]], [[2]]),
-        ([[1]], [[2]], [[3]], [[4]]),
-        (slice(0, 2), [[1], [6]], slice(0, 2), slice(0, 5, 2)),
-        ([[[[1]]]], [[1]], slice(0, 3), [1, 5]),
-        ([[[[1]]]], 3, slice(0, 3), [1, 3]),
-        ([[[[1]]]], 3, slice(0, 3), 0),
-        ([[[[1]]]], [[2], [12]], slice(0, 3), slice(None)),
-        ([1, 2], slice(3, 5), [2, 3], [3, 4]),
-        ([1, 2], slice(3, 5), (2, 3), [3, 4]),
-        range(4),
-        range(3, 0, -1),
-        (range(4,), [1]),
-        # slice(0, 0) does not support output zero-size tensor yet
+                # Basic indexing
+                # Single int as index
+                (0, False), (np.int32(0), False), (np.int64(0), False),
 
 Review comment:
   Fixed. Thx!

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to