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

[CodeStyle] replace `assert np.allclose` with `np.testing.assert_allclose` and...

[CodeStyle] replace `assert np.allclose` with `np.testing.assert_allclose` and `assert np.array_equal` with `np.testing.assert_array_equal` (#55385)
上级 4df4b9fe
...@@ -86,7 +86,7 @@ class TestRandomControl(unittest.TestCase): ...@@ -86,7 +86,7 @@ class TestRandomControl(unittest.TestCase):
mask_tensor_remote = paddle.ones_like(mask_tensor_local) mask_tensor_remote = paddle.ones_like(mask_tensor_local)
dy_broadcast_helper(mask_tensor_remote) dy_broadcast_helper(mask_tensor_remote)
if equal: if equal:
assert np.array_equal( np.testing.assert_array_equal(
mask_tensor_remote.numpy(), mask_tensor_local.numpy() mask_tensor_remote.numpy(), mask_tensor_local.numpy()
) )
else: else:
...@@ -205,7 +205,7 @@ class TestRandomControl(unittest.TestCase): ...@@ -205,7 +205,7 @@ class TestRandomControl(unittest.TestCase):
for i in range(7): for i in range(7):
mask_fw = mask_np_list[i].astype("float32") mask_fw = mask_np_list[i].astype("float32")
mask_rc = mask_np_list[i + 7].astype("float32") mask_rc = mask_np_list[i + 7].astype("float32")
assert np.array_equal( np.testing.assert_array_equal(
mask_fw, mask_fw,
mask_rc, mask_rc,
) )
......
...@@ -208,7 +208,7 @@ def step_check(path1, path2): ...@@ -208,7 +208,7 @@ def step_check(path1, path2):
m1 = paddle.load(path1) m1 = paddle.load(path1)
m2 = paddle.load(path2) m2 = paddle.load(path2)
for v1, v2 in zip(m1, m2): for v1, v2 in zip(m1, m2):
assert np.allclose(v1.numpy(), v2.numpy()) np.testing.assert_allclose(v1.numpy(), v2.numpy())
print(f"value same: {v1.name}") print(f"value same: {v1.name}")
......
...@@ -267,7 +267,7 @@ def step_check(output_dir): ...@@ -267,7 +267,7 @@ def step_check(output_dir):
m1 = np.load(p1).reshape(-1) m1 = np.load(p1).reshape(-1)
m2 = np.load(p2).reshape(-1) m2 = np.load(p2).reshape(-1)
try: try:
assert np.allclose(m1, m2, rtol=1e-5, atol=1e-6) np.testing.assert_allclose(m1, m2, rtol=1e-5, atol=1e-6)
except: except:
diff = m1 - m2 diff = m1 - m2
logger.error(f"max diff{diff.max()}, min diff: {diff.min()}") logger.error(f"max diff{diff.max()}, min diff: {diff.min()}")
......
...@@ -234,7 +234,7 @@ class TestFusedAttentionPassWithMP(unittest.TestCase): ...@@ -234,7 +234,7 @@ class TestFusedAttentionPassWithMP(unittest.TestCase):
def test_pass(self): def test_pass(self):
fused_rst = self.get_rst(use_pass=True) fused_rst = self.get_rst(use_pass=True)
non_fused_rst = self.get_rst() non_fused_rst = self.get_rst()
assert np.allclose(fused_rst, non_fused_rst, atol=1e-5) np.testing.assert_allclose(fused_rst, non_fused_rst, atol=1e-5)
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -58,28 +58,28 @@ class TestNewGroupAPI: ...@@ -58,28 +58,28 @@ class TestNewGroupAPI:
sync_op=True, sync_op=True,
) )
if dp_rank == 0: if dp_rank == 0:
assert np.array_equal(result, self.tensor2) np.testing.assert_array_equal(result, self.tensor2)
elif dp_rank == 1: elif dp_rank == 1:
assert np.array_equal(result, self.tensor1) np.testing.assert_array_equal(result, self.tensor1)
print("test scatter api ok") print("test scatter api ok")
paddle.distributed.broadcast(result, src=1, group=dp_gp, sync_op=True) paddle.distributed.broadcast(result, src=1, group=dp_gp, sync_op=True)
assert np.array_equal(result, self.tensor1) np.testing.assert_array_equal(result, self.tensor1)
print("test broadcast api ok") print("test broadcast api ok")
paddle.distributed.reduce( paddle.distributed.reduce(
result, dst=dp_src_rank, group=dp_gp, sync_op=True result, dst=dp_src_rank, group=dp_gp, sync_op=True
) )
if dp_rank == 0: if dp_rank == 0:
assert np.array_equal( np.testing.assert_array_equal(
result, paddle.add(self.tensor1, self.tensor1) result, paddle.add(self.tensor1, self.tensor1)
) )
elif dp_rank == 1: elif dp_rank == 1:
assert np.array_equal(result, self.tensor1) np.testing.assert_array_equal(result, self.tensor1)
print("test reduce api ok") print("test reduce api ok")
paddle.distributed.all_reduce(result, sync_op=True) paddle.distributed.all_reduce(result, sync_op=True)
assert np.array_equal( np.testing.assert_array_equal(
result, result,
paddle.add(paddle.add(self.tensor1, self.tensor1), self.tensor1), paddle.add(paddle.add(self.tensor1, self.tensor1), self.tensor1),
) )
...@@ -93,8 +93,8 @@ class TestNewGroupAPI: ...@@ -93,8 +93,8 @@ class TestNewGroupAPI:
paddle.distributed.all_gather( paddle.distributed.all_gather(
result, self.tensor1, group=dp_gp, sync_op=True result, self.tensor1, group=dp_gp, sync_op=True
) )
assert np.array_equal(result[0], self.tensor1) np.testing.assert_array_equal(result[0], self.tensor1)
assert np.array_equal(result[1], self.tensor1) np.testing.assert_array_equal(result[1], self.tensor1)
print("test all_gather api ok") print("test all_gather api ok")
paddle.distributed.barrier(group=dp_gp) paddle.distributed.barrier(group=dp_gp)
......
...@@ -36,26 +36,26 @@ class TestNewGroupAPI: ...@@ -36,26 +36,26 @@ class TestNewGroupAPI:
result, [self.tensor2, self.tensor1], src=0, group=gp, sync_op=True result, [self.tensor2, self.tensor1], src=0, group=gp, sync_op=True
) )
if gp.rank == 0: if gp.rank == 0:
assert np.array_equal(result, self.tensor2) np.testing.assert_array_equal(result, self.tensor2)
elif gp.rank == 1: elif gp.rank == 1:
assert np.array_equal(result, self.tensor1) np.testing.assert_array_equal(result, self.tensor1)
print("test scatter api ok") print("test scatter api ok")
paddle.distributed.broadcast(result, src=1, group=gp, sync_op=True) paddle.distributed.broadcast(result, src=1, group=gp, sync_op=True)
assert np.array_equal(result, self.tensor1) np.testing.assert_array_equal(result, self.tensor1)
print("test broadcast api ok") print("test broadcast api ok")
paddle.distributed.reduce(result, dst=0, group=gp, sync_op=True) paddle.distributed.reduce(result, dst=0, group=gp, sync_op=True)
if gp.rank == 0: if gp.rank == 0:
assert np.array_equal( np.testing.assert_array_equal(
result, paddle.add(self.tensor1, self.tensor1) result, paddle.add(self.tensor1, self.tensor1)
) )
elif gp.rank == 1: elif gp.rank == 1:
assert np.array_equal(result, self.tensor1) np.testing.assert_array_equal(result, self.tensor1)
print("test reduce api ok") print("test reduce api ok")
paddle.distributed.all_reduce(result, sync_op=True) paddle.distributed.all_reduce(result, sync_op=True)
assert np.array_equal( np.testing.assert_array_equal(
result, result,
paddle.add(paddle.add(self.tensor1, self.tensor1), self.tensor1), paddle.add(paddle.add(self.tensor1, self.tensor1), self.tensor1),
) )
...@@ -69,8 +69,8 @@ class TestNewGroupAPI: ...@@ -69,8 +69,8 @@ class TestNewGroupAPI:
paddle.distributed.all_gather( paddle.distributed.all_gather(
result, self.tensor1, group=gp, sync_op=True result, self.tensor1, group=gp, sync_op=True
) )
assert np.array_equal(result[0], self.tensor1) np.testing.assert_array_equal(result[0], self.tensor1)
assert np.array_equal(result[1], self.tensor1) np.testing.assert_array_equal(result[1], self.tensor1)
print("test all_gather api ok") print("test all_gather api ok")
paddle.distributed.barrier(group=gp) paddle.distributed.barrier(group=gp)
......
...@@ -201,8 +201,8 @@ class TestAmpScaler(unittest.TestCase): ...@@ -201,8 +201,8 @@ class TestAmpScaler(unittest.TestCase):
data = paddle.rand([10, 1024]) data = paddle.rand([10, 1024])
scaler = paddle.amp.AmpScaler(init_loss_scaling=1024) scaler = paddle.amp.AmpScaler(init_loss_scaling=1024)
scaled_data = scaler.scale(data) scaled_data = scaler.scale(data)
self.assertEqual( np.testing.assert_array_equal(
np.array_equal(scaled_data.numpy(), data.numpy() * 1024), True scaled_data.numpy(), data.numpy() * 1024
) )
def test_scale(self): def test_scale(self):
......
...@@ -39,7 +39,7 @@ class TestDygrapgHybridDP(TestCollectiveAPIRunnerBase): ...@@ -39,7 +39,7 @@ class TestDygrapgHybridDP(TestCollectiveAPIRunnerBase):
paddle.distributed.collective.all_reduce(data_part) paddle.distributed.collective.all_reduce(data_part)
data_reduced = data_part data_reduced = data_part
data_sumed = np.sum(data, axis=0) data_sumed = np.sum(data, axis=0)
assert np.allclose( np.testing.assert_allclose(
data_sumed, data_reduced.numpy(), rtol=1e-8, atol=1e-8 data_sumed, data_reduced.numpy(), rtol=1e-8, atol=1e-8
) )
......
...@@ -197,7 +197,9 @@ class TestDygrapgHybridDPPPMP(TestCollectiveAPIRunnerBase): ...@@ -197,7 +197,9 @@ class TestDygrapgHybridDPPPMP(TestCollectiveAPIRunnerBase):
loss_base_arr.append(loss_base.numpy()) loss_base_arr.append(loss_base.numpy())
loss_hybrid_arr.append(loss.numpy()) loss_hybrid_arr.append(loss.numpy())
assert np.allclose(loss_base_arr, loss_hybrid_arr, rtol=1e-5, atol=1e-5) np.testing.assert_allclose(
loss_base_arr, loss_hybrid_arr, rtol=1e-5, atol=1e-5
)
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -210,7 +210,9 @@ class TestDygraphHybridFp16(TestCollectiveAPIRunnerBase): ...@@ -210,7 +210,9 @@ class TestDygraphHybridFp16(TestCollectiveAPIRunnerBase):
loss_base_arr.append(loss_base.numpy()) loss_base_arr.append(loss_base.numpy())
loss_hybrid_arr.append(loss) loss_hybrid_arr.append(loss)
assert np.allclose(loss_base_arr, loss_hybrid_arr, rtol=1e-3, atol=1e-3) np.testing.assert_allclose(
loss_base_arr, loss_hybrid_arr, rtol=1e-3, atol=1e-3
)
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -186,7 +186,9 @@ class TestDygrapgHybridRecompute(TestCollectiveAPIRunnerBase): ...@@ -186,7 +186,9 @@ class TestDygrapgHybridRecompute(TestCollectiveAPIRunnerBase):
loss_base_arr.append(loss_base.numpy()) loss_base_arr.append(loss_base.numpy())
loss_hybrid_arr.append(loss) loss_hybrid_arr.append(loss)
assert np.allclose(loss_base_arr, loss_hybrid_arr, rtol=1e-5, atol=1e-5) np.testing.assert_allclose(
loss_base_arr, loss_hybrid_arr, rtol=1e-5, atol=1e-5
)
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -76,11 +76,11 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -76,11 +76,11 @@ class TestProcessGroupFp32(unittest.TestCase):
if rank == 0: if rank == 0:
task = pg.allreduce(tensor_x, core.ReduceOp.MAX) task = pg.allreduce(tensor_x, core.ReduceOp.MAX)
task.wait() task.wait()
assert np.array_equal(tensor_x, max_result) np.testing.assert_array_equal(tensor_x, max_result)
else: else:
task = pg.allreduce(tensor_y, core.ReduceOp.MAX) task = pg.allreduce(tensor_y, core.ReduceOp.MAX)
task.wait() task.wait()
assert np.array_equal(tensor_y, max_result) np.testing.assert_array_equal(tensor_y, max_result)
print("test allreduce max api ok") print("test allreduce max api ok")
...@@ -95,10 +95,10 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -95,10 +95,10 @@ class TestProcessGroupFp32(unittest.TestCase):
broadcast_result = paddle.assign(tensor_x) broadcast_result = paddle.assign(tensor_x)
if rank == 0: if rank == 0:
task = pg.broadcast(tensor_x, 0) task = pg.broadcast(tensor_x, 0)
assert np.array_equal(broadcast_result, tensor_x) np.testing.assert_array_equal(broadcast_result, tensor_x)
else: else:
task = pg.broadcast(tensor_y, 0) task = pg.broadcast(tensor_y, 0)
assert np.array_equal(broadcast_result, tensor_y) np.testing.assert_array_equal(broadcast_result, tensor_y)
print("test broadcast api ok") print("test broadcast api ok")
# test send_recv # test send_recv
...@@ -116,11 +116,11 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -116,11 +116,11 @@ class TestProcessGroupFp32(unittest.TestCase):
task = pg.send(tensor_x, pg.size() - 1, True) task = pg.send(tensor_x, pg.size() - 1, True)
elif pg.rank() == pg.size() - 1: elif pg.rank() == pg.size() - 1:
task = pg.recv(tensor_y_1, 0, True) task = pg.recv(tensor_y_1, 0, True)
assert np.array_equal(send_recv_result_1, tensor_y_1) np.testing.assert_array_equal(send_recv_result_1, tensor_y_1)
if pg.rank() == 0: if pg.rank() == 0:
task = pg.recv(tensor_x, pg.size() - 1, True) task = pg.recv(tensor_x, pg.size() - 1, True)
assert np.array_equal(send_recv_result_2, tensor_x) np.testing.assert_array_equal(send_recv_result_2, tensor_x)
elif pg.rank() == pg.size() - 1: elif pg.rank() == pg.size() - 1:
task = pg.send(tensor_y_2, 0, True) task = pg.send(tensor_y_2, 0, True)
print("test send_recv api ok") print("test send_recv api ok")
...@@ -159,8 +159,8 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -159,8 +159,8 @@ class TestProcessGroupFp32(unittest.TestCase):
out_2 = paddle.slice( out_2 = paddle.slice(
tensor_out, [0], [out_shape[0] // 2], [out_shape[0]] tensor_out, [0], [out_shape[0] // 2], [out_shape[0]]
) )
assert np.array_equal(tensor_x, out_1) np.testing.assert_array_equal(tensor_x, out_1)
assert np.array_equal(tensor_y, out_2) np.testing.assert_array_equal(tensor_y, out_2)
print("test allgather api ok\n") print("test allgather api ok\n")
# test Reduce # test Reduce
...@@ -178,7 +178,7 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -178,7 +178,7 @@ class TestProcessGroupFp32(unittest.TestCase):
task = pg.reduce(tensor_y, 0) task = pg.reduce(tensor_y, 0)
task.wait() task.wait()
if pg.rank() == 0: if pg.rank() == 0:
assert np.array_equal(tensor_x, sum_result) np.testing.assert_array_equal(tensor_x, sum_result)
print("test reduce sum api ok\n") print("test reduce sum api ok\n")
# test Scatter # test Scatter
...@@ -199,9 +199,9 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -199,9 +199,9 @@ class TestProcessGroupFp32(unittest.TestCase):
out1 = paddle.slice(tensor_x, [0], [0], [self.shape[0]]) out1 = paddle.slice(tensor_x, [0], [0], [self.shape[0]])
out2 = paddle.slice(tensor_x, [0], [self.shape[0]], [self.shape[0] * 2]) out2 = paddle.slice(tensor_x, [0], [self.shape[0]], [self.shape[0] * 2])
if pg.rank() == 0: if pg.rank() == 0:
assert np.array_equal(tensor_y, out1) np.testing.assert_array_equal(tensor_y, out1)
else: else:
assert np.array_equal(tensor_y, out2) np.testing.assert_array_equal(tensor_y, out2)
print("test scatter api ok\n") print("test scatter api ok\n")
# test Gather # test Gather
...@@ -219,7 +219,7 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -219,7 +219,7 @@ class TestProcessGroupFp32(unittest.TestCase):
if pg.rank() == root: if pg.rank() == root:
task = pg.gather(tensor_y[root], tensor_x, root, True) task = pg.gather(tensor_y[root], tensor_x, root, True)
task.wait() task.wait()
assert np.array_equal(tensor_x, tensor_y) np.testing.assert_array_equal(tensor_x, tensor_y)
else: else:
task = pg.gather(tensor_y[pg.rank()], tensor_x, root, True) task = pg.gather(tensor_y[pg.rank()], tensor_x, root, True)
task.wait() task.wait()
......
...@@ -69,10 +69,10 @@ def test_allreduce_sum(pg, shape, dtype): ...@@ -69,10 +69,10 @@ def test_allreduce_sum(pg, shape, dtype):
sum_result = tensor_x + tensor_y sum_result = tensor_x + tensor_y
if pg.rank() == 0: if pg.rank() == 0:
task = dist.all_reduce(tensor_x) task = dist.all_reduce(tensor_x)
assert np.array_equal(tensor_x, sum_result) np.testing.assert_array_equal(tensor_x, sum_result)
else: else:
task = dist.all_reduce(tensor_y) task = dist.all_reduce(tensor_y)
assert np.array_equal(tensor_y, sum_result) np.testing.assert_array_equal(tensor_y, sum_result)
print("test allreduce sum api ok") print("test allreduce sum api ok")
...@@ -91,13 +91,13 @@ def test_allreduce_max(pg, shape, dtype): ...@@ -91,13 +91,13 @@ def test_allreduce_max(pg, shape, dtype):
tensor_x, dist.ReduceOp.MAX, use_calc_stream=False tensor_x, dist.ReduceOp.MAX, use_calc_stream=False
) )
task.wait() task.wait()
assert np.array_equal(tensor_x, max_result) np.testing.assert_array_equal(tensor_x, max_result)
else: else:
task = dist.all_reduce( task = dist.all_reduce(
tensor_y, dist.ReduceOp.MAX, use_calc_stream=False tensor_y, dist.ReduceOp.MAX, use_calc_stream=False
) )
task.wait() task.wait()
assert np.array_equal(tensor_y, max_result) np.testing.assert_array_equal(tensor_y, max_result)
print("test allreduce max api ok") print("test allreduce max api ok")
...@@ -116,13 +116,13 @@ def test_allreduce_min(pg, shape, dtype): ...@@ -116,13 +116,13 @@ def test_allreduce_min(pg, shape, dtype):
tensor_x, dist.ReduceOp.MIN, use_calc_stream=False tensor_x, dist.ReduceOp.MIN, use_calc_stream=False
) )
task.wait() task.wait()
assert np.array_equal(tensor_x, min_result) np.testing.assert_array_equal(tensor_x, min_result)
else: else:
task = dist.all_reduce( task = dist.all_reduce(
tensor_y, dist.ReduceOp.MIN, use_calc_stream=False tensor_y, dist.ReduceOp.MIN, use_calc_stream=False
) )
task.wait() task.wait()
assert np.array_equal(tensor_y, min_result) np.testing.assert_array_equal(tensor_y, min_result)
print("test allreduce min api ok") print("test allreduce min api ok")
...@@ -141,13 +141,13 @@ def test_allreduce_prod(pg, shape, dtype): ...@@ -141,13 +141,13 @@ def test_allreduce_prod(pg, shape, dtype):
tensor_x, dist.ReduceOp.PROD, use_calc_stream=False tensor_x, dist.ReduceOp.PROD, use_calc_stream=False
) )
task.wait() task.wait()
assert np.array_equal(tensor_x, prod_result) np.testing.assert_array_equal(tensor_x, prod_result)
else: else:
task = dist.all_reduce( task = dist.all_reduce(
tensor_y, dist.ReduceOp.PROD, use_calc_stream=False tensor_y, dist.ReduceOp.PROD, use_calc_stream=False
) )
task.wait() task.wait()
assert np.array_equal(tensor_y, prod_result) np.testing.assert_array_equal(tensor_y, prod_result)
print("test allreduce prod api ok") print("test allreduce prod api ok")
...@@ -164,10 +164,10 @@ def test_broadcast(pg, shape, dtype): ...@@ -164,10 +164,10 @@ def test_broadcast(pg, shape, dtype):
task = dist.broadcast(tensor_x, 0, use_calc_stream=False) task = dist.broadcast(tensor_x, 0, use_calc_stream=False)
task.synchronize() task.synchronize()
assert task.is_completed() assert task.is_completed()
assert np.array_equal(broadcast_result, tensor_x) np.testing.assert_array_equal(broadcast_result, tensor_x)
else: else:
task = dist.broadcast(tensor_y, 0) task = dist.broadcast(tensor_y, 0)
assert np.array_equal(broadcast_result, tensor_y) np.testing.assert_array_equal(broadcast_result, tensor_y)
print("test broadcast api ok") print("test broadcast api ok")
...@@ -205,8 +205,8 @@ def test_allgather(pg, shape, dtype): ...@@ -205,8 +205,8 @@ def test_allgather(pg, shape, dtype):
tensor_out = paddle.concat(tensor_out_list) tensor_out = paddle.concat(tensor_out_list)
out_1 = paddle.slice(tensor_out, [0], [0], [out_shape[0] // 2]) out_1 = paddle.slice(tensor_out, [0], [0], [out_shape[0] // 2])
out_2 = paddle.slice(tensor_out, [0], [out_shape[0] // 2], [out_shape[0]]) out_2 = paddle.slice(tensor_out, [0], [out_shape[0] // 2], [out_shape[0]])
assert np.array_equal(tensor_x, out_1) np.testing.assert_array_equal(tensor_x, out_1)
assert np.array_equal(tensor_y, out_2) np.testing.assert_array_equal(tensor_y, out_2)
print("test allgather api ok\n") print("test allgather api ok\n")
if pg.rank() == 0: if pg.rank() == 0:
...@@ -219,8 +219,8 @@ def test_allgather(pg, shape, dtype): ...@@ -219,8 +219,8 @@ def test_allgather(pg, shape, dtype):
tensor_out = paddle.concat(tensor_out_list) tensor_out = paddle.concat(tensor_out_list)
out_1 = paddle.slice(tensor_out, [0], [0], [out_shape[0] // 2]) out_1 = paddle.slice(tensor_out, [0], [0], [out_shape[0] // 2])
out_2 = paddle.slice(tensor_out, [0], [out_shape[0] // 2], [out_shape[0]]) out_2 = paddle.slice(tensor_out, [0], [out_shape[0] // 2], [out_shape[0]])
assert np.array_equal(tensor_x, out_1) np.testing.assert_array_equal(tensor_x, out_1)
assert np.array_equal(tensor_y, out_2) np.testing.assert_array_equal(tensor_y, out_2)
print("test allgather api2 ok\n") print("test allgather api2 ok\n")
...@@ -249,9 +249,9 @@ def test_all2all(pg, shape, dtype): ...@@ -249,9 +249,9 @@ def test_all2all(pg, shape, dtype):
out1_2 = paddle.slice(tensor_out1, [0], [shape[0] // 2], [shape[0]]) out1_2 = paddle.slice(tensor_out1, [0], [shape[0] // 2], [shape[0]])
out2_1 = paddle.slice(tensor_out2, [0], [0], [shape[0] // 2]) out2_1 = paddle.slice(tensor_out2, [0], [0], [shape[0] // 2])
if pg.rank() == 0: if pg.rank() == 0:
assert np.array_equal(out1_2.numpy(), raw_tensor_y_1.numpy()) np.testing.assert_array_equal(out1_2.numpy(), raw_tensor_y_1.numpy())
else: else:
assert np.array_equal(out2_1, raw_tensor_x_2) np.testing.assert_array_equal(out2_1, raw_tensor_x_2)
print("test alltoall api ok\n") print("test alltoall api ok\n")
x = np.random.random(shape).astype(dtype) x = np.random.random(shape).astype(dtype)
...@@ -277,9 +277,9 @@ def test_all2all(pg, shape, dtype): ...@@ -277,9 +277,9 @@ def test_all2all(pg, shape, dtype):
out1_2 = paddle.slice(tensor_out1, [0], [shape[0] // 2], [shape[0]]) out1_2 = paddle.slice(tensor_out1, [0], [shape[0] // 2], [shape[0]])
out2_1 = paddle.slice(tensor_out2, [0], [0], [shape[0] // 2]) out2_1 = paddle.slice(tensor_out2, [0], [0], [shape[0] // 2])
if pg.rank() == 0: if pg.rank() == 0:
assert np.array_equal(out1_2.numpy(), raw_tensor_y_1.numpy()) np.testing.assert_array_equal(out1_2.numpy(), raw_tensor_y_1.numpy())
else: else:
assert np.array_equal(out2_1, raw_tensor_x_2) np.testing.assert_array_equal(out2_1, raw_tensor_x_2)
print("test alltoall api2 ok\n") print("test alltoall api2 ok\n")
...@@ -297,7 +297,7 @@ def test_reduce_sum(pg, shape, dtype): ...@@ -297,7 +297,7 @@ def test_reduce_sum(pg, shape, dtype):
task = dist.reduce(tensor_y, 0, use_calc_stream=False) task = dist.reduce(tensor_y, 0, use_calc_stream=False)
task.wait() task.wait()
if pg.rank() == 0: if pg.rank() == 0:
assert np.array_equal(tensor_x, sum_result) np.testing.assert_array_equal(tensor_x, sum_result)
print("test reduce sum api ok\n") print("test reduce sum api ok\n")
...@@ -316,7 +316,7 @@ def test_reduce_max(pg, shape, dtype): ...@@ -316,7 +316,7 @@ def test_reduce_max(pg, shape, dtype):
tensor_x, 0, dist.ReduceOp.MAX, use_calc_stream=False tensor_x, 0, dist.ReduceOp.MAX, use_calc_stream=False
) )
task.wait() task.wait()
assert np.array_equal(tensor_x, max_result) np.testing.assert_array_equal(tensor_x, max_result)
else: else:
task = dist.reduce( task = dist.reduce(
tensor_y, 0, dist.ReduceOp.MAX, use_calc_stream=False tensor_y, 0, dist.ReduceOp.MAX, use_calc_stream=False
...@@ -340,7 +340,7 @@ def test_reduce_min(pg, shape, dtype): ...@@ -340,7 +340,7 @@ def test_reduce_min(pg, shape, dtype):
tensor_x, 0, dist.ReduceOp.MIN, use_calc_stream=False tensor_x, 0, dist.ReduceOp.MIN, use_calc_stream=False
) )
task.wait() task.wait()
assert np.array_equal(tensor_x, min_result) np.testing.assert_array_equal(tensor_x, min_result)
else: else:
task = dist.reduce( task = dist.reduce(
tensor_y, 0, dist.ReduceOp.MIN, use_calc_stream=False tensor_y, 0, dist.ReduceOp.MIN, use_calc_stream=False
...@@ -364,7 +364,7 @@ def test_reduce_prod(pg, shape, dtype): ...@@ -364,7 +364,7 @@ def test_reduce_prod(pg, shape, dtype):
tensor_x, 0, dist.ReduceOp.PROD, use_calc_stream=False tensor_x, 0, dist.ReduceOp.PROD, use_calc_stream=False
) )
task.wait() task.wait()
assert np.array_equal(tensor_x, prod_result) np.testing.assert_array_equal(tensor_x, prod_result)
else: else:
task = dist.reduce( task = dist.reduce(
tensor_y, 0, dist.ReduceOp.PROD, use_calc_stream=False tensor_y, 0, dist.ReduceOp.PROD, use_calc_stream=False
...@@ -391,9 +391,9 @@ def test_scatter(pg, shape, dtype): ...@@ -391,9 +391,9 @@ def test_scatter(pg, shape, dtype):
out1 = paddle.slice(tensor_x, [0], [0], [shape[0]]) out1 = paddle.slice(tensor_x, [0], [0], [shape[0]])
out2 = paddle.slice(tensor_x, [0], [shape[0]], [shape[0] * 2]) out2 = paddle.slice(tensor_x, [0], [shape[0]], [shape[0] * 2])
if pg.rank() == 0: if pg.rank() == 0:
assert np.array_equal(tensor_y, out1) np.testing.assert_array_equal(tensor_y, out1)
else: else:
assert np.array_equal(tensor_y, out2) np.testing.assert_array_equal(tensor_y, out2)
print("test scatter api ok\n") print("test scatter api ok\n")
...@@ -411,7 +411,7 @@ def test_send_recv(pg, sub_group, shape, dtype): ...@@ -411,7 +411,7 @@ def test_send_recv(pg, sub_group, shape, dtype):
elif pg.rank() == 1: elif pg.rank() == 1:
task = dist.recv(tensor_y, 0, group=sub_group, use_calc_stream=False) task = dist.recv(tensor_y, 0, group=sub_group, use_calc_stream=False)
task.wait() task.wait()
assert np.array_equal(tensor_y, tensor_x) np.testing.assert_array_equal(tensor_y, tensor_x)
print("test send api ok") print("test send api ok")
...@@ -427,7 +427,7 @@ def test_send_recv(pg, sub_group, shape, dtype): ...@@ -427,7 +427,7 @@ def test_send_recv(pg, sub_group, shape, dtype):
task = dist.send(tensor_x, 1, group=sub_group, use_calc_stream=True) task = dist.send(tensor_x, 1, group=sub_group, use_calc_stream=True)
elif pg.rank() == 1: elif pg.rank() == 1:
task = dist.recv(tensor_y, 0, group=sub_group, use_calc_stream=True) task = dist.recv(tensor_y, 0, group=sub_group, use_calc_stream=True)
assert np.array_equal(tensor_y, tensor_x) np.testing.assert_array_equal(tensor_y, tensor_x)
print("test send api ok") print("test send api ok")
......
...@@ -64,10 +64,10 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -64,10 +64,10 @@ class TestProcessGroupFp32(unittest.TestCase):
sum_result = tensor_x + tensor_y sum_result = tensor_x + tensor_y
if pg.rank() == 0: if pg.rank() == 0:
task = dist.all_reduce(tensor_x) task = dist.all_reduce(tensor_x)
assert np.array_equal(tensor_x, sum_result) np.testing.assert_array_equal(tensor_x, sum_result)
else: else:
task = dist.all_reduce(tensor_y) task = dist.all_reduce(tensor_y)
assert np.array_equal(tensor_y, sum_result) np.testing.assert_array_equal(tensor_y, sum_result)
print("test allreduce sum api ok") print("test allreduce sum api ok")
...@@ -82,10 +82,10 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -82,10 +82,10 @@ class TestProcessGroupFp32(unittest.TestCase):
sum_result = tensor_x + tensor_y sum_result = tensor_x + tensor_y
if pg.rank() == 0: if pg.rank() == 0:
task = dist.all_reduce(tensor_x) task = dist.all_reduce(tensor_x)
assert np.array_equal(tensor_x, sum_result) np.testing.assert_array_equal(tensor_x, sum_result)
else: else:
task = dist.all_reduce(tensor_y) task = dist.all_reduce(tensor_y)
assert np.array_equal(tensor_y, sum_result) np.testing.assert_array_equal(tensor_y, sum_result)
print("test allreduce sum api with = [] ok") print("test allreduce sum api with = [] ok")
...@@ -102,11 +102,11 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -102,11 +102,11 @@ class TestProcessGroupFp32(unittest.TestCase):
if pg.rank() == 0: if pg.rank() == 0:
task = dist.all_reduce(tensor_x, dist.ReduceOp.MAX, sync_op=False) task = dist.all_reduce(tensor_x, dist.ReduceOp.MAX, sync_op=False)
task.wait() task.wait()
assert np.array_equal(tensor_x, max_result) np.testing.assert_array_equal(tensor_x, max_result)
else: else:
task = dist.all_reduce(tensor_y, dist.ReduceOp.MAX, sync_op=False) task = dist.all_reduce(tensor_y, dist.ReduceOp.MAX, sync_op=False)
task.wait() task.wait()
assert np.array_equal(tensor_y, max_result) np.testing.assert_array_equal(tensor_y, max_result)
print("test allreduce max api ok") print("test allreduce max api ok")
...@@ -123,11 +123,11 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -123,11 +123,11 @@ class TestProcessGroupFp32(unittest.TestCase):
if pg.rank() == 0: if pg.rank() == 0:
task = dist.all_reduce(tensor_x, dist.ReduceOp.MAX, sync_op=False) task = dist.all_reduce(tensor_x, dist.ReduceOp.MAX, sync_op=False)
task.wait() task.wait()
assert np.array_equal(tensor_x, max_result) np.testing.assert_array_equal(tensor_x, max_result)
else: else:
task = dist.all_reduce(tensor_y, dist.ReduceOp.MAX, sync_op=False) task = dist.all_reduce(tensor_y, dist.ReduceOp.MAX, sync_op=False)
task.wait() task.wait()
assert np.array_equal(tensor_y, max_result) np.testing.assert_array_equal(tensor_y, max_result)
print("test allreduce max api with shape = [] ok") print("test allreduce max api with shape = [] ok")
...@@ -144,11 +144,11 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -144,11 +144,11 @@ class TestProcessGroupFp32(unittest.TestCase):
if pg.rank() == 0: if pg.rank() == 0:
task = dist.all_reduce(tensor_x, dist.ReduceOp.MIN, sync_op=False) task = dist.all_reduce(tensor_x, dist.ReduceOp.MIN, sync_op=False)
task.wait() task.wait()
assert np.array_equal(tensor_x, min_result) np.testing.assert_array_equal(tensor_x, min_result)
else: else:
task = dist.all_reduce(tensor_y, dist.ReduceOp.MIN, sync_op=False) task = dist.all_reduce(tensor_y, dist.ReduceOp.MIN, sync_op=False)
task.wait() task.wait()
assert np.array_equal(tensor_y, min_result) np.testing.assert_array_equal(tensor_y, min_result)
print("test allreduce min api ok") print("test allreduce min api ok")
...@@ -165,11 +165,11 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -165,11 +165,11 @@ class TestProcessGroupFp32(unittest.TestCase):
if pg.rank() == 0: if pg.rank() == 0:
task = dist.all_reduce(tensor_x, dist.ReduceOp.MIN, sync_op=False) task = dist.all_reduce(tensor_x, dist.ReduceOp.MIN, sync_op=False)
task.wait() task.wait()
assert np.array_equal(tensor_x, min_result) np.testing.assert_array_equal(tensor_x, min_result)
else: else:
task = dist.all_reduce(tensor_y, dist.ReduceOp.MIN, sync_op=False) task = dist.all_reduce(tensor_y, dist.ReduceOp.MIN, sync_op=False)
task.wait() task.wait()
assert np.array_equal(tensor_y, min_result) np.testing.assert_array_equal(tensor_y, min_result)
print("test allreduce min api with shape [] ok") print("test allreduce min api with shape [] ok")
...@@ -186,11 +186,11 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -186,11 +186,11 @@ class TestProcessGroupFp32(unittest.TestCase):
if pg.rank() == 0: if pg.rank() == 0:
task = dist.all_reduce(tensor_x, dist.ReduceOp.PROD, sync_op=False) task = dist.all_reduce(tensor_x, dist.ReduceOp.PROD, sync_op=False)
task.wait() task.wait()
assert np.array_equal(tensor_x, prod_result) np.testing.assert_array_equal(tensor_x, prod_result)
else: else:
task = dist.all_reduce(tensor_y, dist.ReduceOp.PROD, sync_op=False) task = dist.all_reduce(tensor_y, dist.ReduceOp.PROD, sync_op=False)
task.wait() task.wait()
assert np.array_equal(tensor_y, prod_result) np.testing.assert_array_equal(tensor_y, prod_result)
print("test allreduce prod api ok") print("test allreduce prod api ok")
...@@ -207,11 +207,11 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -207,11 +207,11 @@ class TestProcessGroupFp32(unittest.TestCase):
if pg.rank() == 0: if pg.rank() == 0:
task = dist.all_reduce(tensor_x, dist.ReduceOp.PROD, sync_op=False) task = dist.all_reduce(tensor_x, dist.ReduceOp.PROD, sync_op=False)
task.wait() task.wait()
assert np.array_equal(tensor_x, prod_result) np.testing.assert_array_equal(tensor_x, prod_result)
else: else:
task = dist.all_reduce(tensor_y, dist.ReduceOp.PROD, sync_op=False) task = dist.all_reduce(tensor_y, dist.ReduceOp.PROD, sync_op=False)
task.wait() task.wait()
assert np.array_equal(tensor_y, prod_result) np.testing.assert_array_equal(tensor_y, prod_result)
print("test allreduce prod api with shape = [] ok") print("test allreduce prod api with shape = [] ok")
...@@ -229,11 +229,11 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -229,11 +229,11 @@ class TestProcessGroupFp32(unittest.TestCase):
task.synchronize() task.synchronize()
paddle.device.cuda.synchronize() paddle.device.cuda.synchronize()
assert task.is_completed() assert task.is_completed()
assert np.array_equal(broadcast_result, tensor_x) np.testing.assert_array_equal(broadcast_result, tensor_x)
else: else:
task = dist.broadcast(tensor_y, 0) task = dist.broadcast(tensor_y, 0)
paddle.device.cuda.synchronize() paddle.device.cuda.synchronize()
assert np.array_equal(broadcast_result, tensor_y) np.testing.assert_array_equal(broadcast_result, tensor_y)
print("test broadcast api ok") print("test broadcast api ok")
...@@ -251,11 +251,11 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -251,11 +251,11 @@ class TestProcessGroupFp32(unittest.TestCase):
task.synchronize() task.synchronize()
paddle.device.cuda.synchronize() paddle.device.cuda.synchronize()
assert task.is_completed() assert task.is_completed()
assert np.array_equal(broadcast_result, tensor_x) np.testing.assert_array_equal(broadcast_result, tensor_x)
else: else:
task = dist.broadcast(tensor_y, 0) task = dist.broadcast(tensor_y, 0)
paddle.device.cuda.synchronize() paddle.device.cuda.synchronize()
assert np.array_equal(broadcast_result, tensor_y) np.testing.assert_array_equal(broadcast_result, tensor_y)
assert tensor_y.shape == [] assert tensor_y.shape == []
print("test broadcast api with shape=[] ok") print("test broadcast api with shape=[] ok")
...@@ -298,8 +298,8 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -298,8 +298,8 @@ class TestProcessGroupFp32(unittest.TestCase):
out_2 = paddle.slice( out_2 = paddle.slice(
tensor_out, [0], [out_shape[0] // 2], [out_shape[0]] tensor_out, [0], [out_shape[0] // 2], [out_shape[0]]
) )
assert np.array_equal(tensor_x, out_1) np.testing.assert_array_equal(tensor_x, out_1)
assert np.array_equal(tensor_y, out_2) np.testing.assert_array_equal(tensor_y, out_2)
print("test allgather api ok\n") print("test allgather api ok\n")
if pg.rank() == 0: if pg.rank() == 0:
...@@ -316,8 +316,8 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -316,8 +316,8 @@ class TestProcessGroupFp32(unittest.TestCase):
out_2 = paddle.slice( out_2 = paddle.slice(
tensor_out, [0], [out_shape[0] // 2], [out_shape[0]] tensor_out, [0], [out_shape[0] // 2], [out_shape[0]]
) )
assert np.array_equal(tensor_x, out_1) np.testing.assert_array_equal(tensor_x, out_1)
assert np.array_equal(tensor_y, out_2) np.testing.assert_array_equal(tensor_y, out_2)
print("test allgather api2 ok\n") print("test allgather api2 ok\n")
# test allgather with shape = [] # test allgather with shape = []
...@@ -337,8 +337,8 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -337,8 +337,8 @@ class TestProcessGroupFp32(unittest.TestCase):
paddle.device.cuda.synchronize() paddle.device.cuda.synchronize()
out_1 = tensor_out_list[0] out_1 = tensor_out_list[0]
out_2 = tensor_out_list[1] out_2 = tensor_out_list[1]
assert np.array_equal(tensor_x, out_1) np.testing.assert_array_equal(tensor_x, out_1)
assert np.array_equal(tensor_y, out_2) np.testing.assert_array_equal(tensor_y, out_2)
print("test allgather api with shape [] ok\n") print("test allgather api with shape [] ok\n")
# test alltoall # test alltoall
...@@ -371,9 +371,11 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -371,9 +371,11 @@ class TestProcessGroupFp32(unittest.TestCase):
) )
out2_1 = paddle.slice(tensor_out2, [0], [0], [self.shape[0] // 2]) out2_1 = paddle.slice(tensor_out2, [0], [0], [self.shape[0] // 2])
if pg.rank() == 0: if pg.rank() == 0:
assert np.array_equal(out1_2.numpy(), raw_tensor_y_1.numpy()) np.testing.assert_array_equal(
out1_2.numpy(), raw_tensor_y_1.numpy()
)
else: else:
assert np.array_equal(out2_1, raw_tensor_x_2) np.testing.assert_array_equal(out2_1, raw_tensor_x_2)
print("test alltoall api ok\n") print("test alltoall api ok\n")
x = np.random.random(self.shape).astype(self.dtype) x = np.random.random(self.shape).astype(self.dtype)
...@@ -404,9 +406,11 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -404,9 +406,11 @@ class TestProcessGroupFp32(unittest.TestCase):
) )
out2_1 = paddle.slice(tensor_out2, [0], [0], [self.shape[0] // 2]) out2_1 = paddle.slice(tensor_out2, [0], [0], [self.shape[0] // 2])
if pg.rank() == 0: if pg.rank() == 0:
assert np.array_equal(out1_2.numpy(), raw_tensor_y_1.numpy()) np.testing.assert_array_equal(
out1_2.numpy(), raw_tensor_y_1.numpy()
)
else: else:
assert np.array_equal(out2_1, raw_tensor_x_2) np.testing.assert_array_equal(out2_1, raw_tensor_x_2)
print("test alltoall api2 ok\n") print("test alltoall api2 ok\n")
# test Reduce # test Reduce
...@@ -425,7 +429,7 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -425,7 +429,7 @@ class TestProcessGroupFp32(unittest.TestCase):
task.wait() task.wait()
paddle.device.cuda.synchronize() paddle.device.cuda.synchronize()
if pg.rank() == 0: if pg.rank() == 0:
assert np.array_equal(tensor_x, sum_result) np.testing.assert_array_equal(tensor_x, sum_result)
print("test reduce sum api ok\n") print("test reduce sum api ok\n")
# test reduce max # test reduce max
...@@ -441,7 +445,7 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -441,7 +445,7 @@ class TestProcessGroupFp32(unittest.TestCase):
if pg.rank() == 0: if pg.rank() == 0:
task = dist.reduce(tensor_x, 0, dist.ReduceOp.MAX, sync_op=False) task = dist.reduce(tensor_x, 0, dist.ReduceOp.MAX, sync_op=False)
task.wait() task.wait()
assert np.array_equal(tensor_x, max_result) np.testing.assert_array_equal(tensor_x, max_result)
else: else:
task = dist.reduce(tensor_y, 0, dist.ReduceOp.MAX, sync_op=False) task = dist.reduce(tensor_y, 0, dist.ReduceOp.MAX, sync_op=False)
task.wait() task.wait()
...@@ -461,7 +465,7 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -461,7 +465,7 @@ class TestProcessGroupFp32(unittest.TestCase):
if pg.rank() == 0: if pg.rank() == 0:
task = dist.reduce(tensor_x, 0, dist.ReduceOp.MIN, sync_op=False) task = dist.reduce(tensor_x, 0, dist.ReduceOp.MIN, sync_op=False)
task.wait() task.wait()
assert np.array_equal(tensor_x, min_result) np.testing.assert_array_equal(tensor_x, min_result)
else: else:
task = dist.reduce(tensor_y, 0, dist.ReduceOp.MIN, sync_op=False) task = dist.reduce(tensor_y, 0, dist.ReduceOp.MIN, sync_op=False)
task.wait() task.wait()
...@@ -481,7 +485,7 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -481,7 +485,7 @@ class TestProcessGroupFp32(unittest.TestCase):
if pg.rank() == 0: if pg.rank() == 0:
task = dist.reduce(tensor_x, 0, dist.ReduceOp.PROD, sync_op=False) task = dist.reduce(tensor_x, 0, dist.ReduceOp.PROD, sync_op=False)
task.wait() task.wait()
assert np.array_equal(tensor_x, prod_result) np.testing.assert_array_equal(tensor_x, prod_result)
else: else:
task = dist.reduce(tensor_y, 0, dist.ReduceOp.PROD, sync_op=False) task = dist.reduce(tensor_y, 0, dist.ReduceOp.PROD, sync_op=False)
task.wait() task.wait()
...@@ -511,9 +515,9 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -511,9 +515,9 @@ class TestProcessGroupFp32(unittest.TestCase):
out1 = paddle.slice(tensor_x, [0], [0], [self.shape[0]]) out1 = paddle.slice(tensor_x, [0], [0], [self.shape[0]])
out2 = paddle.slice(tensor_x, [0], [self.shape[0]], [self.shape[0] * 2]) out2 = paddle.slice(tensor_x, [0], [self.shape[0]], [self.shape[0] * 2])
if pg.rank() == 0: if pg.rank() == 0:
assert np.array_equal(tensor_y, out1) np.testing.assert_array_equal(tensor_y, out1)
else: else:
assert np.array_equal(tensor_y, out2) np.testing.assert_array_equal(tensor_y, out2)
print("test scatter api ok\n") print("test scatter api ok\n")
# test Scatter with shape=[] # test Scatter with shape=[]
...@@ -534,9 +538,9 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -534,9 +538,9 @@ class TestProcessGroupFp32(unittest.TestCase):
out1 = paddle.assign(tensor_x) out1 = paddle.assign(tensor_x)
out2 = paddle.assign(tensor_x + 1) out2 = paddle.assign(tensor_x + 1)
if pg.rank() == 0: if pg.rank() == 0:
assert np.array_equal(tensor_y, out1) np.testing.assert_array_equal(tensor_y, out1)
else: else:
assert np.array_equal(tensor_y, out2), f"{tensor_y}, {out2}" np.testing.assert_array_equal(tensor_y, out2)
assert tensor_y.shape == [] assert tensor_y.shape == []
print("test scatter api with shape=[] ok\n") print("test scatter api with shape=[] ok\n")
...@@ -554,7 +558,7 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -554,7 +558,7 @@ class TestProcessGroupFp32(unittest.TestCase):
else: else:
task = dist.recv(tensor_y, 0, sync_op=False) task = dist.recv(tensor_y, 0, sync_op=False)
task.wait() task.wait()
assert np.array_equal(tensor_y, tensor_x) np.testing.assert_array_equal(tensor_y, tensor_x)
print("test send api ok") print("test send api ok")
...@@ -570,7 +574,7 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -570,7 +574,7 @@ class TestProcessGroupFp32(unittest.TestCase):
task = dist.send(tensor_x, 1, sync_op=True) task = dist.send(tensor_x, 1, sync_op=True)
else: else:
task = dist.recv(tensor_y, 0, sync_op=True) task = dist.recv(tensor_y, 0, sync_op=True)
assert np.array_equal(tensor_y, tensor_x) np.testing.assert_array_equal(tensor_y, tensor_x)
print("test send api ok") print("test send api ok")
......
...@@ -31,7 +31,7 @@ def _check_using_all_reduce(group): ...@@ -31,7 +31,7 @@ def _check_using_all_reduce(group):
data = paddle.to_tensor([1, 2, 3]) data = paddle.to_tensor([1, 2, 3])
result = paddle.to_tensor([2, 4, 6]) result = paddle.to_tensor([2, 4, 6])
dist.all_reduce(data, group=group) dist.all_reduce(data, group=group)
assert np.array_equal(data, result) np.testing.assert_array_equal(data, result)
def _check_using_send(group, dst): def _check_using_send(group, dst):
...@@ -43,7 +43,7 @@ def _check_using_recv(group, src): ...@@ -43,7 +43,7 @@ def _check_using_recv(group, src):
result = paddle.to_tensor([1, 2, 3]) result = paddle.to_tensor([1, 2, 3])
data = paddle.to_tensor([0, 0, 0]) data = paddle.to_tensor([0, 0, 0])
dist.recv(data, src=src, group=group) dist.recv(data, src=src, group=group)
assert np.array_equal(data, result) np.testing.assert_array_equal(data, result)
class TestStrategyGroupAPI(unittest.TestCase): class TestStrategyGroupAPI(unittest.TestCase):
......
...@@ -698,17 +698,13 @@ class TestCycleGANModel(unittest.TestCase): ...@@ -698,17 +698,13 @@ class TestCycleGANModel(unittest.TestCase):
st_out = self.train(to_static=True) st_out = self.train(to_static=True)
dy_out = self.train(to_static=False) dy_out = self.train(to_static=False)
assert_func = np.allclose
# Note(Aurelius84): Because we disable BN on GPU, # Note(Aurelius84): Because we disable BN on GPU,
# but here we enhance the check on CPU by `np.array_equal` # but here we enhance the check on CPU by `np.array_equal`
# which means the dy_out and st_out shall be exactly same. # which means the dy_out and st_out shall be exactly same.
if not fluid.is_compiled_with_cuda(): if not fluid.is_compiled_with_cuda():
assert_func = np.array_equal np.testing.assert_array_equal(dy_out, st_out)
else:
self.assertTrue( np.testing.assert_allclose(dy_out, st_out, rtol=1e-5, atol=1e-8)
assert_func(dy_out, st_out),
msg=f"dy_out:\n {dy_out}\n st_out:\n{st_out}",
)
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -119,7 +119,7 @@ class TestPredictorRunWithTensor(unittest.TestCase): ...@@ -119,7 +119,7 @@ class TestPredictorRunWithTensor(unittest.TestCase):
inorder_output = self.get_inorder_output() inorder_output = self.get_inorder_output()
disorder_output = self.get_disorder_output() disorder_output = self.get_disorder_output()
assert np.allclose( np.testing.assert_allclose(
inorder_output.numpy().flatten(), disorder_output.numpy().flatten() inorder_output.numpy().flatten(), disorder_output.numpy().flatten()
) )
......
...@@ -3373,9 +3373,15 @@ class TestPow_factor_tensor(TestActivation): ...@@ -3373,9 +3373,15 @@ class TestPow_factor_tensor(TestActivation):
fetch_list=[out_1, out_2, res, out_6], fetch_list=[out_1, out_2, res, out_6],
) )
assert np.allclose(res_1, np.power(input, 2)) np.testing.assert_allclose(
assert np.allclose(res_2, np.power(input, 3)) res_1, np.power(input, 2), rtol=1e-5, atol=1e-8
assert np.allclose(res_6, np.power(input, 3)) )
np.testing.assert_allclose(
res_2, np.power(input, 3), rtol=1e-5, atol=1e-8
)
np.testing.assert_allclose(
res_6, np.power(input, 3), rtol=1e-5, atol=1e-8
)
def ref_stanh(x, scale_a=0.67, scale_b=1.7159): def ref_stanh(x, scale_a=0.67, scale_b=1.7159):
......
...@@ -148,15 +148,21 @@ class TestAdaptiveAvgPool2DAPI(unittest.TestCase): ...@@ -148,15 +148,21 @@ class TestAdaptiveAvgPool2DAPI(unittest.TestCase):
fetch_list=[out_1, out_2, out_3, out_4, out_5], fetch_list=[out_1, out_2, out_3, out_4, out_5],
) )
assert np.allclose(res_1, self.res_1_np) np.testing.assert_allclose(
res_1, self.res_1_np, rtol=1e-5, atol=1e-8
assert np.allclose(res_2, self.res_2_np) )
np.testing.assert_allclose(
assert np.allclose(res_3, self.res_3_np) res_2, self.res_2_np, rtol=1e-5, atol=1e-8
)
assert np.allclose(res_4, self.res_4_np) np.testing.assert_allclose(
res_3, self.res_3_np, rtol=1e-5, atol=1e-8
assert np.allclose(res_5, self.res_5_np) )
np.testing.assert_allclose(
res_4, self.res_4_np, rtol=1e-5, atol=1e-8
)
np.testing.assert_allclose(
res_5, self.res_5_np, rtol=1e-5, atol=1e-8
)
def test_dynamic_graph(self): def test_dynamic_graph(self):
for use_cuda in ( for use_cuda in (
...@@ -169,36 +175,38 @@ class TestAdaptiveAvgPool2DAPI(unittest.TestCase): ...@@ -169,36 +175,38 @@ class TestAdaptiveAvgPool2DAPI(unittest.TestCase):
out_1 = paddle.nn.functional.adaptive_avg_pool2d( out_1 = paddle.nn.functional.adaptive_avg_pool2d(
x=x, output_size=[3, 3] x=x, output_size=[3, 3]
) )
out_2 = paddle.nn.functional.adaptive_avg_pool2d(x=x, output_size=5) out_2 = paddle.nn.functional.adaptive_avg_pool2d(x=x, output_size=5)
out_3 = paddle.nn.functional.adaptive_avg_pool2d( out_3 = paddle.nn.functional.adaptive_avg_pool2d(
x=x, output_size=[2, 5] x=x, output_size=[2, 5]
) )
out_4 = paddle.nn.functional.adaptive_avg_pool2d( out_4 = paddle.nn.functional.adaptive_avg_pool2d(
x=x, output_size=[3, 3], data_format="NHWC" x=x, output_size=[3, 3], data_format="NHWC"
) )
out_5 = paddle.nn.functional.adaptive_avg_pool2d( out_5 = paddle.nn.functional.adaptive_avg_pool2d(
x=x, output_size=[None, 3] x=x, output_size=[None, 3]
) )
out_6 = paddle.nn.functional.interpolate( out_6 = paddle.nn.functional.interpolate(
x=x, mode="area", size=[2, 5] x=x, mode="area", size=[2, 5]
) )
assert np.allclose(out_1.numpy(), self.res_1_np) np.testing.assert_allclose(
out_1.numpy(), self.res_1_np, rtol=1e-5, atol=1e-8
assert np.allclose(out_2.numpy(), self.res_2_np) )
np.testing.assert_allclose(
assert np.allclose(out_3.numpy(), self.res_3_np) out_2.numpy(), self.res_2_np, rtol=1e-5, atol=1e-8
)
assert np.allclose(out_4.numpy(), self.res_4_np) np.testing.assert_allclose(
out_3.numpy(), self.res_3_np, rtol=1e-5, atol=1e-8
assert np.allclose(out_5.numpy(), self.res_5_np) )
np.testing.assert_allclose(
assert np.allclose(out_6.numpy(), self.res_3_np) out_4.numpy(), self.res_4_np, rtol=1e-5, atol=1e-8
)
np.testing.assert_allclose(
out_5.numpy(), self.res_5_np, rtol=1e-5, atol=1e-8
)
np.testing.assert_allclose(
out_6.numpy(), self.res_3_np, rtol=1e-5, atol=1e-8
)
class TestAdaptiveAvgPool2DClassAPI(unittest.TestCase): class TestAdaptiveAvgPool2DClassAPI(unittest.TestCase):
...@@ -260,15 +268,21 @@ class TestAdaptiveAvgPool2DClassAPI(unittest.TestCase): ...@@ -260,15 +268,21 @@ class TestAdaptiveAvgPool2DClassAPI(unittest.TestCase):
fetch_list=[out_1, out_2, out_3, out_4, out_5], fetch_list=[out_1, out_2, out_3, out_4, out_5],
) )
assert np.allclose(res_1, self.res_1_np) np.testing.assert_allclose(
res_1, self.res_1_np, rtol=1e-5, atol=1e-8
assert np.allclose(res_2, self.res_2_np) )
np.testing.assert_allclose(
assert np.allclose(res_3, self.res_3_np) res_2, self.res_2_np, rtol=1e-5, atol=1e-8
)
assert np.allclose(res_4, self.res_4_np) np.testing.assert_allclose(
res_3, self.res_3_np, rtol=1e-5, atol=1e-8
assert np.allclose(res_5, self.res_5_np) )
np.testing.assert_allclose(
res_4, self.res_4_np, rtol=1e-5, atol=1e-8
)
np.testing.assert_allclose(
res_5, self.res_5_np, rtol=1e-5, atol=1e-8
)
def test_dynamic_graph(self): def test_dynamic_graph(self):
for use_cuda in ( for use_cuda in (
...@@ -297,15 +311,21 @@ class TestAdaptiveAvgPool2DClassAPI(unittest.TestCase): ...@@ -297,15 +311,21 @@ class TestAdaptiveAvgPool2DClassAPI(unittest.TestCase):
) )
out_5 = adaptive_avg_pool(x=x) out_5 = adaptive_avg_pool(x=x)
assert np.allclose(out_1.numpy(), self.res_1_np) np.testing.assert_allclose(
out_1.numpy(), self.res_1_np, rtol=1e-5, atol=1e-8
assert np.allclose(out_2.numpy(), self.res_2_np) )
np.testing.assert_allclose(
assert np.allclose(out_3.numpy(), self.res_3_np) out_2.numpy(), self.res_2_np, rtol=1e-5, atol=1e-8
)
assert np.allclose(out_4.numpy(), self.res_4_np) np.testing.assert_allclose(
out_3.numpy(), self.res_3_np, rtol=1e-5, atol=1e-8
assert np.allclose(out_5.numpy(), self.res_5_np) )
np.testing.assert_allclose(
out_4.numpy(), self.res_4_np, rtol=1e-5, atol=1e-8
)
np.testing.assert_allclose(
out_5.numpy(), self.res_5_np, rtol=1e-5, atol=1e-8
)
class TestOutputSizeTensor(UnittestBase): class TestOutputSizeTensor(UnittestBase):
......
...@@ -169,15 +169,21 @@ class TestAdaptiveAvgPool3DAPI(unittest.TestCase): ...@@ -169,15 +169,21 @@ class TestAdaptiveAvgPool3DAPI(unittest.TestCase):
fetch_list=[out_1, out_2, out_3, out_4, out_5], fetch_list=[out_1, out_2, out_3, out_4, out_5],
) )
assert np.allclose(res_1, self.res_1_np) np.testing.assert_allclose(
res_1, self.res_1_np, rtol=1e-5, atol=1e-8
assert np.allclose(res_2, self.res_2_np) )
np.testing.assert_allclose(
assert np.allclose(res_3, self.res_3_np) res_2, self.res_2_np, rtol=1e-5, atol=1e-8
)
assert np.allclose(res_4, self.res_4_np) np.testing.assert_allclose(
res_3, self.res_3_np, rtol=1e-5, atol=1e-8
assert np.allclose(res_5, self.res_5_np) )
np.testing.assert_allclose(
res_4, self.res_4_np, rtol=1e-5, atol=1e-8
)
np.testing.assert_allclose(
res_5, self.res_5_np, rtol=1e-5, atol=1e-8
)
def test_dynamic_graph(self): def test_dynamic_graph(self):
for use_cuda in ( for use_cuda in (
...@@ -209,17 +215,24 @@ class TestAdaptiveAvgPool3DAPI(unittest.TestCase): ...@@ -209,17 +215,24 @@ class TestAdaptiveAvgPool3DAPI(unittest.TestCase):
x=x, mode="area", size=[2, 3, 5] x=x, mode="area", size=[2, 3, 5]
) )
assert np.allclose(out_1.numpy(), self.res_1_np) np.testing.assert_allclose(
out_1.numpy(), self.res_1_np, rtol=1e-5, atol=1e-8
assert np.allclose(out_2.numpy(), self.res_2_np) )
np.testing.assert_allclose(
assert np.allclose(out_3.numpy(), self.res_3_np) out_2.numpy(), self.res_2_np, rtol=1e-5, atol=1e-8
)
assert np.allclose(out_4.numpy(), self.res_4_np) np.testing.assert_allclose(
out_3.numpy(), self.res_3_np, rtol=1e-5, atol=1e-8
assert np.allclose(out_5.numpy(), self.res_5_np) )
np.testing.assert_allclose(
assert np.allclose(out_6.numpy(), self.res_3_np) out_4.numpy(), self.res_4_np, rtol=1e-5, atol=1e-8
)
np.testing.assert_allclose(
out_5.numpy(), self.res_5_np, rtol=1e-5, atol=1e-8
)
np.testing.assert_allclose(
out_6.numpy(), self.res_3_np, rtol=1e-5, atol=1e-8
)
class TestAdaptiveAvgPool3DClassAPI(unittest.TestCase): class TestAdaptiveAvgPool3DClassAPI(unittest.TestCase):
...@@ -288,15 +301,21 @@ class TestAdaptiveAvgPool3DClassAPI(unittest.TestCase): ...@@ -288,15 +301,21 @@ class TestAdaptiveAvgPool3DClassAPI(unittest.TestCase):
fetch_list=[out_1, out_2, out_3, out_4, out_5], fetch_list=[out_1, out_2, out_3, out_4, out_5],
) )
assert np.allclose(res_1, self.res_1_np) np.testing.assert_allclose(
res_1, self.res_1_np, rtol=1e-5, atol=1e-8
assert np.allclose(res_2, self.res_2_np) )
np.testing.assert_allclose(
assert np.allclose(res_3, self.res_3_np) res_2, self.res_2_np, rtol=1e-5, atol=1e-8
)
assert np.allclose(res_4, self.res_4_np) np.testing.assert_allclose(
res_3, self.res_3_np, rtol=1e-5, atol=1e-8
assert np.allclose(res_5, self.res_5_np) )
np.testing.assert_allclose(
res_4, self.res_4_np, rtol=1e-5, atol=1e-8
)
np.testing.assert_allclose(
res_5, self.res_5_np, rtol=1e-5, atol=1e-8
)
def test_dynamic_graph(self): def test_dynamic_graph(self):
for use_cuda in ( for use_cuda in (
...@@ -329,15 +348,21 @@ class TestAdaptiveAvgPool3DClassAPI(unittest.TestCase): ...@@ -329,15 +348,21 @@ class TestAdaptiveAvgPool3DClassAPI(unittest.TestCase):
) )
out_5 = adaptive_avg_pool(x=x) out_5 = adaptive_avg_pool(x=x)
assert np.allclose(out_1.numpy(), self.res_1_np) np.testing.assert_allclose(
out_1.numpy(), self.res_1_np, rtol=1e-5, atol=1e-8
assert np.allclose(out_2.numpy(), self.res_2_np) )
np.testing.assert_allclose(
assert np.allclose(out_3.numpy(), self.res_3_np) out_2.numpy(), self.res_2_np, rtol=1e-5, atol=1e-8
)
assert np.allclose(out_4.numpy(), self.res_4_np) np.testing.assert_allclose(
out_3.numpy(), self.res_3_np, rtol=1e-5, atol=1e-8
assert np.allclose(out_5.numpy(), self.res_5_np) )
np.testing.assert_allclose(
out_4.numpy(), self.res_4_np, rtol=1e-5, atol=1e-8
)
np.testing.assert_allclose(
out_5.numpy(), self.res_5_np, rtol=1e-5, atol=1e-8
)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -149,15 +149,15 @@ class TestAdaptiveMaxPool2DAPI(unittest.TestCase): ...@@ -149,15 +149,15 @@ class TestAdaptiveMaxPool2DAPI(unittest.TestCase):
fetch_list=[out_1, out_2, out_3, out_5], fetch_list=[out_1, out_2, out_3, out_5],
) )
assert np.allclose(res_1, self.res_1_np) np.testing.assert_allclose(res_1, self.res_1_np)
assert np.allclose(res_2, self.res_2_np) np.testing.assert_allclose(res_2, self.res_2_np)
assert np.allclose(res_3, self.res_3_np) np.testing.assert_allclose(res_3, self.res_3_np)
# assert np.allclose(res_4, self.res_4_np) # np.testing.assert_allclose(res_4, self.res_4_np)
assert np.allclose(res_5, self.res_5_np) np.testing.assert_allclose(res_5, self.res_5_np)
def test_dynamic_graph(self): def test_dynamic_graph(self):
for use_cuda in ( for use_cuda in (
...@@ -184,15 +184,15 @@ class TestAdaptiveMaxPool2DAPI(unittest.TestCase): ...@@ -184,15 +184,15 @@ class TestAdaptiveMaxPool2DAPI(unittest.TestCase):
x=x, output_size=[None, 3] x=x, output_size=[None, 3]
) )
assert np.allclose(out_1.numpy(), self.res_1_np) np.testing.assert_allclose(out_1.numpy(), self.res_1_np)
assert np.allclose(out_2.numpy(), self.res_2_np) np.testing.assert_allclose(out_2.numpy(), self.res_2_np)
assert np.allclose(out_3.numpy(), self.res_3_np) np.testing.assert_allclose(out_3.numpy(), self.res_3_np)
# assert np.allclose(out_4.numpy(), self.res_4_np) # np.testing.assert_allclose(out_4.numpy(), self.res_4_np)
assert np.allclose(out_5.numpy(), self.res_5_np) np.testing.assert_allclose(out_5.numpy(), self.res_5_np)
class TestAdaptiveMaxPool2DClassAPI(unittest.TestCase): class TestAdaptiveMaxPool2DClassAPI(unittest.TestCase):
...@@ -255,15 +255,15 @@ class TestAdaptiveMaxPool2DClassAPI(unittest.TestCase): ...@@ -255,15 +255,15 @@ class TestAdaptiveMaxPool2DClassAPI(unittest.TestCase):
fetch_list=[out_1, out_2, out_3, out_5], fetch_list=[out_1, out_2, out_3, out_5],
) )
assert np.allclose(res_1, self.res_1_np) np.testing.assert_allclose(res_1, self.res_1_np)
assert np.allclose(res_2, self.res_2_np) np.testing.assert_allclose(res_2, self.res_2_np)
assert np.allclose(res_3, self.res_3_np) np.testing.assert_allclose(res_3, self.res_3_np)
# assert np.allclose(res_4, self.res_4_np) # np.testing.assert_allclose(res_4, self.res_4_np)
assert np.allclose(res_5, self.res_5_np) np.testing.assert_allclose(res_5, self.res_5_np)
def test_dynamic_graph(self): def test_dynamic_graph(self):
for use_cuda in ( for use_cuda in (
...@@ -291,15 +291,15 @@ class TestAdaptiveMaxPool2DClassAPI(unittest.TestCase): ...@@ -291,15 +291,15 @@ class TestAdaptiveMaxPool2DClassAPI(unittest.TestCase):
) )
out_5 = adaptive_max_pool(x=x) out_5 = adaptive_max_pool(x=x)
assert np.allclose(out_1.numpy(), self.res_1_np) np.testing.assert_allclose(out_1.numpy(), self.res_1_np)
assert np.allclose(out_2.numpy(), self.res_2_np) np.testing.assert_allclose(out_2.numpy(), self.res_2_np)
assert np.allclose(out_3.numpy(), self.res_3_np) np.testing.assert_allclose(out_3.numpy(), self.res_3_np)
# assert np.allclose(out_4.numpy(), self.res_4_np) # np.testing.assert_allclose(out_4.numpy(), self.res_4_np)
assert np.allclose(out_5.numpy(), self.res_5_np) np.testing.assert_allclose(out_5.numpy(), self.res_5_np)
class TestOutDtype(unittest.TestCase): class TestOutDtype(unittest.TestCase):
......
...@@ -170,15 +170,15 @@ class TestAdaptiveMaxPool3DAPI(unittest.TestCase): ...@@ -170,15 +170,15 @@ class TestAdaptiveMaxPool3DAPI(unittest.TestCase):
fetch_list=[out_1, out_2, out_3, out_5], fetch_list=[out_1, out_2, out_3, out_5],
) )
assert np.allclose(res_1, self.res_1_np) np.testing.assert_allclose(res_1, self.res_1_np)
assert np.allclose(res_2, self.res_2_np) np.testing.assert_allclose(res_2, self.res_2_np)
assert np.allclose(res_3, self.res_3_np) np.testing.assert_allclose(res_3, self.res_3_np)
# assert np.allclose(res_4, self.res_4_np) # np.testing.assert_allclose(res_4, self.res_4_np)
assert np.allclose(res_5, self.res_5_np) np.testing.assert_allclose(res_5, self.res_5_np)
def test_dynamic_graph(self): def test_dynamic_graph(self):
for use_cuda in ( for use_cuda in (
...@@ -205,15 +205,15 @@ class TestAdaptiveMaxPool3DAPI(unittest.TestCase): ...@@ -205,15 +205,15 @@ class TestAdaptiveMaxPool3DAPI(unittest.TestCase):
x=x, output_size=[None, 3, None] x=x, output_size=[None, 3, None]
) )
assert np.allclose(out_1.numpy(), self.res_1_np) np.testing.assert_allclose(out_1.numpy(), self.res_1_np)
assert np.allclose(out_2.numpy(), self.res_2_np) np.testing.assert_allclose(out_2.numpy(), self.res_2_np)
assert np.allclose(out_3.numpy(), self.res_3_np) np.testing.assert_allclose(out_3.numpy(), self.res_3_np)
# assert np.allclose(out_4.numpy(), self.res_4_np) # np.testing.assert_allclose(out_4.numpy(), self.res_4_np)
assert np.allclose(out_5.numpy(), self.res_5_np) np.testing.assert_allclose(out_5.numpy(), self.res_5_np)
class TestAdaptiveMaxPool3DClassAPI(unittest.TestCase): class TestAdaptiveMaxPool3DClassAPI(unittest.TestCase):
...@@ -280,15 +280,15 @@ class TestAdaptiveMaxPool3DClassAPI(unittest.TestCase): ...@@ -280,15 +280,15 @@ class TestAdaptiveMaxPool3DClassAPI(unittest.TestCase):
fetch_list=[out_1, out_2, out_3, out_5], fetch_list=[out_1, out_2, out_3, out_5],
) )
assert np.allclose(res_1, self.res_1_np) np.testing.assert_allclose(res_1, self.res_1_np)
assert np.allclose(res_2, self.res_2_np) np.testing.assert_allclose(res_2, self.res_2_np)
assert np.allclose(res_3, self.res_3_np) np.testing.assert_allclose(res_3, self.res_3_np)
# assert np.allclose(res_4, self.res_4_np) # assert np.allclose(res_4, self.res_4_np)
assert np.allclose(res_5, self.res_5_np) np.testing.assert_allclose(res_5, self.res_5_np)
def test_dynamic_graph(self): def test_dynamic_graph(self):
for use_cuda in ( for use_cuda in (
...@@ -320,15 +320,15 @@ class TestAdaptiveMaxPool3DClassAPI(unittest.TestCase): ...@@ -320,15 +320,15 @@ class TestAdaptiveMaxPool3DClassAPI(unittest.TestCase):
) )
out_5 = adaptive_max_pool(x=x) out_5 = adaptive_max_pool(x=x)
assert np.allclose(out_1.numpy(), self.res_1_np) np.testing.assert_allclose(out_1.numpy(), self.res_1_np)
assert np.allclose(out_2.numpy(), self.res_2_np) np.testing.assert_allclose(out_2.numpy(), self.res_2_np)
assert np.allclose(out_3.numpy(), self.res_3_np) np.testing.assert_allclose(out_3.numpy(), self.res_3_np)
# assert np.allclose(out_4.numpy(), self.res_4_np) # assert np.allclose(out_4.numpy(), self.res_4_np)
assert np.allclose(out_5.numpy(), self.res_5_np) np.testing.assert_allclose(out_5.numpy(), self.res_5_np)
class TestOutDtype(unittest.TestCase): class TestOutDtype(unittest.TestCase):
......
...@@ -328,7 +328,9 @@ class TestAddMMOp5(unittest.TestCase): ...@@ -328,7 +328,9 @@ class TestAddMMOp5(unittest.TestCase):
x = fluid.dygraph.to_variable(np_x) x = fluid.dygraph.to_variable(np_x)
y = fluid.dygraph.to_variable(np_y) y = fluid.dygraph.to_variable(np_y)
out = paddle.tensor.addmm(input, x, y) out = paddle.tensor.addmm(input, x, y)
assert np.allclose(np_input + np.dot(np_x, np_y), out.numpy()) np.testing.assert_allclose(
np_input + np.dot(np_x, np_y), out.numpy(), rtol=1e-5, atol=1e-8
)
class TestAddMMAPI(unittest.TestCase): class TestAddMMAPI(unittest.TestCase):
......
...@@ -173,7 +173,7 @@ class TestBatchNorm(unittest.TestCase): ...@@ -173,7 +173,7 @@ class TestBatchNorm(unittest.TestCase):
bn = paddle.nn.BatchNorm2D(shape[1]) bn = paddle.nn.BatchNorm2D(shape[1])
eag_y = bn(paddle.to_tensor(x)) eag_y = bn(paddle.to_tensor(x))
assert np.allclose(eag_y.numpy(), y.numpy()) np.testing.assert_allclose(eag_y.numpy(), y.numpy())
return y.numpy() return y.numpy()
def compute_v3(x, is_test, trainable_statistics): def compute_v3(x, is_test, trainable_statistics):
...@@ -351,10 +351,10 @@ class TestBatchNormChannelLast(unittest.TestCase): ...@@ -351,10 +351,10 @@ class TestBatchNormChannelLast(unittest.TestCase):
y.backward() y.backward()
y2.backward() y2.backward()
assert np.allclose( np.testing.assert_allclose(
y.numpy().flatten(), y2.numpy().flatten(), atol=1e-5, rtol=1e-5 y.numpy().flatten(), y2.numpy().flatten(), atol=1e-5, rtol=1e-5
) )
assert np.allclose( np.testing.assert_allclose(
bn1d.weight.grad.numpy().flatten(), bn1d.weight.grad.numpy().flatten(),
bn2d.weight.grad.numpy().flatten(), bn2d.weight.grad.numpy().flatten(),
atol=1e-5, atol=1e-5,
......
...@@ -66,9 +66,9 @@ class TestBroadcastToAPI(unittest.TestCase): ...@@ -66,9 +66,9 @@ class TestBroadcastToAPI(unittest.TestCase):
}, },
fetch_list=[out_1, out_2, out_3], fetch_list=[out_1, out_2, out_3],
) )
assert np.array_equal(res_1, np.tile(input, (1, 1))) np.testing.assert_array_equal(res_1, np.tile(input, (1, 1)))
assert np.array_equal(res_2, np.tile(input, (1, 1))) np.testing.assert_array_equal(res_2, np.tile(input, (1, 1)))
assert np.array_equal(res_3, np.tile(input, (1, 1))) np.testing.assert_array_equal(res_3, np.tile(input, (1, 1)))
def test_api_fp16_gpu(self): def test_api_fp16_gpu(self):
if paddle.fluid.core.is_compiled_with_cuda(): if paddle.fluid.core.is_compiled_with_cuda():
...@@ -101,9 +101,9 @@ class TestBroadcastToAPI(unittest.TestCase): ...@@ -101,9 +101,9 @@ class TestBroadcastToAPI(unittest.TestCase):
}, },
fetch_list=[out_1, out_2, out_3], fetch_list=[out_1, out_2, out_3],
) )
assert np.array_equal(res_1, np.tile(input, (1, 1))) np.testing.assert_array_equal(res_1, np.tile(input, (1, 1)))
assert np.array_equal(res_2, np.tile(input, (1, 1))) np.testing.assert_array_equal(res_2, np.tile(input, (1, 1)))
assert np.array_equal(res_3, np.tile(input, (1, 1))) np.testing.assert_array_equal(res_3, np.tile(input, (1, 1)))
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -120,8 +120,8 @@ class TestChannelShuffleAPI(unittest.TestCase): ...@@ -120,8 +120,8 @@ class TestChannelShuffleAPI(unittest.TestCase):
use_prune=True, use_prune=True,
) )
assert np.allclose(res_1, self.out_1_np) np.testing.assert_allclose(res_1[0], self.out_1_np)
assert np.allclose(res_2, self.out_2_np) np.testing.assert_allclose(res_2[0], self.out_2_np)
# same test between layer and functional in this op. # same test between layer and functional in this op.
def test_static_graph_layer(self): def test_static_graph_layer(self):
...@@ -160,8 +160,8 @@ class TestChannelShuffleAPI(unittest.TestCase): ...@@ -160,8 +160,8 @@ class TestChannelShuffleAPI(unittest.TestCase):
use_prune=True, use_prune=True,
) )
assert np.allclose(res_1, out_1_np) np.testing.assert_allclose(res_1[0], out_1_np)
assert np.allclose(res_2, out_2_np) np.testing.assert_allclose(res_2[0], out_2_np)
def run_dygraph(self, groups, data_format): def run_dygraph(self, groups, data_format):
n, c, h, w = 2, 9, 4, 4 n, c, h, w = 2, 9, 4, 4
......
...@@ -472,9 +472,15 @@ class TestConcatAPI(unittest.TestCase): ...@@ -472,9 +472,15 @@ class TestConcatAPI(unittest.TestCase):
feed={"x_1": input_2, "x_2": input_2, "x_3": input_3}, feed={"x_1": input_2, "x_2": input_2, "x_3": input_3},
fetch_list=[out_1, out_2, out_3], fetch_list=[out_1, out_2, out_3],
) )
assert np.array_equal(res_1, np.concatenate((input_2, input_3), axis=1)) np.testing.assert_array_equal(
assert np.array_equal(res_2, np.concatenate((input_2, input_3), axis=1)) res_1, np.concatenate((input_2, input_3), axis=1)
assert np.array_equal(res_3, np.concatenate((input_2, input_3), axis=1)) )
np.testing.assert_array_equal(
res_2, np.concatenate((input_2, input_3), axis=1)
)
np.testing.assert_array_equal(
res_3, np.concatenate((input_2, input_3), axis=1)
)
def test_api(self): def test_api(self):
paddle.enable_static() paddle.enable_static()
...@@ -501,10 +507,18 @@ class TestConcatAPI(unittest.TestCase): ...@@ -501,10 +507,18 @@ class TestConcatAPI(unittest.TestCase):
feed={"x_1": input_2, "x_2": input_2, "x_3": input_3}, feed={"x_1": input_2, "x_2": input_2, "x_3": input_3},
fetch_list=[out_1, out_2, out_3, out_4], fetch_list=[out_1, out_2, out_3, out_4],
) )
assert np.array_equal(res_1, np.concatenate((input_2, input_3), axis=1)) np.testing.assert_array_equal(
assert np.array_equal(res_2, np.concatenate((input_2, input_3), axis=1)) res_1, np.concatenate((input_2, input_3), axis=1)
assert np.array_equal(res_3, np.concatenate((input_2, input_3), axis=1)) )
assert np.array_equal(res_4, np.concatenate((input_2, input_3), axis=1)) np.testing.assert_array_equal(
res_2, np.concatenate((input_2, input_3), axis=1)
)
np.testing.assert_array_equal(
res_3, np.concatenate((input_2, input_3), axis=1)
)
np.testing.assert_array_equal(
res_4, np.concatenate((input_2, input_3), axis=1)
)
def test_imperative(self): def test_imperative(self):
in1 = np.array([[1, 2, 3], [4, 5, 6]]) in1 = np.array([[1, 2, 3], [4, 5, 6]])
......
...@@ -124,7 +124,7 @@ class TestCudaGraphAttrAll(unittest.TestCase): ...@@ -124,7 +124,7 @@ class TestCudaGraphAttrAll(unittest.TestCase):
x_data = np.random.random((3, 10)).astype('float32') x_data = np.random.random((3, 10)).astype('float32')
cuda_graph_rst = self.run_with_cuda_graph(x_data) cuda_graph_rst = self.run_with_cuda_graph(x_data)
normal_run_rst = self.normal_run(x_data) normal_run_rst = self.normal_run(x_data)
assert np.array_equal(cuda_graph_rst, normal_run_rst) np.testing.assert_array_equal(cuda_graph_rst, normal_run_rst)
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -172,7 +172,9 @@ class Test_Detach(unittest.TestCase): ...@@ -172,7 +172,9 @@ class Test_Detach(unittest.TestCase):
def test_NoDetachSingle_DetachMulti(self): def test_NoDetachSingle_DetachMulti(self):
array_no_detach_single = self.no_detach_single() array_no_detach_single = self.no_detach_single()
array_detach_multi = self.detach_multi() array_detach_multi = self.detach_multi()
assert np.array_equal(array_no_detach_single, array_detach_multi) np.testing.assert_array_equal(
array_no_detach_single, array_detach_multi
)
class TestInplace(unittest.TestCase): class TestInplace(unittest.TestCase):
......
...@@ -844,6 +844,7 @@ class EagerVariablePropertiesAndMethodsTestCase(unittest.TestCase): ...@@ -844,6 +844,7 @@ class EagerVariablePropertiesAndMethodsTestCase(unittest.TestCase):
ori_place = egr_tensor.place ori_place = egr_tensor.place
new_arr = np.random.rand(4, 16, 16, 32).astype('float32') new_arr = np.random.rand(4, 16, 16, 32).astype('float32')
self.assertFalse(np.array_equal(egr_tensor.numpy(), new_arr)) self.assertFalse(np.array_equal(egr_tensor.numpy(), new_arr))
egr_tensor.set_value(new_arr) egr_tensor.set_value(new_arr)
...@@ -964,6 +965,7 @@ class EagerParamBaseUsageTestCase(unittest.TestCase): ...@@ -964,6 +965,7 @@ class EagerParamBaseUsageTestCase(unittest.TestCase):
linear = paddle.nn.Linear(1, 3) linear = paddle.nn.Linear(1, 3)
ori_place = linear.weight.place ori_place = linear.weight.place
new_weight = np.ones([1, 3]).astype('float32') new_weight = np.ones([1, 3]).astype('float32')
self.assertFalse(np.array_equal(linear.weight.numpy(), new_weight)) self.assertFalse(np.array_equal(linear.weight.numpy(), new_weight))
linear.weight.set_value(new_weight) linear.weight.set_value(new_weight)
......
...@@ -581,7 +581,7 @@ class TestSimpleUndiagonal(unittest.TestCase): ...@@ -581,7 +581,7 @@ class TestSimpleUndiagonal(unittest.TestCase):
A = paddle.to_tensor(np.array([1.0, 2.0])) A = paddle.to_tensor(np.array([1.0, 2.0]))
A_expect = paddle.to_tensor([[1.0, 0.0], [0.0, 2.0]]) A_expect = paddle.to_tensor([[1.0, 0.0], [0.0, 2.0]])
A_actual = paddle.einsum('i->ii', A) A_actual = paddle.einsum('i->ii', A)
assert np.array_equal(A_expect.numpy(), A_actual.numpy()) np.testing.assert_array_equal(A_expect.numpy(), A_actual.numpy())
class TestSimpleUndiagonal2(unittest.TestCase): class TestSimpleUndiagonal2(unittest.TestCase):
...@@ -595,7 +595,7 @@ class TestSimpleUndiagonal2(unittest.TestCase): ...@@ -595,7 +595,7 @@ class TestSimpleUndiagonal2(unittest.TestCase):
B = paddle.to_tensor(np.array([1.0, 1.0])) B = paddle.to_tensor(np.array([1.0, 1.0]))
A_expect = paddle.to_tensor([[2.0, 0.0], [0.0, 4.0]]) A_expect = paddle.to_tensor([[2.0, 0.0], [0.0, 4.0]])
A_actual = paddle.einsum('i,j->ii', A, B) A_actual = paddle.einsum('i,j->ii', A, B)
assert np.array_equal(A_expect.numpy(), A_actual.numpy()) np.testing.assert_array_equal(A_expect.numpy(), A_actual.numpy())
class TestSimpleComplexGrad(unittest.TestCase): class TestSimpleComplexGrad(unittest.TestCase):
......
...@@ -280,7 +280,7 @@ class TestExpandAsV2API(unittest.TestCase): ...@@ -280,7 +280,7 @@ class TestExpandAsV2API(unittest.TestCase):
feed={"x": input1, "target_tensor": input2}, feed={"x": input1, "target_tensor": input2},
fetch_list=[out_1], fetch_list=[out_1],
) )
assert np.array_equal(res_1[0], np.tile(input1, (2, 1, 1))) np.testing.assert_array_equal(res_1[0], np.tile(input1, (2, 1, 1)))
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -318,9 +318,9 @@ class TestExpandV2API(unittest.TestCase): ...@@ -318,9 +318,9 @@ class TestExpandV2API(unittest.TestCase):
}, },
fetch_list=[out_1, out_2, out_3], fetch_list=[out_1, out_2, out_3],
) )
assert np.array_equal(res_1, np.tile(input, (1, 1))) np.testing.assert_array_equal(res_1, np.tile(input, (1, 1)))
assert np.array_equal(res_2, np.tile(input, (1, 1))) np.testing.assert_array_equal(res_2, np.tile(input, (1, 1)))
assert np.array_equal(res_3, np.tile(input, (1, 1))) np.testing.assert_array_equal(res_3, np.tile(input, (1, 1)))
class TestExpandInferShape(unittest.TestCase): class TestExpandInferShape(unittest.TestCase):
......
...@@ -338,14 +338,30 @@ class TestFillConstantAPI(unittest.TestCase): ...@@ -338,14 +338,30 @@ class TestFillConstantAPI(unittest.TestCase):
fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7, out_8], fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7, out_8],
) )
assert np.array_equal(res_1, np.full([1, 2], 1.1, dtype="float32")) np.testing.assert_array_equal(
assert np.array_equal(res_2, np.full([1, 2], 1.1, dtype="float32")) res_1, np.full([1, 2], 1.1, dtype="float32")
assert np.array_equal(res_3, np.full([1, 2], 1.1, dtype="float32")) )
assert np.array_equal(res_4, np.full([1, 2], 1.1, dtype="float32")) np.testing.assert_array_equal(
assert np.array_equal(res_5, np.full([1, 2], 1.1, dtype="float32")) res_2, np.full([1, 2], 1.1, dtype="float32")
assert np.array_equal(res_6, np.full([1, 2], 1.1, dtype="float32")) )
assert np.array_equal(res_7, np.full([1, 2], 1.1, dtype="float32")) np.testing.assert_array_equal(
assert np.array_equal(res_8, np.full([1, 2], 1.1, dtype="float32")) res_3, np.full([1, 2], 1.1, dtype="float32")
)
np.testing.assert_array_equal(
res_4, np.full([1, 2], 1.1, dtype="float32")
)
np.testing.assert_array_equal(
res_5, np.full([1, 2], 1.1, dtype="float32")
)
np.testing.assert_array_equal(
res_6, np.full([1, 2], 1.1, dtype="float32")
)
np.testing.assert_array_equal(
res_7, np.full([1, 2], 1.1, dtype="float32")
)
np.testing.assert_array_equal(
res_8, np.full([1, 2], 1.1, dtype="float32")
)
class TestFillConstantImperative(unittest.TestCase): class TestFillConstantImperative(unittest.TestCase):
...@@ -369,16 +385,16 @@ class TestFillConstantImperative(unittest.TestCase): ...@@ -369,16 +385,16 @@ class TestFillConstantImperative(unittest.TestCase):
res4 = paddle.tensor.fill_constant( res4 = paddle.tensor.fill_constant(
shape=shape, dtype='int32', value=value shape=shape, dtype='int32', value=value
) )
assert np.array_equal( np.testing.assert_array_equal(
res1.numpy(), np.full([1, 2], 1.1, dtype="float32") res1.numpy(), np.full([1, 2], 1.1, dtype="float32")
) )
assert np.array_equal( np.testing.assert_array_equal(
res2.numpy(), np.full([1, 2], 1.1, dtype="float32") res2.numpy(), np.full([1, 2], 1.1, dtype="float32")
) )
assert np.array_equal( np.testing.assert_array_equal(
res3.numpy(), np.full([1, 2], 1.1, dtype="float32") res3.numpy(), np.full([1, 2], 1.1, dtype="float32")
) )
assert np.array_equal( np.testing.assert_array_equal(
res4.numpy(), np.full([1, 2], 88, dtype="int32") res4.numpy(), np.full([1, 2], 88, dtype="int32")
) )
......
...@@ -90,7 +90,7 @@ class TestFlattenOpFP16(unittest.TestCase): ...@@ -90,7 +90,7 @@ class TestFlattenOpFP16(unittest.TestCase):
fetch_list=[y], fetch_list=[y],
) )
assert np.array_equal(res[0].shape, [12 * 14]) np.testing.assert_array_equal(res[0].shape, [12 * 14])
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -74,13 +74,27 @@ class TestFullAPI(unittest.TestCase): ...@@ -74,13 +74,27 @@ class TestFullAPI(unittest.TestCase):
fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7], fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7],
) )
assert np.array_equal(res_1, np.full([1, 2], 1.1, dtype="float32")) np.testing.assert_array_equal(
assert np.array_equal(res_2, np.full([1, 2], 1.1, dtype="float32")) res_1, np.full([1, 2], 1.1, dtype="float32")
assert np.array_equal(res_3, np.full([1, 2], 1.1, dtype="float32")) )
assert np.array_equal(res_4, np.full([1, 2], 1.2, dtype="float32")) np.testing.assert_array_equal(
assert np.array_equal(res_5, np.full([1, 2], 1.1, dtype="float32")) res_2, np.full([1, 2], 1.1, dtype="float32")
assert np.array_equal(res_6, np.full([1, 2], 1.1, dtype="float32")) )
assert np.array_equal(res_7, np.full([1, 2], 1.1, dtype="float32")) np.testing.assert_array_equal(
res_3, np.full([1, 2], 1.1, dtype="float32")
)
np.testing.assert_array_equal(
res_4, np.full([1, 2], 1.2, dtype="float32")
)
np.testing.assert_array_equal(
res_5, np.full([1, 2], 1.1, dtype="float32")
)
np.testing.assert_array_equal(
res_6, np.full([1, 2], 1.1, dtype="float32")
)
np.testing.assert_array_equal(
res_7, np.full([1, 2], 1.1, dtype="float32")
)
def test_api_eager(self): def test_api_eager(self):
with fluid.dygraph.base.guard(): with fluid.dygraph.base.guard():
...@@ -134,18 +148,36 @@ class TestFullAPI(unittest.TestCase): ...@@ -134,18 +148,36 @@ class TestFullAPI(unittest.TestCase):
out_7, dtype=np.float32, fill_value=np.abs(1.1) out_7, dtype=np.float32, fill_value=np.abs(1.1)
) )
assert np.array_equal(out_1, np.full([1, 2], 1.1, dtype="float32")) np.testing.assert_array_equal(
assert np.array_equal(out_2, np.full([1, 2], 1.1, dtype="float32")) out_1, np.full([1, 2], 1.1, dtype="float32")
assert np.array_equal(out_3, np.full([1, 2], 1.1, dtype="float32")) )
assert np.array_equal(out_4, np.full([1, 2], 1.2, dtype="float32")) np.testing.assert_array_equal(
assert np.array_equal(out_5, np.full([1, 2], 1.1, dtype="float32")) out_2, np.full([1, 2], 1.1, dtype="float32")
assert np.array_equal(out_6, np.full([1, 2], 1.1, dtype="float32")) )
assert np.array_equal(out_7, np.full([1, 2], 1.1, dtype="float32")) np.testing.assert_array_equal(
assert np.array_equal(out_8, np.full([2], 1.1, dtype="float32")) out_3, np.full([1, 2], 1.1, dtype="float32")
assert np.array_equal( )
np.testing.assert_array_equal(
out_4, np.full([1, 2], 1.2, dtype="float32")
)
np.testing.assert_array_equal(
out_5, np.full([1, 2], 1.1, dtype="float32")
)
np.testing.assert_array_equal(
out_6, np.full([1, 2], 1.1, dtype="float32")
)
np.testing.assert_array_equal(
out_7, np.full([1, 2], 1.1, dtype="float32")
)
np.testing.assert_array_equal(
out_8, np.full([2], 1.1, dtype="float32")
)
np.testing.assert_array_equal(
out_9, np.full([2, 2, 4], 1.1, dtype="float32") out_9, np.full([2, 2, 4], 1.1, dtype="float32")
) )
assert np.array_equal(out_10, np.full([1, 2], 1.1, dtype="float32")) np.testing.assert_array_equal(
out_10, np.full([1, 2], 1.1, dtype="float32")
)
class TestFullOpError(unittest.TestCase): class TestFullOpError(unittest.TestCase):
......
...@@ -185,7 +185,9 @@ class TestFusedAttentionPass(unittest.TestCase): ...@@ -185,7 +185,9 @@ class TestFusedAttentionPass(unittest.TestCase):
def test_pass(self): def test_pass(self):
fused_rst = self.get_rst(use_pass=True) fused_rst = self.get_rst(use_pass=True)
non_fused_rst = self.get_rst() non_fused_rst = self.get_rst()
assert np.allclose(fused_rst, non_fused_rst) np.testing.assert_allclose(
fused_rst, non_fused_rst, rtol=1e-5, atol=1e-8
)
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -165,7 +165,9 @@ class TestFusedFeedforwadPass(unittest.TestCase): ...@@ -165,7 +165,9 @@ class TestFusedFeedforwadPass(unittest.TestCase):
self.use_dropout_2 = use_dropout_2 self.use_dropout_2 = use_dropout_2
ret_loss = self.get_value() ret_loss = self.get_value()
ret_loss_fused = self.get_value(use_pass=True) ret_loss_fused = self.get_value(use_pass=True)
assert np.allclose(ret_loss, ret_loss_fused) np.testing.assert_allclose(
ret_loss, ret_loss_fused, rtol=1e-5, atol=1e-8
)
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -60,8 +60,8 @@ class TestLayerChildren(unittest.TestCase): ...@@ -60,8 +60,8 @@ class TestLayerChildren(unittest.TestCase):
self.ori_y1, self.ori_y2 = self.func_apply_init_weight() self.ori_y1, self.ori_y2 = self.func_apply_init_weight()
# compare ori dygraph and new egr # compare ori dygraph and new egr
assert np.array_equal(self.ori_y1.numpy(), self.new_y1.numpy()) np.testing.assert_array_equal(self.ori_y1.numpy(), self.new_y1.numpy())
assert np.array_equal(self.ori_y2.numpy(), self.new_y2.numpy()) np.testing.assert_array_equal(self.ori_y2.numpy(), self.new_y2.numpy())
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -44,6 +44,7 @@ class TestImperativeNumpyBridge(unittest.TestCase): ...@@ -44,6 +44,7 @@ class TestImperativeNumpyBridge(unittest.TestCase):
data_np[0][0] = -1 data_np[0][0] = -1
self.assertEqual(data_np[0][0], -1) self.assertEqual(data_np[0][0], -1)
self.assertNotEqual(var2[0][0].numpy(), -1) self.assertNotEqual(var2[0][0].numpy(), -1)
self.assertFalse(np.array_equal(var2.numpy(), data_np)) self.assertFalse(np.array_equal(var2.numpy(), data_np))
......
...@@ -637,8 +637,8 @@ class TestLayer(LayerTest): ...@@ -637,8 +637,8 @@ class TestLayer(LayerTest):
dy_rlt = emb2(base.to_variable(inp_word)) dy_rlt = emb2(base.to_variable(inp_word))
dy_rlt_value = dy_rlt.numpy() dy_rlt_value = dy_rlt.numpy()
self.assertTrue(np.allclose(static_rlt2, static_rlt)) np.testing.assert_allclose(static_rlt2[0], static_rlt)
self.assertTrue(np.allclose(dy_rlt_value, static_rlt)) np.testing.assert_allclose(dy_rlt_value[0], static_rlt)
with self.dynamic_graph(): with self.dynamic_graph():
custom_weight = np.random.randn(dict_size, 32).astype("float32") custom_weight = np.random.randn(dict_size, 32).astype("float32")
......
...@@ -169,7 +169,7 @@ class TestLinspaceAPI(unittest.TestCase): ...@@ -169,7 +169,7 @@ class TestLinspaceAPI(unittest.TestCase):
res_1, res_2, res_3 = exe.run( res_1, res_2, res_3 = exe.run(
fluid.default_main_program(), fetch_list=[out_1, out_2, out_3] fluid.default_main_program(), fetch_list=[out_1, out_2, out_3]
) )
assert np.array_equal(res_1, res_2) np.testing.assert_array_equal(res_1, res_2)
def test_name(self): def test_name(self):
with paddle_static_guard(): with paddle_static_guard():
......
...@@ -179,7 +179,7 @@ class TestLogspaceAPI(unittest.TestCase): ...@@ -179,7 +179,7 @@ class TestLogspaceAPI(unittest.TestCase):
exe = paddle.static.Executor() exe = paddle.static.Executor()
res_1, res_2 = exe.run(prog, fetch_list=[out_1, out_2]) res_1, res_2 = exe.run(prog, fetch_list=[out_1, out_2])
assert np.array_equal(res_1, res_2) np.testing.assert_array_equal(res_1, res_2)
paddle.disable_static() paddle.disable_static()
def test_name(self): def test_name(self):
......
...@@ -371,7 +371,7 @@ class TestLocalResponseNormCAPI(unittest.TestCase): ...@@ -371,7 +371,7 @@ class TestLocalResponseNormCAPI(unittest.TestCase):
fetch_list=[y], fetch_list=[y],
) )
assert np.array_equal(res[0].shape, input.shape) np.testing.assert_array_equal(res[0].shape, input.shape)
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -162,8 +162,8 @@ class TestMeshgridOp3(unittest.TestCase): ...@@ -162,8 +162,8 @@ class TestMeshgridOp3(unittest.TestCase):
feed={'x': input_1, 'y': input_2}, feed={'x': input_1, 'y': input_2},
fetch_list=[grid_x, grid_y], fetch_list=[grid_x, grid_y],
) )
assert np.array_equal(res_1, out_1) np.testing.assert_array_equal(res_1, out_1)
assert np.array_equal(res_2, out_2) np.testing.assert_array_equal(res_2, out_2)
class TestMeshgridOp4(unittest.TestCase): class TestMeshgridOp4(unittest.TestCase):
...@@ -199,8 +199,8 @@ class TestMeshgridOp4(unittest.TestCase): ...@@ -199,8 +199,8 @@ class TestMeshgridOp4(unittest.TestCase):
fetch_list=[grid_x, grid_y], fetch_list=[grid_x, grid_y],
) )
assert np.array_equal(res_1, out_1) np.testing.assert_array_equal(res_1, out_1)
assert np.array_equal(res_2, out_2) np.testing.assert_array_equal(res_2, out_2)
class TestMeshgridOp5(unittest.TestCase): class TestMeshgridOp5(unittest.TestCase):
...@@ -236,8 +236,8 @@ class TestMeshgridOp5(unittest.TestCase): ...@@ -236,8 +236,8 @@ class TestMeshgridOp5(unittest.TestCase):
fetch_list=[grid_x, grid_y], fetch_list=[grid_x, grid_y],
) )
assert np.array_equal(res_1, out_1) np.testing.assert_array_equal(res_1, out_1)
assert np.array_equal(res_2, out_2) np.testing.assert_array_equal(res_2, out_2)
class TestMeshgridOp6(unittest.TestCase): class TestMeshgridOp6(unittest.TestCase):
...@@ -262,8 +262,8 @@ class TestMeshgridOp6(unittest.TestCase): ...@@ -262,8 +262,8 @@ class TestMeshgridOp6(unittest.TestCase):
tensor_4 = fluid.dygraph.to_variable(input_4) tensor_4 = fluid.dygraph.to_variable(input_4)
res_3, res_4 = paddle.tensor.meshgrid(tensor_3, tensor_4) res_3, res_4 = paddle.tensor.meshgrid(tensor_3, tensor_4)
assert np.array_equal(res_3.shape, [100, 200]) np.testing.assert_array_equal(res_3.shape, [100, 200])
assert np.array_equal(res_4.shape, [100, 200]) np.testing.assert_array_equal(res_4.shape, [100, 200])
class TestMeshgridOp7(unittest.TestCase): class TestMeshgridOp7(unittest.TestCase):
...@@ -288,8 +288,8 @@ class TestMeshgridOp7(unittest.TestCase): ...@@ -288,8 +288,8 @@ class TestMeshgridOp7(unittest.TestCase):
tensor_4 = fluid.dygraph.to_variable(input_4) tensor_4 = fluid.dygraph.to_variable(input_4)
res_3, res_4 = paddle.tensor.meshgrid([tensor_3, tensor_4]) res_3, res_4 = paddle.tensor.meshgrid([tensor_3, tensor_4])
assert np.array_equal(res_3.shape, [100, 200]) np.testing.assert_array_equal(res_3.shape, [100, 200])
assert np.array_equal(res_4.shape, [100, 200]) np.testing.assert_array_equal(res_4.shape, [100, 200])
class TestMeshgridOp8(unittest.TestCase): class TestMeshgridOp8(unittest.TestCase):
...@@ -314,8 +314,8 @@ class TestMeshgridOp8(unittest.TestCase): ...@@ -314,8 +314,8 @@ class TestMeshgridOp8(unittest.TestCase):
tensor_4 = fluid.dygraph.to_variable(input_4) tensor_4 = fluid.dygraph.to_variable(input_4)
res_3, res_4 = paddle.tensor.meshgrid((tensor_3, tensor_4)) res_3, res_4 = paddle.tensor.meshgrid((tensor_3, tensor_4))
assert np.array_equal(res_3.shape, [100, 200]) np.testing.assert_array_equal(res_3.shape, [100, 200])
assert np.array_equal(res_4.shape, [100, 200]) np.testing.assert_array_equal(res_4.shape, [100, 200])
class TestMeshGrid_ZeroDim(TestMeshgridOp): class TestMeshGrid_ZeroDim(TestMeshgridOp):
......
...@@ -84,8 +84,8 @@ class TestTensorDataset(unittest.TestCase): ...@@ -84,8 +84,8 @@ class TestTensorDataset(unittest.TestCase):
assert label.shape == [1, 1] assert label.shape == [1, 1]
assert isinstance(input, fluid.core.eager.Tensor) assert isinstance(input, fluid.core.eager.Tensor)
assert isinstance(label, fluid.core.eager.Tensor) assert isinstance(label, fluid.core.eager.Tensor)
assert np.allclose(input.numpy(), input_np[i]) np.testing.assert_allclose(input.numpy(), input_np[i])
assert np.allclose(label.numpy(), label_np[i]) np.testing.assert_allclose(label.numpy(), label_np[i])
def test_main(self): def test_main(self):
places = [paddle.CPUPlace()] places = [paddle.CPUPlace()]
...@@ -109,10 +109,10 @@ class TestComposeDataset(unittest.TestCase): ...@@ -109,10 +109,10 @@ class TestComposeDataset(unittest.TestCase):
input1, label1, input2, label2 = dataset[i] input1, label1, input2, label2 = dataset[i]
input1_t, label1_t = dataset1[i] input1_t, label1_t = dataset1[i]
input2_t, label2_t = dataset2[i] input2_t, label2_t = dataset2[i]
assert np.allclose(input1, input1_t) np.testing.assert_allclose(input1, input1_t)
assert np.allclose(label1, label1_t) np.testing.assert_allclose(label1, label1_t)
assert np.allclose(input2, input2_t) np.testing.assert_allclose(input2, input2_t)
assert np.allclose(label2, label2_t) np.testing.assert_allclose(label2, label2_t)
class TestRandomSplitApi(unittest.TestCase): class TestRandomSplitApi(unittest.TestCase):
...@@ -226,12 +226,12 @@ class TestChainDataset(unittest.TestCase): ...@@ -226,12 +226,12 @@ class TestChainDataset(unittest.TestCase):
idx = 0 idx = 0
for image, label in iter(dataset1): for image, label in iter(dataset1):
assert np.allclose(image, samples[idx][0]) np.testing.assert_allclose(image, samples[idx][0])
assert np.allclose(label, samples[idx][1]) np.testing.assert_allclose(label, samples[idx][1])
idx += 1 idx += 1
for image, label in iter(dataset2): for image, label in iter(dataset2):
assert np.allclose(image, samples[idx][0]) np.testing.assert_allclose(image, samples[idx][0])
assert np.allclose(label, samples[idx][1]) np.testing.assert_allclose(label, samples[idx][1])
idx += 1 idx += 1
def test_main(self): def test_main(self):
......
...@@ -79,10 +79,10 @@ class TestNanToNum(unittest.TestCase): ...@@ -79,10 +79,10 @@ class TestNanToNum(unittest.TestCase):
exe = paddle.static.Executor(self.place) exe = paddle.static.Executor(self.place)
res = exe.run(feed={'X': x_np}, fetch_list=[out1, out2, out3, out4]) res = exe.run(feed={'X': x_np}, fetch_list=[out1, out2, out3, out4])
self.assertTrue(np.allclose(out1_np, res[0])) np.testing.assert_allclose(out1_np, res[0])
self.assertTrue(np.allclose(out2_np, res[1])) np.testing.assert_allclose(out2_np, res[1])
self.assertTrue(np.allclose(out3_np, res[2])) np.testing.assert_allclose(out3_np, res[2])
self.assertTrue(np.allclose(out4_np, res[3])) np.testing.assert_allclose(out4_np, res[3])
def test_dygraph(self): def test_dygraph(self):
paddle.disable_static(place=self.place) paddle.disable_static(place=self.place)
...@@ -97,23 +97,23 @@ class TestNanToNum(unittest.TestCase): ...@@ -97,23 +97,23 @@ class TestNanToNum(unittest.TestCase):
out_tensor = paddle.nan_to_num(x_tensor) out_tensor = paddle.nan_to_num(x_tensor)
out_np = np_nan_to_num(x_np) out_np = np_nan_to_num(x_np)
self.assertTrue(np.allclose(out_tensor.numpy(), out_np)) np.testing.assert_allclose(out_tensor.numpy(), out_np)
out_tensor = paddle.nan_to_num(x_tensor, 1.0, None, None) out_tensor = paddle.nan_to_num(x_tensor, 1.0, None, None)
out_np = np_nan_to_num(x_np, 1, None, None) out_np = np_nan_to_num(x_np, 1, None, None)
self.assertTrue(np.allclose(out_tensor.numpy(), out_np)) np.testing.assert_allclose(out_tensor.numpy(), out_np)
out_tensor = paddle.nan_to_num(x_tensor, 1.0, 2.0, None) out_tensor = paddle.nan_to_num(x_tensor, 1.0, 2.0, None)
out_np = np_nan_to_num(x_np, 1, 2, None) out_np = np_nan_to_num(x_np, 1, 2, None)
self.assertTrue(np.allclose(out_tensor.numpy(), out_np)) np.testing.assert_allclose(out_tensor.numpy(), out_np)
out_tensor = paddle.nan_to_num(x_tensor, 1.0, None, -10.0) out_tensor = paddle.nan_to_num(x_tensor, 1.0, None, -10.0)
out_np = np_nan_to_num(x_np, 1, None, -10) out_np = np_nan_to_num(x_np, 1, None, -10)
self.assertTrue(np.allclose(out_tensor.numpy(), out_np)) np.testing.assert_allclose(out_tensor.numpy(), out_np)
out_tensor = paddle.nan_to_num(x_tensor, 1.0, 100.0, -10.0) out_tensor = paddle.nan_to_num(x_tensor, 1.0, 100.0, -10.0)
out_np = np_nan_to_num(x_np, 1, 100, -10) out_np = np_nan_to_num(x_np, 1, 100, -10)
self.assertTrue(np.allclose(out_tensor.numpy(), out_np)) np.testing.assert_allclose(out_tensor.numpy(), out_np)
paddle.enable_static() paddle.enable_static()
...@@ -128,7 +128,7 @@ class TestNanToNum(unittest.TestCase): ...@@ -128,7 +128,7 @@ class TestNanToNum(unittest.TestCase):
dx = paddle.grad(y, x_tensor)[0].numpy() dx = paddle.grad(y, x_tensor)[0].numpy()
np_grad = np_nan_to_num_grad(x_np, np.ones_like(x_np)) np_grad = np_nan_to_num_grad(x_np, np.ones_like(x_np))
self.assertTrue(np.allclose(np_grad, dx)) np.testing.assert_allclose(np_grad, dx)
paddle.enable_static() paddle.enable_static()
......
...@@ -71,7 +71,7 @@ class TestNumberCountAPI(unittest.TestCase): ...@@ -71,7 +71,7 @@ class TestNumberCountAPI(unittest.TestCase):
paddle.disable_static() paddle.disable_static()
x = paddle.to_tensor(self.x) x = paddle.to_tensor(self.x)
out = utils._number_count(x, self.upper_num) out = utils._number_count(x, self.upper_num)
assert np.allclose(out.numpy(), self.out) np.testing.assert_allclose(out.numpy(), self.out)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -120,10 +120,10 @@ class TestNumelAPI(unittest.TestCase): ...@@ -120,10 +120,10 @@ class TestNumelAPI(unittest.TestCase):
}, },
fetch_list=[out_1, out_2], fetch_list=[out_1, out_2],
) )
assert np.array_equal( np.testing.assert_array_equal(
res_1, np.array(np.size(input_1)).astype("int64") res_1, np.array(np.size(input_1)).astype("int64")
) )
assert np.array_equal( np.testing.assert_array_equal(
res_2, np.array(np.size(input_2)).astype("int64") res_2, np.array(np.size(input_2)).astype("int64")
) )
...@@ -135,8 +135,8 @@ class TestNumelAPI(unittest.TestCase): ...@@ -135,8 +135,8 @@ class TestNumelAPI(unittest.TestCase):
x_2 = paddle.to_tensor(input_2) x_2 = paddle.to_tensor(input_2)
out_1 = paddle.numel(x_1) out_1 = paddle.numel(x_1)
out_2 = paddle.numel(x_2) out_2 = paddle.numel(x_2)
assert np.array_equal(out_1.numpy().item(0), np.size(input_1)) np.testing.assert_array_equal(out_1.numpy().item(0), np.size(input_1))
assert np.array_equal(out_2.numpy().item(0), np.size(input_2)) np.testing.assert_array_equal(out_2.numpy().item(0), np.size(input_2))
paddle.enable_static() paddle.enable_static()
def test_error(self): def test_error(self):
......
...@@ -186,17 +186,17 @@ class TestPixelShuffleAPI(unittest.TestCase): ...@@ -186,17 +186,17 @@ class TestPixelShuffleAPI(unittest.TestCase):
feed={"x": self.x_1_np}, feed={"x": self.x_1_np},
fetch_list=out_1, fetch_list=out_1,
use_prune=True, use_prune=True,
) )[0]
res_2 = exe.run( res_2 = exe.run(
fluid.default_main_program(), fluid.default_main_program(),
feed={"x2": self.x_2_np}, feed={"x2": self.x_2_np},
fetch_list=out_2, fetch_list=out_2,
use_prune=True, use_prune=True,
) )[0]
assert np.allclose(res_1, self.out_1_np) np.testing.assert_allclose(res_1, self.out_1_np)
assert np.allclose(res_2, self.out_2_np) np.testing.assert_allclose(res_2, self.out_2_np)
def test_api_fp16(self): def test_api_fp16(self):
paddle.enable_static() paddle.enable_static()
...@@ -226,15 +226,15 @@ class TestPixelShuffleAPI(unittest.TestCase): ...@@ -226,15 +226,15 @@ class TestPixelShuffleAPI(unittest.TestCase):
feed={"x": self.x_1_np}, feed={"x": self.x_1_np},
fetch_list=out_1, fetch_list=out_1,
use_prune=True, use_prune=True,
) )[0]
res_2 = exe.run( res_2 = exe.run(
fluid.default_main_program(), fluid.default_main_program(),
feed={"x2": self.x_2_np}, feed={"x2": self.x_2_np},
fetch_list=out_2, fetch_list=out_2,
use_prune=True, use_prune=True,
) )[0]
assert np.allclose(res_1, out_1_np) np.testing.assert_allclose(res_1, out_1_np)
assert np.allclose(res_2, out_2_np) np.testing.assert_allclose(res_2, out_2_np)
# same test between layer and functional in this op. # same test between layer and functional in this op.
def test_static_graph_layer(self): def test_static_graph_layer(self):
...@@ -264,17 +264,17 @@ class TestPixelShuffleAPI(unittest.TestCase): ...@@ -264,17 +264,17 @@ class TestPixelShuffleAPI(unittest.TestCase):
feed={"x": self.x_1_np}, feed={"x": self.x_1_np},
fetch_list=out_1, fetch_list=out_1,
use_prune=True, use_prune=True,
) )[0]
res_2 = exe.run( res_2 = exe.run(
fluid.default_main_program(), fluid.default_main_program(),
feed={"x2": self.x_2_np}, feed={"x2": self.x_2_np},
fetch_list=out_2, fetch_list=out_2,
use_prune=True, use_prune=True,
) )[0]
assert np.allclose(res_1, out_1_np) np.testing.assert_allclose(res_1, out_1_np, rtol=1e-5, atol=1e-8)
assert np.allclose(res_2, out_2_np) np.testing.assert_allclose(res_2, out_2_np, rtol=1e-5, atol=1e-8)
def run_dygraph(self, up_factor, data_format): def run_dygraph(self, up_factor, data_format):
n, c, h, w = 2, 9, 4, 4 n, c, h, w = 2, 9, 4, 4
......
...@@ -225,17 +225,17 @@ class TestPixelUnshuffleAPI(unittest.TestCase): ...@@ -225,17 +225,17 @@ class TestPixelUnshuffleAPI(unittest.TestCase):
feed={"x": self.x_1_np}, feed={"x": self.x_1_np},
fetch_list=out_1, fetch_list=out_1,
use_prune=True, use_prune=True,
) )[0]
res_2 = exe.run( res_2 = exe.run(
fluid.default_main_program(), fluid.default_main_program(),
feed={"x2": self.x_2_np}, feed={"x2": self.x_2_np},
fetch_list=out_2, fetch_list=out_2,
use_prune=True, use_prune=True,
) )[0]
assert np.allclose(res_1, self.out_1_np) np.testing.assert_allclose(res_1, self.out_1_np)
assert np.allclose(res_2, self.out_2_np) np.testing.assert_allclose(res_2, self.out_2_np)
# same test between layer and functional in this op. # same test between layer and functional in this op.
def test_static_graph_layer(self): def test_static_graph_layer(self):
...@@ -267,17 +267,17 @@ class TestPixelUnshuffleAPI(unittest.TestCase): ...@@ -267,17 +267,17 @@ class TestPixelUnshuffleAPI(unittest.TestCase):
feed={"x": self.x_1_np}, feed={"x": self.x_1_np},
fetch_list=out_1, fetch_list=out_1,
use_prune=True, use_prune=True,
) )[0]
res_2 = exe.run( res_2 = exe.run(
fluid.default_main_program(), fluid.default_main_program(),
feed={"x2": self.x_2_np}, feed={"x2": self.x_2_np},
fetch_list=out_2, fetch_list=out_2,
use_prune=True, use_prune=True,
) )[0]
assert np.allclose(res_1, out_1_np) np.testing.assert_allclose(res_1, out_1_np)
assert np.allclose(res_2, out_2_np) np.testing.assert_allclose(res_2, out_2_np)
def run_dygraph(self, down_factor, data_format): def run_dygraph(self, down_factor, data_format):
'''run_dygraph''' '''run_dygraph'''
......
...@@ -391,7 +391,7 @@ class TestPool3D_API(unittest.TestCase): ...@@ -391,7 +391,7 @@ class TestPool3D_API(unittest.TestCase):
fetch_list=[y], fetch_list=[y],
) )
assert np.array_equal(res[0].shape, [1, 2, 1, 16, 16]) np.testing.assert_array_equal(res[0].shape, [1, 2, 1, 16, 16])
def test_static_bf16_gpu(self): def test_static_bf16_gpu(self):
paddle.enable_static() paddle.enable_static()
...@@ -421,7 +421,7 @@ class TestPool3D_API(unittest.TestCase): ...@@ -421,7 +421,7 @@ class TestPool3D_API(unittest.TestCase):
fetch_list=[y], fetch_list=[y],
) )
assert np.array_equal(res[0].shape, [1, 2, 1, 16, 16]) np.testing.assert_array_equal(res[0].shape, [1, 2, 1, 16, 16])
class TestPool3DError_API(unittest.TestCase): class TestPool3DError_API(unittest.TestCase):
......
...@@ -62,7 +62,7 @@ def prune_gate_by_capacity(gate_idx, expert_count, n_expert, n_worker): ...@@ -62,7 +62,7 @@ def prune_gate_by_capacity(gate_idx, expert_count, n_expert, n_worker):
def assert_allclose(output, expected, n_expert): def assert_allclose(output, expected, n_expert):
c1 = count(output, n_expert) c1 = count(output, n_expert)
c2 = count(expected, n_expert) c2 = count(expected, n_expert)
assert np.allclose(c1, c2) np.testing.assert_allclose(c1, c2)
@unittest.skipIf( @unittest.skipIf(
......
...@@ -59,7 +59,7 @@ class TestNumberCountAPIFp32(unittest.TestCase): ...@@ -59,7 +59,7 @@ class TestNumberCountAPIFp32(unittest.TestCase):
value = paddle.to_tensor(self.topk_value) value = paddle.to_tensor(self.topk_value)
prob = paddle.to_tensor(self.prob) prob = paddle.to_tensor(self.prob)
out = utils._random_routing(x, value, prob) out = utils._random_routing(x, value, prob)
assert np.allclose(out.numpy(), self.out) np.testing.assert_allclose(out.numpy(), self.out)
@unittest.skipIf( @unittest.skipIf(
......
...@@ -400,10 +400,10 @@ class TestReshapeAPI(unittest.TestCase): ...@@ -400,10 +400,10 @@ class TestReshapeAPI(unittest.TestCase):
fetch_list=[out_1, out_2, out_3, out_4], fetch_list=[out_1, out_2, out_3, out_4],
) )
assert np.array_equal(res_1, input.reshape(shape)) np.testing.assert_array_equal(res_1, input.reshape(shape))
assert np.array_equal(res_2, input.reshape(shape)) np.testing.assert_array_equal(res_2, input.reshape(shape))
assert np.array_equal(res_3, input.reshape([5, 10])) np.testing.assert_array_equal(res_3, input.reshape([5, 10]))
assert np.array_equal(res_4, input.reshape(shape)) np.testing.assert_array_equal(res_4, input.reshape(shape))
def test_paddle_api(self): def test_paddle_api(self):
self._set_paddle_api() self._set_paddle_api()
...@@ -424,9 +424,9 @@ class TestReshapeAPI(unittest.TestCase): ...@@ -424,9 +424,9 @@ class TestReshapeAPI(unittest.TestCase):
shape_tensor = self.to_tensor(np.array([2, 5, 5]).astype("int32")) shape_tensor = self.to_tensor(np.array([2, 5, 5]).astype("int32"))
out_3 = self.reshape(x, shape=shape_tensor) out_3 = self.reshape(x, shape=shape_tensor)
assert np.array_equal(out_1.numpy(), input.reshape(shape)) np.testing.assert_array_equal(out_1.numpy(), input.reshape(shape))
assert np.array_equal(out_2.numpy(), input.reshape([5, 10])) np.testing.assert_array_equal(out_2.numpy(), input.reshape([5, 10]))
assert np.array_equal(out_3.numpy(), input.reshape(shape)) np.testing.assert_array_equal(out_3.numpy(), input.reshape(shape))
class TestStaticReshape_(TestReshapeAPI): class TestStaticReshape_(TestReshapeAPI):
...@@ -448,9 +448,9 @@ class TestStaticReshape_(TestReshapeAPI): ...@@ -448,9 +448,9 @@ class TestStaticReshape_(TestReshapeAPI):
shape_tensor = self.to_tensor(np.array([2, 5, 5]).astype("int32")) shape_tensor = self.to_tensor(np.array([2, 5, 5]).astype("int32"))
out_3 = self.reshape(x, shape=shape_tensor) out_3 = self.reshape(x, shape=shape_tensor)
assert np.array_equal(out_1.numpy(), input.reshape(shape)) np.testing.assert_array_equal(out_1.numpy(), input.reshape(shape))
assert np.array_equal(out_2.numpy(), input.reshape(shape)) np.testing.assert_array_equal(out_2.numpy(), input.reshape(shape))
assert np.array_equal(out_3.numpy(), input.reshape(shape)) np.testing.assert_array_equal(out_3.numpy(), input.reshape(shape))
# Test Input Error # Test Input Error
......
...@@ -83,10 +83,10 @@ class TestSizeAPI(unittest.TestCase): ...@@ -83,10 +83,10 @@ class TestSizeAPI(unittest.TestCase):
}, },
fetch_list=[out_1, out_2], fetch_list=[out_1, out_2],
) )
assert np.array_equal( np.testing.assert_array_equal(
res_1, np.array(np.size(input_1)).astype("int64") res_1, np.array(np.size(input_1)).astype("int64")
) )
assert np.array_equal( np.testing.assert_array_equal(
res_2, np.array(np.size(input_2)).astype("int64") res_2, np.array(np.size(input_2)).astype("int64")
) )
...@@ -98,8 +98,8 @@ class TestSizeAPI(unittest.TestCase): ...@@ -98,8 +98,8 @@ class TestSizeAPI(unittest.TestCase):
x_2 = paddle.to_tensor(input_2) x_2 = paddle.to_tensor(input_2)
out_1 = paddle.numel(x_1) out_1 = paddle.numel(x_1)
out_2 = paddle.numel(x_2) out_2 = paddle.numel(x_2)
assert np.array_equal(out_1.numpy().item(0), np.size(input_1)) np.testing.assert_array_equal(out_1.numpy().item(0), np.size(input_1))
assert np.array_equal(out_2.numpy().item(0), np.size(input_2)) np.testing.assert_array_equal(out_2.numpy().item(0), np.size(input_2))
paddle.enable_static() paddle.enable_static()
def test_error(self): def test_error(self):
......
...@@ -631,13 +631,13 @@ class TestSliceAPI(unittest.TestCase): ...@@ -631,13 +631,13 @@ class TestSliceAPI(unittest.TestCase):
fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7], fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7],
) )
assert np.array_equal(res_1, input[-3:3, 0:100, 2:-1, :]) np.testing.assert_array_equal(res_1, input[-3:3, 0:100, 2:-1, :])
assert np.array_equal(res_2, input[-3:3, 0:100, :, 2:-1]) np.testing.assert_array_equal(res_2, input[-3:3, 0:100, :, 2:-1])
assert np.array_equal(res_3, input[-3:3, 0:100, :, 2:-1]) np.testing.assert_array_equal(res_3, input[-3:3, 0:100, :, 2:-1])
assert np.array_equal(res_4, input[-3:3, 0:100, 2:-1, :]) np.testing.assert_array_equal(res_4, input[-3:3, 0:100, 2:-1, :])
assert np.array_equal(res_5, input[-3:3, 0:100, 2:-1, :]) np.testing.assert_array_equal(res_5, input[-3:3, 0:100, 2:-1, :])
assert np.array_equal(res_6, input[-3:3, 0:100, :, 2:-1]) np.testing.assert_array_equal(res_6, input[-3:3, 0:100, :, 2:-1])
assert np.array_equal(res_7, input[-1, 0:100, :, 2:-1]) np.testing.assert_array_equal(res_7, input[-1, 0:100, :, 2:-1])
class TestSliceApiWithTensor(unittest.TestCase): class TestSliceApiWithTensor(unittest.TestCase):
......
...@@ -94,7 +94,7 @@ class TestSparseConv(unittest.TestCase): ...@@ -94,7 +94,7 @@ class TestSparseConv(unittest.TestCase):
) )
out.backward(out) out.backward(out)
out = paddle.sparse.coalesce(out) out = paddle.sparse.coalesce(out)
assert np.array_equal(correct_out_values, out.values().numpy()) np.testing.assert_array_equal(correct_out_values, out.values().numpy())
def test_subm_conv2d(self): def test_subm_conv2d(self):
indices = [[0, 0, 0, 0], [0, 0, 1, 2], [1, 3, 2, 3]] indices = [[0, 0, 0, 0], [0, 0, 1, 2], [1, 3, 2, 3]]
...@@ -126,7 +126,9 @@ class TestSparseConv(unittest.TestCase): ...@@ -126,7 +126,9 @@ class TestSparseConv(unittest.TestCase):
y = paddle.sparse.nn.functional.subm_conv3d( y = paddle.sparse.nn.functional.subm_conv3d(
sparse_x, weight, key='subm_conv' sparse_x, weight, key='subm_conv'
) )
assert np.array_equal(sparse_x.indices().numpy(), y.indices().numpy()) np.testing.assert_array_equal(
sparse_x.indices().numpy(), y.indices().numpy()
)
def test_Conv2D(self): def test_Conv2D(self):
# (3, non_zero_num), 3-D:(N, H, W) # (3, non_zero_num), 3-D:(N, H, W)
...@@ -223,7 +225,7 @@ class TestSparseConv(unittest.TestCase): ...@@ -223,7 +225,7 @@ class TestSparseConv(unittest.TestCase):
sparse_out = subm_conv3d(sparse_input) sparse_out = subm_conv3d(sparse_input)
# the output shape of subm_conv is same as input shape # the output shape of subm_conv is same as input shape
assert np.array_equal(indices, sparse_out.indices().numpy()) np.testing.assert_array_equal(indices, sparse_out.indices().numpy())
# test errors # test errors
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
...@@ -294,14 +296,16 @@ class TestSparseConv(unittest.TestCase): ...@@ -294,14 +296,16 @@ class TestSparseConv(unittest.TestCase):
dense_out = sp_out.to_dense() dense_out = sp_out.to_dense()
sp_loss = dense_out.mean() sp_loss = dense_out.mean()
sp_loss.backward() sp_loss.backward()
assert np.allclose(out.numpy(), dense_out.numpy(), atol=1e-3, rtol=1e-3) np.testing.assert_allclose(
assert np.allclose( out.numpy(), dense_out.numpy(), atol=1e-3, rtol=1e-3
)
np.testing.assert_allclose(
conv3d.weight.grad.numpy().transpose(2, 3, 4, 1, 0), conv3d.weight.grad.numpy().transpose(2, 3, 4, 1, 0),
sp_conv3d.weight.grad.numpy(), sp_conv3d.weight.grad.numpy(),
atol=1e-3, atol=1e-3,
rtol=1e-3, rtol=1e-3,
) )
assert np.allclose( np.testing.assert_allclose(
conv3d.bias.grad.numpy(), conv3d.bias.grad.numpy(),
sp_conv3d.bias.grad.numpy(), sp_conv3d.bias.grad.numpy(),
atol=1e-5, atol=1e-5,
......
...@@ -30,7 +30,7 @@ class TestSparseCopy(unittest.TestCase): ...@@ -30,7 +30,7 @@ class TestSparseCopy(unittest.TestCase):
dense_x_2 = paddle.to_tensor(np_x_2, dtype='float32') dense_x_2 = paddle.to_tensor(np_x_2, dtype='float32')
coo_x_2 = dense_x_2.to_sparse_coo(2) coo_x_2 = dense_x_2.to_sparse_coo(2)
coo_x_2.copy_(coo_x, True) coo_x_2.copy_(coo_x, True)
assert np.array_equal(np_values, coo_x_2.values().numpy()) np.testing.assert_array_equal(np_values, coo_x_2.values().numpy())
def test_copy_sparse_csr(self): def test_copy_sparse_csr(self):
np_x = [[0, 1.0, 0], [2.0, 0, 0], [0, 3.0, 0]] np_x = [[0, 1.0, 0], [2.0, 0, 0], [0, 3.0, 0]]
...@@ -42,4 +42,4 @@ class TestSparseCopy(unittest.TestCase): ...@@ -42,4 +42,4 @@ class TestSparseCopy(unittest.TestCase):
dense_x_2 = paddle.to_tensor(np_x_2, dtype='float32') dense_x_2 = paddle.to_tensor(np_x_2, dtype='float32')
csr_x_2 = dense_x_2.to_sparse_csr() csr_x_2 = dense_x_2.to_sparse_csr()
csr_x_2.copy_(csr_x, True) csr_x_2.copy_(csr_x, True)
assert np.array_equal(np_values, csr_x_2.values().numpy()) np.testing.assert_array_equal(np_values, csr_x_2.values().numpy())
...@@ -54,13 +54,19 @@ class TestGradientAdd(unittest.TestCase): ...@@ -54,13 +54,19 @@ class TestGradientAdd(unittest.TestCase):
sparse_loss = sparse_out.values().mean() sparse_loss = sparse_out.values().mean()
sparse_loss.backward(retain_graph=True) sparse_loss.backward(retain_graph=True)
assert np.allclose(dense_out.numpy(), sparse_out.to_dense().numpy()) np.testing.assert_allclose(
assert np.allclose(x.grad.numpy(), sparse_x.grad.to_dense().numpy()) dense_out.numpy(), sparse_out.to_dense().numpy()
)
np.testing.assert_allclose(
x.grad.numpy(), sparse_x.grad.to_dense().numpy()
)
loss.backward() loss.backward()
sparse_loss.backward() sparse_loss.backward()
assert np.allclose(x.grad.numpy(), sparse_x.grad.to_dense().numpy()) np.testing.assert_allclose(
x.grad.numpy(), sparse_x.grad.to_dense().numpy()
)
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -48,7 +48,7 @@ class TestSparseBatchNorm(unittest.TestCase): ...@@ -48,7 +48,7 @@ class TestSparseBatchNorm(unittest.TestCase):
sparse_y = sparse_batch_norm(sparse_x) sparse_y = sparse_batch_norm(sparse_x)
# compare the result with dense batch_norm # compare the result with dense batch_norm
assert np.allclose( np.testing.assert_allclose(
dense_y.flatten().numpy(), dense_y.flatten().numpy(),
sparse_y.values().flatten().numpy(), sparse_y.values().flatten().numpy(),
atol=1e-5, atol=1e-5,
...@@ -57,7 +57,7 @@ class TestSparseBatchNorm(unittest.TestCase): ...@@ -57,7 +57,7 @@ class TestSparseBatchNorm(unittest.TestCase):
# test backward # test backward
sparse_y.backward(sparse_y) sparse_y.backward(sparse_y)
assert np.allclose( np.testing.assert_allclose(
dense_x.grad.flatten().numpy(), dense_x.grad.flatten().numpy(),
sparse_x.grad.values().flatten().numpy(), sparse_x.grad.values().flatten().numpy(),
atol=1e-5, atol=1e-5,
...@@ -85,7 +85,9 @@ class TestSparseBatchNorm(unittest.TestCase): ...@@ -85,7 +85,9 @@ class TestSparseBatchNorm(unittest.TestCase):
dense_bn = paddle.nn.BatchNorm1D(channels) dense_bn = paddle.nn.BatchNorm1D(channels)
dense_x = dense_x.reshape((-1, dense_x.shape[-1])) dense_x = dense_x.reshape((-1, dense_x.shape[-1]))
dense_out = dense_bn(dense_x) dense_out = dense_bn(dense_x)
assert np.allclose(dense_out.numpy(), batch_norm_out.values().numpy()) np.testing.assert_allclose(
dense_out.numpy(), batch_norm_out.values().numpy()
)
# [1, 6, 6, 6, 3] # [1, 6, 6, 6, 3]
def check(self, shape): def check(self, shape):
...@@ -141,7 +143,7 @@ class TestSyncBatchNorm(unittest.TestCase): ...@@ -141,7 +143,7 @@ class TestSyncBatchNorm(unittest.TestCase):
dense_sync_bn = paddle.nn.SyncBatchNorm(2) dense_sync_bn = paddle.nn.SyncBatchNorm(2)
x = x.reshape((-1, x.shape[-1])) x = x.reshape((-1, x.shape[-1]))
dense_hidden = dense_sync_bn(x) dense_hidden = dense_sync_bn(x)
assert np.allclose( np.testing.assert_allclose(
sparse_hidden.values().numpy(), dense_hidden.numpy() sparse_hidden.values().numpy(), dense_hidden.numpy()
) )
......
...@@ -64,8 +64,10 @@ class TestMaxPool3DFunc(unittest.TestCase): ...@@ -64,8 +64,10 @@ class TestMaxPool3DFunc(unittest.TestCase):
dense_out.backward(dense_out) dense_out.backward(dense_out)
# compare with dense # compare with dense
assert np.allclose(dense_out.numpy(), out.numpy()) np.testing.assert_allclose(dense_out.numpy(), out.numpy())
assert np.allclose(dense_x.grad.numpy(), self.dense_x.grad.numpy()) np.testing.assert_allclose(
dense_x.grad.numpy(), self.dense_x.grad.numpy()
)
class TestStride(TestMaxPool3DFunc): class TestStride(TestMaxPool3DFunc):
...@@ -111,7 +113,7 @@ class TestMaxPool3DAPI(unittest.TestCase): ...@@ -111,7 +113,7 @@ class TestMaxPool3DAPI(unittest.TestCase):
dense_out = paddle.nn.functional.max_pool3d( dense_out = paddle.nn.functional.max_pool3d(
dense_x, 3, data_format='NDHWC' dense_x, 3, data_format='NDHWC'
) )
assert np.allclose(dense_out.numpy(), out.numpy()) np.testing.assert_allclose(dense_out.numpy(), out.numpy())
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -33,17 +33,17 @@ class TestSparseCreate(unittest.TestCase): ...@@ -33,17 +33,17 @@ class TestSparseCreate(unittest.TestCase):
dense_indices, dense_elements, dense_shape, stop_gradient=False dense_indices, dense_elements, dense_shape, stop_gradient=False
) )
# test the to_string.py # test the to_string.py
assert np.array_equal(indices, coo.indices().numpy()) np.testing.assert_array_equal(indices, coo.indices().numpy())
assert np.array_equal(values, coo.values().numpy()) np.testing.assert_array_equal(values, coo.values().numpy())
def test_create_coo_by_np(self): def test_create_coo_by_np(self):
indices = [[0, 1, 2], [1, 2, 0]] indices = [[0, 1, 2], [1, 2, 0]]
values = [1.0, 2.0, 3.0] values = [1.0, 2.0, 3.0]
dense_shape = [3, 3] dense_shape = [3, 3]
coo = paddle.sparse.sparse_coo_tensor(indices, values, dense_shape) coo = paddle.sparse.sparse_coo_tensor(indices, values, dense_shape)
assert np.array_equal(3, coo.nnz()) np.testing.assert_array_equal(3, coo.nnz())
assert np.array_equal(indices, coo.indices().numpy()) np.testing.assert_array_equal(indices, coo.indices().numpy())
assert np.array_equal(values, coo.values().numpy()) np.testing.assert_array_equal(values, coo.values().numpy())
def test_create_csr_by_tensor(self): def test_create_csr_by_tensor(self):
crows = [0, 2, 3, 5] crows = [0, 2, 3, 5]
...@@ -69,10 +69,10 @@ class TestSparseCreate(unittest.TestCase): ...@@ -69,10 +69,10 @@ class TestSparseCreate(unittest.TestCase):
dense_shape = [3, 4] dense_shape = [3, 4]
csr = paddle.sparse.sparse_csr_tensor(crows, cols, values, dense_shape) csr = paddle.sparse.sparse_csr_tensor(crows, cols, values, dense_shape)
# test the to_string.py # test the to_string.py
assert np.array_equal(5, csr.nnz()) np.testing.assert_array_equal(5, csr.nnz())
assert np.array_equal(crows, csr.crows().numpy()) np.testing.assert_array_equal(crows, csr.crows().numpy())
assert np.array_equal(cols, csr.cols().numpy()) np.testing.assert_array_equal(cols, csr.cols().numpy())
assert np.array_equal(values, csr.values().numpy()) np.testing.assert_array_equal(values, csr.values().numpy())
def test_place(self): def test_place(self):
place = core.CPUPlace() place = core.CPUPlace()
...@@ -132,8 +132,8 @@ class TestSparseConvert(unittest.TestCase): ...@@ -132,8 +132,8 @@ class TestSparseConvert(unittest.TestCase):
values = [1.0, 2.0, 3.0, 4.0, 5.0] values = [1.0, 2.0, 3.0, 4.0, 5.0]
dense_x = paddle.to_tensor(x, dtype='float32', stop_gradient=False) dense_x = paddle.to_tensor(x, dtype='float32', stop_gradient=False)
out = dense_x.to_sparse_coo(2) out = dense_x.to_sparse_coo(2)
assert np.array_equal(out.indices().numpy(), indices) np.testing.assert_array_equal(out.indices().numpy(), indices)
assert np.array_equal(out.values().numpy(), values) np.testing.assert_array_equal(out.values().numpy(), values)
# test to_sparse_coo_grad backward # test to_sparse_coo_grad backward
out_grad_indices = [[0, 1], [0, 1]] out_grad_indices = [[0, 1], [0, 1]]
out_grad_values = [2.0, 3.0] out_grad_values = [2.0, 3.0]
...@@ -144,7 +144,9 @@ class TestSparseConvert(unittest.TestCase): ...@@ -144,7 +144,9 @@ class TestSparseConvert(unittest.TestCase):
stop_gradient=True, stop_gradient=True,
) )
out.backward(out_grad) out.backward(out_grad)
assert np.array_equal(dense_x.grad.numpy(), out_grad.to_dense().numpy()) np.testing.assert_array_equal(
dense_x.grad.numpy(), out_grad.to_dense().numpy()
)
def test_coo_to_dense(self): def test_coo_to_dense(self):
indices = [[0, 0, 1, 2, 2], [1, 3, 2, 0, 1]] indices = [[0, 0, 1, 2, 2], [1, 3, 2, 0, 1]]
...@@ -168,7 +170,7 @@ class TestSparseConvert(unittest.TestCase): ...@@ -168,7 +170,7 @@ class TestSparseConvert(unittest.TestCase):
dense_tensor.backward(paddle.to_tensor(out_grad)) dense_tensor.backward(paddle.to_tensor(out_grad))
# mask the out_grad by sparse_x.indices() # mask the out_grad by sparse_x.indices()
correct_x_grad = [2.0, 4.0, 7.0, 9.0, 10.0] correct_x_grad = [2.0, 4.0, 7.0, 9.0, 10.0]
assert np.array_equal( np.testing.assert_array_equal(
correct_x_grad, sparse_x.grad.values().numpy() correct_x_grad, sparse_x.grad.values().numpy()
) )
...@@ -182,7 +184,7 @@ class TestSparseConvert(unittest.TestCase): ...@@ -182,7 +184,7 @@ class TestSparseConvert(unittest.TestCase):
sparse_x_cpu.retain_grads() sparse_x_cpu.retain_grads()
dense_tensor_cpu = sparse_x_cpu.to_dense() dense_tensor_cpu = sparse_x_cpu.to_dense()
dense_tensor_cpu.backward(paddle.to_tensor(out_grad)) dense_tensor_cpu.backward(paddle.to_tensor(out_grad))
assert np.array_equal( np.testing.assert_array_equal(
correct_x_grad, sparse_x_cpu.grad.values().numpy() correct_x_grad, sparse_x_cpu.grad.values().numpy()
) )
...@@ -193,12 +195,12 @@ class TestSparseConvert(unittest.TestCase): ...@@ -193,12 +195,12 @@ class TestSparseConvert(unittest.TestCase):
values = [1, 2, 3, 4, 5] values = [1, 2, 3, 4, 5]
dense_x = paddle.to_tensor(x) dense_x = paddle.to_tensor(x)
out = dense_x.to_sparse_csr() out = dense_x.to_sparse_csr()
assert np.array_equal(out.crows().numpy(), crows) np.testing.assert_array_equal(out.crows().numpy(), crows)
assert np.array_equal(out.cols().numpy(), cols) np.testing.assert_array_equal(out.cols().numpy(), cols)
assert np.array_equal(out.values().numpy(), values) np.testing.assert_array_equal(out.values().numpy(), values)
dense_tensor = out.to_dense() dense_tensor = out.to_dense()
assert np.array_equal(dense_tensor.numpy(), x) np.testing.assert_array_equal(dense_tensor.numpy(), x)
def test_coo_values_grad(self): def test_coo_values_grad(self):
indices = [[0, 0, 1, 2, 2], [1, 3, 2, 0, 1]] indices = [[0, 0, 1, 2, 2], [1, 3, 2, 0, 1]]
...@@ -214,7 +216,7 @@ class TestSparseConvert(unittest.TestCase): ...@@ -214,7 +216,7 @@ class TestSparseConvert(unittest.TestCase):
out_grad = [2.0, 3.0, 5.0, 8.0, 9.0] out_grad = [2.0, 3.0, 5.0, 8.0, 9.0]
# test coo_values_grad # test coo_values_grad
values_tensor.backward(paddle.to_tensor(out_grad)) values_tensor.backward(paddle.to_tensor(out_grad))
assert np.array_equal(out_grad, sparse_x.grad.values().numpy()) np.testing.assert_array_equal(out_grad, sparse_x.grad.values().numpy())
indices = [[0, 0, 1, 2, 2], [1, 3, 2, 0, 1]] indices = [[0, 0, 1, 2, 2], [1, 3, 2, 0, 1]]
values = [ values = [
[1.0, 1.0], [1.0, 1.0],
...@@ -240,7 +242,7 @@ class TestSparseConvert(unittest.TestCase): ...@@ -240,7 +242,7 @@ class TestSparseConvert(unittest.TestCase):
] ]
# test coo_values_grad # test coo_values_grad
values_tensor.backward(paddle.to_tensor(out_grad)) values_tensor.backward(paddle.to_tensor(out_grad))
assert np.array_equal(out_grad, sparse_x.grad.values().numpy()) np.testing.assert_array_equal(out_grad, sparse_x.grad.values().numpy())
def test_sparse_coo_tensor_grad(self): def test_sparse_coo_tensor_grad(self):
for device in devices: for device in devices:
...@@ -266,7 +268,9 @@ class TestSparseConvert(unittest.TestCase): ...@@ -266,7 +268,9 @@ class TestSparseConvert(unittest.TestCase):
) )
sparse_x.backward(sparse_out_grad) sparse_x.backward(sparse_out_grad)
correct_values_grad = [0, 3] correct_values_grad = [0, 3]
assert np.array_equal(correct_values_grad, values.grad.numpy()) np.testing.assert_array_equal(
correct_values_grad, values.grad.numpy()
)
# test the non-zero values is a vector # test the non-zero values is a vector
values = [[1, 1], [2, 2]] values = [[1, 1], [2, 2]]
...@@ -283,7 +287,9 @@ class TestSparseConvert(unittest.TestCase): ...@@ -283,7 +287,9 @@ class TestSparseConvert(unittest.TestCase):
) )
sparse_x.backward(sparse_out_grad) sparse_x.backward(sparse_out_grad)
correct_values_grad = [[0, 0], [3, 3]] correct_values_grad = [[0, 0], [3, 3]]
assert np.array_equal(correct_values_grad, values.grad.numpy()) np.testing.assert_array_equal(
correct_values_grad, values.grad.numpy()
)
def test_sparse_coo_tensor_sorted(self): def test_sparse_coo_tensor_sorted(self):
for device in devices: for device in devices:
...@@ -300,10 +306,12 @@ class TestSparseConvert(unittest.TestCase): ...@@ -300,10 +306,12 @@ class TestSparseConvert(unittest.TestCase):
sparse_x = paddle.sparse.coalesce(sparse_x) sparse_x = paddle.sparse.coalesce(sparse_x)
indices_sorted = [[0, 1], [1, 0]] indices_sorted = [[0, 1], [1, 0]]
values_sorted = [5.0, 1.0] values_sorted = [5.0, 1.0]
assert np.array_equal( np.testing.assert_array_equal(
indices_sorted, sparse_x.indices().numpy() indices_sorted, sparse_x.indices().numpy()
) )
assert np.array_equal(values_sorted, sparse_x.values().numpy()) np.testing.assert_array_equal(
values_sorted, sparse_x.values().numpy()
)
# test the non-zero values is a vector # test the non-zero values is a vector
values = [[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]] values = [[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]]
...@@ -311,16 +319,18 @@ class TestSparseConvert(unittest.TestCase): ...@@ -311,16 +319,18 @@ class TestSparseConvert(unittest.TestCase):
sparse_x = paddle.sparse.sparse_coo_tensor(indices, values) sparse_x = paddle.sparse.sparse_coo_tensor(indices, values)
sparse_x = paddle.sparse.coalesce(sparse_x) sparse_x = paddle.sparse.coalesce(sparse_x)
values_sorted = [[5.0, 5.0], [1.0, 1.0]] values_sorted = [[5.0, 5.0], [1.0, 1.0]]
assert np.array_equal( np.testing.assert_array_equal(
indices_sorted, sparse_x.indices().numpy() indices_sorted, sparse_x.indices().numpy()
) )
assert np.array_equal(values_sorted, sparse_x.values().numpy()) np.testing.assert_array_equal(
values_sorted, sparse_x.values().numpy()
)
def test_batch_csr(self): def test_batch_csr(self):
def verify(dense_x): def verify(dense_x):
sparse_x = dense_x.to_sparse_csr() sparse_x = dense_x.to_sparse_csr()
out = sparse_x.to_dense() out = sparse_x.to_dense()
assert np.allclose(out.numpy(), dense_x.numpy()) np.testing.assert_allclose(out.numpy(), dense_x.numpy())
shape = np.random.randint(low=1, high=10, size=3) shape = np.random.randint(low=1, high=10, size=3)
shape = list(shape) shape = list(shape)
......
...@@ -323,12 +323,12 @@ class TestSplitAPI(unittest.TestCase): ...@@ -323,12 +323,12 @@ class TestSplitAPI(unittest.TestCase):
) )
out = np.split(input_1, [2, 3], 1) out = np.split(input_1, [2, 3], 1)
assert np.array_equal(res_0, out[0]) np.testing.assert_array_equal(res_0, out[0])
assert np.array_equal(res_1, out[1]) np.testing.assert_array_equal(res_1, out[1])
assert np.array_equal(res_2, out[2]) np.testing.assert_array_equal(res_2, out[2])
assert np.array_equal(res_3, out[0]) np.testing.assert_array_equal(res_3, out[0])
assert np.array_equal(res_4, out[1]) np.testing.assert_array_equal(res_4, out[1])
assert np.array_equal(res_5, out[2]) np.testing.assert_array_equal(res_5, out[2])
class TestSplitOpError(unittest.TestCase): class TestSplitOpError(unittest.TestCase):
......
...@@ -605,13 +605,13 @@ class TestStridedSliceAPI(unittest.TestCase): ...@@ -605,13 +605,13 @@ class TestStridedSliceAPI(unittest.TestCase):
}, },
fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7], fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7],
) )
assert np.array_equal(res_1, input[-3:3, 0:100, 2:-1, :]) np.testing.assert_array_equal(res_1, input[-3:3, 0:100, 2:-1, :])
assert np.array_equal(res_2, input[-3:3, 0:100, :, 2:-1]) np.testing.assert_array_equal(res_2, input[-3:3, 0:100, :, 2:-1])
assert np.array_equal(res_3, input[-3:3, 0:100, :, 2:-1]) np.testing.assert_array_equal(res_3, input[-3:3, 0:100, :, 2:-1])
assert np.array_equal(res_4, input[-3:3, 0:100, 2:-1, :]) np.testing.assert_array_equal(res_4, input[-3:3, 0:100, 2:-1, :])
assert np.array_equal(res_5, input[-3:3, 0:100:2, -1:2:-1, :]) np.testing.assert_array_equal(res_5, input[-3:3, 0:100:2, -1:2:-1, :])
assert np.array_equal(res_6, input[-3:3, 0:100:2, :, -1:2:-1]) np.testing.assert_array_equal(res_6, input[-3:3, 0:100:2, :, -1:2:-1])
assert np.array_equal(res_7, input[-1, 0:100:2, :, -1:2:-1]) np.testing.assert_array_equal(res_7, input[-1, 0:100:2, :, -1:2:-1])
def test_dygraph_op(self): def test_dygraph_op(self):
x = paddle.zeros(shape=[3, 4, 5, 6], dtype="float32") x = paddle.zeros(shape=[3, 4, 5, 6], dtype="float32")
......
...@@ -387,9 +387,9 @@ class TestTileAPI(unittest.TestCase): ...@@ -387,9 +387,9 @@ class TestTileAPI(unittest.TestCase):
out_2 = paddle.tile(x, repeat_times=[positive_2, 3]) out_2 = paddle.tile(x, repeat_times=[positive_2, 3])
out_3 = paddle.tile(x, repeat_times=repeat_times) out_3 = paddle.tile(x, repeat_times=repeat_times)
assert np.array_equal(out_1.numpy(), np.tile(np_x, (2, 3))) np.testing.assert_array_equal(out_1.numpy(), np.tile(np_x, (2, 3)))
assert np.array_equal(out_2.numpy(), np.tile(np_x, (2, 3))) np.testing.assert_array_equal(out_2.numpy(), np.tile(np_x, (2, 3)))
assert np.array_equal(out_3.numpy(), np.tile(np_x, (2, 3))) np.testing.assert_array_equal(out_3.numpy(), np.tile(np_x, (2, 3)))
class TestTileDoubleGradCheck(unittest.TestCase): class TestTileDoubleGradCheck(unittest.TestCase):
......
...@@ -38,8 +38,8 @@ class TestUnbind(unittest.TestCase): ...@@ -38,8 +38,8 @@ class TestUnbind(unittest.TestCase):
fetch_list=[out_0, out_1], fetch_list=[out_0, out_1],
) )
assert np.array_equal(res_1, input_1[0, 0:100]) np.testing.assert_array_equal(res_1, input_1[0, 0:100])
assert np.array_equal(res_2, input_1[1, 0:100]) np.testing.assert_array_equal(res_2, input_1[1, 0:100])
def test_unbind_static_fp16_gpu(self): def test_unbind_static_fp16_gpu(self):
if paddle.fluid.core.is_compiled_with_cuda(): if paddle.fluid.core.is_compiled_with_cuda():
...@@ -61,8 +61,8 @@ class TestUnbind(unittest.TestCase): ...@@ -61,8 +61,8 @@ class TestUnbind(unittest.TestCase):
fetch_list=[y], fetch_list=[y],
) )
assert np.array_equal(res[0], input[0, :]) np.testing.assert_array_equal(res[0], input[0, :])
assert np.array_equal(res[1], input[1, :]) np.testing.assert_array_equal(res[1], input[1, :])
def test_unbind_dygraph(self): def test_unbind_dygraph(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
...@@ -96,8 +96,8 @@ class TestLayersUnbind(unittest.TestCase): ...@@ -96,8 +96,8 @@ class TestLayersUnbind(unittest.TestCase):
fetch_list=[out_0, out_1], fetch_list=[out_0, out_1],
) )
assert np.array_equal(res_1, input_1[0, 0:100]) np.testing.assert_array_equal(res_1, input_1[0, 0:100])
assert np.array_equal(res_2, input_1[1, 0:100]) np.testing.assert_array_equal(res_2, input_1[1, 0:100])
class TestUnbindOp(OpTest): class TestUnbindOp(OpTest):
......
...@@ -275,11 +275,11 @@ class TestUnsqueezeAPI(unittest.TestCase): ...@@ -275,11 +275,11 @@ class TestUnsqueezeAPI(unittest.TestCase):
fetch_list=[out_1, out_2, out_3, out_4, out_5], fetch_list=[out_1, out_2, out_3, out_4, out_5],
) )
assert np.array_equal(res_1, input.reshape([3, 1, 1, 2, 5, 1])) np.testing.assert_array_equal(res_1, input.reshape([3, 1, 1, 2, 5, 1]))
assert np.array_equal(res_2, input.reshape([3, 1, 1, 2, 5, 1])) np.testing.assert_array_equal(res_2, input.reshape([3, 1, 1, 2, 5, 1]))
assert np.array_equal(res_3, input.reshape([3, 1, 1, 2, 5, 1])) np.testing.assert_array_equal(res_3, input.reshape([3, 1, 1, 2, 5, 1]))
assert np.array_equal(res_4, input.reshape([3, 2, 5, 1])) np.testing.assert_array_equal(res_4, input.reshape([3, 2, 5, 1]))
assert np.array_equal(res_5, input.reshape([3, 1, 1, 2, 5, 1])) np.testing.assert_array_equal(res_5, input.reshape([3, 1, 1, 2, 5, 1]))
def test_error(self): def test_error(self):
def test_axes_type(): def test_axes_type():
......
...@@ -274,14 +274,20 @@ class TestUpdateLossScalingLayer(unittest.TestCase): ...@@ -274,14 +274,20 @@ class TestUpdateLossScalingLayer(unittest.TestCase):
], ],
) )
assert np.array_equal(result_v[0], a_v) np.testing.assert_array_equal(result_v[0], a_v)
assert np.array_equal(result_v[1], b_v) np.testing.assert_array_equal(result_v[1], b_v)
assert np.array_equal(result_v[0], result_v[2]) np.testing.assert_array_equal(result_v[0], result_v[2])
assert np.array_equal(result_v[1], result_v[3]) np.testing.assert_array_equal(result_v[1], result_v[3])
assert np.array_equal(result_v[4], found_inf_v) np.testing.assert_array_equal(result_v[4], found_inf_v)
assert np.array_equal(result_v[5], prev_loss_scaling_v * incr_ratio) np.testing.assert_array_equal(
assert np.array_equal(result_v[6], np.zeros_like(num_good_steps_v)) result_v[5], prev_loss_scaling_v * incr_ratio
assert np.array_equal(result_v[7], np.zeros_like(num_bad_steps_v)) )
np.testing.assert_array_equal(
result_v[6], np.zeros_like(num_good_steps_v)
)
np.testing.assert_array_equal(
result_v[7], np.zeros_like(num_bad_steps_v)
)
def loss_scaling_check_inf(self, use_cuda=True, scope=fluid.Scope()): def loss_scaling_check_inf(self, use_cuda=True, scope=fluid.Scope()):
with paddle_static_guard(): with paddle_static_guard():
...@@ -353,14 +359,20 @@ class TestUpdateLossScalingLayer(unittest.TestCase): ...@@ -353,14 +359,20 @@ class TestUpdateLossScalingLayer(unittest.TestCase):
num_bad_steps, num_bad_steps,
], ],
) )
assert np.array_equal(result_v[0], np.zeros_like(a_v)) np.testing.assert_array_equal(result_v[0], np.zeros_like(a_v))
assert np.array_equal(result_v[1], np.zeros_like(b_v)) np.testing.assert_array_equal(result_v[1], np.zeros_like(b_v))
assert np.array_equal(result_v[2], np.zeros_like(a_v)) np.testing.assert_array_equal(result_v[2], np.zeros_like(a_v))
assert np.array_equal(result_v[3], np.zeros_like(b_v)) np.testing.assert_array_equal(result_v[3], np.zeros_like(b_v))
assert np.array_equal(result_v[4], found_inf_v) np.testing.assert_array_equal(result_v[4], found_inf_v)
assert np.array_equal(result_v[5], prev_loss_scaling_v * decr_ratio) np.testing.assert_array_equal(
assert np.array_equal(result_v[6], np.zeros_like(num_good_steps_v)) result_v[5], prev_loss_scaling_v * decr_ratio
assert np.array_equal(result_v[7], np.zeros_like(num_bad_steps_v)) )
np.testing.assert_array_equal(
result_v[6], np.zeros_like(num_good_steps_v)
)
np.testing.assert_array_equal(
result_v[7], np.zeros_like(num_bad_steps_v)
)
def test_loss_scaling_cpu(self): def test_loss_scaling_cpu(self):
with paddle_static_guard(): with paddle_static_guard():
......
...@@ -166,17 +166,17 @@ class TestWhereAPI(unittest.TestCase): ...@@ -166,17 +166,17 @@ class TestWhereAPI(unittest.TestCase):
feed={'cond': self.cond, 'x': self.x, 'y': self.y}, feed={'cond': self.cond, 'x': self.x, 'y': self.y},
fetch_list=fetch_list, fetch_list=fetch_list,
) )
assert np.array_equal(out[0], self.out) np.testing.assert_array_equal(out[0], self.out)
if x_stop_gradient is False: if x_stop_gradient is False:
assert np.array_equal( np.testing.assert_array_equal(
out[2], self.ref_x_backward(out[1]) out[2], self.ref_x_backward(out[1])
) )
if y.stop_gradient is False: if y.stop_gradient is False:
assert np.array_equal( np.testing.assert_array_equal(
out[3], self.ref_y_backward(out[1]) out[3], self.ref_y_backward(out[1])
) )
elif y.stop_gradient is False: elif y.stop_gradient is False:
assert np.array_equal( np.testing.assert_array_equal(
out[2], self.ref_y_backward(out[1]) out[2], self.ref_y_backward(out[1])
) )
...@@ -202,7 +202,9 @@ class TestWhereAPI(unittest.TestCase): ...@@ -202,7 +202,9 @@ class TestWhereAPI(unittest.TestCase):
feed={'x': x_i, 'y': y_i}, feed={'x': x_i, 'y': y_i},
fetch_list=[result], fetch_list=[result],
) )
assert np.array_equal(out[0], np.where((x_i > 1), x_i, y_i)) np.testing.assert_array_equal(
out[0], np.where((x_i > 1), x_i, y_i)
)
def test_scalar(self): def test_scalar(self):
paddle.enable_static() paddle.enable_static()
...@@ -228,7 +230,7 @@ class TestWhereAPI(unittest.TestCase): ...@@ -228,7 +230,7 @@ class TestWhereAPI(unittest.TestCase):
fetch_list=[result], fetch_list=[result],
) )
expect = np.where(cond_data, x_data, y_data) expect = np.where(cond_data, x_data, y_data)
assert np.array_equal(out[0], expect) np.testing.assert_array_equal(out[0], expect)
def __test_where_with_broadcast_static(self, cond_shape, x_shape, y_shape): def __test_where_with_broadcast_static(self, cond_shape, x_shape, y_shape):
paddle.enable_static() paddle.enable_static()
...@@ -262,7 +264,7 @@ class TestWhereAPI(unittest.TestCase): ...@@ -262,7 +264,7 @@ class TestWhereAPI(unittest.TestCase):
fetch_list=[result], fetch_list=[result],
) )
expect = np.where(cond_data, x_data, y_data) expect = np.where(cond_data, x_data, y_data)
assert np.array_equal(out[0], expect) np.testing.assert_array_equal(out[0], expect)
def test_static_api_broadcast_1(self): def test_static_api_broadcast_1(self):
cond_shape = [2, 4] cond_shape = [2, 4]
...@@ -323,7 +325,9 @@ class TestWhereDygraphAPI(unittest.TestCase): ...@@ -323,7 +325,9 @@ class TestWhereDygraphAPI(unittest.TestCase):
y = fluid.dygraph.to_variable(y_i) y = fluid.dygraph.to_variable(y_i)
cond = fluid.dygraph.to_variable(cond_i) cond = fluid.dygraph.to_variable(cond_i)
out = paddle.where(cond, x, y) out = paddle.where(cond, x, y)
assert np.array_equal(out.numpy(), np.where(cond_i, x_i, y_i)) np.testing.assert_array_equal(
out.numpy(), np.where(cond_i, x_i, y_i)
)
def test_scalar(self): def test_scalar(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
...@@ -332,7 +336,7 @@ class TestWhereDygraphAPI(unittest.TestCase): ...@@ -332,7 +336,7 @@ class TestWhereDygraphAPI(unittest.TestCase):
y = 2.0 y = 2.0
cond = fluid.dygraph.to_variable(cond_i) cond = fluid.dygraph.to_variable(cond_i)
out = paddle.where(cond, x, y) out = paddle.where(cond, x, y)
assert np.array_equal(out.numpy(), np.where(cond_i, x, y)) np.testing.assert_array_equal(out.numpy(), np.where(cond_i, x, y))
def __test_where_with_broadcast_dygraph(self, cond_shape, a_shape, b_shape): def __test_where_with_broadcast_dygraph(self, cond_shape, a_shape, b_shape):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
......
...@@ -54,7 +54,7 @@ def check(): ...@@ -54,7 +54,7 @@ def check():
np_res = np.add(a_np, b_np) np_res = np.add(a_np, b_np)
np_res = np.matmul(np_res, np.transpose(b_np, (0, 2, 1))) np_res = np.matmul(np_res, np.transpose(b_np, (0, 2, 1)))
np_res = np.maximum(np_res, 0) np_res = np.maximum(np_res, 0)
assert np.allclose(res1.numpy(), np_res, atol=1e-3) np.testing.assert_allclose(res1.numpy(), np_res, atol=1e-3)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -38,7 +38,7 @@ def check(): ...@@ -38,7 +38,7 @@ def check():
a = fluid.dygraph.to_variable(a_np) a = fluid.dygraph.to_variable(a_np)
res1 = func(a) res1 = func(a)
res2 = np.maximum(a_np, 0) res2 = np.maximum(a_np, 0)
assert np.array_equal(res1.numpy(), res2) np.testing.assert_array_equal(res1.numpy(), res2)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -92,7 +92,7 @@ class TestQuant2Int8MkldnnPassMul(unittest.TestCase): ...@@ -92,7 +92,7 @@ class TestQuant2Int8MkldnnPassMul(unittest.TestCase):
param.set(self.variables_mul["mul_weights"], self.place) param.set(self.variables_mul["mul_weights"], self.place)
qpass._dequantize_op_weights(graph, op_node, "Y", "Out") qpass._dequantize_op_weights(graph, op_node, "Y", "Out")
assert np.allclose( np.testing.assert_allclose(
self.scope.find_var("mul_weights").get_tensor(), self.scope.find_var("mul_weights").get_tensor(),
[ [
[ [
......
...@@ -86,11 +86,11 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -86,11 +86,11 @@ class TestProcessGroupFp32(unittest.TestCase):
# XPU don't support event query by now, so just use sync op here # XPU don't support event query by now, so just use sync op here
task = dist.broadcast(tensor_x, 0) task = dist.broadcast(tensor_x, 0)
paddle.device.xpu.synchronize() paddle.device.xpu.synchronize()
assert np.array_equal(broadcast_result, tensor_x) np.testing.assert_array_equal(broadcast_result, tensor_x)
else: else:
task = dist.broadcast(tensor_y, 0) task = dist.broadcast(tensor_y, 0)
paddle.device.xpu.synchronize() paddle.device.xpu.synchronize()
assert np.array_equal(broadcast_result, tensor_y) np.testing.assert_array_equal(broadcast_result, tensor_y)
sys.stdout.write(f"rank {pg.rank()}: test broadcast api ok\n") sys.stdout.write(f"rank {pg.rank()}: test broadcast api ok\n")
...@@ -132,8 +132,8 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -132,8 +132,8 @@ class TestProcessGroupFp32(unittest.TestCase):
out_2 = paddle.slice( out_2 = paddle.slice(
tensor_out, [0], [out_shape[0] // 2], [out_shape[0]] tensor_out, [0], [out_shape[0] // 2], [out_shape[0]]
) )
assert np.array_equal(tensor_x, out_1) np.testing.assert_array_equal(tensor_x, out_1)
assert np.array_equal(tensor_y, out_2) np.testing.assert_array_equal(tensor_y, out_2)
sys.stdout.write(f"rank {pg.rank()}: test allgather api ok\n") sys.stdout.write(f"rank {pg.rank()}: test allgather api ok\n")
if pg.rank() == 0: if pg.rank() == 0:
...@@ -150,8 +150,8 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -150,8 +150,8 @@ class TestProcessGroupFp32(unittest.TestCase):
out_2 = paddle.slice( out_2 = paddle.slice(
tensor_out, [0], [out_shape[0] // 2], [out_shape[0]] tensor_out, [0], [out_shape[0] // 2], [out_shape[0]]
) )
assert np.array_equal(tensor_x, out_1) np.testing.assert_array_equal(tensor_x, out_1)
assert np.array_equal(tensor_y, out_2) np.testing.assert_array_equal(tensor_y, out_2)
sys.stdout.write(f"rank {pg.rank()}: test allgather api2 ok\n") sys.stdout.write(f"rank {pg.rank()}: test allgather api2 ok\n")
# test Reduce # test Reduce
...@@ -171,8 +171,8 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -171,8 +171,8 @@ class TestProcessGroupFp32(unittest.TestCase):
task.wait() task.wait()
paddle.device.xpu.synchronize() paddle.device.xpu.synchronize()
if pg.rank() == 0: if pg.rank() == 0:
assert np.array_equal(tensor_x, sum_result) np.testing.assert_array_equal(tensor_x, sum_result)
assert np.array_equal(tensor_y, old_tensor_y) np.testing.assert_array_equal(tensor_y, old_tensor_y)
sys.stdout.write(f"rank {pg.rank()}: test reduce sum api ok\n") sys.stdout.write(f"rank {pg.rank()}: test reduce sum api ok\n")
# test reduce_scatter # test reduce_scatter
...@@ -196,9 +196,9 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -196,9 +196,9 @@ class TestProcessGroupFp32(unittest.TestCase):
task.wait() task.wait()
paddle.device.xpu.synchronize() paddle.device.xpu.synchronize()
if pg.rank() == 0: if pg.rank() == 0:
assert np.array_equal(need_result0, tensor_out) np.testing.assert_array_equal(need_result0, tensor_out)
else: else:
assert np.array_equal(need_result1, tensor_out) np.testing.assert_array_equal(need_result1, tensor_out)
sys.stdout.write(f"rank {pg.rank()}: test reduce_scatter sum api ok\n") sys.stdout.write(f"rank {pg.rank()}: test reduce_scatter sum api ok\n")
# test send async api # test send async api
...@@ -215,7 +215,7 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -215,7 +215,7 @@ class TestProcessGroupFp32(unittest.TestCase):
else: else:
task = dist.recv(tensor_y, 0, sync_op=False) task = dist.recv(tensor_y, 0, sync_op=False)
task.wait() task.wait()
assert np.array_equal(tensor_y, tensor_x) np.testing.assert_array_equal(tensor_y, tensor_x)
# test send sync api # test send sync api
# rank 0 # rank 0
...@@ -229,7 +229,7 @@ class TestProcessGroupFp32(unittest.TestCase): ...@@ -229,7 +229,7 @@ class TestProcessGroupFp32(unittest.TestCase):
task = dist.send(tensor_x, 1, sync_op=True) task = dist.send(tensor_x, 1, sync_op=True)
else: else:
task = dist.recv(tensor_y, 0, sync_op=True) task = dist.recv(tensor_y, 0, sync_op=True)
assert np.array_equal(tensor_y, tensor_x) np.testing.assert_array_equal(tensor_y, tensor_x)
# test send 0-d tensor # test send 0-d tensor
# rank 0 # rank 0
......
...@@ -147,7 +147,7 @@ class TestExpandAsV2API(unittest.TestCase): ...@@ -147,7 +147,7 @@ class TestExpandAsV2API(unittest.TestCase):
feed={"x": x_np, "target_tensor": y_np}, feed={"x": x_np, "target_tensor": y_np},
fetch_list=[out_1], fetch_list=[out_1],
) )
assert np.array_equal(res_1[0], np.tile(x_np, (2, 1, 1))) np.testing.assert_array_equal(res_1[0], np.tile(x_np, (2, 1, 1)))
support_types = get_xpu_op_support_types('expand_as_v2') support_types = get_xpu_op_support_types('expand_as_v2')
......
...@@ -226,9 +226,9 @@ class TestExpandV2API(unittest.TestCase): ...@@ -226,9 +226,9 @@ class TestExpandV2API(unittest.TestCase):
fetch_list=[out_1, out_2, out_3], fetch_list=[out_1, out_2, out_3],
) )
assert np.array_equal(res_1, np.tile(input, (1, 1))) np.testing.assert_array_equal(res_1, np.tile(input, (1, 1)))
assert np.array_equal(res_2, np.tile(input, (1, 1))) np.testing.assert_array_equal(res_2, np.tile(input, (1, 1)))
assert np.array_equal(res_3, np.tile(input, (1, 1))) np.testing.assert_array_equal(res_3, np.tile(input, (1, 1)))
support_types = get_xpu_op_support_types('expand_v2') support_types = get_xpu_op_support_types('expand_v2')
......
...@@ -86,7 +86,7 @@ class TestMaskedSelectAPI(unittest.TestCase): ...@@ -86,7 +86,7 @@ class TestMaskedSelectAPI(unittest.TestCase):
mask = paddle.to_tensor(np_mask) mask = paddle.to_tensor(np_mask)
out = paddle.masked_select(x, mask) out = paddle.masked_select(x, mask)
np_out = np_masked_select(np_x, np_mask) np_out = np_masked_select(np_x, np_mask)
self.assertEqual(np.allclose(out.numpy(), np_out), True) np.testing.assert_allclose(out.numpy(), np_out)
paddle.enable_static() paddle.enable_static()
def test_static_mode(self): def test_static_mode(self):
......
...@@ -29,17 +29,17 @@ class TestSparseCreate(unittest.TestCase): ...@@ -29,17 +29,17 @@ class TestSparseCreate(unittest.TestCase):
coo = paddle.sparse.sparse_coo_tensor( coo = paddle.sparse.sparse_coo_tensor(
dense_indices, dense_elements, dense_shape, stop_gradient=False dense_indices, dense_elements, dense_shape, stop_gradient=False
) )
assert np.array_equal(indices, coo.indices().numpy()) np.testing.assert_array_equal(indices, coo.indices().numpy())
assert np.array_equal(values, coo.values().numpy()) np.testing.assert_array_equal(values, coo.values().numpy())
def test_create_coo_by_np(self): def test_create_coo_by_np(self):
indices = [[0, 1, 2], [1, 2, 0]] indices = [[0, 1, 2], [1, 2, 0]]
values = [1.0, 2.0, 3.0] values = [1.0, 2.0, 3.0]
dense_shape = [3, 3] dense_shape = [3, 3]
coo = paddle.sparse.sparse_coo_tensor(indices, values, dense_shape) coo = paddle.sparse.sparse_coo_tensor(indices, values, dense_shape)
assert np.array_equal(3, coo.nnz()) np.testing.assert_array_equal(3, coo.nnz())
assert np.array_equal(indices, coo.indices().numpy()) np.testing.assert_array_equal(indices, coo.indices().numpy())
assert np.array_equal(values, coo.values().numpy()) np.testing.assert_array_equal(values, coo.values().numpy())
def test_place(self): def test_place(self):
indices = [[0, 1], [0, 1]] indices = [[0, 1], [0, 1]]
......
...@@ -219,9 +219,9 @@ class TestTileAPI(unittest.TestCase): ...@@ -219,9 +219,9 @@ class TestTileAPI(unittest.TestCase):
out_2 = paddle.tile(x, repeat_times=[positive_2, 3]) out_2 = paddle.tile(x, repeat_times=[positive_2, 3])
out_3 = paddle.tile(x, repeat_times=repeat_times) out_3 = paddle.tile(x, repeat_times=repeat_times)
assert np.array_equal(out_1.numpy(), np.tile(np_x, (2, 3))) np.testing.assert_array_equal(out_1.numpy(), np.tile(np_x, (2, 3)))
assert np.array_equal(out_2.numpy(), np.tile(np_x, (2, 3))) np.testing.assert_array_equal(out_2.numpy(), np.tile(np_x, (2, 3)))
assert np.array_equal(out_3.numpy(), np.tile(np_x, (2, 3))) np.testing.assert_array_equal(out_3.numpy(), np.tile(np_x, (2, 3)))
class TestTileAPI_ZeroDim(unittest.TestCase): class TestTileAPI_ZeroDim(unittest.TestCase):
......
...@@ -50,8 +50,8 @@ class XPUTestUnbindOP(XPUOpTestWrapper): ...@@ -50,8 +50,8 @@ class XPUTestUnbindOP(XPUOpTestWrapper):
fetch_list=[out_0, out_1], fetch_list=[out_0, out_1],
) )
assert np.array_equal(res_1, input_1[0, 0:100]) np.testing.assert_array_equal(res_1, input_1[0, 0:100])
assert np.array_equal(res_2, input_1[1, 0:100]) np.testing.assert_array_equal(res_2, input_1[1, 0:100])
def test_unbind_dygraph(self): def test_unbind_dygraph(self):
with fluid.dygraph.guard(): with fluid.dygraph.guard():
...@@ -89,8 +89,8 @@ class XPUTestUnbindOP(XPUOpTestWrapper): ...@@ -89,8 +89,8 @@ class XPUTestUnbindOP(XPUOpTestWrapper):
fetch_list=[out_0, out_1], fetch_list=[out_0, out_1],
) )
assert np.array_equal(res_1, input_1[0, 0:100]) np.testing.assert_array_equal(res_1, input_1[0, 0:100])
assert np.array_equal(res_2, input_1[1, 0:100]) np.testing.assert_array_equal(res_2, input_1[1, 0:100])
class TestUnbindOp(XPUOpTest): class TestUnbindOp(XPUOpTest):
def initParameters(self): def initParameters(self):
......
...@@ -174,14 +174,20 @@ class XPUTestUpdateLossScalingOp(XPUOpTestWrapper): ...@@ -174,14 +174,20 @@ class XPUTestUpdateLossScalingOp(XPUOpTestWrapper):
num_bad_steps, num_bad_steps,
], ],
) )
assert np.array_equal(result_v[0], a_v) np.testing.assert_array_equal(result_v[0], a_v)
assert np.array_equal(result_v[1], b_v) np.testing.assert_array_equal(result_v[1], b_v)
assert np.array_equal(result_v[0], result_v[2]) np.testing.assert_array_equal(result_v[0], result_v[2])
assert np.array_equal(result_v[1], result_v[3]) np.testing.assert_array_equal(result_v[1], result_v[3])
assert np.array_equal(result_v[4], found_inf_v) np.testing.assert_array_equal(result_v[4], found_inf_v)
assert np.array_equal(result_v[5], prev_loss_scaling_v * incr_ratio) np.testing.assert_array_equal(
assert np.array_equal(result_v[6], np.zeros_like(num_good_steps_v)) result_v[5], prev_loss_scaling_v * incr_ratio
assert np.array_equal(result_v[7], np.zeros_like(num_bad_steps_v)) )
np.testing.assert_array_equal(
result_v[6], np.zeros_like(num_good_steps_v)
)
np.testing.assert_array_equal(
result_v[7], np.zeros_like(num_bad_steps_v)
)
def loss_scaling_check_inf(self, use_cuda=True, scope=fluid.Scope()): def loss_scaling_check_inf(self, use_cuda=True, scope=fluid.Scope()):
a = paddle.static.data( a = paddle.static.data(
...@@ -252,14 +258,20 @@ class XPUTestUpdateLossScalingOp(XPUOpTestWrapper): ...@@ -252,14 +258,20 @@ class XPUTestUpdateLossScalingOp(XPUOpTestWrapper):
num_bad_steps, num_bad_steps,
], ],
) )
assert np.array_equal(result_v[0], np.zeros_like(a_v)) np.testing.assert_array_equal(result_v[0], np.zeros_like(a_v))
assert np.array_equal(result_v[1], np.zeros_like(b_v)) np.testing.assert_array_equal(result_v[1], np.zeros_like(b_v))
assert np.array_equal(result_v[2], np.zeros_like(a_v)) np.testing.assert_array_equal(result_v[2], np.zeros_like(a_v))
assert np.array_equal(result_v[3], np.zeros_like(b_v)) np.testing.assert_array_equal(result_v[3], np.zeros_like(b_v))
assert np.array_equal(result_v[4], found_inf_v) np.testing.assert_array_equal(result_v[4], found_inf_v)
assert np.array_equal(result_v[5], prev_loss_scaling_v * decr_ratio) np.testing.assert_array_equal(
assert np.array_equal(result_v[6], np.zeros_like(num_good_steps_v)) result_v[5], prev_loss_scaling_v * decr_ratio
assert np.array_equal(result_v[7], np.zeros_like(num_bad_steps_v)) )
np.testing.assert_array_equal(
result_v[6], np.zeros_like(num_good_steps_v)
)
np.testing.assert_array_equal(
result_v[7], np.zeros_like(num_bad_steps_v)
)
def test_loss_scaling(self): def test_loss_scaling(self):
main = fluid.Program() main = fluid.Program()
......
...@@ -132,18 +132,18 @@ class TestXPUWhereAPI(unittest.TestCase): ...@@ -132,18 +132,18 @@ class TestXPUWhereAPI(unittest.TestCase):
feed={'cond': self.cond, 'x': self.x, 'y': self.y}, feed={'cond': self.cond, 'x': self.x, 'y': self.y},
fetch_list=fetch_list, fetch_list=fetch_list,
) )
assert np.array_equal(out[0], self.out) np.testing.assert_array_equal(out[0], self.out)
if x_stop_gradient is False: if x_stop_gradient is False:
assert np.array_equal( np.testing.assert_array_equal(
out[2], self.ref_x_backward(out[1]) out[2], self.ref_x_backward(out[1])
) )
if y.stop_gradient is False: if y.stop_gradient is False:
assert np.array_equal( np.testing.assert_array_equal(
out[3], self.ref_y_backward(out[1]) out[3], self.ref_y_backward(out[1])
) )
elif y.stop_gradient is False: elif y.stop_gradient is False:
assert np.array_equal( np.testing.assert_array_equal(
out[2], self.ref_y_backward(out[1]) out[2], self.ref_y_backward(out[1])
) )
...@@ -165,7 +165,7 @@ class TestXPUWhereAPI(unittest.TestCase): ...@@ -165,7 +165,7 @@ class TestXPUWhereAPI(unittest.TestCase):
out = exe.run( out = exe.run(
train_prog, feed={'x': x_i, 'y': y_i}, fetch_list=[result] train_prog, feed={'x': x_i, 'y': y_i}, fetch_list=[result]
) )
assert np.array_equal(out[0], np.where(x_i > 1, x_i, y_i)) np.testing.assert_array_equal(out[0], np.where(x_i > 1, x_i, y_i))
class TestWhereDygraphAPI(unittest.TestCase): class TestWhereDygraphAPI(unittest.TestCase):
...@@ -178,7 +178,9 @@ class TestWhereDygraphAPI(unittest.TestCase): ...@@ -178,7 +178,9 @@ class TestWhereDygraphAPI(unittest.TestCase):
y = fluid.dygraph.to_variable(y_i) y = fluid.dygraph.to_variable(y_i)
cond = fluid.dygraph.to_variable(cond_i) cond = fluid.dygraph.to_variable(cond_i)
out = paddle.where(cond, x, y) out = paddle.where(cond, x, y)
assert np.array_equal(out.numpy(), np.where(cond_i, x_i, y_i)) np.testing.assert_array_equal(
out.numpy(), np.where(cond_i, x_i, y_i)
)
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册