From 6c1a9044da02fa5a71bdcc37033b1aa7e5f3e9b9 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Thu, 23 Jul 2020 20:37:39 +0000 Subject: [PATCH 1/3] add large tensor test for syrk, foward and backward --- tests/nightly/test_large_array.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/nightly/test_large_array.py b/tests/nightly/test_large_array.py index d50f3ff7e539..ec3a3c90a31a 100644 --- a/tests/nightly/test_large_array.py +++ b/tests/nightly/test_large_array.py @@ -40,6 +40,7 @@ LARGE_SIZE = LARGE_X * SMALL_Y LARGE_TENSOR_SHAPE = 2**32 RNN_LARGE_TENSOR = 2**28 +LARGE_SQ_X = 70000 def test_nn(): @@ -1791,6 +1792,25 @@ def test_sparse_dot(): assert out.shape == (2, 2) +def test_linalg_operators(): + def check_syrk_batch(): + # test both forward and backward + # batch syrk will be applied to the last two dimensions + A = nd.zeros((1, LARGE_SQ_X, LARGE_SQ_X)) + for i in range(LARGE_SQ_X): + A[0,i,i] = 1 + A.attach_grad() + with mx.autograd.record(): + out = nd.linalg.syrk(A, alpha=2, transpose=False) + for i in range(LARGE_SQ_X): + assert out[0,i,i] == 2 + out.backward() + for i in range(LARGE_SQ_X): + assert A.grad[0,0,i] == 4 + + check_syrk_batch() + + if __name__ == '__main__': import nose nose.runmodule() From ae4318a0d896a2a191c1a68976973c51cb6caf74 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Fri, 24 Jul 2020 00:48:52 +0000 Subject: [PATCH 2/3] change to batch input --- tests/nightly/test_large_array.py | 36 +++++++++++++++++-------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/tests/nightly/test_large_array.py b/tests/nightly/test_large_array.py index ec3a3c90a31a..adfb6059540c 100644 --- a/tests/nightly/test_large_array.py +++ b/tests/nightly/test_large_array.py @@ -1793,22 +1793,26 @@ def test_sparse_dot(): def test_linalg_operators(): - def check_syrk_batch(): - # test both forward and backward - # batch syrk will be applied to the last two dimensions - A = nd.zeros((1, LARGE_SQ_X, LARGE_SQ_X)) - for i in range(LARGE_SQ_X): - A[0,i,i] = 1 - A.attach_grad() - with mx.autograd.record(): - out = nd.linalg.syrk(A, alpha=2, transpose=False) - for i in range(LARGE_SQ_X): - assert out[0,i,i] == 2 - out.backward() - for i in range(LARGE_SQ_X): - assert A.grad[0,0,i] == 4 - - check_syrk_batch() + def check_syrk_batch(): + # test both forward and backward + # batch syrk will be applied to the last two dimensions + A = nd.zeros((2, LARGE_SQ_X, LARGE_SQ_X)) + for i in range(LARGE_SQ_X): + A[0,i,i] = 1 + A[1,i,i] = 0.1 + A.attach_grad() + with mx.autograd.record(): + out = nd.linalg.syrk(A, alpha=2, transpose=False) + for i in range(LARGE_SQ_X): + assert out[0,i,i] == 2 + assert_almost_equal(out[1,i,i], nd.array([0.02]), rtol=1e-3, atol=1e-5) + out.backward() + for i in range(LARGE_SQ_X): + # check the first row + assert A.grad[0,0,i] == 4 + assert_almost_equal(A.grad[1,0,i], nd.array([0.4]), rtol=1e-3, atol=1e-5) + + check_syrk_batch() if __name__ == '__main__': From b49f999a89235f5c169624b60ed1febfcc4cfe22 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Fri, 24 Jul 2020 17:21:55 +0000 Subject: [PATCH 3/3] move syrk test into test-linalg --- tests/nightly/test_large_array.py | 43 +++++++++++++------------------ 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/tests/nightly/test_large_array.py b/tests/nightly/test_large_array.py index 35fb4373185a..f2128ba70df1 100644 --- a/tests/nightly/test_large_array.py +++ b/tests/nightly/test_large_array.py @@ -37,11 +37,10 @@ LARGE_X = 100000000 SMALL_X = 100 SMALL_Y = 50 -LARGE_SQ_X = 80000 +LARGE_SQ_X = 70000 LARGE_SIZE = LARGE_X * SMALL_Y LARGE_TENSOR_SHAPE = 2**32 RNN_LARGE_TENSOR = 2**28 -LARGE_SQ_X = 70000 def test_nn(): @@ -1191,9 +1190,26 @@ def check_potri(): # output should be an identity matrix for i in range(LARGE_SQ_X): assert out[i,i] == 1 + + def check_syrk_batch(): + # test both forward and backward + # batch syrk will be applied to the last two dimensions + A = nd.zeros((2, LARGE_SQ_X, LARGE_SQ_X)) + for i in range(LARGE_SQ_X): + A[0,i,i] = 1 + A[1,i,i] = 0.1 + A.attach_grad() + with mx.autograd.record(): + out = nd.linalg.syrk(A, alpha=2, transpose=False) + assert out[0,0,0] == 2 + assert_almost_equal(out[1,0,0], nd.array([0.02]), rtol=1e-3, atol=1e-5) + out.backward() + assert A.grad[0,0,0] == 4 + assert_almost_equal(A.grad[1,0,0], nd.array([0.4]), rtol=1e-3, atol=1e-5) check_potrf() check_potri() + check_syrk_batch() def test_basic(): @@ -1819,29 +1835,6 @@ def test_sparse_dot(): assert out.shape == (2, 2) -def test_linalg_operators(): - def check_syrk_batch(): - # test both forward and backward - # batch syrk will be applied to the last two dimensions - A = nd.zeros((2, LARGE_SQ_X, LARGE_SQ_X)) - for i in range(LARGE_SQ_X): - A[0,i,i] = 1 - A[1,i,i] = 0.1 - A.attach_grad() - with mx.autograd.record(): - out = nd.linalg.syrk(A, alpha=2, transpose=False) - for i in range(LARGE_SQ_X): - assert out[0,i,i] == 2 - assert_almost_equal(out[1,i,i], nd.array([0.02]), rtol=1e-3, atol=1e-5) - out.backward() - for i in range(LARGE_SQ_X): - # check the first row - assert A.grad[0,0,i] == 4 - assert_almost_equal(A.grad[1,0,i], nd.array([0.4]), rtol=1e-3, atol=1e-5) - - check_syrk_batch() - - if __name__ == '__main__': import nose nose.runmodule()