[Fix] Modify the default device of as_tensor.
This commit is contained in:
@@ -68,7 +68,11 @@ def jit_script(obj, optimize=None, _frames_up=0, _rcb=None):
|
||||
|
||||
|
||||
def _as_tensor(*args, **kwargs):
|
||||
dst_device = "cpu"
|
||||
if isinstance(args[0], torch.Tensor):
|
||||
dst_device = args[0].device
|
||||
else:
|
||||
dst_device = "cpu"
|
||||
|
||||
if kwargs and "device" in kwargs:
|
||||
dst_device = kwargs.pop("device")
|
||||
|
||||
|
||||
@@ -177,6 +177,12 @@ class TestCreationOps(TestCase):
|
||||
cpu_output = torch.as_tensor(npu_input, device="cpu")
|
||||
self.assertRtolEqual(cpu_output.numpy(), npu_input.cpu().numpy())
|
||||
|
||||
# npu tesor to npu tensor
|
||||
npu_input = torch.tensor([1, 2, 3]).npu()
|
||||
npu_output = torch.as_tensor(npu_input)
|
||||
self.assertEqual(npu_input.device, npu_output.device)
|
||||
self.assertRtolEqual(npu_input.cpu().numpy(), npu_output.cpu().numpy())
|
||||
|
||||
def test_as_strided(self):
|
||||
x = torch.randn(3, 3, device='npu:0')
|
||||
npu_output1 = torch.as_strided(x, (2, 2), (1, 2))
|
||||
|
||||
Reference in New Issue
Block a user