From 95bf6d62d50285b835e52a8517fd941777e48959 Mon Sep 17 00:00:00 2001 From: jiajiechen Date: Tue, 13 Jun 2017 23:15:47 +0000 Subject: [PATCH 1/5] add unittest --- python/mxnet/sparse_ndarray.py | 32 ++++++++--------- tests/python/unittest/test_sparse_ndarray.py | 36 +++++++++++++++++--- 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/python/mxnet/sparse_ndarray.py b/python/mxnet/sparse_ndarray.py index c3d4453c1d99..dcc601e5996f 100644 --- a/python/mxnet/sparse_ndarray.py +++ b/python/mxnet/sparse_ndarray.py @@ -84,44 +84,44 @@ class SparseNDArray(NDArray): fixed-size items, stored in sparse format. See CSRNDArray and RowSparseNDArray for more details. """ - def __add__(self, other): - raise Exception('Not implemented for SparseND yet!') + # def __add__(self, other): + # raise Exception('Not implemented for SparseND yet!') def __iadd__(self, other): raise Exception('Not implemented for SparseND yet!') - def __radd__(self, other): - raise Exception('Not implemented for SparseND yet!') + # def __radd__(self, other): + # raise Exception('Not implemented for SparseND yet!') def __isub__(self, other): raise Exception('Not implemented for SparseND yet!') - def __rsub__(self, other): - raise Exception('Not implemented for SparseND yet!') + # def __rsub__(self, other): + # raise Exception('Not implemented for SparseND yet!') def __imul__(self, other): raise Exception('Not implemented for SparseND yet!') - def __rmul__(self, other): - raise Exception('Not implemented for SparseND yet!') + # def __rmul__(self, other): + # raise Exception('Not implemented for SparseND yet!') - def __rdiv__(self, other): - raise Exception('Not implemented for SparseND yet!') + # def __rdiv__(self, other): + # raise Exception('Not implemented for SparseND yet!') def __idiv__(self, other): raise Exception('Not implemented for SparseND yet!') - def __rtruediv__(self, other): - raise Exception('Not implemented for SparseND yet!') + # def __rtruediv__(self, other): + # raise Exception('Not implemented for SparseND yet!') def __itruediv__(self, other): raise Exception('Not implemented for SparseND yet!') - def __pow__(self, other): - raise Exception('Not implemented for SparseND yet!') + # def __pow__(self, other): + # raise Exception('Not implemented for SparseND yet!') - def __rpow__(self, other): - raise Exception('Not implemented for SparseND yet!') + # def __rpow__(self, other): + # raise Exception('Not implemented for SparseND yet!') def __setitem__(self, key, value): """x.__setitem__(i, y) <=> x[i]=y diff --git a/tests/python/unittest/test_sparse_ndarray.py b/tests/python/unittest/test_sparse_ndarray.py index 5048f3d15962..2d9517b9cbd1 100644 --- a/tests/python/unittest/test_sparse_ndarray.py +++ b/tests/python/unittest/test_sparse_ndarray.py @@ -243,7 +243,7 @@ def test_sparse_nd_lesser_equal(): def test_sparse_nd_binary(): - N = 100 + N = 1 def check_binary(fn): for _ in range(N): ndim = 2 @@ -257,18 +257,19 @@ def check_binary(fn): lshape[ndim-i-1] = 1 elif sep < 0.66: rshape[bdim-i-1] = 1 - lhs = np.random.normal(0, 1, size=lshape) - rhs = np.random.normal(0, 1, size=rshape) + lhs = np.random.uniform(0, 1, size=lshape) + rhs = np.random.uniform(0, 1, size=rshape) lhs_nd = mx.nd.array(lhs).to_csr() rhs_nd = mx.nd.array(rhs).to_csr() assert_allclose(fn(lhs, rhs), fn(lhs_nd, rhs_nd).asnumpy(), rtol=1e-4, atol=1e-4) - #check_binary(lambda x, y: x + y) + check_binary(lambda x, y: x + y) check_binary(lambda x, y: x - y) check_binary(lambda x, y: x * y) check_binary(lambda x, y: x / y) + check_binary(lambda x, y: x ** y) check_binary(lambda x, y: x > y) check_binary(lambda x, y: x < y) check_binary(lambda x, y: x >= y) @@ -276,6 +277,33 @@ def check_binary(fn): check_binary(lambda x, y: x == y) +def test_sparse_nd_rhs_op_overload(): + N = 100 + def check(fn): + for _ in range(N): + ndim = 2 + shape = np.random.randint(1, 6, size=(ndim,)) + np_nd = np.random.normal(0, 1, size=shape) + csr_nd = mx.nd.array(np_nd).to_csr() + assert_allclose( + fn(np_nd), + fn(csr_nd).asnumpy(), + rtol=1e-4, + atol=1e-4 + ) + check(lambda x: 1 + x) + check(lambda x: 1 - x) + check(lambda x: 1 * x) + check(lambda x: 1 / x) + check(lambda x: 2 ** x) + check(lambda x: 1 > x) + check(lambda x: 0.5 > x) + check(lambda x: 0.5 < x) + check(lambda x: 0.5 >= x) + check(lambda x: 0.5 <= x) + check(lambda x: 0.5 == x) + + def test_sparse_nd_negate(): npy = np.random.uniform(-10, 10, rand_shape_2d()) arr = mx.nd.array(npy).to_csr() From f08b8ce35b186ae0506bcdc20ac43ccf674f6198 Mon Sep 17 00:00:00 2001 From: jiajiechen Date: Tue, 13 Jun 2017 23:18:23 +0000 Subject: [PATCH 2/5] minor fix --- tests/python/unittest/test_sparse_ndarray.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/unittest/test_sparse_ndarray.py b/tests/python/unittest/test_sparse_ndarray.py index 2d9517b9cbd1..c175fe4dddf6 100644 --- a/tests/python/unittest/test_sparse_ndarray.py +++ b/tests/python/unittest/test_sparse_ndarray.py @@ -243,7 +243,7 @@ def test_sparse_nd_lesser_equal(): def test_sparse_nd_binary(): - N = 1 + N = 100 def check_binary(fn): for _ in range(N): ndim = 2 From 722bc626225e3e280b0069b6299d1541cbe4593c Mon Sep 17 00:00:00 2001 From: jiajiechen Date: Tue, 13 Jun 2017 23:29:23 +0000 Subject: [PATCH 3/5] remove commented lines --- python/mxnet/sparse_ndarray.py | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/python/mxnet/sparse_ndarray.py b/python/mxnet/sparse_ndarray.py index dcc601e5996f..0839af97b75d 100644 --- a/python/mxnet/sparse_ndarray.py +++ b/python/mxnet/sparse_ndarray.py @@ -84,45 +84,21 @@ class SparseNDArray(NDArray): fixed-size items, stored in sparse format. See CSRNDArray and RowSparseNDArray for more details. """ - # def __add__(self, other): - # raise Exception('Not implemented for SparseND yet!') - def __iadd__(self, other): raise Exception('Not implemented for SparseND yet!') - # def __radd__(self, other): - # raise Exception('Not implemented for SparseND yet!') - def __isub__(self, other): raise Exception('Not implemented for SparseND yet!') - # def __rsub__(self, other): - # raise Exception('Not implemented for SparseND yet!') - def __imul__(self, other): raise Exception('Not implemented for SparseND yet!') - # def __rmul__(self, other): - # raise Exception('Not implemented for SparseND yet!') - - # def __rdiv__(self, other): - # raise Exception('Not implemented for SparseND yet!') - def __idiv__(self, other): raise Exception('Not implemented for SparseND yet!') - # def __rtruediv__(self, other): - # raise Exception('Not implemented for SparseND yet!') - def __itruediv__(self, other): raise Exception('Not implemented for SparseND yet!') - # def __pow__(self, other): - # raise Exception('Not implemented for SparseND yet!') - - # def __rpow__(self, other): - # raise Exception('Not implemented for SparseND yet!') - def __setitem__(self, key, value): """x.__setitem__(i, y) <=> x[i]=y From cddc13daada4f6475f05285994ff76a69f30a47c Mon Sep 17 00:00:00 2001 From: jiajiechen Date: Tue, 13 Jun 2017 23:31:32 +0000 Subject: [PATCH 4/5] change test func name --- tests/python/unittest/test_sparse_ndarray.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/unittest/test_sparse_ndarray.py b/tests/python/unittest/test_sparse_ndarray.py index c175fe4dddf6..7845dd8e8dc3 100644 --- a/tests/python/unittest/test_sparse_ndarray.py +++ b/tests/python/unittest/test_sparse_ndarray.py @@ -277,7 +277,7 @@ def check_binary(fn): check_binary(lambda x, y: x == y) -def test_sparse_nd_rhs_op_overload(): +def test_sparse_nd_binary_rop(): N = 100 def check(fn): for _ in range(N): From 5023e1b5353a868c99e76c4061d063bb237b7f5a Mon Sep 17 00:00:00 2001 From: jiajiechen Date: Wed, 14 Jun 2017 00:10:34 +0000 Subject: [PATCH 5/5] add test rsp --- tests/python/unittest/test_sparse_ndarray.py | 169 ++++++++++--------- 1 file changed, 88 insertions(+), 81 deletions(-) diff --git a/tests/python/unittest/test_sparse_ndarray.py b/tests/python/unittest/test_sparse_ndarray.py index 7845dd8e8dc3..587eb0ab03fc 100644 --- a/tests/python/unittest/test_sparse_ndarray.py +++ b/tests/python/unittest/test_sparse_ndarray.py @@ -165,81 +165,81 @@ def check_sparse_nd_csr_slice(shape): def test_sparse_nd_equal(): - stype = 'csr' - shape = rand_shape_2d() - x = mx.sparse_nd.zeros(stype, shape) - y = sparse_nd_ones(shape, stype) - z = x == y - assert (z.asnumpy() == np.zeros(shape)).all() - z = 0 == x - assert (z.asnumpy() == np.ones(shape)).all() + for stype in ['row_sparse', 'csr']: + shape = rand_shape_2d() + x = mx.sparse_nd.zeros(stype, shape) + y = sparse_nd_ones(shape, stype) + z = x == y + assert (z.asnumpy() == np.zeros(shape)).all() + z = 0 == x + assert (z.asnumpy() == np.ones(shape)).all() def test_sparse_nd_not_equal(): - stype = 'csr' - shape = rand_shape_2d() - x = mx.sparse_nd.zeros(stype, shape) - y = sparse_nd_ones(shape, stype) - z = x != y - assert (z.asnumpy() == np.ones(shape)).all() - z = 0 != x - assert (z.asnumpy() == np.zeros(shape)).all() + for stype in ['row_sparse', 'csr']: + shape = rand_shape_2d() + x = mx.sparse_nd.zeros(stype, shape) + y = sparse_nd_ones(shape, stype) + z = x != y + assert (z.asnumpy() == np.ones(shape)).all() + z = 0 != x + assert (z.asnumpy() == np.zeros(shape)).all() def test_sparse_nd_greater(): - stype = 'csr' - shape = rand_shape_2d() - x = mx.sparse_nd.zeros(stype, shape) - y = sparse_nd_ones(shape, stype) - z = x > y - assert (z.asnumpy() == np.zeros(shape)).all() - z = y > 0 - assert (z.asnumpy() == np.ones(shape)).all() - z = 0 > y - assert (z.asnumpy() == np.zeros(shape)).all() + for stype in ['row_sparse', 'csr']: + shape = rand_shape_2d() + x = mx.sparse_nd.zeros(stype, shape) + y = sparse_nd_ones(shape, stype) + z = x > y + assert (z.asnumpy() == np.zeros(shape)).all() + z = y > 0 + assert (z.asnumpy() == np.ones(shape)).all() + z = 0 > y + assert (z.asnumpy() == np.zeros(shape)).all() def test_sparse_nd_greater_equal(): - stype = 'csr' - shape = rand_shape_2d() - x = mx.sparse_nd.zeros(stype, shape) - y = sparse_nd_ones(shape, stype) - z = x >= y - assert (z.asnumpy() == np.zeros(shape)).all() - z = y >= 0 - assert (z.asnumpy() == np.ones(shape)).all() - z = 0 >= y - assert (z.asnumpy() == np.zeros(shape)).all() - z = y >= 1 - assert (z.asnumpy() == np.ones(shape)).all() + for stype in ['row_sparse', 'csr']: + shape = rand_shape_2d() + x = mx.sparse_nd.zeros(stype, shape) + y = sparse_nd_ones(shape, stype) + z = x >= y + assert (z.asnumpy() == np.zeros(shape)).all() + z = y >= 0 + assert (z.asnumpy() == np.ones(shape)).all() + z = 0 >= y + assert (z.asnumpy() == np.zeros(shape)).all() + z = y >= 1 + assert (z.asnumpy() == np.ones(shape)).all() def test_sparse_nd_lesser(): - stype = 'csr' - shape = rand_shape_2d() - x = mx.sparse_nd.zeros(stype, shape) - y = sparse_nd_ones(shape, stype) - z = y < x - assert (z.asnumpy() == np.zeros(shape)).all() - z = 0 < y - assert (z.asnumpy() == np.ones(shape)).all() - z = y < 0 - assert (z.asnumpy() == np.zeros(shape)).all() + for stype in ['row_sparse', 'csr']: + shape = rand_shape_2d() + x = mx.sparse_nd.zeros(stype, shape) + y = sparse_nd_ones(shape, stype) + z = y < x + assert (z.asnumpy() == np.zeros(shape)).all() + z = 0 < y + assert (z.asnumpy() == np.ones(shape)).all() + z = y < 0 + assert (z.asnumpy() == np.zeros(shape)).all() def test_sparse_nd_lesser_equal(): - stype = 'csr' - shape = rand_shape_2d() - x = mx.sparse_nd.zeros(stype, shape) - y = sparse_nd_ones(shape, stype) - z = y <= x - assert (z.asnumpy() == np.zeros(shape)).all() - z = 0 <= y - assert (z.asnumpy() == np.ones(shape)).all() - z = y <= 0 - assert (z.asnumpy() == np.zeros(shape)).all() - z = 1 <= y - assert (z.asnumpy() == np.ones(shape)).all() + for stype in ['row_sparse', 'csr']: + shape = rand_shape_2d() + x = mx.sparse_nd.zeros(stype, shape) + y = sparse_nd_ones(shape, stype) + z = y <= x + assert (z.asnumpy() == np.zeros(shape)).all() + z = 0 <= y + assert (z.asnumpy() == np.ones(shape)).all() + z = y <= 0 + assert (z.asnumpy() == np.zeros(shape)).all() + z = 1 <= y + assert (z.asnumpy() == np.ones(shape)).all() def test_sparse_nd_binary(): @@ -259,11 +259,14 @@ def check_binary(fn): rshape[bdim-i-1] = 1 lhs = np.random.uniform(0, 1, size=lshape) rhs = np.random.uniform(0, 1, size=rshape) - lhs_nd = mx.nd.array(lhs).to_csr() - rhs_nd = mx.nd.array(rhs).to_csr() - assert_allclose(fn(lhs, rhs), - fn(lhs_nd, rhs_nd).asnumpy(), - rtol=1e-4, atol=1e-4) + lhs_nd_csr = mx.nd.array(lhs).to_csr() + rhs_nd_csr = mx.nd.array(rhs).to_csr() + lhs_nd_rsp = mx.nd.array(lhs).to_rsp() + rhs_nd_rsp = mx.nd.array(rhs).to_rsp() + for lhs_nd, rhs_nd in [(lhs_nd_csr, rhs_nd_csr), (lhs_nd_rsp, rhs_nd_rsp)]: + assert_allclose(fn(lhs, rhs), + fn(lhs_nd, rhs_nd).asnumpy(), + rtol=1e-4, atol=1e-4) check_binary(lambda x, y: x + y) check_binary(lambda x, y: x - y) @@ -283,14 +286,16 @@ def check(fn): for _ in range(N): ndim = 2 shape = np.random.randint(1, 6, size=(ndim,)) - np_nd = np.random.normal(0, 1, size=shape) - csr_nd = mx.nd.array(np_nd).to_csr() - assert_allclose( - fn(np_nd), - fn(csr_nd).asnumpy(), - rtol=1e-4, - atol=1e-4 - ) + npy_nd = np.random.normal(0, 1, size=shape) + csr_nd = mx.nd.array(npy_nd).to_csr() + rsp_nd = mx.nd.array(npy_nd).to_rsp() + for sparse_nd in [csr_nd, rsp_nd]: + assert_allclose( + fn(npy_nd), + fn(sparse_nd).asnumpy(), + rtol=1e-4, + atol=1e-4 + ) check(lambda x: 1 + x) check(lambda x: 1 - x) check(lambda x: 1 * x) @@ -306,14 +311,16 @@ def check(fn): def test_sparse_nd_negate(): npy = np.random.uniform(-10, 10, rand_shape_2d()) - arr = mx.nd.array(npy).to_csr() - assert_almost_equal(npy, arr.asnumpy()) - assert_almost_equal(-npy, (-arr).asnumpy()) - - # a final check to make sure the negation (-) is not implemented - # as inplace operation, so the contents of arr does not change after - # we compute (-arr) - assert_almost_equal(npy, arr.asnumpy()) + arr_csr = mx.nd.array(npy).to_csr() + arr_rsp = mx.nd.array(npy).to_rsp() + for arr in [arr_csr, arr_rsp]: + assert_almost_equal(npy, arr.asnumpy()) + assert_almost_equal(-npy, (-arr).asnumpy()) + + # a final check to make sure the negation (-) is not implemented + # as inplace operation, so the contents of arr does not change after + # we compute (-arr) + assert_almost_equal(npy, arr.asnumpy()) def test_sparse_nd_output_fallback():