If I'm not specifying to use CPU/GPU, which on

2019-05-16 11:24发布

In pytorch, if I'm not writing anything about using CPU/GPU, and my machine supports CUDA (torch.cuda.is_available() == True):

  1. What is my script using, CPU or GPU?
  2. If CPU, what should I do to make it run on GPU? Do I need to rewrite everything?
  3. If GPU, will this script crash if torch.cuda.is_available() == False?
  4. Does this do anything about making the training faster?
  5. I'm aware of Porting PyTorch code from CPU to GPU but this is old. Does this situation change in v0.4 or the upcoming v1.0?

3条回答
ゆ 、 Hurt°
2楼-- · 2019-05-16 12:06

You should write you code so that it will use GPU processing if torch.cuda.is_available == True.

So:

if torch.cuda.is_available():
    model.cuda()
else:
    # Do Nothing. Run as CPU.
查看更多
我想做一个坏孩纸
3楼-- · 2019-05-16 12:11

My way is like this (below pytorch 0.4):

dtype = torch.cuda.float if torch.cuda.is_available() else torch.float
torch.zeros(2, 2, dtype=dtype)

UPDATE pytorch 0.4:

device = torch.device("cuda" if use_cuda else "cpu")
model = MyRNN().to(device)

from PyTorch 0.4.0 Migration Guide.

查看更多
时光不老,我们不散
4楼-- · 2019-05-16 12:13

PyTorch defaults to the CPU, unless you use the .cuda() methods on your models and the torch.cuda.XTensor variants of PyTorch's tensors.

查看更多
登录 后发表回答