k2-fsa / k2

FSA/FST algorithms, differentiable, with PyTorch compatibility.
https://k2-fsa.github.io/k2
Apache License 2.0
1.11k stars 213 forks source link

Can not copy k2.RaggedTensor from CPU to GPU by RaggedTensor.to(device_id) #1266

Closed kobenaxie closed 9 months ago

kobenaxie commented 9 months ago
In [1]: import k2

In [2]: import torch

In [3]: tensor = torch.tensor([0])

In [4]: tensor
Out[4]: tensor([0])

In [5]: tensor.to(0)
Out[5]: tensor([0], device='cuda:0')

In [6]: tensor = k2.RaggedTensor([[0], [1,2]])

In [7]: tensor
Out[7]:
RaggedTensor([[0],
              [1, 2]], dtype=torch.int32)

In [8]: tensor.to(0)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In [8], line 1
----> 1 tensor.to(0)

RuntimeError: Invalid device string: '0'

In [9]: tensor.to(torch.device("cuda:0"))
Out[9]:
RaggedTensor([[0],
              [1, 2]], dtype=torch.int32)

In [10]: x = tensor.to(torch.device("cuda:0"))

In [11]: x.device
Out[11]: device(type='cuda', index=0)

I try to copy a RaggedTensor from CPU to GPU by tensor.to(0), but failed with error RuntimeError: Invalid device string: '0', while torch.Tensor succeeded.

csukuangfj commented 9 months ago

We don't support tensor.to(0).

You can use

tensor.to('cpu')

or

tensor.to('cuda:0')

or

device = torch.device('cpu')
tensor.to(device)

or

device = torch.device('cuda', 0)
tensor.to(device)

Please only use the above methods and never try tensor.to(0), which I have never seen before.