已合并
Rollback for bugfix #25595
guoguanghao创建于 2025年10月11日
Rollback for bugfix #25595
已合并
共 2 个文件变更+4-5
| @@ -394,6 +394,7 @@ class TestNpuMultiNpu(TestCase): | |||
| 394 | z = torch.cat([x, y], 0) | 394 | z = torch.cat([x, y], 0) |
| 395 | self.assertEqual(z.get_device(), x.get_device()) | 395 | self.assertEqual(z.get_device(), x.get_device()) |
| 396 | 396 | ||
| 397 | + | ||
| 397 | def test_load_nonexistent_device(self): | 398 | def test_load_nonexistent_device(self): |
| 398 | # Setup: create a serialized file object with a 'npu:9' restore location | 399 | # Setup: create a serialized file object with a 'npu:9' restore location |
| 399 | tensor = torch.randn(2, device='npu') | 400 | tensor = torch.randn(2, device='npu') |
| @@ -22,17 +22,15 @@ def _rebuild_npu_tensor(storage, storage_offset, size, stride, requires_grad, ba | |||
| 22 | "please use newer torch to re-store the weight file." | 22 | "please use newer torch to re-store the weight file." |
| 23 | ) | 23 | ) |
| 24 | se._warn_legacy_serialization(warn_massages, "oldfile") | 24 | se._warn_legacy_serialization(warn_massages, "oldfile") |
| 25 | - device = storage._untyped_storage.device | ||
| 26 | - storage = storage.cpu() | ||
| 27 | tensor = torch.tensor([], dtype=storage.dtype, device=storage._untyped_storage.device) | 25 | tensor = torch.tensor([], dtype=storage.dtype, device=storage._untyped_storage.device) |
| 28 | tensor.set_(storage, storage_offset, size, stride) | 26 | tensor.set_(storage, storage_offset, size, stride) |
| 29 | tensor.requires_grad = requires_grad | 27 | tensor.requires_grad = requires_grad |
| 30 | tensor._backward_hooks = backward_hooks | 28 | tensor._backward_hooks = backward_hooks |
| 31 | if not se.RE_MAP_CPU: | 29 | if not se.RE_MAP_CPU: |
| 32 | if isinstance(npu_storage_info, bool): | 30 | if isinstance(npu_storage_info, bool): |
| 33 | - tensor = tensor.to(device) | 31 | + tensor = tensor.npu() |
| 34 | else: | 32 | else: |
| 35 | - tensor = torch_npu.npu_format_cast(tensor.to(device), npu_storage_info) | 33 | + tensor = torch_npu.npu_format_cast(tensor.npu(), npu_storage_info) |
| 36 | return tensor | 34 | return tensor |
| 37 | 35 | ||
| 38 | 36 | ||
| @@ -55,7 +53,7 @@ def _reduce_ex(self, proto): | |||
| 55 | backward_hooks: Dict[Any, Any] = OrderedDict() | 53 | backward_hooks: Dict[Any, Any] = OrderedDict() |
| 56 | if self.device.type == "npu": | 54 | if self.device.type == "npu": |
| 57 | npu_storage_format = torch_npu.get_npu_format(self) | 55 | npu_storage_format = torch_npu.get_npu_format(self) |
| 58 | - tmp_tensor = self | 56 | + tmp_tensor = self.cpu() |
| 59 | arg_npu = ( | 57 | arg_npu = ( |
| 60 | tmp_tensor.storage() if has_torch_function_unary(tmp_tensor) else tmp_tensor._typed_storage(), | 58 | tmp_tensor.storage() if has_torch_function_unary(tmp_tensor) else tmp_tensor._typed_storage(), |
| 61 | tmp_tensor.storage_offset(), | 59 | tmp_tensor.storage_offset(), |