From 26e20e93f3904dfd7cfa6e693702461d7709700f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=A3=8A?= Date: Mon, 18 Mar 2024 19:30:54 +0800 Subject: [PATCH 1/3] Fix ut for certain device Modification of UT for 910A not supporting fp32 data type --- test/nn/test_linear_functions.py | 34 ++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/test/nn/test_linear_functions.py b/test/nn/test_linear_functions.py index 8fd8be8a4c..5ee1b0c403 100644 --- a/test/nn/test_linear_functions.py +++ b/test/nn/test_linear_functions.py @@ -7,10 +7,23 @@ import torch_npu from torch_npu.testing.testcase import TestCase, run_tests +DEVICE_NAME = torch_npu.npu.get_device_name(0)[:10] class TestLinearFunctions(TestCase): - @unittest.skip("skip test_linear now") def test_linear(self): + input1 = torch.randn(2, 3, 4, dtype=torch.float16) + weight = torch.randn(3, 4, dtype=torch.float16) + npu_input = copy.deepcopy(input1).npu() + npu_weight = copy.deepcopy(weight).npu() + + cpu_output = F.linear(input1, weight) + npu_output = F.linear(npu_input, npu_weight) + + self.assertRtolEqual(cpu_output.numpy(), npu_output.cpu().numpy()) + + @unittest.skipIf(DEVICE_NAME == 'Ascend910A', + "fp32 is not supported on 910A, skip this ut for this device type!") + def test_linear_32(self): input1 = torch.randn(2, 3, 4) weight = torch.randn(3, 4) npu_input = copy.deepcopy(input1).npu() @@ -21,8 +34,25 @@ class TestLinearFunctions(TestCase): self.assertRtolEqual(cpu_output.numpy(), npu_output.cpu().numpy()) - @unittest.skip("skip test_bilinear now") def test_bilinear(self): + input1 = torch.randn(10, 30, dtype=torch.float16) + input2 = torch.randn(10, 40, dtype=torch.float16) + weight = torch.randn(5, 30, 40, dtype=torch.float16) + bias = torch.randn(5, dtype=torch.float16) + + npu_input1 = copy.deepcopy(input1).npu() + npu_input2 = copy.deepcopy(input2).npu() + npu_weight = copy.deepcopy(weight).npu() + npu_bias = copy.deepcopy(bias).npu() + + cpu_output = F.bilinear(input1, input2, weight, bias) + npu_output = F.bilinear(npu_input1, npu_input2, npu_weight, npu_bias) + + self.assertRtolEqual(cpu_output.numpy(), npu_output.cpu().numpy()) + + @unittest.skipIf(DEVICE_NAME == 'Ascend910A', + "fp32 is not supported on 910A, skip this ut for this device type!") + def test_bilinear_32(self): input1 = torch.randn(10, 30) input2 = torch.randn(10, 40) weight = torch.randn(5, 30, 40) -- Gitee From 7b28fbf0d71e39c3c0e8a6594386f3df4e3bf856 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=A3=8A?= Date: Wed, 20 Mar 2024 15:51:12 +0800 Subject: [PATCH 2/3] Modify duplicate code --- test/nn/test_linear_functions.py | 47 ++++++++++++-------------------- 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/test/nn/test_linear_functions.py b/test/nn/test_linear_functions.py index 5ee1b0c403..96d8d01d76 100644 --- a/test/nn/test_linear_functions.py +++ b/test/nn/test_linear_functions.py @@ -6,17 +6,16 @@ import torch.nn.functional as F import torch_npu from torch_npu.testing.testcase import TestCase, run_tests +from torch_npu.testing.common_utils import create_common_tensor DEVICE_NAME = torch_npu.npu.get_device_name(0)[:10] class TestLinearFunctions(TestCase): def test_linear(self): - input1 = torch.randn(2, 3, 4, dtype=torch.float16) - weight = torch.randn(3, 4, dtype=torch.float16) - npu_input = copy.deepcopy(input1).npu() - npu_weight = copy.deepcopy(weight).npu() + cpu_input, npu_input = create_common_tensor([torch.float16, 2, [2, 3, 4]], 0, 1) + cpu_weight, npu_weight = create_common_tensor([torch.float16, 2, [3, 4]], 0, 1) - cpu_output = F.linear(input1, weight) + cpu_output = F.linear(cpu_input, cpu_weight) npu_output = F.linear(npu_input, npu_weight) self.assertRtolEqual(cpu_output.numpy(), npu_output.cpu().numpy()) @@ -24,28 +23,21 @@ class TestLinearFunctions(TestCase): @unittest.skipIf(DEVICE_NAME == 'Ascend910A', "fp32 is not supported on 910A, skip this ut for this device type!") def test_linear_32(self): - input1 = torch.randn(2, 3, 4) - weight = torch.randn(3, 4) - npu_input = copy.deepcopy(input1).npu() - npu_weight = copy.deepcopy(weight).npu() + cpu_input, npu_input = create_common_tensor([torch.float32, 2, [2, 3, 4]], 0, 1) + cpu_weight, npu_weight = create_common_tensor([torch.float32, 2, [3, 4]], 0, 1) - cpu_output = F.linear(input1, weight) + cpu_output = F.linear(cpu_input, cpu_weight) npu_output = F.linear(npu_input, npu_weight) self.assertRtolEqual(cpu_output.numpy(), npu_output.cpu().numpy()) def test_bilinear(self): - input1 = torch.randn(10, 30, dtype=torch.float16) - input2 = torch.randn(10, 40, dtype=torch.float16) - weight = torch.randn(5, 30, 40, dtype=torch.float16) - bias = torch.randn(5, dtype=torch.float16) + cpu_input1, npu_input1 = create_common_tensor([torch.float16, 2, [10, 30]], 0, 1) + cpu_input2, npu_input2 = create_common_tensor([torch.float16, 2, [10, 40]], 0, 1) + cpu_weight, npu_weight = create_common_tensor([torch.float16, 2, [5, 30, 40]], 0, 1) + cpu_bias, npu_bias = create_common_tensor([torch.float16, 2, [5]], 0, 1) - npu_input1 = copy.deepcopy(input1).npu() - npu_input2 = copy.deepcopy(input2).npu() - npu_weight = copy.deepcopy(weight).npu() - npu_bias = copy.deepcopy(bias).npu() - - cpu_output = F.bilinear(input1, input2, weight, bias) + cpu_output = F.bilinear(cpu_input1, cpu_input2, cpu_weight, cpu_bias) npu_output = F.bilinear(npu_input1, npu_input2, npu_weight, npu_bias) self.assertRtolEqual(cpu_output.numpy(), npu_output.cpu().numpy()) @@ -53,17 +45,12 @@ class TestLinearFunctions(TestCase): @unittest.skipIf(DEVICE_NAME == 'Ascend910A', "fp32 is not supported on 910A, skip this ut for this device type!") def test_bilinear_32(self): - input1 = torch.randn(10, 30) - input2 = torch.randn(10, 40) - weight = torch.randn(5, 30, 40) - bias = torch.randn(5) - - npu_input1 = copy.deepcopy(input1).npu() - npu_input2 = copy.deepcopy(input2).npu() - npu_weight = copy.deepcopy(weight).npu() - npu_bias = copy.deepcopy(bias).npu() + cpu_input1, npu_input1 = create_common_tensor([torch.float16, 2, [10, 30]], 0, 1) + cpu_input2, npu_input2 = create_common_tensor([torch.float16, 2, [10, 40]], 0, 1) + cpu_weight, npu_weight = create_common_tensor([torch.float16, 2, [5, 30, 40]], 0, 1) + cpu_bias, npu_bias = create_common_tensor([torch.float16, 2, [5]], 0, 1) - cpu_output = F.bilinear(input1, input2, weight, bias) + cpu_output = F.bilinear(cpu_input1, cpu_input2, cpu_weight, cpu_bias) npu_output = F.bilinear(npu_input1, npu_input2, npu_weight, npu_bias) self.assertRtolEqual(cpu_output.numpy(), npu_output.cpu().numpy()) -- Gitee From 30a05f5e249bc7f8d8e46592891c02fb46e9aed1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=A3=8A?= Date: Wed, 20 Mar 2024 16:00:28 +0800 Subject: [PATCH 3/3] Modify duplicate code --- test/nn/test_linear_functions.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/test/nn/test_linear_functions.py b/test/nn/test_linear_functions.py index 96d8d01d76..ec7795483a 100644 --- a/test/nn/test_linear_functions.py +++ b/test/nn/test_linear_functions.py @@ -4,6 +4,7 @@ import unittest import torch import torch.nn.functional as F import torch_npu +import numpy as np from torch_npu.testing.testcase import TestCase, run_tests from torch_npu.testing.common_utils import create_common_tensor @@ -12,8 +13,8 @@ DEVICE_NAME = torch_npu.npu.get_device_name(0)[:10] class TestLinearFunctions(TestCase): def test_linear(self): - cpu_input, npu_input = create_common_tensor([torch.float16, 2, [2, 3, 4]], 0, 1) - cpu_weight, npu_weight = create_common_tensor([torch.float16, 2, [3, 4]], 0, 1) + cpu_input, npu_input = create_common_tensor([np.float16, 2, [2, 3, 4]], 0, 1) + cpu_weight, npu_weight = create_common_tensor([np.float16, 2, [3, 4]], 0, 1) cpu_output = F.linear(cpu_input, cpu_weight) npu_output = F.linear(npu_input, npu_weight) @@ -23,8 +24,8 @@ class TestLinearFunctions(TestCase): @unittest.skipIf(DEVICE_NAME == 'Ascend910A', "fp32 is not supported on 910A, skip this ut for this device type!") def test_linear_32(self): - cpu_input, npu_input = create_common_tensor([torch.float32, 2, [2, 3, 4]], 0, 1) - cpu_weight, npu_weight = create_common_tensor([torch.float32, 2, [3, 4]], 0, 1) + cpu_input, npu_input = create_common_tensor([np.float32, 2, [2, 3, 4]], 0, 1) + cpu_weight, npu_weight = create_common_tensor([np.float32, 2, [3, 4]], 0, 1) cpu_output = F.linear(cpu_input, cpu_weight) npu_output = F.linear(npu_input, npu_weight) @@ -32,10 +33,10 @@ class TestLinearFunctions(TestCase): self.assertRtolEqual(cpu_output.numpy(), npu_output.cpu().numpy()) def test_bilinear(self): - cpu_input1, npu_input1 = create_common_tensor([torch.float16, 2, [10, 30]], 0, 1) - cpu_input2, npu_input2 = create_common_tensor([torch.float16, 2, [10, 40]], 0, 1) - cpu_weight, npu_weight = create_common_tensor([torch.float16, 2, [5, 30, 40]], 0, 1) - cpu_bias, npu_bias = create_common_tensor([torch.float16, 2, [5]], 0, 1) + cpu_input1, npu_input1 = create_common_tensor([np.float16, 2, [10, 30]], 0, 1) + cpu_input2, npu_input2 = create_common_tensor([np.float16, 2, [10, 40]], 0, 1) + cpu_weight, npu_weight = create_common_tensor([np.float16, 2, [5, 30, 40]], 0, 1) + cpu_bias, npu_bias = create_common_tensor([np.float16, 2, [5]], 0, 1) cpu_output = F.bilinear(cpu_input1, cpu_input2, cpu_weight, cpu_bias) npu_output = F.bilinear(npu_input1, npu_input2, npu_weight, npu_bias) @@ -45,10 +46,10 @@ class TestLinearFunctions(TestCase): @unittest.skipIf(DEVICE_NAME == 'Ascend910A', "fp32 is not supported on 910A, skip this ut for this device type!") def test_bilinear_32(self): - cpu_input1, npu_input1 = create_common_tensor([torch.float16, 2, [10, 30]], 0, 1) - cpu_input2, npu_input2 = create_common_tensor([torch.float16, 2, [10, 40]], 0, 1) - cpu_weight, npu_weight = create_common_tensor([torch.float16, 2, [5, 30, 40]], 0, 1) - cpu_bias, npu_bias = create_common_tensor([torch.float16, 2, [5]], 0, 1) + cpu_input1, npu_input1 = create_common_tensor([np.float32, 2, [10, 30]], 0, 1) + cpu_input2, npu_input2 = create_common_tensor([np.float32, 2, [10, 40]], 0, 1) + cpu_weight, npu_weight = create_common_tensor([np.float32, 2, [5, 30, 40]], 0, 1) + cpu_bias, npu_bias = create_common_tensor([np.float32, 2, [5]], 0, 1) cpu_output = F.bilinear(cpu_input1, cpu_input2, cpu_weight, cpu_bias) npu_output = F.bilinear(npu_input1, npu_input2, npu_weight, npu_bias) -- Gitee