From e5d0861154a2b85587f0fffe6f98b3e80b508dee Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <46243324+zrr1999@users.noreply.github.com> Date: Mon, 3 Jul 2023 08:25:23 +0800 Subject: [PATCH] [CodeStyle][CINN] replace `unittest.assertTrue` with `np.testing.assert_allclose` (#55038) * Replace testing * fix bug * fix shape * fix bug * revert changes not in CINN * update rtol to 1e-5 * empty commit, re-trigger all ci * empty commit, re-trigger all ci --------- Co-authored-by: SigureMo --- test/cinn/test_computation.py | 4 ++-- test/cinn/test_efficientnet.py | 2 +- test/cinn/test_facedet.py | 2 +- test/cinn/test_frontend.py | 4 ++-- test/cinn/test_hlir_framework.py | 2 +- test/cinn/test_matmul.py | 4 ++-- test/cinn/test_mobilenetv1.py | 2 +- test/cinn/test_mobilenetv2.py | 2 +- test/cinn/test_netbuilder.py | 2 +- test/cinn/test_op_benchmark.py | 14 +++++--------- test/cinn/test_pe_reduction.py | 10 ++++------ test/cinn/test_pe_transform.py | 14 ++++++-------- test/cinn/test_resnet.py | 2 +- test/cinn/test_resnet18.py | 2 +- test/cinn/test_resnet50.py | 2 +- test/cinn/test_squeezenet.py | 2 +- test/cinn/test_utils.py | 2 +- 17 files changed, 32 insertions(+), 40 deletions(-) diff --git a/test/cinn/test_computation.py b/test/cinn/test_computation.py index 43fc0403311..945ad417e2b 100755 --- a/test/cinn/test_computation.py +++ b/test/cinn/test_computation.py @@ -92,7 +92,7 @@ class TestNetBuilder(unittest.TestCase): edata_paddle = self.get_paddle_result([A_data, B_data, D_data]) - self.assertTrue(np.allclose(edata_cinn, edata_paddle, atol=1e-5)) + np.testing.assert_allclose(edata_cinn, edata_paddle, atol=1e-5) class TestCompilePaddleModel(unittest.TestCase): @@ -125,7 +125,7 @@ class TestCompilePaddleModel(unittest.TestCase): paddle_out = paddle_predictor.run([data]) res_paddle = paddle_out[0].as_ndarray() - self.assertTrue(np.allclose(res_cinn, res_paddle, atol=1e-5)) + np.testing.assert_allclose(res_cinn, res_paddle, atol=1e-5) if __name__ == "__main__": diff --git a/test/cinn/test_efficientnet.py b/test/cinn/test_efficientnet.py index 29dc601d715..e7e0c4ffb06 100755 --- a/test/cinn/test_efficientnet.py +++ b/test/cinn/test_efficientnet.py @@ -105,7 +105,7 @@ class TestLoadEfficientNetModel(unittest.TestCase): ". Diff is: ", out[i] - target_result[i], ) - self.assertTrue(np.allclose(out, target_result, atol=1e-3)) + np.testing.assert_allclose(out, target_result, atol=1e-3) def test_model(self): self.apply_test() diff --git a/test/cinn/test_facedet.py b/test/cinn/test_facedet.py index 2a25084c277..0afef60b003 100755 --- a/test/cinn/test_facedet.py +++ b/test/cinn/test_facedet.py @@ -109,7 +109,7 @@ class TestLoadFaceDetModel(unittest.TestCase): ". Diff is: ", out[i] - target_result[i], ) - self.assertTrue(np.allclose(out, target_result, atol=1e-3)) + np.testing.assert_allclose(out, target_result, atol=1e-3) def test_model(self): self.apply_test() diff --git a/test/cinn/test_frontend.py b/test/cinn/test_frontend.py index 7470fc348ea..257da984708 100755 --- a/test/cinn/test_frontend.py +++ b/test/cinn/test_frontend.py @@ -147,7 +147,7 @@ class TestLoadPaddleModel_FC(unittest.TestCase): out_np = out.numpy(self.target) print("cinn data's shape is: ", out_np.shape) - self.assertTrue(np.allclose(out_np, target_data, atol=1e-4)) + np.testing.assert_allclose(out_np, target_data, atol=1e-4) class TestLoadPaddleModel_MultiFC(unittest.TestCase): @@ -184,7 +184,7 @@ class TestLoadPaddleModel_MultiFC(unittest.TestCase): out = self.executor.get_tensor("fc_5.tmp_2") target = self.get_paddle_inference_result(self.model_dir, x_data) - self.assertTrue(np.allclose(out.numpy(self.target), target, atol=1e-4)) + np.testing.assert_allclose(out.numpy(self.target), target, atol=1e-4) if __name__ == "__main__": diff --git a/test/cinn/test_hlir_framework.py b/test/cinn/test_hlir_framework.py index cb200802eae..1e7d36cacac 100644 --- a/test/cinn/test_hlir_framework.py +++ b/test/cinn/test_hlir_framework.py @@ -28,7 +28,7 @@ class TensorTest(unittest.TestCase): data = np.random.random([10, 5]) tensor.from_numpy(data, target) - self.assertTrue(np.allclose(tensor.numpy(), data)) + np.testing.assert_allclose(tensor.numpy(), data) if __name__ == "__main__": diff --git a/test/cinn/test_matmul.py b/test/cinn/test_matmul.py index da96434ed00..8deac2bb701 100755 --- a/test/cinn/test_matmul.py +++ b/test/cinn/test_matmul.py @@ -47,7 +47,7 @@ class TestMamul(unittest.TestCase): matmul(args) cd = c.numpy() cd_target = c_target.numpy() - self.assertTrue(np.allclose(cd, cd_target, atol=1e-4)) + np.testing.assert_allclose(cd, cd_target, atol=1e-4, rtol=1e-5) print(utils.HostEventRecorder.table()) def test_matmul_tile(self): @@ -59,7 +59,7 @@ class TestMamul(unittest.TestCase): matmul(args) cd = c.numpy() cd_target = c_target.numpy() - self.assertTrue(np.allclose(cd, cd_target, atol=1e-4)) + np.testing.assert_allclose(cd, cd_target, atol=1e-4, rtol=1e-5) def create_matmul_basic(target, m, n, k): diff --git a/test/cinn/test_mobilenetv1.py b/test/cinn/test_mobilenetv1.py index 4058df24b7f..e6cf251bd19 100644 --- a/test/cinn/test_mobilenetv1.py +++ b/test/cinn/test_mobilenetv1.py @@ -108,7 +108,7 @@ class TestLoadMobilenetV1Model(unittest.TestCase): out[i] - target_result[i], ) # TODO(thisjiang): revert atol to 1e-3 after fix inference mul problem - self.assertTrue(np.allclose(out, target_result, atol=1e-1)) + np.testing.assert_allclose(out, target_result, atol=1e-1) def test_model(self): self.apply_test() diff --git a/test/cinn/test_mobilenetv2.py b/test/cinn/test_mobilenetv2.py index f96c333a8a0..6ca646823ea 100755 --- a/test/cinn/test_mobilenetv2.py +++ b/test/cinn/test_mobilenetv2.py @@ -110,7 +110,7 @@ class TestLoadResnetModel(unittest.TestCase): out[i] - target_result[i], ) # TODO(thisjiang): revert atol to 1e-3 after fix inference mul problem - self.assertTrue(np.allclose(out, target_result, atol=1e-1)) + np.testing.assert_allclose(out, target_result, atol=1e-1) def test_model(self): self.apply_test() diff --git a/test/cinn/test_netbuilder.py b/test/cinn/test_netbuilder.py index 526b325bf24..8e1787824ac 100755 --- a/test/cinn/test_netbuilder.py +++ b/test/cinn/test_netbuilder.py @@ -76,7 +76,7 @@ class TestNetBuilder(unittest.TestCase): ". Diff is: ", output[i] - result[len(result) - 1][i], ) - self.assertTrue(np.allclose(result[len(result) - 1], output, atol=1e-4)) + np.testing.assert_allclose(result[len(result) - 1], output, atol=1e-4) def test_basic(self): builder = NetBuilder("test_basic") diff --git a/test/cinn/test_op_benchmark.py b/test/cinn/test_op_benchmark.py index ce63e4e4f69..119ca6af153 100755 --- a/test/cinn/test_op_benchmark.py +++ b/test/cinn/test_op_benchmark.py @@ -74,7 +74,7 @@ class TestBenchmark(unittest.TestCase): ". Diff is: ", output[i] - result[len(result) - 1][i], ) - self.assertTrue(np.allclose(result[len(result) - 1], output, atol=1e-4)) + np.testing.assert_allclose(result[len(result) - 1], output, atol=1e-4) def atest_conv2d_cinn(self): prog = Program() @@ -414,10 +414,8 @@ typedef char int8_t; "TESTING [elementwise_add] time cost with shape [64, 64]...", ) result = result.numpy(self.target).reshape(-1) - self.assertTrue( - np.allclose( - (tensor_data[0] + tensor_data[1]).reshape(-1), result, atol=1e-4 - ) + np.testing.assert_allclose( + (tensor_data[0] + tensor_data[1]).reshape(-1), result, atol=1e-4 ) def atest_elementwise2(self): @@ -438,10 +436,8 @@ typedef char int8_t; "TESTING [elementwise_add] time cost with shape [2, 512, 112, 112]...", ) result = result.numpy(self.target).reshape(-1) - self.assertTrue( - np.allclose( - (tensor_data[0] + tensor_data[1]).reshape(-1), result, atol=1e-4 - ) + np.testing.assert_allclose( + (tensor_data[0] + tensor_data[1]).reshape(-1), result, atol=1e-4 ) def atest_elementwise2(self): diff --git a/test/cinn/test_pe_reduction.py b/test/cinn/test_pe_reduction.py index d8d4a8b6f01..94698349688 100644 --- a/test/cinn/test_pe_reduction.py +++ b/test/cinn/test_pe_reduction.py @@ -125,12 +125,10 @@ class TestPEReduction(unittest.TestCase): x_data, x_buf, out_buf, *args = self.create_data(axes, keep_dims) fn(args) - self.assertTrue( - np.allclose( - out_buf.numpy(), - self.create_target_data(x_data, np_fn, axes, keep_dims), - atol=1e-4, - ) + np.testing.assert_allclose( + out_buf.numpy(), + self.create_target_data(x_data, np_fn, axes, keep_dims), + atol=1e-4, ) def create_target_data(self, x_data, np_target_fn, axes, keep_dims): diff --git a/test/cinn/test_pe_transform.py b/test/cinn/test_pe_transform.py index bc78bc66dde..becf4aa02e2 100644 --- a/test/cinn/test_pe_transform.py +++ b/test/cinn/test_pe_transform.py @@ -87,14 +87,12 @@ class TestPETransform(unittest.TestCase): ) fn(args) - self.assertTrue( - np.allclose( - out_buf.numpy(), - self.create_target_data( - np_fn, x_data, y_data, trans_a, trans_b, alpha - ), - atol=1e-4, - ) + np.testing.assert_allclose( + out_buf.numpy(), + self.create_target_data( + np_fn, x_data, y_data, trans_a, trans_b, alpha + ), + atol=1e-4, ) def create_target_data( diff --git a/test/cinn/test_resnet.py b/test/cinn/test_resnet.py index 69c3d256cc9..3b9b7f1b88e 100755 --- a/test/cinn/test_resnet.py +++ b/test/cinn/test_resnet.py @@ -83,7 +83,7 @@ class TestLoadResnetModel(unittest.TestCase): ". Diff is: ", out[i] - target_result[i], ) - self.assertTrue(np.allclose(out, target_result, atol=1e-3)) + np.testing.assert_allclose(out, target_result, atol=1e-3) def test_model(self): self.apply_test() diff --git a/test/cinn/test_resnet18.py b/test/cinn/test_resnet18.py index 2d9b227ab55..9bb0c210a32 100755 --- a/test/cinn/test_resnet18.py +++ b/test/cinn/test_resnet18.py @@ -110,7 +110,7 @@ class TestLoadResnetModel(unittest.TestCase): out[i] - target_result[i], ) # TODO(thisjiang): revert atol to 1e-3 after fix inference mul problem - self.assertTrue(np.allclose(out, target_result, atol=1.0)) + np.testing.assert_allclose(out, target_result, atol=1.0) def test_model(self): self.apply_test() diff --git a/test/cinn/test_resnet50.py b/test/cinn/test_resnet50.py index 9ecf60c22eb..554553d62d6 100755 --- a/test/cinn/test_resnet50.py +++ b/test/cinn/test_resnet50.py @@ -113,7 +113,7 @@ class TestLoadResnet50Model(unittest.TestCase): ". Diff is: ", out[i] - target_result[i], ) - self.assertTrue(np.allclose(out, target_result, atol=1e-3)) + np.testing.assert_allclose(out, target_result, atol=1e-3) def test_model(self): self.apply_test() diff --git a/test/cinn/test_squeezenet.py b/test/cinn/test_squeezenet.py index 65a6576915f..3983716036f 100644 --- a/test/cinn/test_squeezenet.py +++ b/test/cinn/test_squeezenet.py @@ -107,7 +107,7 @@ class TestLoadSqueezeNetModel(unittest.TestCase): ". Diff is: ", out[i] - target_result[i], ) - self.assertTrue(np.allclose(out, target_result, atol=1e-3)) + np.testing.assert_allclose(out, target_result, atol=1e-3) def test_model(self): self.apply_test() diff --git a/test/cinn/test_utils.py b/test/cinn/test_utils.py index f97caaab288..ec29b28f9a1 100755 --- a/test/cinn/test_utils.py +++ b/test/cinn/test_utils.py @@ -144,7 +144,7 @@ class SingleOpTester(unittest.TestCase): out_result = out[len(out) - 1].numpy() if out_index != None: out_result = out[out_index].numpy() - self.assertTrue(np.allclose(out_result, correct_result, atol=1e-4)) + np.testing.assert_allclose(out_result, correct_result, atol=1e-4) def __lower(self, op_name, inputs, output_shapes, attrs): types = [common.Float(32)] -- GitLab