haojin2 commented on a change in pull request #17254: [numpy] change unary
infer type
URL: https://github.com/apache/incubator-mxnet/pull/17254#discussion_r364604576
##########
File path: tests/python/unittest/test_numpy_op.py
##########
@@ -1865,15 +1889,54 @@ 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)
+
+ assertRaises(NotImplementedError, getattr(np, func), mx_test_data,
where=False)
+ assertRaises(NotImplementedError, getattr(np, func), mx_test_data,
subok=False)
+ assertRaises(NotImplementedError, getattr(np, func), mx_test_data,
dtype=_np.int8)
+ assertRaises(TypeError, getattr(np, func), mx_test_data,
dtype="abcdefg")
+ assertRaises(NotImplementedError, getattr(np, func), mx_test_data,
casting='safe')
+ assertRaises(TypeError, getattr(np, func), mx_test_data,
casting='mxnet')
+ assertRaises(NotImplementedError, getattr(np, func), mx_test_data,
order='C')
+ assertRaises(NotImplementedError, getattr(np, func), mx_test_data,
order='mxnet')
+
Review comment:
get rid of this blank line.
----------------------------------------------------------------
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