Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

test_np_average #19071

Description

@leezu
[2020-09-01T19:26:55.175Z] =================================== FAILURES ===================================
[2020-09-01T19:26:55.175Z] ____ test_np_average[null-False-True-True-float32-a_shape1-w_shape1-axes1] _____
[2020-09-01T19:26:55.175Z] [gw2] linux -- Python 3.6.9 /opt/rh/rh-python36/root/usr/bin/python3
[2020-09-01T19:26:55.175Z] 
[2020-09-01T19:26:55.175Z] a_shape = (4, 5, 6), w_shape = (4, 5, 6), axes = (0, 2), is_weighted = True
[2020-09-01T19:26:55.175Z] req_a = 'null', hybridize = True, returned = False, dtype = 'float32'
[2020-09-01T19:26:55.175Z] 
[2020-09-01T19:26:55.175Z]     @with_seed()
[2020-09-01T19:26:55.175Z]     @use_np
[2020-09-01T19:26:55.175Z]     @pytest.mark.parametrize('a_shape,w_shape,axes', [
[2020-09-01T19:26:55.175Z]         ((3, 5), (3, 5), None),
[2020-09-01T19:26:55.175Z]         ((4, 5, 6), (4, 5, 6), (0, 2)),
[2020-09-01T19:26:55.175Z]         ((3,), (3,), 0),
[2020-09-01T19:26:55.175Z]         ((2, 3), (3,), 1),
[2020-09-01T19:26:55.175Z]         ((2, 3, 4), (2,), 0),
[2020-09-01T19:26:55.175Z]         ((2, 3, 4), (3,), 1),
[2020-09-01T19:26:55.175Z]         ((2, 3, 4), (4,), -1),
[2020-09-01T19:26:55.175Z]         ((2, 3, 4, 5), (5,), 3)
[2020-09-01T19:26:55.175Z]     ])
[2020-09-01T19:26:55.175Z]     @pytest.mark.parametrize('dtype', ['float32', 'float64'])
[2020-09-01T19:26:55.175Z]     @pytest.mark.parametrize('hybridize', [True, False])
[2020-09-01T19:26:55.175Z]     @pytest.mark.parametrize('is_weighted', [True, False])
[2020-09-01T19:26:55.175Z]     @pytest.mark.parametrize('returned', [True, False])
[2020-09-01T19:26:55.175Z]     @pytest.mark.parametrize('req_a', ['null', 'add', 'write'])
[2020-09-01T19:26:55.175Z]     def test_np_average(a_shape, w_shape, axes, is_weighted, req_a,
[2020-09-01T19:26:55.175Z]                         hybridize, returned, dtype):
[2020-09-01T19:26:55.175Z]         class TestAverage(HybridBlock):
[2020-09-01T19:26:55.175Z]             def __init__(self, axis=None, returned=False):
[2020-09-01T19:26:55.175Z]                 super(TestAverage, self).__init__()
[2020-09-01T19:26:55.175Z]                 # necessary initializations
[2020-09-01T19:26:55.175Z]                 self._axis = axis
[2020-09-01T19:26:55.175Z]                 self._returned = returned
[2020-09-01T19:26:55.175Z]     
[2020-09-01T19:26:55.175Z]             def hybrid_forward(self, F, a, weights):
[2020-09-01T19:26:55.175Z]                 return F.np.average(a, weights=weights, axis=self._axis, returned=self._returned)
[2020-09-01T19:26:55.175Z]     
[2020-09-01T19:26:55.175Z]         def avg_backward(a, w, avg, axes, init_a_grad=None, init_w_grad=None):
[2020-09-01T19:26:55.175Z]             # avg = sum(a * w) / sum(w)
[2020-09-01T19:26:55.175Z]             if axes is not None and not isinstance(axes, tuple) and axes < 0:
[2020-09-01T19:26:55.175Z]                 axes += a.ndim
[2020-09-01T19:26:55.175Z]             if w is None:
[2020-09-01T19:26:55.175Z]                 a_grad = _np.ones(shape=a.shape, dtype=a.dtype)/(a.size/avg.size)
[2020-09-01T19:26:55.175Z]                 if init_a_grad is not None:
[2020-09-01T19:26:55.175Z]                     a_grad += init_a_grad.asnumpy()
[2020-09-01T19:26:55.175Z]                 return [a_grad, None]
[2020-09-01T19:26:55.175Z]             onedim = a.ndim != w.ndim
[2020-09-01T19:26:55.175Z]             if onedim:
[2020-09-01T19:26:55.175Z]                 new_shape = [a.shape[i] if i == axes else 1 for i in range(a.ndim)]
[2020-09-01T19:26:55.175Z]                 w = w.reshape(new_shape)
[2020-09-01T19:26:55.175Z]                 w = _np.broadcast_to(w, a.shape)
[2020-09-01T19:26:55.175Z]     
[2020-09-01T19:26:55.175Z]             # partial a = w / sum(w)
[2020-09-01T19:26:55.175Z]             # partial w = (a*sum(w) - sum(a*w)) / (sum(w) * sum(w))
[2020-09-01T19:26:55.175Z]             scl = _np.sum(w, axis=axes, keepdims=True)
[2020-09-01T19:26:55.175Z]             a_grad = _np.divide(w, scl)
[2020-09-01T19:26:55.175Z]             w_grad = _np.divide(a*scl-_np.sum(a*w, axis=axes, keepdims=True), scl*scl)
[2020-09-01T19:26:55.175Z]     
[2020-09-01T19:26:55.175Z]             if onedim:
[2020-09-01T19:26:55.175Z]                 axis = list(range(a.ndim))
[2020-09-01T19:26:55.175Z]                 axis.remove(axes)
[2020-09-01T19:26:55.175Z]                 w_grad = _np.sum(w_grad, axis=tuple(axis))
[2020-09-01T19:26:55.175Z]             if init_a_grad is not None:
[2020-09-01T19:26:55.175Z]                 a_grad += init_a_grad.asnumpy()
[2020-09-01T19:26:55.175Z]             if init_w_grad is not None:
[2020-09-01T19:26:55.175Z]                 w_grad += init_w_grad.asnumpy()
[2020-09-01T19:26:55.175Z]             return [a_grad, w_grad]
[2020-09-01T19:26:55.175Z]     
[2020-09-01T19:26:55.175Z]         if req_a == 'null' and not is_weighted:
[2020-09-01T19:26:55.175Z]             return
[2020-09-01T19:26:55.175Z]         rtol, atol = 1e-3, 1e-4
[2020-09-01T19:26:55.175Z]         test_average = TestAverage(axes, returned)
[2020-09-01T19:26:55.175Z]         if hybridize:
[2020-09-01T19:26:55.175Z]             test_average.hybridize()
[2020-09-01T19:26:55.175Z]         a = np.random.uniform(-1.0, 1.0, size=a_shape, dtype=dtype)
[2020-09-01T19:26:55.175Z]         a.attach_grad(req_a)
[2020-09-01T19:26:55.176Z]         init_a_grad = np.random.uniform(-1.0, 1.0, size=a_shape, dtype=dtype) if req_a == 'add' else None
[2020-09-01T19:26:55.176Z]         init_w_grad = None
[2020-09-01T19:26:55.176Z]         req_w = req_a
[2020-09-01T19:26:55.176Z]         w, np_w = None, None
[2020-09-01T19:26:55.176Z]         if is_weighted:
[2020-09-01T19:26:55.176Z]             w = np.random.uniform(-1.0, 1.0, size=w_shape, dtype=dtype)
[2020-09-01T19:26:55.176Z]             if req_a == 'null':
[2020-09-01T19:26:55.176Z]                 req_w = random.choice(['add', 'write'])
[2020-09-01T19:26:55.176Z]             w.attach_grad(req_w)
[2020-09-01T19:26:55.176Z]             if req_w == 'add':
[2020-09-01T19:26:55.176Z]                 init_w_grad = np.random.uniform(-1.0, 1.0, size=w_shape, dtype=dtype)
[2020-09-01T19:26:55.176Z]             np_w = w.asnumpy()
[2020-09-01T19:26:55.176Z]         np_out = _np.average(a.asnumpy(), axis=axes, weights=np_w, returned=returned)
[2020-09-01T19:26:55.176Z]         with mx.autograd.record():
[2020-09-01T19:26:55.176Z]             mx_out = test_average(a, w)
[2020-09-01T19:26:55.176Z]         if returned:
[2020-09-01T19:26:55.176Z]             np_out, np_sum_of_weights = np_out
[2020-09-01T19:26:55.176Z]             mx_out, mx_sum_of_weights = mx_out
[2020-09-01T19:26:55.176Z]             assert_almost_equal(mx_sum_of_weights.asnumpy(), np_sum_of_weights, rtol=rtol, atol=atol)
[2020-09-01T19:26:55.176Z]         assert mx_out.shape == np_out.shape
[2020-09-01T19:26:55.176Z] >       assert_almost_equal(mx_out.asnumpy(), np_out, rtol=rtol, atol=atol)
[2020-09-01T19:26:55.176Z] 
[2020-09-01T19:26:55.176Z] tests/python/unittest/test_numpy_op.py:941: 
[2020-09-01T19:26:55.176Z] _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
[2020-09-01T19:26:55.176Z] 
[2020-09-01T19:26:55.176Z] a = array([    -0.7383507,     -0.892482 , -28541.63     ,     -0.5654906,
[2020-09-01T19:26:55.176Z]            11.051897 ], dtype=float32)
[2020-09-01T19:26:55.176Z] b = array([    -0.7383507,     -0.8924821, -28500.564    ,     -0.5654906,
[2020-09-01T19:26:55.176Z]            11.0519   ], dtype=float32)
[2020-09-01T19:26:55.176Z] rtol = 0.001, atol = 0.0001, names = ('a', 'b'), equal_nan = False
[2020-09-01T19:26:55.176Z] use_broadcast = True, mismatches = (10, 10)
[2020-09-01T19:26:55.176Z] 
[2020-09-01T19:26:55.176Z]     def assert_almost_equal(a, b, rtol=None, atol=None, names=('a', 'b'), equal_nan=False,
[2020-09-01T19:26:55.176Z]                             use_broadcast=True, mismatches=(10, 10)):
[2020-09-01T19:26:55.176Z]         """Test that two numpy arrays are almost equal. Raise exception message if not.
[2020-09-01T19:26:55.176Z]     
[2020-09-01T19:26:55.176Z]         Parameters
[2020-09-01T19:26:55.176Z]         ----------
[2020-09-01T19:26:55.176Z]         a : np.ndarray or mx.nd.array
[2020-09-01T19:26:55.176Z]         b : np.ndarray or mx.nd.array
[2020-09-01T19:26:55.176Z]         rtol : None or float or dict of dtype -> float
[2020-09-01T19:26:55.176Z]             The relative threshold. Default threshold will be used if set to ``None``.
[2020-09-01T19:26:55.176Z]         atol : None or float or dict of dtype -> float
[2020-09-01T19:26:55.176Z]             The absolute threshold. Default threshold will be used if set to ``None``.
[2020-09-01T19:26:55.176Z]         names : tuple of names, optional
[2020-09-01T19:26:55.176Z]             The names used in error message when an exception occurs
[2020-09-01T19:26:55.176Z]         equal_nan : boolean, optional
[2020-09-01T19:26:55.176Z]             The flag determining how to treat NAN values in comparison
[2020-09-01T19:26:55.176Z]         mismatches : tuple of mismatches
[2020-09-01T19:26:55.176Z]             Maximum number of mismatches to be printed (mismatches[0]) and determine (mismatches[1])
[2020-09-01T19:26:55.176Z]         """
[2020-09-01T19:26:55.176Z]         if not use_broadcast:
[2020-09-01T19:26:55.176Z]             checkShapes(a, b)
[2020-09-01T19:26:55.176Z]     
[2020-09-01T19:26:55.176Z]         rtol, atol = get_tols(a, b, rtol, atol)
[2020-09-01T19:26:55.176Z]     
[2020-09-01T19:26:55.176Z]         if isinstance(a, mx.numpy.ndarray):
[2020-09-01T19:26:55.176Z]             a = a.asnumpy()
[2020-09-01T19:26:55.176Z]         if isinstance(b, mx.numpy.ndarray):
[2020-09-01T19:26:55.176Z]             b = b.asnumpy()
[2020-09-01T19:26:55.176Z]         use_np_allclose = isinstance(a, np.ndarray) and isinstance(b, np.ndarray)
[2020-09-01T19:26:55.176Z]         if not use_np_allclose:
[2020-09-01T19:26:55.176Z]             if not (hasattr(a, 'ctx') and hasattr(b, 'ctx') and a.ctx == b.ctx and a.dtype == b.dtype):
[2020-09-01T19:26:55.176Z]                 use_np_allclose = True
[2020-09-01T19:26:55.176Z]                 if isinstance(a, mx.nd.NDArray):
[2020-09-01T19:26:55.176Z]                     a = a.asnumpy()
[2020-09-01T19:26:55.176Z]                 if isinstance(b, mx.nd.NDArray):
[2020-09-01T19:26:55.176Z]                     b = b.asnumpy()
[2020-09-01T19:26:55.176Z]     
[2020-09-01T19:26:55.176Z]         if use_np_allclose:
[2020-09-01T19:26:55.176Z]             if hasattr(a, 'dtype') and a.dtype == np.bool_ and hasattr(b, 'dtype') and b.dtype == np.bool_:
[2020-09-01T19:26:55.176Z]                 np.testing.assert_equal(a, b)
[2020-09-01T19:26:55.176Z]                 return
[2020-09-01T19:26:55.176Z]             if almost_equal(a, b, rtol, atol, equal_nan=equal_nan):
[2020-09-01T19:26:55.176Z]                 return
[2020-09-01T19:26:55.176Z]         else:
[2020-09-01T19:26:55.176Z]             output = mx.nd.contrib.allclose(a, b, rtol, atol, equal_nan)
[2020-09-01T19:26:55.176Z]             if output.asnumpy() == 1:
[2020-09-01T19:26:55.176Z]                 return
[2020-09-01T19:26:55.176Z]     
[2020-09-01T19:26:55.176Z]             a = a.asnumpy()
[2020-09-01T19:26:55.176Z]             b = b.asnumpy()
[2020-09-01T19:26:55.176Z]     
[2020-09-01T19:26:55.176Z]         index, rel = _find_max_violation(a, b, rtol, atol)
[2020-09-01T19:26:55.176Z]         if index != ():
[2020-09-01T19:26:55.176Z]             # a, b are the numpy arrays
[2020-09-01T19:26:55.176Z]             indexErr = index
[2020-09-01T19:26:55.176Z]             relErr = rel
[2020-09-01T19:26:55.176Z]     
[2020-09-01T19:26:55.176Z]             print('\n*** Maximum errors for vector of size {}:  rtol={}, atol={}\n'.format(a.size, rtol, atol))
[2020-09-01T19:26:55.176Z]             aTmp = a.copy()
[2020-09-01T19:26:55.176Z]             bTmp = b.copy()
[2020-09-01T19:26:55.176Z]             i = 1
[2020-09-01T19:26:55.176Z]             while i <= a.size:
[2020-09-01T19:26:55.176Z]                 if i <= mismatches[0]:
[2020-09-01T19:26:55.176Z]                     print("%3d: Error %f  %s" %(i, rel, locationError(a, b, index, names)))
[2020-09-01T19:26:55.176Z]     
[2020-09-01T19:26:55.176Z]                 aTmp[index] = bTmp[index] = 0
[2020-09-01T19:26:55.176Z]                 if almost_equal(aTmp, bTmp, rtol, atol, equal_nan=equal_nan):
[2020-09-01T19:26:55.176Z]                     break
[2020-09-01T19:26:55.176Z]     
[2020-09-01T19:26:55.176Z]                 i += 1
[2020-09-01T19:26:55.176Z]                 if i <= mismatches[1] or mismatches[1] <= 0:
[2020-09-01T19:26:55.176Z]                     index, rel = _find_max_violation(aTmp, bTmp, rtol, atol)
[2020-09-01T19:26:55.176Z]                 else:
[2020-09-01T19:26:55.176Z]                     break
[2020-09-01T19:26:55.176Z]     
[2020-09-01T19:26:55.176Z]             mismatchDegree = "at least " if mismatches[1] > 0 and i > mismatches[1] else ""
[2020-09-01T19:26:55.176Z]             errMsg = "Error %f exceeds tolerance rtol=%e, atol=%e (mismatch %s%f%%).\n%s" % \
[2020-09-01T19:26:55.176Z]                      (relErr, rtol, atol, mismatchDegree, 100*i/a.size, \
[2020-09-01T19:26:55.176Z]                       locationError(a, b, indexErr, names, maxError=True))
[2020-09-01T19:26:55.176Z]         else:
[2020-09-01T19:26:55.176Z]             errMsg = "Error %f exceeds tolerance rtol=%e, atol=%e.\n" % (rel, rtol, atol)
[2020-09-01T19:26:55.176Z]     
[2020-09-01T19:26:55.176Z]         np.set_printoptions(threshold=4, suppress=True)
[2020-09-01T19:26:55.176Z]         msg = npt.build_err_msg([a, b], err_msg=errMsg)
[2020-09-01T19:26:55.176Z]     
[2020-09-01T19:26:55.176Z] >       raise AssertionError(msg)
[2020-09-01T19:26:55.176Z] E       AssertionError: 
[2020-09-01T19:26:55.176Z] E       Items are not equal:
[2020-09-01T19:26:55.176Z] E       Error 1.440893 exceeds tolerance rtol=1.000000e-03, atol=1.000000e-04 (mismatch 20.000000%).
[2020-09-01T19:26:55.176Z] E       Location of maximum error: (2,), a=-28541.63085938, b=-28500.56445312
[2020-09-01T19:26:55.176Z] E        ACTUAL: array([    -0.7383507,     -0.892482 , -28541.63     ,     -0.5654906,
[2020-09-01T19:26:55.176Z] E                  11.051897 ], dtype=float32)
[2020-09-01T19:26:55.176Z] E        DESIRED: array([    -0.7383507,     -0.8924821, -28500.564    ,     -0.5654906,
[2020-09-01T19:26:55.176Z] E                  11.0519   ], dtype=float32)
[2020-09-01T19:26:55.176Z] 
[2020-09-01T19:26:55.176Z] python/mxnet/test_utils.py:735: AssertionError

https://jenkins.mxnet-ci.amazon-ml.com/blue/organizations/jenkins/mxnet-validation%2Fcentos-gpu/detail/PR-19057/2/pipeline/

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions