Gulzar
Gulzar

Reputation: 27946

How to check if a tensor is on cuda or send it to cuda in Pytorch?

I have a tensor

t = torch.zeros((4, 5, 6))

How to check if it is on gpu or not, and send it to gpu and back?

Upvotes: 24

Views: 46695

Answers (3)

chakrr
chakrr

Reputation: 527

Old question and correct answers, but why not use

print(tensor.get_device()), as mentioned here?

This returns 0 if tensor is on GPU, and -1 if on CPU.

Upvotes: 0

Leon Brant
Leon Brant

Reputation: 21

@Gulzar only tells you how to check whether the tensor is on the cpu or on the gpu. You can calculate the tensor on the GPU by the following method:

t = torch.rand(5, 3)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
t = t.to(device)

Upvotes: 1

Gulzar
Gulzar

Reputation: 27946

From the pytorch forum

use t.is_cuda, t.cuda(), t.cpu()

t = torch.randn(2,2)
t.is_cuda  # returns False
t = torch.randn(2,2).cuda()
t.is_cuda  # returns True
t = t.cpu()
t.is_cuda  # returns False

When passing to and from gpu and cpu, new arrays are allocated on the relevant device.

Upvotes: 43

Related Questions