ckt624 commented on a change in pull request #15349: Numpy Tensordot Operator URL: https://github.com/apache/incubator-mxnet/pull/15349#discussion_r302852496
########## File path: tests/python/unittest/test_numpy_op.py ########## @@ -26,6 +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]: Review comment: Don't support fp16 at CPU. ---------------------------------------------------------------- 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