Skip to content

Commit

Permalink
[NPU] use np.testing.assert_allclose instead of assertTrue(np.allclos…
Browse files Browse the repository at this point in the history
…e(...)) (#44798)
  • Loading branch information
ronny1996 authored Aug 8, 2022
1 parent 99fb293 commit 73be70a
Show file tree
Hide file tree
Showing 58 changed files with 296 additions and 265 deletions.
8 changes: 4 additions & 4 deletions python/paddle/fluid/tests/unittests/npu/test_adam_op_npu.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ def test_npu(self):
cpu_pred, cpu_loss = self._test(False)
npu_pred, npu_loss = self._test(True)

self.assertTrue(np.allclose(npu_pred, cpu_pred, rtol=1e-3))
self.assertTrue(np.allclose(npu_loss, cpu_loss, rtol=1e-3))
np.testing.assert_allclose(npu_pred, cpu_pred, rtol=1e-3)
np.testing.assert_allclose(npu_loss, cpu_loss, rtol=1e-3)


class TestNetWithEpsilonTensor(unittest.TestCase):
Expand Down Expand Up @@ -447,9 +447,9 @@ def _test_with_place(self, place):
preds.append(pred)
losses.append(loss)
for pred in preds:
self.assertTrue(np.allclose(pred, preds[0]))
np.testing.assert_allclose(pred, preds[0])
for loss in losses:
self.assertTrue(np.allclose(loss, losses[0]))
np.testing.assert_allclose(loss, losses[0])

def test_adam_api(self):
# NOTE(zhiqiu): cpu and gpu has different seed, so should compare separatly.
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/fluid/tests/unittests/npu/test_adamw_op_npu.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ def _test(self, run_npu=True):
def test_npu(self):
npu_pred, npu_loss = self._test(True)
cpu_pred, cpu_loss = self._test(False)
self.assertTrue(np.allclose(npu_pred, cpu_pred, rtol=1e-3))
self.assertTrue(np.allclose(npu_loss, cpu_loss, rtol=1e-3))
np.testing.assert_allclose(npu_pred, cpu_pred, rtol=5e-3)
np.testing.assert_allclose(npu_loss, cpu_loss, rtol=5e-3)


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_not_contains_nan_inf(self):
out, found_inf = self.run_prog(a, b, scale)
print(out, found_inf)

self.assertTrue(np.allclose(out, (a / b) / scale[0]))
np.testing.assert_allclose(out, (a / b) / scale[0])
self.assertFalse(found_inf[0])


Expand Down Expand Up @@ -159,7 +159,7 @@ def test_not_contains_nan_inf(self):
out, found_inf = self.run_prog(a, b, scale)
print(out, found_inf)

self.assertTrue(np.allclose(out, (a + b) / scale[0]))
np.testing.assert_allclose(out, (a + b) / scale[0])
self.assertFalse(found_inf[0])


Expand Down
26 changes: 14 additions & 12 deletions python/paddle/fluid/tests/unittests/npu/test_assign_value_op_npu.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from __future__ import print_function

import unittest
import numpy
import numpy as np
import sys

sys.path.append("..")
Expand All @@ -27,7 +27,7 @@
import paddle.fluid.layers as layers

paddle.enable_static()
numpy.random.seed(2021)
np.random.seed(2021)


class TestAssignValueNPUOp(op_test.OpTest):
Expand All @@ -50,7 +50,7 @@ def set_npu(self):
self.__class__.use_npu = True

def init_data(self):
self.value = numpy.random.random(size=(2, 5)).astype(numpy.float32)
self.value = np.random.random(size=(2, 5)).astype(np.float32)
self.attrs["fp32_values"] = [float(v) for v in self.value.flat]

def test_forward(self):
Expand All @@ -60,30 +60,30 @@ def test_forward(self):
class TestAssignValueNPUOp2(TestAssignValueNPUOp):

def init_data(self):
self.value = numpy.random.random(size=(2, 5)).astype(numpy.int32)
self.value = np.random.random(size=(2, 5)).astype(np.int32)
self.attrs["int32_values"] = [int(v) for v in self.value.flat]


class TestAssignValueNPUOp3(TestAssignValueNPUOp):

def init_data(self):
self.value = numpy.random.random(size=(2, 5)).astype(numpy.int64)
self.value = np.random.random(size=(2, 5)).astype(np.int64)
self.attrs["int64_values"] = [int(v) for v in self.value.flat]


class TestAssignValueNPUOp4(TestAssignValueNPUOp):

def init_data(self):
self.value = numpy.random.choice(a=[False, True],
size=(2, 5)).astype(numpy.bool)
self.value = np.random.choice(a=[False, True],
size=(2, 5)).astype(np.bool)
self.attrs["bool_values"] = [int(v) for v in self.value.flat]


class TestAssignApi(unittest.TestCase):

def setUp(self):
self.init_dtype()
self.value = (-100 + 200 * numpy.random.random(size=(2, 5))).astype(
self.value = (-100 + 200 * np.random.random(size=(2, 5))).astype(
self.dtype)
self.place = fluid.NPUPlace(
0) if fluid.core.is_compiled_with_npu() else fluid.CPUPlace()
Expand All @@ -99,8 +99,10 @@ def test_assign(self):

exe = fluid.Executor(self.place)
[fetched_x] = exe.run(main_program, feed={}, fetch_list=[x])
self.assertTrue(numpy.array_equal(fetched_x, self.value),
"fetch_x=%s val=%s" % (fetched_x, self.value))
np.testing.assert_allclose(fetched_x,
self.value,
err_msg="fetch_x=%s val=%s" %
(fetched_x, self.value))
self.assertEqual(fetched_x.dtype, self.value.dtype)


Expand All @@ -120,8 +122,8 @@ class TestAssignApi4(TestAssignApi):

def setUp(self):
self.init_dtype()
self.value = numpy.random.choice(a=[False, True],
size=(2, 5)).astype(numpy.bool)
self.value = np.random.choice(a=[False, True],
size=(2, 5)).astype(np.bool)
self.place = fluid.NPUPlace(
0) if fluid.core.is_compiled_with_npu() else fluid.CPUPlace()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ def setUp(self):
self.data_formats = ["NCHW", "NHWC"]

def __assert_close(self, tensor, np_array, msg, atol=1e-4):
self.assertTrue(np.allclose(np.array(tensor), np_array, atol=atol), msg)
np.testing.assert_allclose(np.array(tensor),
np_array,
atol=atol,
err_msg=msg)

def check_with_place(self, place, data_layout, dtype, shape):
epsilon = epsilon = 0.00001
Expand Down Expand Up @@ -475,7 +478,7 @@ def compute(x, is_test, trainable_statistics):
x = np.random.randn(*shape).astype("float32")
y1 = compute(x, False, False)
y2 = compute(x, True, True)
self.assertTrue(np.allclose(y1, y2))
np.testing.assert_allclose(y1, y2, rtol=1e-5)

def test_static(self):
places = [fluid.NPUPlace(0)]
Expand All @@ -498,7 +501,7 @@ def compute(x_np, is_test, trainable_statistics):
x = np.random.randn(*shape).astype("float32")
y1 = compute(x, False, False)
y2 = compute(x, True, True)
self.assertTrue(np.allclose(y1, y2, atol=1e-5))
np.testing.assert_allclose(y1, y2, atol=1e-5)


if __name__ == "__main__":
Expand Down
32 changes: 18 additions & 14 deletions python/paddle/fluid/tests/unittests/npu/test_bce_loss_npu.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_static_layer(place,
"weight": weight_np
},
fetch_list=[res])
return static_result
return static_result[0]


def test_static_functional(place,
Expand Down Expand Up @@ -100,7 +100,7 @@ def test_static_functional(place,
"weight": weight_np
},
fetch_list=[res])
return static_result
return static_result[0]


def test_dygraph_layer(place,
Expand Down Expand Up @@ -178,16 +178,18 @@ def test_BCELoss(self):
dy_result = test_dygraph_layer(place, input_np, label_np,
reduction)
expected = calc_bceloss(input_np, label_np, reduction)
self.assertTrue(np.allclose(static_result, expected))
self.assertTrue(np.allclose(static_result, dy_result))
self.assertTrue(np.allclose(dy_result, expected))
np.testing.assert_allclose(static_result, expected, rtol=1e-6)
np.testing.assert_allclose(static_result, dy_result)
np.testing.assert_allclose(dy_result, expected, rtol=1e-6)
static_functional = test_static_functional(
place, input_np, label_np, reduction)
dy_functional = test_dygraph_functional(place, input_np,
label_np, reduction)
self.assertTrue(np.allclose(static_functional, expected))
self.assertTrue(np.allclose(static_functional, dy_functional))
self.assertTrue(np.allclose(dy_functional, expected))
np.testing.assert_allclose(static_functional,
expected,
rtol=1e-6)
np.testing.assert_allclose(static_functional, dy_functional)
np.testing.assert_allclose(dy_functional, expected, rtol=1e-6)

def test_BCELoss_weight(self):
input_np = np.random.uniform(0.1, 0.8,
Expand All @@ -212,9 +214,9 @@ def test_BCELoss_weight(self):
label_np,
reduction,
weight_np=weight_np)
self.assertTrue(np.allclose(static_result, expected))
self.assertTrue(np.allclose(static_result, dy_result))
self.assertTrue(np.allclose(dy_result, expected))
np.testing.assert_allclose(static_result, expected, rtol=1e-6)
np.testing.assert_allclose(static_result, dy_result, rtol=1e-6)
np.testing.assert_allclose(dy_result, expected, rtol=1e-6)
static_functional = test_static_functional(place,
input_np,
label_np,
Expand All @@ -225,9 +227,11 @@ def test_BCELoss_weight(self):
label_np,
reduction,
weight_np=weight_np)
self.assertTrue(np.allclose(static_functional, expected))
self.assertTrue(np.allclose(static_functional, dy_functional))
self.assertTrue(np.allclose(dy_functional, expected))
np.testing.assert_allclose(static_functional, expected, rtol=1e-6)
np.testing.assert_allclose(static_functional,
dy_functional,
rtol=1e-6)
np.testing.assert_allclose(dy_functional, expected, rtol=1e-6)

def test_BCELoss_error(self):
paddle.disable_static(paddle.NPUPlace(0))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ def test_get_set(self):

expected_data = np.array(
[0, 2, 3, 1, 0, 2, 1, 0, 4, 5, 3, 5, 0, 4, 5, 3, 1], "int64")
self.assertTrue(np.array_equal(np.array(sentence_ids), expected_data))
self.assertTrue(np.array_equal(np.array(sentence_scores),
expected_data))
np.testing.assert_array_equal(np.array(sentence_ids), expected_data)
np.testing.assert_array_equal(np.array(sentence_scores), expected_data)


if __name__ == '__main__':
Expand Down
22 changes: 11 additions & 11 deletions python/paddle/fluid/tests/unittests/npu/test_clip_op_npu.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ def test_clip(self):
},
fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7, out_8])

