已合并
[fix] revert #34662:restore double type conversion for Ascend950 #36057
wanlinan创建于 5月19日
[fix] revert #34662:restore double type conversion for Ascend950 #36057
已合并
共 2 个文件变更+14-28
| @@ -58,9 +58,7 @@ class TestStorage(TestCase): | |||
| 58 | def _test_untyped(cpu_storage, npu_storage): | 58 | def _test_untyped(cpu_storage, npu_storage): |
| 59 | cpu_res = cpu_storage.untyped() | 59 | cpu_res = cpu_storage.untyped() |
| 60 | npu_res = npu_storage.untyped() | 60 | npu_res = npu_storage.untyped() |
| 61 | - npu_device_name = torch_npu.npu.get_device_name() | 61 | + if dtype == torch.float64: |
| 62 | - # Devices below Ascend950 do not support double; the NPU silently downcasts | ||
| 63 | - if dtype == torch.float64 and npu_device_name < "Ascend950": | ||
| 64 | self.assertEqual(cpu_storage.float().untyped(), npu_res) | 62 | self.assertEqual(cpu_storage.float().untyped(), npu_res) |
| 65 | else: | 63 | else: |
| 66 | self.assertEqual(cpu_res, npu_res.cpu()) | 64 | self.assertEqual(cpu_res, npu_res.cpu()) |
| @@ -74,9 +72,7 @@ class TestStorage(TestCase): | |||
| 74 | def _test_element_size(cpu_storage, npu_storage): | 72 | def _test_element_size(cpu_storage, npu_storage): |
| 75 | cpu_res = cpu_storage.element_size() | 73 | cpu_res = cpu_storage.element_size() |
| 76 | npu_res = npu_storage.element_size() | 74 | npu_res = npu_storage.element_size() |
| 77 | - npu_device_name = torch_npu.npu.get_device_name() | 75 | + if dtype == torch.float64: |
| 78 | - # Devices below Ascend950 do not support double; the NPU silently downcasts | ||
| 79 | - if dtype == torch.float64 and npu_device_name < "Ascend950": | ||
| 80 | self.assertEqual(cpu_res, npu_res * 2) | 76 | self.assertEqual(cpu_res, npu_res * 2) |
| 81 | else: | 77 | else: |
| 82 | self.assertEqual(cpu_res, npu_res) | 78 | self.assertEqual(cpu_res, npu_res) |
| @@ -112,9 +108,7 @@ class TestStorage(TestCase): | |||
| 112 | def _test_nbytes(cpu_storage, npu_storage): | 108 | def _test_nbytes(cpu_storage, npu_storage): |
| 113 | cpu_res = cpu_storage.nbytes() | 109 | cpu_res = cpu_storage.nbytes() |
| 114 | npu_res = npu_storage.nbytes() | 110 | npu_res = npu_storage.nbytes() |
| 115 | - npu_device_name = torch_npu.npu.get_device_name() | 111 | + if dtype == torch.float64: |
| 116 | - # Devices below Ascend950 do not support double; the NPU silently downcasts | ||
| 117 | - if dtype == torch.float64 and npu_device_name < "Ascend950": | ||
| 118 | self.assertEqual(cpu_res, npu_res * 2) | 112 | self.assertEqual(cpu_res, npu_res * 2) |
| 119 | else: | 113 | else: |
| 120 | self.assertEqual(cpu_res, npu_res) | 114 | self.assertEqual(cpu_res, npu_res) |
| @@ -122,9 +116,7 @@ class TestStorage(TestCase): | |||
| 122 | def _test_pickle_storage_type(cpu_storage, npu_storage): | 116 | def _test_pickle_storage_type(cpu_storage, npu_storage): |
| 123 | cpu_res = cpu_storage.pickle_storage_type() | 117 | cpu_res = cpu_storage.pickle_storage_type() |
| 124 | npu_res = npu_storage.pickle_storage_type() | 118 | npu_res = npu_storage.pickle_storage_type() |
| 125 | - npu_device_name = torch_npu.npu.get_device_name() | 119 | + if dtype == torch.float64: |
| 126 | - # Devices below Ascend950 do not support double; the NPU silently downcasts | ||
| 127 | - if dtype == torch.float64 and npu_device_name < "Ascend950": | ||
| 128 | self.assertEqual(npu_res, "FloatStorage") | 120 | self.assertEqual(npu_res, "FloatStorage") |
| 129 | else: | 121 | else: |
| 130 | self.assertEqual(cpu_res, npu_res) | 122 | self.assertEqual(cpu_res, npu_res) |
| @@ -183,9 +175,7 @@ class TestStorage(TestCase): | |||
| 183 | def _test_dtype(cpu_storage, npu_storage): | 175 | def _test_dtype(cpu_storage, npu_storage): |
| 184 | cpu_res = cpu_storage.dtype | 176 | cpu_res = cpu_storage.dtype |
| 185 | npu_res = npu_storage.dtype | 177 | npu_res = npu_storage.dtype |
| 186 | - npu_device_name = torch_npu.npu.get_device_name() | 178 | + if cpu_res == torch.float64: |
| 187 | - # Devices below Ascend950 do not support double; the NPU silently downcasts | ||
| 188 | - if cpu_res == torch.float64 and npu_device_name < "Ascend950": | ||
| 189 | self.assertEqual(npu_res, torch.float32) | 179 | self.assertEqual(npu_res, torch.float32) |
| 190 | else: | 180 | else: |
| 191 | self.assertEqual(npu_res, cpu_res) | 181 | self.assertEqual(npu_res, cpu_res) |
| @@ -32,20 +32,16 @@ at::Tensor NPUNativeFunctions::_to_copy( | |||
| 32 | c10::optional<bool> pin_memory, | 32 | c10::optional<bool> pin_memory, |
| 33 | bool non_blocking, | 33 | bool non_blocking, |
| 34 | c10::optional<c10::MemoryFormat> optional_memory_format) { | 34 | c10::optional<c10::MemoryFormat> optional_memory_format) { |
| 35 | - auto soc_version = c10_npu::GetSocVersion(); | 35 | + if (dtype.has_value() && !layout.has_value() && !device.has_value()) { |
| 36 | - // converting double to float type when devices below Ascend950 | 36 | + // _to_copy is used by to(..., copy=True). Same dtype tensors must |
| 37 | - if (soc_version < c10_npu::SocVersion::Ascend950) { | 37 | + // still fall through to the existing copy path instead of returning self. |
| 38 | - if (dtype.has_value() && !layout.has_value() && !device.has_value()) { | 38 | + if (self.dtype() != dtype) { |
| 39 | - // _to_copy is used by to(..., copy=True). Same dtype tensors must | 39 | + if (dtype == at::ScalarType::Double) { |
| 40 | - // still fall through to the existing copy path instead of returning self. | 40 | + TORCH_NPU_WARN_ONCE( |
| 41 | - if (self.dtype() != dtype) { | 41 | + "Device do not support double dtype now, " |
| 42 | - if (dtype == at::ScalarType::Double) { | 42 | + "dtype cast replace with float."); |
| 43 | - TORCH_NPU_WARN_ONCE( | ||
| 44 | - "Device do not support double dtype now, " | ||
| 45 | - "dtype cast replace with float."); | ||
| 46 | - } | ||
| 47 | - dtype = (dtype == at::ScalarType::Double) ? at::ScalarType::Float : dtype; | ||
| 48 | } | 43 | } |
| 44 | + dtype = (dtype == at::ScalarType::Double) ? at::ScalarType::Float : dtype; | ||
| 49 | } | 45 | } |
| 50 | } | 46 | } |
| 51 | 47 | ||