Skip to content

Commit e5d0861

Browse files
zrr1999SigureMo
andauthored
[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 <sigure.qaq@gmail.com>
1 parent 1aa056b commit e5d0861

17 files changed

+32
-40
lines changed

test/cinn/test_computation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def test_build_and_compile(self):
9292

9393
edata_paddle = self.get_paddle_result([A_data, B_data, D_data])
9494

95-
self.assertTrue(np.allclose(edata_cinn, edata_paddle, atol=1e-5))
95+
np.testing.assert_allclose(edata_cinn, edata_paddle, atol=1e-5)
9696

9797

9898
class TestCompilePaddleModel(unittest.TestCase):
@@ -125,7 +125,7 @@ def test_compile_paddle_model(self):
125125
paddle_out = paddle_predictor.run([data])
126126
res_paddle = paddle_out[0].as_ndarray()
127127

128-
self.assertTrue(np.allclose(res_cinn, res_paddle, atol=1e-5))
128+
np.testing.assert_allclose(res_cinn, res_paddle, atol=1e-5)
129129

130130

131131
if __name__ == "__main__":

test/cinn/test_efficientnet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def apply_test(self):
105105
". Diff is: ",
106106
out[i] - target_result[i],
107107
)
108-
self.assertTrue(np.allclose(out, target_result, atol=1e-3))
108+
np.testing.assert_allclose(out, target_result, atol=1e-3)
109109

110110
def test_model(self):
111111
self.apply_test()

test/cinn/test_facedet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def apply_test(self):
109109
". Diff is: ",
110110
out[i] - target_result[i],
111111
)
112-
self.assertTrue(np.allclose(out, target_result, atol=1e-3))
112+
np.testing.assert_allclose(out, target_result, atol=1e-3)
113113

114114
def test_model(self):
115115
self.apply_test()

test/cinn/test_frontend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def test_model(self):
147147
out_np = out.numpy(self.target)
148148
print("cinn data's shape is: ", out_np.shape)
149149

150-
self.assertTrue(np.allclose(out_np, target_data, atol=1e-4))
150+
np.testing.assert_allclose(out_np, target_data, atol=1e-4)
151151

152152

153153
class TestLoadPaddleModel_MultiFC(unittest.TestCase):
@@ -184,7 +184,7 @@ def test_model(self):
184184
out = self.executor.get_tensor("fc_5.tmp_2")
185185
target = self.get_paddle_inference_result(self.model_dir, x_data)
186186

187-
self.assertTrue(np.allclose(out.numpy(self.target), target, atol=1e-4))
187+
np.testing.assert_allclose(out.numpy(self.target), target, atol=1e-4)
188188

189189

190190
if __name__ == "__main__":

test/cinn/test_hlir_framework.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_basic(self):
2828
data = np.random.random([10, 5])
2929
tensor.from_numpy(data, target)
3030

31-
self.assertTrue(np.allclose(tensor.numpy(), data))
31+
np.testing.assert_allclose(tensor.numpy(), data)
3232

3333

3434
if __name__ == "__main__":

test/cinn/test_matmul.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_matmul_basic(self):
4747
matmul(args)
4848
cd = c.numpy()
4949
cd_target = c_target.numpy()
50-
self.assertTrue(np.allclose(cd, cd_target, atol=1e-4))
50+
np.testing.assert_allclose(cd, cd_target, atol=1e-4, rtol=1e-5)
5151
print(utils.HostEventRecorder.table())
5252

5353
def test_matmul_tile(self):
@@ -59,7 +59,7 @@ def test_matmul_tile(self):
5959
matmul(args)
6060
cd = c.numpy()
6161
cd_target = c_target.numpy()
62-
self.assertTrue(np.allclose(cd, cd_target, atol=1e-4))
62+
np.testing.assert_allclose(cd, cd_target, atol=1e-4, rtol=1e-5)
6363

6464

6565
def create_matmul_basic(target, m, n, k):

test/cinn/test_mobilenetv1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def apply_test(self):
108108
out[i] - target_result[i],
109109
)
110110
# TODO(thisjiang): revert atol to 1e-3 after fix inference mul problem
111-
self.assertTrue(np.allclose(out, target_result, atol=1e-1))
111+
np.testing.assert_allclose(out, target_result, atol=1e-1)
112112

113113
def test_model(self):
114114
self.apply_test()

test/cinn/test_mobilenetv2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def apply_test(self):
110110
out[i] - target_result[i],
111111
)
112112
# TODO(thisjiang): revert atol to 1e-3 after fix inference mul problem
113-
self.assertTrue(np.allclose(out, target_result, atol=1e-1))
113+
np.testing.assert_allclose(out, target_result, atol=1e-1)
114114

115115
def test_model(self):
116116
self.apply_test()

test/cinn/test_netbuilder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def paddle_verify_basic(self, result):
7676
". Diff is: ",
7777
output[i] - result[len(result) - 1][i],
7878
)
79-
self.assertTrue(np.allclose(result[len(result) - 1], output, atol=1e-4))
79+
np.testing.assert_allclose(result[len(result) - 1], output, atol=1e-4)
8080