self.assertTrue(np.allclose(res1, data.clip(0.2, 0.8)))
self.assertTrue(np.allclose(res2, data.clip(0.2, 0.9)))
self.assertTrue(np.allclose(res3, data.clip(min=0.3)))
self.assertTrue(np.allclose(res4, data.clip(max=0.7)))
self.assertTrue(np.allclose(res5, data.clip(min=0.2)))
self.assertTrue(np.allclose(res6, data.clip(max=0.8)))
self.assertTrue(np.allclose(res7, data.clip(max=-1)))
self.assertTrue(np.allclose(res8, data))
np.testing.assert_allclose(res1, data.clip(0.2, 0.8))
np.testing.assert_allclose(res2, data.clip(0.2, 0.9))
np.testing.assert_allclose(res3, data.clip(min=0.3))
np.testing.assert_allclose(res4, data.clip(max=0.7))
np.testing.assert_allclose(res5, data.clip(min=0.2))
np.testing.assert_allclose(res6, data.clip(max=0.8))
np.testing.assert_allclose(res7, data.clip(max=-1))
np.testing.assert_allclose(res8, data)
paddle.disable_static()

def test_clip_dygraph(self):
Expand All @@ -200,9 +200,9 @@ def test_clip_dygraph(self):
images = paddle.to_tensor(data, dtype='float32')
out_3 = self._executed_api(images, min=v_min, max=v_max)

