From 90fc617bbd2d2de9ee6ba5e05b7393323f7c3525 Mon Sep 17 00:00:00 2001 From: cuishiang Date: Fri, 28 Jan 2022 10:24:43 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E4=BA=86=E9=83=A8?= =?UTF-8?q?=E5=88=86=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=A1=86=E6=9E=B6=E6=96=B9=E6=B3=95=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- torch_npu/testing/common_device_type.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/torch_npu/testing/common_device_type.py b/torch_npu/testing/common_device_type.py index b85f001bc0..b6a204e7db 100644 --- a/torch_npu/testing/common_device_type.py +++ b/torch_npu/testing/common_device_type.py @@ -19,7 +19,8 @@ import threading from functools import wraps import unittest import torch -from torch.testing._internal.common_utils import TestCase, TEST_MKL +from torch.testing._internal.common_utils import TEST_MKL +from common_utils import TestCase # Note: Generic Device-Type Testing # -- Gitee From a7ecc7caeb48eae825b89dcdc57509f86f0e6627 Mon Sep 17 00:00:00 2001 From: cuishiang Date: Fri, 28 Jan 2022 15:22:08 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=B0=86=E9=83=A8=E5=88=86util=5Ftest?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E4=B8=AD=E7=9A=84=E4=BB=A3=E7=A0=81=E8=BF=81?= =?UTF-8?q?=E7=A7=BB=E8=87=B3common=5Ftuils.TestCase.setUp()=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- torch_npu/testing/common_utils.py | 7 +++++++ torch_npu/testing/util_test.py | 28 +++++++--------------------- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/torch_npu/testing/common_utils.py b/torch_npu/testing/common_utils.py index 5b2143b8c7..a48a0822da 100644 --- a/torch_npu/testing/common_utils.py +++ b/torch_npu/testing/common_utils.py @@ -589,6 +589,13 @@ class TestCase(expecttest.TestCase): def setUp(self): + npu_device = os.environ.get('SET_NPU_DEVICE') + if npu_device is None: + npu_device = "npu:0" + else: + npu_device = f"npu:{npu_device}" + torch.npu.set_device(npu_device) + print(f"Your device is {npu_device}") if TEST_SKIP_FAST: if not getattr(self, self._testMethodName).__dict__.get('slow_test', False): diff --git a/torch_npu/testing/util_test.py b/torch_npu/testing/util_test.py index a460af4ae3..cb6d4f157d 100644 --- a/torch_npu/testing/util_test.py +++ b/torch_npu/testing/util_test.py @@ -18,21 +18,7 @@ import torch import numpy as np import os -threshold = 1.e-4 -threshold2 = 1.e-3 - -npu_device = os.environ.get('SET_NPU_DEVICE') -if npu_device is None: - npu_device = "npu:0" -else: - npu_device = f"npu:{npu_device}" -torch.npu.set_device(npu_device) -print(f"Your device is {npu_device}") - -threshold = 1.e-4 -threshold2 = 1.e-3 - -UT_FAST_MODE = os.getenv('UT_FAST_MODE') == '1' +UT_FAST_MODE = os.getenv('UT_FAST_MODE') == '1' def create_common_tensor(item, minValue, maxValue): dtype = item[0] @@ -40,7 +26,7 @@ def create_common_tensor(item, minValue, maxValue): shape = item[2] input1 = np.random.uniform(minValue, maxValue, shape).astype(dtype) cpu_input = torch.from_numpy(input1) - npu_input = torch.from_numpy(input1).to(npu_device) + npu_input = torch.from_numpy(input1).npu() if npu_format != -1: npu_input = npu_input.npu_format_cast(npu_format) return cpu_input, npu_input @@ -77,10 +63,10 @@ def __generate_2args_broadcast_cases(): Y = np.random.rand(1, 1, 1).astype(np.float32) cpu_x = torch.from_numpy(X) - npu_x = torch.from_numpy(X).to(npu_device) + npu_x = torch.from_numpy(X).npu() cpu_y = torch.from_numpy(Y) - npu_y = torch.from_numpy(Y).to(npu_device) + npu_y = torch.from_numpy(Y).npu() yield cpu_x, cpu_y, npu_x, npu_y @@ -89,10 +75,10 @@ def __generate_2args_broadcast_cases(): Y = np.random.rand(4, 5).astype(np.float32) cpu_x = torch.from_numpy(X) - npu_x = torch.from_numpy(X).to(npu_device) + npu_x = torch.from_numpy(X).npu() cpu_y = torch.from_numpy(Y) - npu_y = torch.from_numpy(Y).to(npu_device) + npu_y = torch.from_numpy(Y).npu() yield cpu_x, cpu_y, npu_x, npu_y @@ -123,7 +109,7 @@ def create_dtype_tensor(shape, dtype, npu_format=-1, min_value=-5, max_value=5, x = np.where(x != 0, x, ones) cpu_input = torch.from_numpy(x) - npu_input = torch.from_numpy(x).to(npu_device) + npu_input = torch.from_numpy(x).npu() if npu_format != -1 and (dtype in [torch.float, torch.half]): npu_input = npu_input.npu_format_cast(npu_format) return cpu_input, npu_input \ No newline at end of file -- Gitee From 9c41fdbb12b8d3f105eaef2e943d72ade2c71fca Mon Sep 17 00:00:00 2001 From: cuishiang Date: Tue, 8 Feb 2022 10:50:10 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=AF=B9TestCas?= =?UTF-8?q?e=E7=B1=BB=E6=8E=A5=E5=8F=A3=E8=B0=83=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test_testing.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 test/test_testing.py diff --git a/test/test_testing.py b/test/test_testing.py new file mode 100644 index 0000000000..1b422a64bf --- /dev/null +++ b/test/test_testing.py @@ -0,0 +1,38 @@ +import numpy as np +from torch_npu.testing.common_utils import TestCase, run_tests +from torch_npu.testing.util_test import create_common_tensor +from torch_npu.testing.common_device_type import instantiate_device_type_tests +import torch + +# For testing TestCase assertEqual methods +class TestTestingEqual(TestCase): + def test_assertTensorsSlowEqual_API(self, device): + a = torch.tensor([[1, 1], [1, 1], [1, 1]], dtype=torch.float) + b = torch.tensor([[2, 2], [2, 2], [2, 2]], dtype=torch.double) + self.assertTensorsSlowEqual(a, b, prec=3) + + def test_assertRtolEqual_API(self, device): + a = torch.tensor([[1, 2], [3, 4]], dtype=torch.float) + b = torch.tensor([[1, 2], [3, 4]], dtype=torch.float) + self.assertRtolEqual(a, b) + + def test_assertEqual_API(self, device): + a = torch.tensor([[1, 2], [3, 4]], dtype=torch.float) + b = torch.tensor([[1, 2], [3, 4]], dtype=torch.double) + self.assertEqual(a, b) + + def test_assertAlmostEqual_API(self, device): + a = torch.tensor(1, dtype=torch.float) + b = torch.tensor(1.05, dtype=torch.double) + self.assertAlmostEqual(a, b, places=1) + + def test_assertNotEqual_API(self, device): + a = torch.tensor(0, dtype=torch.float) + b = torch.tensor(1, dtype=torch.double) + self.assertNotEqual(a, b) + + +instantiate_device_type_tests(TestTestingEqual, globals(), except_for="cpu") + +if __name__ == "__main__": + run_tests() -- Gitee From e46a4a48bb6e68ab69044641e676274b59ad4e84 Mon Sep 17 00:00:00 2001 From: cuishiang Date: Tue, 8 Feb 2022 11:17:47 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=E6=97=B6=E6=89=BE?= =?UTF-8?q?=E4=B8=8D=E5=88=B0common=5Futils=E6=A8=A1=E5=9D=97=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- torch_npu/testing/common_device_type.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torch_npu/testing/common_device_type.py b/torch_npu/testing/common_device_type.py index b6a204e7db..ac5c651192 100644 --- a/torch_npu/testing/common_device_type.py +++ b/torch_npu/testing/common_device_type.py @@ -20,7 +20,7 @@ from functools import wraps import unittest import torch from torch.testing._internal.common_utils import TEST_MKL -from common_utils import TestCase +from torch_npu.testing.common_utils import TestCase # Note: Generic Device-Type Testing # -- Gitee