Skip to content

Commit

Permalink
[Backport] Fix for test_ndarray.test_order failing on CI (v1.3.x) (ap…
Browse files Browse the repository at this point in the history
…ache#12725)

* fix for test order

* fix formatting

* fix formatting
  • Loading branch information
ankkhedia authored and anirudh2290 committed Oct 8, 2018
1 parent 9caa847 commit 0a286a0
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions tests/python/unittest/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,29 +685,33 @@ def get_large_matrix():
return data

large_matrix_npy = get_large_matrix()
large_matrix_nd = mx.nd.array(large_matrix_npy, ctx=ctx)
large_matrix_nd = mx.nd.array(large_matrix_npy, ctx=ctx, dtype=large_matrix_npy.dtype)

nd_ret_topk = mx.nd.topk(large_matrix_nd, axis=1, ret_typ="indices", k=5, is_ascend=False).asnumpy()
gt = gt_topk(large_matrix_npy, axis=1, ret_typ="indices", k=5, is_ascend=False)
assert_almost_equal(nd_ret_topk, gt)

for dtype in [np.int16, np.int32, np.int64, np.float32, np.float64]:
for dtype in [np.int32, np.int64, np.float32, np.float64]:
a_npy = get_values(ensure_unique=True, dtype=dtype)
a_nd = mx.nd.array(a_npy, ctx=ctx)
a_nd = mx.nd.array(a_npy, ctx=ctx, dtype=dtype)

# test for ret_typ=indices
nd_ret_topk = mx.nd.topk(a_nd, axis=1, ret_typ="indices", k=3, is_ascend=True).asnumpy()
assert nd_ret_topk.dtype == np.float32 # Test the default dtype
gt = gt_topk(a_npy, axis=1, ret_typ="indices", k=3, is_ascend=True)
assert_almost_equal(nd_ret_topk, gt)
nd_ret_topk = mx.nd.topk(a_nd, axis=3, ret_typ="indices", k=2, is_ascend=False).asnumpy()
nd_ret_topk = mx.nd.topk(a_nd, axis=3, ret_typ="indices", k=2, is_ascend=False, dtype=np.float64).asnumpy()
assert nd_ret_topk.dtype == np.float64
gt = gt_topk(a_npy, axis=3, ret_typ="indices", k=2, is_ascend=False)
assert_almost_equal(nd_ret_topk, gt)
nd_ret_topk = mx.nd.topk(a_nd, axis=None, ret_typ="indices", k=21, is_ascend=False).asnumpy()
nd_ret_topk = mx.nd.topk(a_nd, axis=None, ret_typ="indices", k=21, is_ascend=False, dtype=np.int32).asnumpy()
assert nd_ret_topk.dtype == np.int32
gt = gt_topk(a_npy, axis=None, ret_typ="indices", k=21, is_ascend=False)
assert_almost_equal(nd_ret_topk, gt)

# test for ret_typ=value
nd_ret_topk = mx.nd.topk(a_nd, axis=1, ret_typ="value", k=3, is_ascend=True).asnumpy()
assert nd_ret_topk.dtype == dtype
gt = gt_topk(a_npy, axis=1, ret_typ="value", k=3, is_ascend=True)
assert_almost_equal(nd_ret_topk, gt)
nd_ret_topk = mx.nd.topk(a_nd, axis=3, ret_typ="value", k=2, is_ascend=False).asnumpy()
Expand All @@ -718,7 +722,11 @@ def get_large_matrix():
assert_almost_equal(nd_ret_topk, gt)

# test for ret_typ=mask
# test needs to be re-enabled once flaky topk gets fixed
# tracked in https://github.com/apache/incubator-mxnet/pull/12446
'''
nd_ret_topk = mx.nd.topk(a_nd, axis=1, ret_typ="mask", k=3, is_ascend=True).asnumpy()
assert nd_ret_topk.dtype == dtype
gt = gt_topk(a_npy, axis=1, ret_typ="mask", k=3, is_ascend=True)
assert_almost_equal(nd_ret_topk, gt)
nd_ret_topk = mx.nd.topk(a_nd, axis=1, ret_typ="mask", k=2, is_ascend=False).asnumpy()
Expand All @@ -727,17 +735,20 @@ def get_large_matrix():
nd_ret_topk = mx.nd.topk(a_nd, axis=None, ret_typ="mask", k=21, is_ascend=False).asnumpy()
gt = gt_topk(a_npy, axis=None, ret_typ="mask", k=21, is_ascend=False)
assert_almost_equal(nd_ret_topk, gt)

'''
# test for ret_typ=both
nd_ret_topk_val, nd_ret_topk_ind = mx.nd.topk(a_nd, axis=1, ret_typ="both", k=3, is_ascend=True)
nd_ret_topk_val = nd_ret_topk_val.asnumpy()
nd_ret_topk_ind = nd_ret_topk_ind.asnumpy()
assert nd_ret_topk_val.dtype == dtype
assert nd_ret_topk_ind.dtype == np.float32
gt_val = gt_topk(a_npy, axis=1, ret_typ="value", k=3, is_ascend=True)
gt_ind = gt_topk(a_npy, axis=1, ret_typ="indices", k=3, is_ascend=True)
assert_almost_equal(nd_ret_topk_val, gt_val)
assert_almost_equal(nd_ret_topk_ind, gt_ind)
# test for kNullOp
_, nd_ret_topk_ind = mx.nd.topk(a_nd, axis=1, ret_typ="both", k=3, is_ascend=True)
_, nd_ret_topk_ind = mx.nd.topk(a_nd, axis=1, ret_typ="both", k=3, is_ascend=True, dtype=np.float64)
assert nd_ret_topk_ind.dtype == np.float64
nd_ret_topk_ind = nd_ret_topk_ind.asnumpy()
assert_almost_equal(nd_ret_topk_ind, gt_ind)
# test for kNullOp
Expand All @@ -760,6 +771,7 @@ def get_large_matrix():
gt = gt_topk(a_npy, axis=3, ret_typ="indices", k=dat_size, is_ascend=True)
assert_almost_equal(nd_ret_argsort, gt)
nd_ret_argsort = mx.nd.argsort(a_nd, axis=None, is_ascend=False, dtype=idtype).asnumpy()
assert nd_ret_argsort.dtype == idtype
gt = gt_topk(a_npy, axis=None, ret_typ="indices",
k=dat_size*dat_size*dat_size*dat_size, is_ascend=False)
assert_almost_equal(nd_ret_argsort, gt)
Expand All @@ -768,7 +780,7 @@ def get_large_matrix():
# duplicated input data values (over many repeated runs with different random seeds,
# this will be tested).
a_npy = get_values(ensure_unique=False, dtype=dtype)
a_nd = mx.nd.array(a_npy, ctx=ctx)
a_nd = mx.nd.array(a_npy, ctx=ctx, dtype=dtype)

# test for ret_typ=value
nd_ret_topk = mx.nd.topk(a_nd, axis=1, ret_typ="value", k=3, is_ascend=True).asnumpy()
Expand Down Expand Up @@ -819,9 +831,9 @@ def get_large_matrix():
# Repeat those tests that don't involve indices. These should pass even with
# duplicated input data values (over many repeated runs with different random seeds,
# this will be tested).
for dtype in [np.int16, np.int32, np.int64, np.float32, np.float64]:
for dtype in [np.int32, np.int64, np.float32, np.float64]:
a_npy = get_values(ensure_unique=False, dtype=dtype)
a_nd = mx.nd.array(a_npy, ctx=ctx)
a_nd = mx.nd.array(a_npy, ctx=ctx, dtype=dtype)

# test for ret_typ=value
nd_ret_topk = mx.nd.topk(a_nd, axis=1, ret_typ="value", k=3, is_ascend=True).asnumpy()
Expand Down

0 comments on commit 0a286a0

Please sign in to comment.