self.assertTrue(np.allclose(out_1.numpy(), data.clip(0.2, 0.8)))
self.assertTrue(np.allclose(out_2.numpy(), data.clip(0.2, 0.9)))
self.assertTrue(np.allclose(out_3.numpy(), data.clip(0.2, 0.8)))
np.testing.assert_allclose(out_1.numpy(), data.clip(0.2, 0.8))
np.testing.assert_allclose(out_2.numpy(), data.clip(0.2, 0.9))
np.testing.assert_allclose(out_3.numpy(), data.clip(0.2, 0.8))

def test_errors(self):
paddle.enable_static()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,5 @@ def check_with_place(self, model_file, col_type, need_envs={}):
if col_type == "identity":
need_result1 = input1
need_result2 = input2
self.assertTrue(np.allclose(tr0_out, need_result1, rtol=0, atol=0))
self.assertTrue(np.allclose(tr1_out, need_result2, rtol=0, atol=0))
np.testing.assert_allclose(tr0_out, need_result1, rtol=0, atol=0)
np.testing.assert_allclose(tr1_out, need_result2, rtol=0, atol=0)
6 changes: 2 additions & 4 deletions python/paddle/fluid/tests/unittests/npu/test_concat_op_npu.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,8 @@ def _run_static_mode(self, use_fluid_api):
self.assertTrue(self.out_var.shape[self.axis] == -1)
exe = fluid.Executor(self.place)
res = exe.run(self.program, fetch_list=self.out_var)
self.assertTrue(
np.array_equal(
res[0], np.concatenate([self.x] * self.iter_num,
axis=self.axis)))
np.testing.assert_allclose(
res[0], np.concatenate([self.x] * self.iter_num, axis=self.axis))


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/fluid/tests/unittests/npu/test_cos_op_npu.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ def test_npu(self):
cpu_pred, cpu_loss = self._test(False)
npu_pred, npu_loss = self._test(True)

