sxjscience commented on a change in pull request #18277:
URL: https://github.com/apache/incubator-mxnet/pull/18277#discussion_r427795479



##########
File path: tests/python/unittest/test_numpy_op.py
##########
@@ -2660,6 +2660,67 @@ def hybrid_forward(self, F, a, b, *args, **kwargs):
                 check_mixed_precision_binary_func(func, low, high, lshape, 
rshape, lgrad, rgrad, type1, type2)
 
 
+@with_seed()
+@use_np
+def test_np_binary_scalar_funcs():
+    itypes = [np.int8, np.int32, np.int64]
+    def check_binary_scalar_func(func, low, high, lshape, lgrad, ltype, 
scalar_is_int, hybridize):
+        class TestBinaryScalar(HybridBlock):
+            def __init__(self, func, scalar):
+                super(TestBinaryScalar, self).__init__()
+                self._func = func
+                self._scalar = scalar
+
+            def hybrid_forward(self, F, a, *args, **kwargs):
+                return getattr(F.np, self._func)(a, self._scalar)
+
+        np_test_x1 = _np.random.uniform(low, high, lshape).astype(ltype)
+        np_test_x2 = int(_np.random.uniform(low, high)) if scalar_is_int else 
_np.random.uniform(low, high)
+        mx_test_x1 = np.array(np_test_x1, dtype=ltype)
+        mx_test_x2 = np_test_x2
+        np_func = getattr(_np, func)
+        mx_func = TestBinaryScalar(func, np_test_x2)
+        if hybridize:
+            mx_func.hybridize()
+        rtol = 1e-2 if ltype is np.float16 else 1e-3
+        atol = 1e-3 if ltype is np.float16 else 1e-5
+        if ltype not in itypes:
+            if lgrad:
+                mx_test_x1.attach_grad()
+            np_out = np_func(np_test_x1, np_test_x2)
+            with mx.autograd.record():
+                y = mx_func(mx_test_x1, mx_test_x2)
+            assert y.shape == np_out.shape
+            assert_almost_equal(y.asnumpy(), np_out.astype(y.dtype), 
rtol=rtol, atol=atol)
+            if lgrad:
+                y.backward()
+                assert_almost_equal(mx_test_x1.grad.asnumpy(),
+                                    collapse_sum_like(lgrad(y.asnumpy(), 
np_test_x1, np_test_x2), mx_test_x1.shape),
+                                    rtol=1e-1, atol=1e-2, equal_nan=True, 
use_broadcast=False)

Review comment:
       Can we set rtol=`1e-2` of some smaller value?




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


Reply via email to