未验证 提交 e5d08611 编写于 作者: Z Zhan Rongrui 提交者: GitHub

[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: NSigureMo <sigure.qaq@gmail.com>
上级 1aa056bc
......@@ -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__":
......
......@@ -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()
......
......@@ -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()
......
......@@ -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__":
......
......@@ -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__":
......
......@@ -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):
......
......@@ -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()
......
......@@ -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()
......
......@@ -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")
......
......@@ -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):
......
......@@ -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):
......
......@@ -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(
......
......@@ -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()
......
......@@ -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()
......
......@@ -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()
......
......@@ -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()
......
......@@ -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)]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册