self.assertTrue(np.allclose(npu_pred, cpu_pred))
self.assertTrue(np.allclose(npu_loss, cpu_loss))
np.testing.assert_allclose(npu_pred, cpu_pred, rtol=1e-6)
np.testing.assert_allclose(npu_loss, cpu_loss, rtol=1e-6)


if __name__ == '__main__':
Expand Down
16 changes: 8 additions & 8 deletions python/paddle/fluid/tests/unittests/npu/test_cumsum_op_npu.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ def run_cases(self):

y = paddle.cumsum(data)
z = np.cumsum(data_np)
self.assertTrue(np.array_equal(z, y.numpy()))
np.testing.assert_array_equal(z, y.numpy())

y = paddle.cumsum(data, axis=0)
z = np.cumsum(data_np, axis=0)
self.assertTrue(np.array_equal(z, y.numpy()))
np.testing.assert_array_equal(z, y.numpy())

y = paddle.cumsum(data, axis=-1)
z = np.cumsum(data_np, axis=-1)
self.assertTrue(np.array_equal(z, y.numpy()))
np.testing.assert_array_equal(z, y.numpy())

y = paddle.cumsum(data, dtype='float32')
self.assertTrue(y.dtype == core.VarDesc.VarType.FP32)
Expand All @@ -51,7 +51,7 @@ def run_cases(self):

y = paddle.cumsum(data, axis=-2)
z = np.cumsum(data_np, axis=-2)
self.assertTrue(np.array_equal(z, y.numpy()))
np.testing.assert_array_equal(z, y.numpy())

def run_static(self, use_npu=False):
with fluid.program_guard(fluid.Program()):
Expand All @@ -74,15 +74,15 @@ def run_static(self, use_npu=False):
])

z = np.cumsum(data_np)
self.assertTrue(np.allclose(z, out[0]))
np.testing.assert_allclose(z, out[0])
z = np.cumsum(data_np, axis=0)
self.assertTrue(np.allclose(z, out[1]))
np.testing.assert_allclose(z, out[1])
z = np.cumsum(data_np, axis=-1)
self.assertTrue(np.allclose(z, out[2]))
np.testing.assert_allclose(z, out[2])
self.assertTrue(out[3].dtype == np.float32)
self.assertTrue(out[4].dtype == np.int32)
z = np.cumsum(data_np, axis=-2)
self.assertTrue(np.allclose(z, out[5]))
np.testing.assert_allclose(z, out[5])

def test_npu(self):
# Now, npu tests need setting paddle.enable_static()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,11 @@ def check_static_result(self, place):
fetches = exe.run(fluid.default_main_program(),
feed={"input": in_np},
fetch_list=[res])
self.assertTrue(np.allclose(fetches[0], res_np))
np.testing.assert_allclose(fetches[0], res_np)
fetches2 = exe.run(fluid.default_main_program(),
feed={"input": in_np},
fetch_list=[res6])
self.assertTrue(np.allclose(fetches2[0], res_np2))
np.testing.assert_allclose(fetches2[0], res_np2)

def test_static(self):
for place in self.places:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ def test_npu(self):
cpu_pred, cpu_loss = self._test(False)
npu_pred, npu_loss = self._test(True)

self.assertTrue(np.allclose(npu_pred, cpu_pred))
self.assertTrue(np.allclose(npu_loss, cpu_loss))
np.testing.assert_allclose(npu_pred, cpu_pred, rtol=1e-6)
np.testing.assert_allclose(npu_loss, cpu_loss, rtol=1e-6)


class TestFloatStatus(unittest.TestCase):
Expand Down
Loading

0 comments on commit 73be70a

Please sign in to comment.