Sobir
Sobir

Reputation: 165

How to run a pytorch project with CPU?

A Pytorch project is supposed to run on GPU. I want to run it on my laptop only with CPU. There are a lot of places calling .cuda() on models, tensors, etc., which fail to execute when cuda is not available. Is it possible to do it without changing the code everywhere?

Upvotes: 5

Views: 9291

Answers (2)

stackoverflowuser2010
stackoverflowuser2010

Reputation: 40909

Here's the simplest fix I can think of:

  1. Put the following line near the top of your code:
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
  1. Do a global replace. Change .cuda() to .to(device), where device is the variable set in step 1.

Upvotes: 5

Adam
Adam

Reputation: 46

That depends wholly on the structure and design of the code within the project in question. Usually there would be an argument to specify on which device to run on. You should check your code. It is also not advisable to run it on your laptop CPU especially if the project consists of a moderately large model. Good luck!

Upvotes: 0

Related Questions