已合并
[Fix] Enable UT for isin.Tensor_Tensor. #13576
liu-jiaweikf创建于 2024年8月7日
[Fix] Enable UT for isin.Tensor_Tensor. #13576
已合并
从refs/pull/13576/head合入到master
共 2 个文件变更+17-22
| @@ -6,16 +6,19 @@ import numpy as np | |||
| 6 | import torch | 6 | import torch |
| 7 | from torch import nan | 7 | from torch import nan |
| 8 | from torch.testing import make_tensor | 8 | from torch.testing import make_tensor |
| 9 | - | 9 | +from torch.testing._internal.common_dtype import (all_types, all_types_and, floating_types_and, |
| 10 | -from torch.testing._internal.common_dtype import all_types, all_types_and, floating_types_and, integral_types | 10 | + integral_types, _dispatch_dtypes) |
| 11 | from torch.testing._internal.common_utils import (TestCase, run_tests, slowTest, skipIfTorchDynamo) | 11 | from torch.testing._internal.common_utils import (TestCase, run_tests, slowTest, skipIfTorchDynamo) |
| 12 | -import torch_npu | ||
| 13 | -import torch_npu.testing | ||
| 14 | from torch.testing._internal.common_device_type import \ | 12 | from torch.testing._internal.common_device_type import \ |
| 15 | (instantiate_device_type_tests, dtypes, onlyNativeDeviceTypes, | 13 | (instantiate_device_type_tests, dtypes, onlyNativeDeviceTypes, |
| 16 | onlyPRIVATEUSE1, dtypesIfPRIVATEUSE1, dtypesIfCPU, onlyCPU, largeTensorTest) | 14 | onlyPRIVATEUSE1, dtypesIfPRIVATEUSE1, dtypesIfCPU, onlyCPU, largeTensorTest) |
| 17 | 15 | ||
| 16 | +import torch_npu | ||
| 17 | +import torch_npu.testing | ||
| 18 | + | ||
| 18 | SIZE = 100 | 19 | SIZE = 100 |
| 20 | +all_types_without_double = _dispatch_dtypes((torch.half, torch.float32, torch.uint8, | ||
| 21 | + torch.int8, torch.int16, torch.int32, torch.int64)) | ||
| 19 | 22 | ||
| 20 | 23 | ||
| 21 | class TestSortAndSelect(TestCase): | 24 | class TestSortAndSelect(TestCase): |
| @@ -1014,6 +1017,7 @@ class TestSortAndSelect(TestCase): | |||
| 1014 | self.assertEqual(res[0], ref[0].squeeze()) | 1017 | self.assertEqual(res[0], ref[0].squeeze()) |
| 1015 | self.assertEqual(res[1], ref[1].squeeze()) | 1018 | self.assertEqual(res[1], ref[1].squeeze()) |
| 1016 | 1019 | ||
| 1020 | + # isin.Tensor_Tensor do not support float64 currently. | ||
| 1017 | 1021 | ||
| 1018 | 1022 | ||
| 1019 | def test_isin(self, device, dtype): | 1023 | def test_isin(self, device, dtype): |
| @@ -1026,7 +1030,7 @@ class TestSortAndSelect(TestCase): | |||
| 1026 | self.assertEqual(x, y) | 1030 | self.assertEqual(x, y) |
| 1027 | 1031 | ||
| 1028 | # multi-dim tensor, multi-dim tensor | 1032 | # multi-dim tensor, multi-dim tensor |
| 1029 | - a = torch.arange(24, device=device, dtype=dtype).reshape([2, 3, 4]) | 1033 | + a = torch.tensor(np.arange(24)).to(dtype).to(device).reshape([2, 3, 4]) |
| 1030 | b = torch.tensor([[10, 20, 30], [0, 1, 3], [11, 22, 33]], device=device, dtype=dtype) | 1034 | b = torch.tensor([[10, 20, 30], [0, 1, 3], [11, 22, 33]], device=device, dtype=dtype) |
| 1031 | assert_isin_equal(a, b) | 1035 | assert_isin_equal(a, b) |
| 1032 | 1036 | ||
| @@ -1104,14 +1108,14 @@ class TestSortAndSelect(TestCase): | |||
| 1104 | 1108 | ||
| 1105 | # multi-dimensional input case using sort-based algo | 1109 | # multi-dimensional input case using sort-based algo |
| 1106 | for assume_unique in [False, True]: | 1110 | for assume_unique in [False, True]: |
| 1107 | - a = torch.arange(6, device=device, dtype=dtype).reshape([2, 3]) | 1111 | + a = torch.tensor(np.arange(6)).to(dtype).to(device).reshape([2, 3]) |
| 1108 | - b = torch.arange(3, 30, device=device, dtype=dtype) | 1112 | + b = torch.tensor(np.arange(3, 30)).to(dtype).to(device) |
| 1109 | ec = define_expected([[False, False, False], [True, True, True]], invert=invert) | 1113 | ec = define_expected([[False, False, False], [True, True, True]], invert=invert) |
| 1110 | c = torch.isin(a, b, invert=invert, assume_unique=assume_unique) | 1114 | c = torch.isin(a, b, invert=invert, assume_unique=assume_unique) |
| 1111 | self.assertEqual(c, ec) | 1115 | self.assertEqual(c, ec) |
| 1112 | 1116 | ||
| 1113 | def test_isin_different_dtypes(self, device): | 1117 | def test_isin_different_dtypes(self, device): |
| 1114 | - supported_types = all_types() if device == 'cpu' else all_types_and(torch.half) | 1118 | + supported_types = all_types() if device == 'cpu' else all_types_without_double |
| 1115 | for mult in [1, 10]: | 1119 | for mult in [1, 10]: |
| 1116 | for assume_unique in [False, True]: | 1120 | for assume_unique in [False, True]: |
| 1117 | for dtype1, dtype2 in product(supported_types, supported_types): | 1121 | for dtype1, dtype2 in product(supported_types, supported_types): |
| @@ -1124,13 +1128,13 @@ class TestSortAndSelect(TestCase): | |||
| 1124 | 1128 | ||
| 1125 | 1129 | ||
| 1126 | def test_isin_different_devices(self, device, dtype): | 1130 | def test_isin_different_devices(self, device, dtype): |
| 1127 | - a = torch.arange(6, device=device, dtype=dtype).reshape([2, 3]) | 1131 | + a = torch.tensor(np.arange(6)).to(dtype).to(device).reshape([2, 3]) |
| 1128 | - b = torch.arange(3, 30, device='cpu', dtype=dtype) | 1132 | + b = torch.tensor(np.arange(3, 30)).to(dtype).to('cpu') |
| 1129 | with self.assertRaises(RuntimeError): | 1133 | with self.assertRaises(RuntimeError): |
| 1130 | torch.isin(a, b) | 1134 | torch.isin(a, b) |
| 1131 | 1135 | ||
| 1132 | - c = torch.arange(6, device='cpu', dtype=dtype).reshape([2, 3]) | 1136 | + c = torch.tensor(np.arange(6)).to(dtype).to('cpu').reshape([2, 3]) |
| 1133 | - d = torch.arange(3, 30, device=device, dtype=dtype) | 1137 | + d = torch.tensor(np.arange(3, 30)).to(dtype).to(device) |
| 1134 | with self.assertRaises(RuntimeError): | 1138 | with self.assertRaises(RuntimeError): |
| 1135 | torch.isin(c, d) | 1139 | torch.isin(c, d) |
| 1136 | 1140 | ||
| @@ -2032,18 +2032,9 @@ | |||
| 2032 | "test_nonzero_npu_uint8 (__main__.TestShapeOpsPRIVATEUSE1)": ["", [""]], | 2032 | "test_nonzero_npu_uint8 (__main__.TestShapeOpsPRIVATEUSE1)": ["", [""]], |
| 2033 | "test_sparse_dense_dim_npu_complex128 (__main__.TestShapeOpsPRIVATEUSE1)": ["", [""]], | 2033 | "test_sparse_dense_dim_npu_complex128 (__main__.TestShapeOpsPRIVATEUSE1)": ["", [""]], |
| 2034 | "test_trace_npu_float64 (__main__.TestShapeOpsPRIVATEUSE1)": ["", [""]], | 2034 | "test_trace_npu_float64 (__main__.TestShapeOpsPRIVATEUSE1)": ["", [""]], |
| 2035 | - "test_isin_different_devices_npu_float32 (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], | ||
| 2036 | "test_isin_different_devices_npu_float64 (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], | 2035 | "test_isin_different_devices_npu_float64 (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], |
| 2037 | - "test_isin_different_devices_npu_int16 (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], | ||
| 2038 | - "test_isin_different_devices_npu_int32 (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], | ||
| 2039 | - "test_isin_different_devices_npu_int64 (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], | ||
| 2040 | - "test_isin_different_devices_npu_int8 (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], | ||
| 2041 | - "test_isin_different_devices_npu_uint8 (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], | ||
| 2042 | - "test_isin_different_dtypes_npu (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], | ||
| 2043 | "test_isin_npu_float16 (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], | 2036 | "test_isin_npu_float16 (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], |
| 2044 | - "test_isin_npu_int16 (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], | 2037 | + "test_isin_npu_float64 (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], |
| 2045 | - "test_isin_npu_int8 (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], | ||
| 2046 | - "test_isin_npu_uint8 (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], | ||
| 2047 | "test_kthvalue_npu_float64 (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], | 2038 | "test_kthvalue_npu_float64 (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], |
| 2048 | "test_kthvalue_scalar_npu_float32 (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], | 2039 | "test_kthvalue_scalar_npu_float32 (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], |
| 2049 | "test_msort_npu_bfloat16 (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], | 2040 | "test_msort_npu_bfloat16 (__main__.TestSortAndSelectPRIVATEUSE1)": ["", [""]], |