sxjscience commented on a change in pull request #15851: [Numpy] Numpy copysign URL: https://github.com/apache/incubator-mxnet/pull/15851#discussion_r324474508
########## File path: tests/python/unittest/test_numpy_op.py ########## @@ -1853,6 +1853,111 @@ def hybrid_forward(self, F, x): assert_almost_equal(mx_ret.asnumpy(), np_ret, atol=1e-5, rtol=1e-4) +@with_seed() +@use_np +def test_np_copysign(): + class TestCopysign(HybridBlock): + def __init__(self): + super(TestCopysign, self).__init__() + + def hybrid_forward(self, F, a1, a2): + return F.np.copysign(a1, a2) + + def get_grad(a1, a2): + sign = _np.logical_or(_np.logical_and(a1 < 0, a2 < 0), + _np.logical_and(a1 >= 0, a2 >= 0)) + sign = 2 * sign.astype(int) - 1 + sign = sign.reshape(-1, *a1.shape) + sign = _np.sum(sign, axis=0) + return sign, _np.zeros_like(a2) + + def get_grad_left(a1, a2): + sign = _np.logical_or(_np.logical_and(a1 < 0, a2 < 0), + _np.logical_and(a1 >= 0, a2 >= 0)) + sign = 2 * sign.astype(int) - 1 + sign = sign.reshape(a1.shape) + return sign + + def get_grad_right(a1, a2): + return _np.zeros_like(a2) + + shapes = [ + (), + (1), + (2, 1), + (3, 2, 1), + (4, 3, 2, 1), + (2, 4, 3, 2, 1) + ] + types = ['float16', 'float32', 'float64', 'int8', 'int32', 'int64'] + for a1shape in shapes: + for a2shape in shapes: + for hybridize in [True, False]: + for dtype in types: + test_copysign = TestCopysign() + if hybridize: + test_copysign.hybridize() + rtol = 1e-3 + atol = 1e-5 + a1_np = _np.array(_np.random.uniform(-1.0, 1.0, a1shape), dtype=dtype) + a2_np = _np.array(_np.random.uniform(-1.0, 1.0, a2shape), dtype=dtype) + a1 = np.array(a1_np, dtype=dtype) + a2 = np.array(a2_np, dtype=dtype) + a1.attach_grad() + a2.attach_grad() + expected_np = _np.copysign(a1_np, a2_np) + with mx.autograd.record(): + mx_out = test_copysign(a1, a2) + assert mx_out.shape == expected_np.shape Review comment: Test if mx_out.dtype matches expected_np.dtype. I think we can later add a utility to match the shape + dtype. ---------------------------------------------------------------- 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