From 04fab9ce5ac59753c29b9273a971328300f55c60 Mon Sep 17 00:00:00 2001 From: Lin Yuan Date: Mon, 2 Sep 2019 22:56:24 -0700 Subject: [PATCH 1/5] add test_mean and fix a few potential bugs --- src/common/tensor_inspector.h | 1 - src/operator/mshadow_op.h | 2 +- src/operator/tensor/broadcast_reduce-inl.h | 4 +++- tests/nightly/test_large_vector.py | 6 ++++++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/common/tensor_inspector.h b/src/common/tensor_inspector.h index 2df94b7fc04f..d1ff595f5c91 100644 --- a/src/common/tensor_inspector.h +++ b/src/common/tensor_inspector.h @@ -33,7 +33,6 @@ #include #include #include "../../3rdparty/mshadow/mshadow/base.h" -#include "../../tests/cpp/include/test_util.h" namespace mxnet { diff --git a/src/operator/mshadow_op.h b/src/operator/mshadow_op.h index ab53e7733066..616192e4af57 100644 --- a/src/operator/mshadow_op.h +++ b/src/operator/mshadow_op.h @@ -98,7 +98,7 @@ MXNET_UNARY_MATH_OP(identity_grad, 1); struct identity_with_cast { template - MSHADOW_XINLINE static void Map(int i, DTypeOut *out, DTypeIn *in) { + MSHADOW_XINLINE static void Map(index_t i, DTypeOut *out, DTypeIn *in) { out[i] = DTypeOut(in[i]); } }; diff --git a/src/operator/tensor/broadcast_reduce-inl.h b/src/operator/tensor/broadcast_reduce-inl.h index d107e89806aa..b429377c74e8 100644 --- a/src/operator/tensor/broadcast_reduce-inl.h +++ b/src/operator/tensor/broadcast_reduce-inl.h @@ -98,8 +98,10 @@ MSHADOW_XINLINE int diff(const Shape& small, const Shape& big, Shape mdim += small[i] != big[i]; (*dims)[i] = (*stride)[i] = 1; } + + index_t s = 1; #pragma unroll - for (int i = ndim-1, j = mdim, s = 1; i >= 0; --i) { + for (int i = ndim-1, j = mdim; i >= 0; --i) { if (small[i] != big[i]) { --j; (*stride)[j] = s; diff --git a/tests/nightly/test_large_vector.py b/tests/nightly/test_large_vector.py index 64bfa8a1d3e9..5f4de53c9540 100644 --- a/tests/nightly/test_large_vector.py +++ b/tests/nightly/test_large_vector.py @@ -170,6 +170,12 @@ def test_topk(): assert val.sum() == (LARGE_X - 1) +def test_mean(): + a = nd.arange(-LARGE_X / 2, LARGE_X / 2 + 1, dtype=np.int64) + b = nd.mean(a, axis=0) + assert b == 0 + + if __name__ == '__main__': import nose nose.runmodule() From 0cdf325ad67adb32ea78abe33e773dd1256e48d0 Mon Sep 17 00:00:00 2001 From: Lin Yuan Date: Tue, 3 Sep 2019 11:08:06 -0700 Subject: [PATCH 2/5] address reviewer commnet --- tests/nightly/test_large_vector.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/nightly/test_large_vector.py b/tests/nightly/test_large_vector.py index 97ee3f031388..e6e87965714d 100644 --- a/tests/nightly/test_large_vector.py +++ b/tests/nightly/test_large_vector.py @@ -170,7 +170,7 @@ def test_topk(): def test_mean(): - a = nd.arange(-LARGE_X / 2, LARGE_X / 2 + 1, dtype=np.int64) + a = nd.arange(-LARGE_X // 2, LARGE_X // 2 + 1, dtype=np.int64) b = nd.mean(a, axis=0) assert b == 0 From a30fb914b37ddfc644d0e6b7b31365c94658b210 Mon Sep 17 00:00:00 2001 From: Lin Yuan Date: Wed, 4 Sep 2019 19:30:38 +0000 Subject: [PATCH 3/5] fix and refactor test --- tests/nightly/test_large_vector.py | 91 +++++++++++++++--------------- 1 file changed, 45 insertions(+), 46 deletions(-) diff --git a/tests/nightly/test_large_vector.py b/tests/nightly/test_large_vector.py index e6e87965714d..406e2eccc21b 100644 --- a/tests/nightly/test_large_vector.py +++ b/tests/nightly/test_large_vector.py @@ -23,16 +23,15 @@ from tests.python.unittest.common import with_seed # dimension constants -LARGE_X = 5000000000 +LARGE_X = 4300000000 MEDIUM_X = 1000000000 def test_slice(): a = nd.ones(LARGE_X) res = nd.slice(a, begin=(LARGE_X - MEDIUM_X), end=LARGE_X) - assert a[0] == 1 assert res.shape[0] == MEDIUM_X - + assert res[0] == 1 def test_ndarray_zeros(): a = nd.zeros(shape=LARGE_X) @@ -44,7 +43,7 @@ def test_ndarray_zeros(): def test_ndarray_ones(): a = nd.ones(shape=LARGE_X) assert a[-1] == 1 - assert nd.sum(a).asnumpy() == LARGE_X + assert nd.sum(a) == LARGE_X @with_seed() @@ -55,15 +54,12 @@ def test_ndarray_random_uniform(): @with_seed() def test_ndarray_random_randint(): - a = nd.random.randint(100, 10000, shape=LARGE_X) - assert a.shape == (LARGE_X,) # check if randint can generate value greater than 2**32 (large) - low_large_value = 2**32 - high_large_value = 2**34 - a = nd.random.randint(low_large_value, high_large_value, dtype=np.int64) - low = mx.nd.array([low_large_value], dtype='int64') - high = mx.nd.array([high_large_value], dtype='int64') - assert a > low and a < high + low = 4294967296 + high = 17179869184 + a = nd.random.randint(low, high, dtype=np.int64, shape=LARGE_X).asnumpy() + assert a.shape == (LARGE_X,) + assert (a >= low).all() and (a < high).all() def test_ndarray_empty(): @@ -82,15 +78,10 @@ def test_elementwise(): assert res[-1].asnumpy() == 3 -def test_reduce(): - a = nd.ones(shape=(LARGE_X, 1)) - assert nd.sum(a).asnumpy() == a.shape[0] * a.shape[1] - - def test_clip(): a = create_vector(LARGE_X) res = nd.clip(a, a_min=100, a_max=1000) - assert np.sum(res[-1].asnumpy() == 1000) == 1 + assert res[-1] == 1000 def test_argmin(): @@ -146,27 +137,34 @@ def test_Dense(ctx=mx.cpu(0)): def test_argsort(): - b = create_vector(size=LARGE_X) - s = nd.argsort(b, axis=0, is_ascend=False, dtype=np.int64) - assert (s[0].asnumpy() == (LARGE_X - 1)).all() + a = create_vector(size=LARGE_X) + s = nd.argsort(a, axis=0, is_ascend=False, dtype=np.int64) + assert s[0] == (LARGE_X - 1) def test_sort(): - b = create_vector(size=LARGE_X) - s = nd.sort(b, axis=0, is_ascend=False) - assert np.sum(s[-1].asnumpy() == 0).all() - s = nd.sort(b, is_ascend=True) - assert np.sum(s[0].asnumpy() == 0).all() + a = create_vector(size=LARGE_X) + + def test_descend(x): + s = nd.sort(x, axis=0, is_ascend=False) + assert s[-1] == 0 + + def test_ascend(x): + s = nd.sort(x, is_ascend=True) + assert s[0] == 0 + + test_descend(a) + test_ascend(a) def test_topk(): - b = create_vector(size=LARGE_X) - ind = nd.topk(b, k=10, axis=0, dtype=np.int64) - assert np.sum(ind.asnumpy() == (LARGE_X - 1)) == 1 - ind, val = mx.nd.topk(b, k=3, axis=0, dtype=np.int64, ret_typ="both", is_ascend=False) - assert np.all(ind == val) - val = nd.topk(b, k=1, axis=0, dtype=np.int64, ret_typ="value") - assert val.sum() == (LARGE_X - 1) + a = create_vector(size=LARGE_X) + ind = nd.topk(a, k=10, axis=0, dtype=np.int64) + assert ind == (LARGE_X - 1) + ind, val = mx.nd.topk(a, k=3, axis=0, dtype=np.int64, ret_typ="both", is_ascend=False) + assert ind == val + val = nd.topk(a, k=1, axis=0, dtype=np.int64, ret_typ="value") + assert val == (LARGE_X - 1) def test_mean(): @@ -320,21 +318,21 @@ def test_eq(): a = nd.full(LARGE_X, 3) b = nd.full(LARGE_X, 3) c = (a == b) - assert np.sum(c[0].asnumpy() == 1).all() + assert (c.asnumpy() == 1).all() def test_neq(): a = nd.full(LARGE_X, 2) b = nd.full(LARGE_X, 3) c = (a != b) - assert np.sum(c[0].asnumpy() == 1).all() + assert (c.asnumpy() == 1).all() def test_lt(): a = nd.full(LARGE_X, 2) b = nd.full(LARGE_X, 3) d = (a <= b) - assert np.sum(d[0].asnumpy() == 1).all() + assert (d.asnumpy() == 1).all() def test_lte(): @@ -342,16 +340,16 @@ def test_lte(): b = nd.full(LARGE_X, 3) c = nd.full(LARGE_X, 2) d = (a <= b) - assert np.sum(d[0].asnumpy() == 1).all() + assert (d.asnumpy() == 1).all() d = (a <= c) - assert np.sum(d[0].asnumpy() == 1).all() + assert (d.asnumpy() == 1).all() def test_gt(): a = nd.full(LARGE_X, 3) b = nd.full(LARGE_X, 2) d = (a > b) - assert np.sum(d[0].asnumpy() == 1).all() + assert (d.asnumpy() == 1).all() def test_gte(): @@ -359,9 +357,9 @@ def test_gte(): b = nd.full(LARGE_X, 2) c = nd.full(LARGE_X, 3) d = (a >= b) - assert np.sum(d[0].asnumpy() == 1).all() + assert (d.asnumpy() == 1).all() d = (a >= c) - assert np.sum(d[0].asnumpy() == 1).all() + assert (d.asnumpy() == 1).all() def test_slice_like(): @@ -370,20 +368,21 @@ def test_slice_like(): c = nd.slice_like(a, b) assert c.shape == b.shape assert c[0] == 0 - assert c[-1] == (LARGE_X//2-1) + assert c[-1] == (LARGE_X // 2 - 1) def test_slice_axis(): a = create_vector(size=LARGE_X) - c = nd.slice_axis(a, axis=0, begin=0, end=LARGE_X//2) - assert c.shape[0] == a.shape[0]//2 - assert c[-1][0] == (LARGE_X//2-1) + med = LARGE_X // 2 + c = nd.slice_axis(a, axis=0, begin=0, end=med) + assert c.shape[0] == a.shape[0] // 2 + assert c[-1][0] == (med - 1) def test_full(): a = nd.full(LARGE_X, 3) assert a.shape[0] == LARGE_X - assert a[LARGE_X//2] == 3 + assert a[LARGE_X // 2] == 3 assert a[-1] == 3 From 58dda55693f048f54ca311b076d754fa48712405 Mon Sep 17 00:00:00 2001 From: Lin Yuan Date: Wed, 4 Sep 2019 22:28:50 +0000 Subject: [PATCH 4/5] fix topk test --- tests/nightly/test_large_vector.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/nightly/test_large_vector.py b/tests/nightly/test_large_vector.py index e0249a27029d..5f34f65630eb 100644 --- a/tests/nightly/test_large_vector.py +++ b/tests/nightly/test_large_vector.py @@ -160,9 +160,10 @@ def test_ascend(x): def test_topk(): a = create_vector(size=LARGE_X) ind = nd.topk(a, k=10, axis=0, dtype=np.int64) - assert ind == (LARGE_X - 1) + for i in range(10): + assert ind[i] == (LARGE_X - i - 1) ind, val = mx.nd.topk(a, k=3, axis=0, dtype=np.int64, ret_typ="both", is_ascend=False) - assert ind == val + assert np.all(ind == val) val = nd.topk(a, k=1, axis=0, dtype=np.int64, ret_typ="value") assert val == (LARGE_X - 1) From aed27ea8d8aaac7d91dd95e088a234a2e8fa1e69 Mon Sep 17 00:00:00 2001 From: Lin Yuan Date: Wed, 4 Sep 2019 22:30:38 -0700 Subject: [PATCH 5/5] address reviewer comment --- src/operator/tensor/broadcast_reduce-inl.h | 2 +- tests/nightly/test_large_vector.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/operator/tensor/broadcast_reduce-inl.h b/src/operator/tensor/broadcast_reduce-inl.h index b429377c74e8..415059ac12a0 100644 --- a/src/operator/tensor/broadcast_reduce-inl.h +++ b/src/operator/tensor/broadcast_reduce-inl.h @@ -101,7 +101,7 @@ MSHADOW_XINLINE int diff(const Shape& small, const Shape& big, Shape index_t s = 1; #pragma unroll - for (int i = ndim-1, j = mdim; i >= 0; --i) { + for (int i = ndim - 1, j = mdim; i >= 0; --i) { if (small[i] != big[i]) { --j; (*stride)[j] = s; diff --git a/tests/nightly/test_large_vector.py b/tests/nightly/test_large_vector.py index 5f34f65630eb..b59a4462d922 100644 --- a/tests/nightly/test_large_vector.py +++ b/tests/nightly/test_large_vector.py @@ -34,6 +34,7 @@ def test_slice(): assert res.shape[0] == MEDIUM_X assert res[0] == 1 + def test_ndarray_zeros(): a = nd.zeros(shape=LARGE_X) assert a[-1] == 0 @@ -56,8 +57,8 @@ def test_ndarray_random_uniform(): @with_seed() def test_ndarray_random_randint(): # check if randint can generate value greater than 2**32 (large) - low = 4294967296 - high = 17179869184 + low = 2**32 + high = 2**34 a = nd.random.randint(low, high, dtype=np.int64, shape=LARGE_X).asnumpy() assert a.shape == (LARGE_X,) assert (a >= low).all() and (a < high).all()