Skip to content

Commit

Permalink
[CodeStyle][CINN] replace unittest.assertTrue with `np.testing.asse…
Browse files Browse the repository at this point in the history
…rt_allclose` (PaddlePaddle#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 <[email protected]>
  • Loading branch information
zrr1999 and SigureMo authored Jul 3, 2023
1 parent 1aa056b commit e5d0861
Show file tree
Hide file tree
Showing 17 changed files with 32 additions and 40 deletions.
4 changes: 2 additions & 2 deletions test/cinn/test_computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_build_and_compile(self):

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):
Expand Down Expand Up @@ -125,7 +125,7 @@ def test_compile_paddle_model(self):
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__":
Expand Down
2 changes: 1 addition & 1 deletion test/cinn/test_efficientnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def apply_test(self):
". 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()
Expand Down
2 changes: 1 addition & 1 deletion test/cinn/test_facedet.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def apply_test(self):
". 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()
Expand Down
4 changes: 2 additions & 2 deletions test/cinn/test_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def test_model(self):
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):
Expand Down Expand Up @@ -184,7 +184,7 @@ def test_model(self):
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__":
Expand Down
2 changes: 1 addition & 1 deletion test/cinn/test_hlir_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_basic(self):
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__":
Expand Down
4 changes: 2 additions & 2 deletions test/cinn/test_matmul.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_matmul_basic(self):
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):
Expand All @@ -59,7 +59,7 @@ def test_matmul_tile(self):
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):
Expand Down
2 changes: 1 addition & 1 deletion test/cinn/test_mobilenetv1.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def apply_test(self):
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()
Expand Down
2 changes: 1 addition & 1 deletion test/cinn/test_mobilenetv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def apply_test(self):
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()
Expand Down
2 changes: 1 addition & 1 deletion test/cinn/test_netbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def paddle_verify_basic(self, result):
". 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")
Expand Down
14 changes: 5 additions & 9 deletions test/cinn/test_op_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def paddle_verify(self, result):
". 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()
Expand Down Expand Up @@ -414,10 +414,8 @@ def atest_elementwise1(self):
"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):
Expand All @@ -438,10 +436,8 @@ def atest_elementwise2(self):
"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):
Expand Down
10 changes: 4 additions & 6 deletions test/cinn/test_pe_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,10 @@ def reduction_tester(self, fn_name, cinn_fn, np_fn, axes, keep_dims):
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):
Expand Down
14 changes: 6 additions & 8 deletions test/cinn/test_pe_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,12 @@ def transform_matmul_tester(
)
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(
Expand Down
2 changes: 1 addition & 1 deletion test/cinn/test_resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def apply_test(self):
". 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()
Expand Down
2 changes: 1 addition & 1 deletion test/cinn/test_resnet18.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def apply_test(self):
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()
Expand Down
2 changes: 1 addition & 1 deletion test/cinn/test_resnet50.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def apply_test(self):
". 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()
Expand Down
2 changes: 1 addition & 1 deletion test/cinn/test_squeezenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def apply_test(self):
". 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()
Expand Down
2 changes: 1 addition & 1 deletion test/cinn/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def to_test_op(
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)]
Expand Down

0 comments on commit e5d0861

Please sign in to comment.