From 80eb44b6cd889b0098c244b22c40906a77705b17 Mon Sep 17 00:00:00 2001 From: ChaiBapchya Date: Wed, 9 Oct 2019 15:27:56 -0700 Subject: [PATCH 1/9] add hyperbolic --- tests/nightly/test_large_array.py | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/nightly/test_large_array.py b/tests/nightly/test_large_array.py index 99856f770d5c..96cbd5fd0242 100644 --- a/tests/nightly/test_large_array.py +++ b/tests/nightly/test_large_array.py @@ -1212,6 +1212,40 @@ def test_full(): assert a[-1][-1] == 3 +def test_hyperbolic(): + def test_arccosh(a): + mx_res = mx.nd.arccosh(a) + assert_almost_equal(mx_res[-1][-1].asnumpy(), np.arccosh(a[-1][-1].asnumpy())) + + def test_arcsinh(a): + mx_res = mx.nd.arcsinh(a) + assert_almost_equal(mx_res[-1][-1].asnumpy(), np.arcsinh(a[-1][-1].asnumpy())) + + def test_arctanh(a): + a[-1][-1] = 0 # arctanh of 1 is inf, assert_almost_equal gives "divide by 0" for comparing 2 inf values + mx_res = mx.nd.arctanh(a) + assert_almost_equal(mx_res[-1][-1].asnumpy(), np.arctanh(a[-1][-1].asnumpy())) + + def test_cosh(a): + mx_res = mx.nd.cosh(a) + assert_almost_equal(mx_res[-1][-1].asnumpy(), np.cosh(a[-1][-1].asnumpy())) + + def test_sinh(a): + mx_res = mx.nd.sinh(a) + assert_almost_equal(mx_res[-1][-1].asnumpy(), np.sinh(a[-1][-1].asnumpy())) + + def test_tanh(a): + mx_res = mx.nd.tanh(a) + assert_almost_equal(mx_res[-1][-1].asnumpy(), np.tanh(a[-1][-1].asnumpy())) + + a = mx.nd.ones((LARGE_X, SMALL_Y)) + test_arccosh(a) + test_arcsinh(a) + test_arctanh(a) + test_cosh(a) + test_sinh(a) + test_tanh(a) + if __name__ == '__main__': import nose nose.runmodule() From fcc566db0bd67177e7f70a2e34fefd84c28bdbad Mon Sep 17 00:00:00 2001 From: ChaiBapchya Date: Wed, 9 Oct 2019 16:15:12 -0700 Subject: [PATCH 2/9] add logical ops, batch_dot, sign and divide --- tests/nightly/test_large_array.py | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/nightly/test_large_array.py b/tests/nightly/test_large_array.py index 96cbd5fd0242..c59ba3498a89 100644 --- a/tests/nightly/test_large_array.py +++ b/tests/nightly/test_large_array.py @@ -841,7 +841,9 @@ def test_div(): b = 3*nd.ones(shape=(LARGE_X, SMALL_Y)) c = b c = c.__div__(a) + mx_divide = nd.divide(c, a) assert c[0][-1] == 3/2 + assert mx_divide[0][-1] == c[0][-1] assert c.shape == a.shape @@ -1246,6 +1248,46 @@ def test_tanh(a): test_sinh(a) test_tanh(a) + +def test_sign(): + a = mx.nd.random.normal(-1,1, shape=(LARGE_X, SMALL_Y)) + mx_res = mx.nd.sign(a) + assert_almost_equal(mx_res[-1][-1].asnumpy(), np.sign(a[-1][-1].asnumpy())) + + +def test_logical(): + def test_logical_and(a, b): + mx_res = mx.nd.logical_and(a, b) + assert_almost_equal(mx_res[-1][-1].asnumpy(), np.logical_and(a[-1][-1].asnumpy(), b[-1][-1].asnumpy())) + + def test_logical_or(a, b): + mx_res = mx.nd.logical_and(a, b) + assert_almost_equal(mx_res[-1][-1].asnumpy(), np.logical_and(a[-1][-1].asnumpy(), b[-1][-1].asnumpy())) + + def test_logical_not(a, b): + mx_res = mx.nd.logical_and(a, b) + assert_almost_equal(mx_res[-1][-1].asnumpy(), np.logical_and(a[-1][-1].asnumpy(), b[-1][-1].asnumpy())) + + def test_logical_xor(a, b): + mx_res = mx.nd.logical_and(a, b) + assert_almost_equal(mx_res[-1][-1].asnumpy(), np.logical_and(a[-1][-1].asnumpy(), b[-1][-1].asnumpy())) + + a = mx.nd.ones((LARGE_X, SMALL_Y)) + b = mx.nd.zeros((LARGE_X, SMALL_Y)) + test_logical_and(a, b) + test_logical_or(a, b) + test_logical_not(a, b) + test_logical_xor(a, b) + + +def test_batch_dot(): + a = mx.nd.ones((LARGE_X, 5, 10)) + b = 2*mx.nd.ones((LARGE_X, 10, 6)) + res = mx.nd.batch_dot(a, b) + assert res[0][0][0] == 20 + assert res.shape == (LARGE_X, 5, 6) + + if __name__ == '__main__': import nose nose.runmodule() From 93d7ad35805eb3f310eb1a51b806133f894fc817 Mon Sep 17 00:00:00 2001 From: ChaiBapchya Date: Wed, 9 Oct 2019 16:29:34 -0700 Subject: [PATCH 3/9] fix divide --- tests/nightly/test_large_array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/nightly/test_large_array.py b/tests/nightly/test_large_array.py index c59ba3498a89..9ff3e821f30b 100644 --- a/tests/nightly/test_large_array.py +++ b/tests/nightly/test_large_array.py @@ -841,7 +841,7 @@ def test_div(): b = 3*nd.ones(shape=(LARGE_X, SMALL_Y)) c = b c = c.__div__(a) - mx_divide = nd.divide(c, a) + mx_divide = nd.divide(b, a) assert c[0][-1] == 3/2 assert mx_divide[0][-1] == c[0][-1] assert c.shape == a.shape From 47cd1ec82477e027a401abb9cef4dbf1c9369cf2 Mon Sep 17 00:00:00 2001 From: ChaiBapchya Date: Tue, 15 Oct 2019 13:44:33 -0700 Subject: [PATCH 4/9] add linear regression, logistic regression --- tests/nightly/test_large_array.py | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/nightly/test_large_array.py b/tests/nightly/test_large_array.py index 5f156bcb0771..c5fb751a8edc 100644 --- a/tests/nightly/test_large_array.py +++ b/tests/nightly/test_large_array.py @@ -1275,6 +1275,44 @@ def test_batch_dot(): assert res.shape == (LARGE_X, 5, 6) +def test_regression(): + shape = (LARGE_X, SMALL_Y) + + def check_regression(symbol, forward, backward, shape): + # init executor + data_s = mx.symbol.Variable('data') + label_s = mx.symbol.Variable('label') + out_s = symbol(data=data_s, label=label_s) + grad_req = {'data': 'write', 'label': 'null'} + exe = out_s.simple_bind(ctx=default_context(), data=shape, label=shape, grad_req=grad_req) + + arg_map = dict(zip(out_s.list_arguments(), exe.arg_arrays)) + grad_map = dict(zip(out_s.list_arguments(), exe.grad_arrays)) + + # init data + data = mx.random.uniform(-1, -1, shape) + arg_map["data"][:] = data + atol = 1e-5 + density = 0.5 + stype = 'default' + label = arg_map["label"] + label[:] = rand_ndarray(shape, stype, density=density) + exe.forward(is_train=True) + exe.backward() + np_out = forward(data.asnumpy()) + out_grad = backward(np_out, label.asnumpy().reshape(np_out.shape)) / shape[1] + assert_almost_equal(exe.outputs[0].asnumpy(), np_out, atol=atol) + assert_almost_equal(grad_map["data"].asnumpy(), out_grad, atol=atol) + + check_regression(mx.symbol.LogisticRegressionOutput, + lambda x: 1.0 / (1.0 + np.exp(-x)), + lambda x, y: x - y, + shape) + check_regression(mx.symbol.LinearRegressionOutput, + lambda x: x, + lambda x, y: x - y, + shape) + if __name__ == '__main__': import nose nose.runmodule() From d6d2d5ff9706b3c5155ac571aeda5b5af794719f Mon Sep 17 00:00:00 2001 From: ChaiBapchya Date: Tue, 15 Oct 2019 14:27:54 -0700 Subject: [PATCH 5/9] make arithmetic op readable --- tests/nightly/test_large_array.py | 48 +++++++++++-------------------- 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/tests/nightly/test_large_array.py b/tests/nightly/test_large_array.py index c5fb751a8edc..263a6af1a891 100644 --- a/tests/nightly/test_large_array.py +++ b/tests/nightly/test_large_array.py @@ -798,8 +798,7 @@ def test_batchnorm(): def test_add(): a = nd.ones(shape=(LARGE_X, SMALL_Y)) b = nd.ones(shape=(LARGE_X, SMALL_Y)) - c = b - c = c.__add__(a) + c = b.__add__(a) assert c[0][-1] == 2 assert c.shape == a.shape @@ -807,8 +806,7 @@ def test_add(): def test_sub(): a = 3*nd.ones(shape=(LARGE_X, SMALL_Y)) b = nd.ones(shape=(LARGE_X, SMALL_Y)) - c = b - c = c.__sub__(a) + c = b.__sub__(a) assert c[0][-1] == -2 assert c.shape == a.shape @@ -816,16 +814,14 @@ def test_sub(): def test_rsub(): a = 3*nd.ones(shape=(LARGE_X, SMALL_Y)) b = nd.ones(shape=(LARGE_X, SMALL_Y)) - c = b - c = c.__rsub__(a) + c = b.__rsub__(a) assert c[0][-1] == 2 assert c.shape == a.shape def test_neg(): a = nd.ones(shape=(LARGE_X, SMALL_Y)) - c = a - c = c.__neg__() + c = a.__neg__() assert c[0][-1] == -1 assert c.shape == a.shape @@ -833,8 +829,7 @@ def test_neg(): def test_mul(): a = 2*nd.ones(shape=(LARGE_X, SMALL_Y)) b = 3*nd.ones(shape=(LARGE_X, SMALL_Y)) - c = b - c = c.__mul__(a) + c = b.__mul__(a) assert c[0][-1] == 6 assert c.shape == a.shape @@ -842,8 +837,7 @@ def test_mul(): def test_div(): a = 2*nd.ones(shape=(LARGE_X, SMALL_Y)) b = 3*nd.ones(shape=(LARGE_X, SMALL_Y)) - c = b - c = c.__div__(a) + c = b.__div__(a) mx_divide = nd.divide(b, a) assert c[0][-1] == 3/2 assert mx_divide[0][-1] == c[0][-1] @@ -853,8 +847,7 @@ def test_div(): def test_rdiv(): a = 2*nd.ones(shape=(LARGE_X, SMALL_Y)) b = 3*nd.ones(shape=(LARGE_X, SMALL_Y)) - c = b - c = c.__rdiv__(a) + c = b.__rdiv__(a) assert c[0][-1] == 2/3 assert c.shape == a.shape @@ -862,8 +855,7 @@ def test_rdiv(): def test_mod(): a = 2*nd.ones(shape=(LARGE_X, SMALL_Y)) b = 3*nd.ones(shape=(LARGE_X, SMALL_Y)) - c = b - c = c.__mod__(a) + c = b.__mod__(a) assert c[0][-1] == 1 assert c.shape == a.shape @@ -871,8 +863,7 @@ def test_mod(): def test_rmod(): a = 2*nd.ones(shape=(LARGE_X, SMALL_Y)) b = 3*nd.ones(shape=(LARGE_X, SMALL_Y)) - c = b - c = c.__rmod__(a) + c = b.__rmod__(a) assert c[0][-1] == 2 assert c.shape == a.shape @@ -880,8 +871,7 @@ def test_rmod(): def test_imod(): a = 2*nd.ones(shape=(LARGE_X, SMALL_Y)) b = 3*nd.ones(shape=(LARGE_X, SMALL_Y)) - c = b - c = c.__imod__(a) + c = b.__imod__(a) assert c[0][-1] == 1 assert c.shape == a.shape @@ -889,8 +879,7 @@ def test_imod(): def test_pow(): a = 2*nd.ones(shape=(LARGE_X, SMALL_Y)) b = 3*nd.ones(shape=(LARGE_X, SMALL_Y)) - c = b - c = c.__pow__(a) + c = b.__pow__(a) assert c[0][-1] == 9 assert c.shape == a.shape @@ -898,8 +887,7 @@ def test_pow(): def test_rpow(): a = 2*nd.ones(shape=(LARGE_X, SMALL_Y)) b = 3*nd.ones(shape=(LARGE_X, SMALL_Y)) - c = b - c = c.__rpow__(a) + c = b.__rpow__(a) assert c[0][-1] == 8 assert c.shape == a.shape @@ -1081,8 +1069,7 @@ def test_log_softmax(): def test_iadd(): a = nd.array(np.ones((SMALL_Y, LARGE_X))) b = nd.array(np.ones((SMALL_Y, LARGE_X))) - c = b - c += a + c = b+a assert c.shape == a.shape assert c[0][-1] == 2 @@ -1090,8 +1077,7 @@ def test_iadd(): def test_isub(): a = nd.array(np.array(np.full((SMALL_Y, LARGE_X), 3))) b = nd.array(np.ones((SMALL_Y, LARGE_X))) - c = a - c -= b + c = a-b assert c.shape == a.shape assert c[0][-1] == 2 @@ -1099,8 +1085,7 @@ def test_isub(): def test_imul(): a = nd.array(np.array(np.full((SMALL_Y, LARGE_X), 3))) b = nd.array(np.ones((SMALL_Y, LARGE_X))) - c = b - c *= a + c = b*a assert c.shape == a.shape assert c[0][-1] == 3 @@ -1108,8 +1093,7 @@ def test_imul(): def test_idiv(): a = nd.array(np.array(np.full((SMALL_Y, LARGE_X), 4))) b = nd.array(np.array(np.full((SMALL_Y, LARGE_X), 2))) - c = a - c /= b + c = a/b assert c.shape == a.shape assert c[0][-1] == 2 From 06e0817a7233b844b2a2dca0365b1f600d32dd49 Mon Sep 17 00:00:00 2001 From: ChaiBapchya Date: Fri, 18 Oct 2019 11:50:42 -0700 Subject: [PATCH 6/9] remove hyperbolic --- tests/nightly/test_large_array.py | 35 ------------------------------- 1 file changed, 35 deletions(-) diff --git a/tests/nightly/test_large_array.py b/tests/nightly/test_large_array.py index c412d4b3c27c..c701aadb20df 100644 --- a/tests/nightly/test_large_array.py +++ b/tests/nightly/test_large_array.py @@ -1187,41 +1187,6 @@ def test_full(): assert a[-1][-1] == 3 -def test_hyperbolic(): - def test_arccosh(a): - mx_res = mx.nd.arccosh(a) - assert_almost_equal(mx_res[-1][-1].asnumpy(), np.arccosh(a[-1][-1].asnumpy())) - - def test_arcsinh(a): - mx_res = mx.nd.arcsinh(a) - assert_almost_equal(mx_res[-1][-1].asnumpy(), np.arcsinh(a[-1][-1].asnumpy())) - - def test_arctanh(a): - a[-1][-1] = 0 # arctanh of 1 is inf, assert_almost_equal gives "divide by 0" for comparing 2 inf values - mx_res = mx.nd.arctanh(a) - assert_almost_equal(mx_res[-1][-1].asnumpy(), np.arctanh(a[-1][-1].asnumpy())) - - def test_cosh(a): - mx_res = mx.nd.cosh(a) - assert_almost_equal(mx_res[-1][-1].asnumpy(), np.cosh(a[-1][-1].asnumpy())) - - def test_sinh(a): - mx_res = mx.nd.sinh(a) - assert_almost_equal(mx_res[-1][-1].asnumpy(), np.sinh(a[-1][-1].asnumpy())) - - def test_tanh(a): - mx_res = mx.nd.tanh(a) - assert_almost_equal(mx_res[-1][-1].asnumpy(), np.tanh(a[-1][-1].asnumpy())) - - a = mx.nd.ones((LARGE_X, SMALL_Y)) - test_arccosh(a) - test_arcsinh(a) - test_arctanh(a) - test_cosh(a) - test_sinh(a) - test_tanh(a) - - def test_sign(): a = mx.nd.random.normal(-1,1, shape=(LARGE_X, SMALL_Y)) mx_res = mx.nd.sign(a) From 3e8ac9dd14683c086d5d5608b2672a938da0dbb7 Mon Sep 17 00:00:00 2001 From: ChaiBapchya Date: Fri, 18 Oct 2019 14:49:09 -0700 Subject: [PATCH 7/9] remove batch dot due to seg fault --- tests/nightly/test_large_array.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/nightly/test_large_array.py b/tests/nightly/test_large_array.py index c701aadb20df..049fb5bdc034 100644 --- a/tests/nightly/test_large_array.py +++ b/tests/nightly/test_large_array.py @@ -1218,14 +1218,6 @@ def test_logical_xor(a, b): test_logical_xor(a, b) -def test_batch_dot(): - a = mx.nd.ones((LARGE_X, 5, 10)) - b = 2*mx.nd.ones((LARGE_X, 10, 6)) - res = mx.nd.batch_dot(a, b) - assert res[0][0][0] == 20 - assert res.shape == (LARGE_X, 5, 6) - - def test_regression(): shape = (LARGE_X, SMALL_Y) From 23ef181de1f9ba15e5eba7fb4634002ed0b69781 Mon Sep 17 00:00:00 2001 From: ChaiBapchya Date: Mon, 21 Oct 2019 17:19:52 -0700 Subject: [PATCH 8/9] fix numpy ands --- tests/nightly/test_large_array.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/nightly/test_large_array.py b/tests/nightly/test_large_array.py index fef5cb969272..9bd10718ccd6 100644 --- a/tests/nightly/test_large_array.py +++ b/tests/nightly/test_large_array.py @@ -1200,15 +1200,15 @@ def test_logical_and(a, b): def test_logical_or(a, b): mx_res = mx.nd.logical_or(a, b) - assert_almost_equal(mx_res[-1][-1].asnumpy(), np.logical_and(a[-1][-1].asnumpy(), b[-1][-1].asnumpy())) + assert_almost_equal(mx_res[-1][-1].asnumpy(), np.logical_or(a[-1][-1].asnumpy(), b[-1][-1].asnumpy())) def test_logical_not(a, b): mx_res = mx.nd.logical_not(a, b) - assert_almost_equal(mx_res[-1][-1].asnumpy(), np.logical_and(a[-1][-1].asnumpy(), b[-1][-1].asnumpy())) + assert_almost_equal(mx_res[-1][-1].asnumpy(), np.logical_not(a[-1][-1].asnumpy(), b[-1][-1].asnumpy())) def test_logical_xor(a, b): mx_res = mx.nd.logical_xor(a, b) - assert_almost_equal(mx_res[-1][-1].asnumpy(), np.logical_and(a[-1][-1].asnumpy(), b[-1][-1].asnumpy())) + assert_almost_equal(mx_res[-1][-1].asnumpy(), np.logical_xor(a[-1][-1].asnumpy(), b[-1][-1].asnumpy())) a = mx.nd.ones((LARGE_X, SMALL_Y)) b = mx.nd.zeros((LARGE_X, SMALL_Y)) From 10376f829acac21b3e8ff4b3bb23afc6eb7b3230 Mon Sep 17 00:00:00 2001 From: ChaiBapchya Date: Mon, 21 Oct 2019 17:20:28 -0700 Subject: [PATCH 9/9] nit space --- tests/nightly/test_large_array.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/nightly/test_large_array.py b/tests/nightly/test_large_array.py index 9bd10718ccd6..0cb21cedee35 100644 --- a/tests/nightly/test_large_array.py +++ b/tests/nightly/test_large_array.py @@ -1071,7 +1071,7 @@ def test_log_softmax(): def test_iadd(): a = nd.array(np.ones((SMALL_Y, LARGE_X))) b = nd.array(np.ones((SMALL_Y, LARGE_X))) - c = b+a + c = b + a assert c.shape == a.shape assert c[0][-1] == 2 @@ -1079,7 +1079,7 @@ def test_iadd(): def test_isub(): a = nd.array(np.array(np.full((SMALL_Y, LARGE_X), 3))) b = nd.array(np.ones((SMALL_Y, LARGE_X))) - c = a-b + c = a - b assert c.shape == a.shape assert c[0][-1] == 2 @@ -1087,7 +1087,7 @@ def test_isub(): def test_imul(): a = nd.array(np.array(np.full((SMALL_Y, LARGE_X), 3))) b = nd.array(np.ones((SMALL_Y, LARGE_X))) - c = b*a + c = b * a assert c.shape == a.shape assert c[0][-1] == 3 @@ -1095,7 +1095,7 @@ def test_imul(): def test_idiv(): a = nd.array(np.array(np.full((SMALL_Y, LARGE_X), 4))) b = nd.array(np.array(np.full((SMALL_Y, LARGE_X), 2))) - c = a/b + c = a / b assert c.shape == a.shape assert c[0][-1] == 2