haojin2 commented on a change in pull request #17254: [numpy] change unary 
infer type
URL: https://github.com/apache/incubator-mxnet/pull/17254#discussion_r365556490
 
 

 ##########
 File path: tests/python/unittest/test_numpy_op.py
 ##########
 @@ -1865,15 +1890,53 @@ def hybrid_forward(self, F, a, *args, **kwargs):
         'arccosh' : (lambda x: 1./(x**2 - 1.)**(1./2.), 2.0, 5.0),
         'arctanh' : (lambda x: -1./(x**2 - 1.), -0.99, 0.99)
     }
-    if has_tvm_ops():
-        funcs['rad2deg'] = (lambda x: 180. / _np.pi * _np.ones(x.shape), -1.0, 
1.0)
-        funcs['deg2rad'] = (lambda x: _np.pi / 180. * _np.ones(x.shape), -1.0, 
1.0)
+
+    dtypes = ['float16', 'float32', 'float64', 'int8', 'uint8', 'int32', 
'int64', 'bool']
     ndim = random.choice([2, 3, 4])
-    shape = random.choice([rand_shape_nd(ndim, dim=3), (1, 0, 2)])
-    for shape in [rand_shape_nd(ndim, dim=3), (1, 0, 2)]:
-        for func, func_data in funcs.items():
+    i = random.choice([rand_shape_nd(ndim, dim=3), (1, 0, 2)])
+    shapes = [i for i in [rand_shape_nd(ndim, dim=3), (1, 0, 2)]];
+    for func, func_data in funcs.items():
+        for dtype, shape in itertools.product(dtypes, shapes):
+            rtol = 1e-2 if dtype == np.float16 else 1e-3
+            atol = 1e-4 if dtype == np.float16 else 1e-5
             ref_grad, low, high = func_data
-            check_unary_func(func, ref_grad, shape, low, high)
+            # get rid of warning: divide by zero
+            if((func=='log' or func=='log10' or func=='log2') and
+                (dtype=='int8' or dtype=='uint8' or dtype=='int32' or
+                dtype=='int64')):
+                low = 1
+            if (func=='arctanh' and dtype=='bool'):
+                continue
+            np_func = getattr(_np, func)
+            mx_func = TestUnary2(func)
+            np_test_data = _np.random.uniform(low, high, shape).astype(dtype)
+            mx_test_data = mx.numpy.array(np_test_data)
+            for hybridize in [True, False]:
+                if hybridize:
+                    mx_func.hybridize()
+                if ref_grad:
+                    mx_test_data.attach_grad()
+                np_out = np_func(np_test_data)
+                with mx.autograd.record():
+                    y = mx_func(mx_test_data)
+                assert y.shape == np_out.shape
+                assert_almost_equal(y.asnumpy(), np_out, rtol=1e-3, atol=1e-5)
+                if np_out.dtype == np.bool_:
+                    assert y.dtype == np.bool_
+
+            np_out = getattr(_np, func)(np_test_data)
+            mx_out = getattr(mx.np, func)(mx_test_data)
+            assert mx_out.shape == np_out.shape
+            assert_almost_equal(mx_out.asnumpy(), np_out, rtol=1e-3, atol=1e-5)
+
 
 Review comment:
   Please also add checks for backward computation for floating point input 
cases.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to