8181
def test_basic(self):
8282
builder = NetBuilder("test_basic")

test/cinn/test_op_benchmark.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def paddle_verify(self, result):
7474
". Diff is: ",
7575
output[i] - result[len(result) - 1][i],
7676
)
77-
self.assertTrue(np.allclose(result[len(result) - 1], output, atol=1e-4))
77+
np.testing.assert_allclose(result[len(result) - 1], output, atol=1e-4)
7878

7979
def atest_conv2d_cinn(self):
8080
prog = Program()
@@ -414,10 +414,8 @@ def atest_elementwise1(self):
414414
"TESTING [elementwise_add] time cost with shape [64, 64]...",
415415
)
416416
result = result.numpy(self.target).reshape(-1)
417-
self.assertTrue(
418-
np.allclose(
419-
(tensor_data[0] + tensor_data[1]).reshape(-1), result, atol=1e-4
420-
)
417+
np.testing.assert_allclose(
418+
(tensor_data[0] + tensor_data[1]).reshape(-1), result, atol=1e-4
421419
)
422420

423421
def atest_elementwise2(self):
@@ -438,10 +436,8 @@ def atest_elementwise2(self):
438436
"TESTING [elementwise_add] time cost with shape [2, 512, 112, 112]...",
439437
)
440438
result = result.numpy(self.target).reshape(-1)
441-
self.assertTrue(
442-
np.allclose(
443-
(tensor_data[0] + tensor_data[1]).reshape(-1), result, atol=1e-4
444-
)
439+
np.testing.assert_allclose(
440+
(tensor_data[0] + tensor_data[1]).reshape(-1), result, atol=1e-4
445441
)
446442

447443
def atest_elementwise2(self):

test/cinn/test_pe_reduction.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,10 @@ def reduction_tester(self, fn_name, cinn_fn, np_fn, axes, keep_dims):
125125
x_data, x_buf, out_buf, *args = self.create_data(axes, keep_dims)
126126
fn(args)
127127

128-
self.assertTrue(
129-
np.allclose(
130-
out_buf.numpy(),
131-
self.create_target_data(x_data, np_fn, axes, keep_dims),
132-
atol=1e-4,
133-
)
128+
np.testing.assert_allclose(
129+
out_buf.numpy(),
130+
self.create_target_data(x_data, np_fn, axes, keep_dims),
131+
atol=1e-4,
134132
)
135133

136134
def create_target_data(self, x_data, np_target_fn, axes, keep_dims):

test/cinn/test_pe_transform.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,12 @@ def transform_matmul_tester(
8787
)
8888
fn(args)
8989

90-
self.assertTrue(
91-
np.allclose(
92-
out_buf.numpy(),
93-
self.create_target_data(
94-
np_fn, x_data, y_data, trans_a, trans_b, alpha
95-
),
96-
atol=1e-4,
97-
)
90+
np.testing.assert_allclose(
91+
out_buf.numpy(),
92+
self.create_target_data(
93+
np_fn, x_data, y_data, trans_a, trans_b, alpha
94+
),
95+
atol=1e-4,
9896
)
9997

10098
def create_target_data(

test/cinn/test_resnet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def apply_test(self):
8383
". Diff is: ",
8484
out[i] - target_result[i],
8585
)
86-
self.assertTrue(np.allclose(out, target_result, atol=1e-3))
86+
np.testing.assert_allclose(out, target_result, atol=1e-3)
8787

8888
def test_model(self):
8989
self.apply_test()

test/cinn/test_resnet18.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def apply_test(self):
110110
out[i] - target_result[i],
111111
)
112112
# TODO(thisjiang): revert atol to 1e-3 after fix inference mul problem
113-
self.assertTrue(np.allclose(out, target_result, atol=1.0))
113+
np.testing.assert_allclose(out, target_result, atol=1.0)
114114

115115
def test_model(self):
116116
self.apply_test()

test/cinn/test_resnet50.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def apply_test(self):
113113
". Diff is: ",
114114
out[i] - target_result[i],
115115
)
116-
self.assertTrue(np.allclose(out, target_result, atol=1e-3))
116+
np.testing.assert_allclose(out, target_result, atol=1e-3)
117117

118118
def test_model(self):
119119
self.apply_test()

test/cinn/test_squeezenet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def apply_test(self):
107107
". Diff is: ",
108108
out[i] - target_result[i],
109109
)
110-
self.assertTrue(np.allclose(out, target_result, atol=1e-3))
110+
np.testing.assert_allclose(out, target_result, atol=1e-3)
111111

112112
def test_model(self):
113113
self.apply_test()

test/cinn/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def to_test_op(
144144
out_result = out[len(out) - 1].numpy()
145145
if out_index != None:
146146
out_result = out[out_index].numpy()
147-
self.assertTrue(np.allclose(out_result, correct_result, atol=1e-4))
147+
np.testing.assert_allclose(out_result, correct_result, atol=1e-4)
148148

149149
def __lower(self, op_name, inputs, output_shapes, attrs):
150150
types = [common.Float(32)]

0 commit comments

Comments
 (0)