What is the cpu() in pytorch


correct += pred_eq(target.data).cpu().sum()

What`s the meaning of cpu() ?
Can anyone understand that code ?

1 Like

This is used to move the tensor to cpu(). Some operations on tensors cannot be performed on cuda tensors so you need to move them to cpu first.

6 Likes

tensor.cuda() is used to move a tensor to GPU memory.
tensor.cpu() moves it back to memory accessible to the CPU.

5 Likes

But after doing tensor.cpu() when I check the device of tensor using tensor.device it gives the original cuda:0 where it was before moving to cpu. How can I be sure that the tensor is moved to CPU?

You have to assign the tensor after moving:

tensor = tensor.cpu()
# or using the new method
tensor = tensor.to('cpu)
16 Likes

Thanks @ptrblck. I see that .cpu() or .cuda() works differently for model and tensor. :+1:

8 Likes

Is there an official list of operations that are unable to performed on cuda tensors?

I’m wondering if there is some guideline to understand when you can and cannot perform certain operations?

Thanks!

2 Likes

I have a big model and my RAM is 6.
Is it helpful to send some tensors to CPU, and others remain on cuda to solve the cuda out of memory error?

I don’t know what “my RAM is 6” means, but CPU offloading can generally reduce the GPU memory usage.