haojin2 commented on a change in pull request #15349: Numpy Tensordot Operator 
URL: https://github.com/apache/incubator-mxnet/pull/15349#discussion_r302263201
 
 

 ##########
 File path: tests/python/unittest/test_numpy_op.py
 ##########
 @@ -26,7 +26,151 @@
 from mxnet.test_utils import check_numeric_gradient
 from common import assertRaises, with_seed
 import random
+import collections
 
+@with_seed()
+@npx.use_np_shape
+def test_np_tensordot():
+    class TestTensordot(HybridBlock):
+        def __init__(self, axes):
+            super(TestTensordot, self).__init__()
+            self._axes = axes
+            
+        def hybrid_forward(self, F, a, b):
+            return F.np.tensordot(a, b, self._axes)
+
+    def tensordot_backward(a, b, axes = 2):
+        if (a.ndim < 1) or (b.ndim < 1):
+            raise ValueError('An input is zero-dim')
+
+        if isinstance(axes, collections.abc.Sequence):
+            if len(axes) != 2:
+                raise ValueError('Axes must consist of two arrays.')
+            a_axes_summed, b_axes_summed = axes
+            if _np.isscalar(a_axes_summed):
+                a_axes_summed = a_axes_summed,
+            if _np.isscalar(b_axes_summed):
+                b_axes_summed = b_axes_summed,
+        else:
+            a_axes_summed = [i + a.ndim - axes for i in range(axes)]
+            b_axes_summed = [i for i in range(axes)]
+
+        if len(a_axes_summed) != len(b_axes_summed):
+            raise ValueError('Axes length mismatch') 
+
+        a_axes_remained = []
+        for i in range(a.ndim):
+            if not (i in a_axes_summed):
+                a_axes_remained.append(i)
+        a_axes = a_axes_remained[:] + a_axes_summed[:]
+
+        b_axes_remained = []
+        for i in range(b.ndim):
+            if not (i in b_axes_summed):
+                b_axes_remained.append(i)
+        b_axes = b_axes_summed[:] + b_axes_remained[:]
+        
+        ad1 = _np.prod([a.shape[i] for i in a_axes_remained]) if 
len(a_axes_remained) > 0 else 1
+        ad2 = _np.prod([a.shape[i] for i in a_axes_summed]) if 
len(a_axes_summed) > 0 else 1
+        bd1 = _np.prod([b.shape[i] for i in b_axes_summed]) if 
len(b_axes_summed) > 0 else 1
+        bd2 = _np.prod([b.shape[i] for i in b_axes_remained]) if 
len(b_axes_remained) > 0 else 1
+        
+        out_grad = _np.ones((ad1, bd2))
+
+        new_a = _np.transpose(a, a_axes)
+        new_a_shape = new_a.shape[:]
+        new_a = new_a.reshape((ad1, ad2)) 
+        new_b = _np.transpose(b, b_axes) 
+        new_b_shape = new_b.shape[:]
+        new_b = new_b.reshape((bd1, bd2))
+        
+        reverse_a_axes = [0 for i in a_axes]
+        for i in range(len(a_axes)):
+            reverse_a_axes[a_axes[i]] = i
+            
+        reverse_b_axes = [0 for i in b_axes]
+        for i in range(len(b_axes)):
+            reverse_b_axes[b_axes[i]] = i
+
+        grad_b = _np.dot(new_a.T, out_grad).reshape(new_b_shape)
+        grad_b = _np.transpose(grad_b, reverse_b_axes)
+        grad_a = _np.dot(out_grad, new_b.T).reshape(new_a_shape)
+        grad_a = _np.transpose(grad_a, reverse_a_axes)
+        
+        return [grad_a, grad_b]
+
+    # test non zero size input
+    tensor_shapes = [ 
+        ((3, 5), (5, 4), 1),  # (a_shape, b_shape, axes)
+        ((3,), (3,), 1),       
+        ((3, 4, 5, 6, 7), (5, 6, 7, 1, 2), 3),
+        ((3, 5, 4, 6, 7), (7, 6, 5, 1, 2), [[1, 3, 4], [2, 1, 0]]),
+        ((2, 2), (2, 2), 2),
+        ((3, 5, 4), (5, ), [[1], [0]]),  
+        ((2,), (2, 3), 1),
+        ((3,), (3,), 0),
+        ((2,), (2, 3), 0),
+        ((3, 5, 4), (5, ), 0)
+    ]
+    
+    for hybridize in [True, False]:
+        for a_shape, b_shape, axes in tensor_shapes:
+            for dtype in [_np.float32, _np.float64]:
+                test_tensordot = TestTensordot(axes)
+                if hybridize:
+                    test_tensordot.hybridize()
+                a = rand_ndarray(shape = a_shape, dtype = 
dtype).as_np_ndarray() 
+                b = rand_ndarray(shape = b_shape, dtype = 
dtype).as_np_ndarray() 
+                a.attach_grad()
+                b.attach_grad()
+
+                np_out = _np.tensordot(a.asnumpy(), b.asnumpy(), axes)
+                with mx.autograd.record():
+                    mx_out = test_tensordot(a, b)               
+                assert mx_out.shape == np_out.shape
+                assert_almost_equal(mx_out.asnumpy(), np_out, rtol = 1e-3, 
atol = 1e-5)
+                mx_out.backward()
+                np_backward = tensordot_backward(a.asnumpy(), b.asnumpy(), 
axes)
+                assert_almost_equal(a.grad.asnumpy(), np_backward[0], rtol = 
1e-3, atol=1e-5)
+                assert_almost_equal(b.grad.asnumpy(), np_backward[1], rtol = 
1e-3, atol=1e-5)
+
+                # Test imperative once again
+                mx_out = np.tensordot(a, b, axes)
+                np_out = _np.tensordot(a.asnumpy(), b.asnumpy(), axes)
+                assert_almost_equal(mx_out.asnumpy(), np_out, rtol=1e-3, 
atol=1e-5)
+                
+                # test numeric gradient
+                a_sym = mx.sym.Variable("a").as_np_ndarray()
+                b_sym = mx.sym.Variable("b").as_np_ndarray()
+                mx_sym = mx.sym.np.tensordot(a_sym, b_sym, 
axes).as_nd_ndarray()
+                check_numeric_gradient(mx_sym, {"a": a.as_nd_ndarray(), "b": 
b.as_nd_ndarray()},
+                  rtol=1e-2, atol=1e-2, dtype = dtype)
+
+    # test zero size input
+    zero_shapes = [
+        ((3, 0), (0, 5), 1),    
+        ((0, 3), (3, 5), 1)    
+    ]
+    
+    for hybridize in [True, False]:
+        for a_shape, b_shape, axes in zero_shapes:
+            for dtype in [_np.float32, _np.float64]:
+                test_tensordot = TestTensordot(axes)
+                if hybridize:
+                    test_tensordot.hybridize()
+                a = rand_ndarray(shape = a_shape, dtype = 
dtype).as_np_ndarray() 
+                b = rand_ndarray(shape = b_shape, dtype = 
dtype).as_np_ndarray() 
+
+                np_out = _np.tensordot(a.asnumpy(), b.asnumpy(), axes)
+                with mx.autograd.record():
+                    mx_out = test_tensordot(a, b)               
+                assert mx_out.shape == np_out.shape
+                assert_almost_equal(mx_out.asnumpy(), np_out, rtol = 1e-3, 
atol = 1e-5)
+                
+                # Test imperative once again
+                mx_out = np.tensordot(a, b, axes)
+                np_out = _np.tensordot(a.asnumpy(), b.asnumpy(), axes)
+                assert_almost_equal(mx_out.asnumpy(), np_out, rtol=1e-3, 
atol=1e-5)
 
 Review comment:
   One more blank line below.

----------------------------------------------------------------
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