From d1caa76cdac887fff65329bb8b65b13d3ae463f2 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Tue, 20 Nov 2018 02:53:47 +0000 Subject: [PATCH 1/6] adding test for mkldnn softmax operator for large negative inputs --- tests/python/mkl/test_mkldnn.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/python/mkl/test_mkldnn.py b/tests/python/mkl/test_mkldnn.py index e5490096f60f..c83b12a2b3a6 100644 --- a/tests/python/mkl/test_mkldnn.py +++ b/tests/python/mkl/test_mkldnn.py @@ -383,6 +383,17 @@ def check_fullyconnected_training(stype): for stype in stypes: check_fullyconnected_training(stype) +def test_softmax_with_large_negative_inputs(): + input_data = mx.nd.array([[[[-1e30,-1e30]]]]) + data = mx.sym.Variable('data') + out1 = data.softmax(axis=1) + exec1 = out1.bind(mx.cpu(), args={'data': input_data, 'softmax_label': mx.nd.ones([1]), + 'fc_weight': mx.nd.ones([2,2]), 'fc1_weight': mx.nd.ones([2,2])}) + exec1.forward()[0].wait_to_read() + ndarr = exec1.outputs[0][0][0][0] + nparr = ndarr.asnumpy() + assert(np.isnan(nparr).any(), False) + @with_seed() def test_non_mkldnn_fcomputeex(): # test special case where MKLDNN formatted NDArray feeds into non-mkldnn fcomputeex operator From ed390f866c54a607060aa7aa0225da11a10125f2 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Tue, 20 Nov 2018 23:55:10 +0000 Subject: [PATCH 2/6] asserting real output --- tests/python/mkl/test_mkldnn.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/mkl/test_mkldnn.py b/tests/python/mkl/test_mkldnn.py index c83b12a2b3a6..ae883719566d 100644 --- a/tests/python/mkl/test_mkldnn.py +++ b/tests/python/mkl/test_mkldnn.py @@ -392,7 +392,7 @@ def test_softmax_with_large_negative_inputs(): exec1.forward()[0].wait_to_read() ndarr = exec1.outputs[0][0][0][0] nparr = ndarr.asnumpy() - assert(np.isnan(nparr).any(), False) + assert np.array_equal(nparr, np.array([1.0,1.0])) @with_seed() def test_non_mkldnn_fcomputeex(): From d12bbcca420a45426fd7a674f7e69997cd2e9ada Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 21 Nov 2018 00:08:18 +0000 Subject: [PATCH 3/6] add test for non mkldnn case too --- tests/python/unittest/test_operator.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/python/unittest/test_operator.py b/tests/python/unittest/test_operator.py index 1bf9ca0237ab..5b0bfc80eb02 100644 --- a/tests/python/unittest/test_operator.py +++ b/tests/python/unittest/test_operator.py @@ -4449,6 +4449,16 @@ def test_log_softmax(): check_symbolic_forward(sym, [data], [np.log(np_softmax(data, axis=axis)+1e-20)]) check_numeric_gradient(sym, [data], rtol=0.05, atol=1e-3) +def test_softmax_with_large_negative_inputs(): + input_data = mx.nd.array([[[[-1e30,-1e30]]]]) + data = mx.sym.Variable('data') + out1 = data.softmax(axis=1) + exec1 = out1.bind(mx.cpu(), args={'data': input_data, 'softmax_label': mx.nd.ones([1]), + 'fc_weight': mx.nd.ones([2,2]), 'fc1_weight': mx.nd.ones([2,2])}) + exec1.forward()[0].wait_to_read() + ndarr = exec1.outputs[0][0][0][0] + nparr = ndarr.asnumpy() + assert np.array_equal(nparr, np.array([1.0,1.0])) @with_seed() def test_pick(): From 16e4e3e76af06be10b019e9b463d88f50176c050 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 21 Nov 2018 02:52:53 +0000 Subject: [PATCH 4/6] adding more corner cases --- tests/python/mkl/test_mkldnn.py | 24 ++++++++++++++---------- tests/python/unittest/test_operator.py | 24 ++++++++++++++---------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/tests/python/mkl/test_mkldnn.py b/tests/python/mkl/test_mkldnn.py index ae883719566d..a96ff8c019e6 100644 --- a/tests/python/mkl/test_mkldnn.py +++ b/tests/python/mkl/test_mkldnn.py @@ -383,16 +383,20 @@ def check_fullyconnected_training(stype): for stype in stypes: check_fullyconnected_training(stype) -def test_softmax_with_large_negative_inputs(): - input_data = mx.nd.array([[[[-1e30,-1e30]]]]) - data = mx.sym.Variable('data') - out1 = data.softmax(axis=1) - exec1 = out1.bind(mx.cpu(), args={'data': input_data, 'softmax_label': mx.nd.ones([1]), - 'fc_weight': mx.nd.ones([2,2]), 'fc1_weight': mx.nd.ones([2,2])}) - exec1.forward()[0].wait_to_read() - ndarr = exec1.outputs[0][0][0][0] - nparr = ndarr.asnumpy() - assert np.array_equal(nparr, np.array([1.0,1.0])) +def test_softmax_with_large_inputs(): + def softmax_forward(input_data, true_output): + data = mx.sym.Variable('data') + out1 = data.softmax(axis=1) + exec1 = out1.bind(mx.cpu(), args={'data': input_data}) + exec1.forward()[0].wait_to_read() + ndarr = exec1.outputs[0][0][0][0] + nparr = ndarr.asnumpy() + assert np.array_equal(nparr, true_output) + + softmax_forward(mx.nd.array([[[[-1e30,-1e30]]]]), np.array([1.0,1.0])) + softmax_forward(mx.nd.array([[[[1e30,1e30]]]]), np.array([1.0,1.0])) + softmax_forward(mx.nd.array([[[[-3.4e38,-3.4e38]]]]), np.array([1.0,1.0])) + softmax_forward(mx.nd.array([[[[3.4e38,3.4e38]]]]), np.array([1.0,1.0])) @with_seed() def test_non_mkldnn_fcomputeex(): diff --git a/tests/python/unittest/test_operator.py b/tests/python/unittest/test_operator.py index 5b0bfc80eb02..0f811a1d5071 100644 --- a/tests/python/unittest/test_operator.py +++ b/tests/python/unittest/test_operator.py @@ -4449,16 +4449,20 @@ def test_log_softmax(): check_symbolic_forward(sym, [data], [np.log(np_softmax(data, axis=axis)+1e-20)]) check_numeric_gradient(sym, [data], rtol=0.05, atol=1e-3) -def test_softmax_with_large_negative_inputs(): - input_data = mx.nd.array([[[[-1e30,-1e30]]]]) - data = mx.sym.Variable('data') - out1 = data.softmax(axis=1) - exec1 = out1.bind(mx.cpu(), args={'data': input_data, 'softmax_label': mx.nd.ones([1]), - 'fc_weight': mx.nd.ones([2,2]), 'fc1_weight': mx.nd.ones([2,2])}) - exec1.forward()[0].wait_to_read() - ndarr = exec1.outputs[0][0][0][0] - nparr = ndarr.asnumpy() - assert np.array_equal(nparr, np.array([1.0,1.0])) +def test_softmax_with_large_inputs(): + def softmax_forward(input_data, true_output): + data = mx.sym.Variable('data') + out1 = data.softmax(axis=1) + exec1 = out1.bind(mx.cpu(), args={'data': input_data}) + exec1.forward()[0].wait_to_read() + ndarr = exec1.outputs[0][0][0][0] + nparr = ndarr.asnumpy() + assert np.array_equal(nparr, true_output) + + softmax_forward(mx.nd.array([[[[-1e30,-1e30]]]]), np.array([1.0,1.0])) + softmax_forward(mx.nd.array([[[[1e30,1e30]]]]), np.array([1.0,1.0])) + softmax_forward(mx.nd.array([[[[-3.4e38,-3.4e38]]]]), np.array([1.0,1.0])) + softmax_forward(mx.nd.array([[[[3.4e38,3.4e38]]]]), np.array([1.0,1.0])) @with_seed() def test_pick(): From 09f57d25de924899804d36c8f2b0460086e0a041 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 21 Nov 2018 04:45:23 +0000 Subject: [PATCH 5/6] replacing assert with assert_almost_equal --- tests/python/mkl/test_mkldnn.py | 2 +- tests/python/unittest/test_operator.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/python/mkl/test_mkldnn.py b/tests/python/mkl/test_mkldnn.py index a96ff8c019e6..c6c0a0832f1f 100644 --- a/tests/python/mkl/test_mkldnn.py +++ b/tests/python/mkl/test_mkldnn.py @@ -391,7 +391,7 @@ def softmax_forward(input_data, true_output): exec1.forward()[0].wait_to_read() ndarr = exec1.outputs[0][0][0][0] nparr = ndarr.asnumpy() - assert np.array_equal(nparr, true_output) + assert_almost_equal(nparr, true_output, rtol=1e-5, atol=1e-5) softmax_forward(mx.nd.array([[[[-1e30,-1e30]]]]), np.array([1.0,1.0])) softmax_forward(mx.nd.array([[[[1e30,1e30]]]]), np.array([1.0,1.0])) diff --git a/tests/python/unittest/test_operator.py b/tests/python/unittest/test_operator.py index 0f811a1d5071..d73c104d9532 100644 --- a/tests/python/unittest/test_operator.py +++ b/tests/python/unittest/test_operator.py @@ -4457,7 +4457,7 @@ def softmax_forward(input_data, true_output): exec1.forward()[0].wait_to_read() ndarr = exec1.outputs[0][0][0][0] nparr = ndarr.asnumpy() - assert np.array_equal(nparr, true_output) + assert_almost_equal(nparr, true_output, rtol=1e-5, atol=1e-5) softmax_forward(mx.nd.array([[[[-1e30,-1e30]]]]), np.array([1.0,1.0])) softmax_forward(mx.nd.array([[[[1e30,1e30]]]]), np.array([1.0,1.0])) From 270778f55b4c1e3917fdb435aee50114d859ef6c Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 21 Nov 2018 07:35:02 +0000 Subject: [PATCH 6/6] adding default context --- tests/python/unittest/test_operator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/unittest/test_operator.py b/tests/python/unittest/test_operator.py index d73c104d9532..19b90b09ceb5 100644 --- a/tests/python/unittest/test_operator.py +++ b/tests/python/unittest/test_operator.py @@ -4453,7 +4453,7 @@ def test_softmax_with_large_inputs(): def softmax_forward(input_data, true_output): data = mx.sym.Variable('data') out1 = data.softmax(axis=1) - exec1 = out1.bind(mx.cpu(), args={'data': input_data}) + exec1 = out1.bind(default_context(), args={'data': input_data}) exec1.forward()[0].wait_to_read() ndarr = exec1.outputs[0][0][0][0] nparr = ndarr.asnumpy()