How to check if Model is on cuda
napsternxg
As replied on the github issues, an easy way is: <SCODE>next(model.parameters()).is_cuda # returns a boolean
<ECODE>
smth
mikolchon
I tested it right now, and it works even in pytorch v0.4
thadar
But why is it even necessary? If a model is on cuda and you call model.cuda() it should be a no-op and if the model is on cpu and you call model.cpu() it should also be a no-op.
justusschock
mikolchon
Personally, I develop and debug 99% of the code on macOS, and then sync it over to a headless cluster, which is why this pattern is useful to me, for example. Yes, e.g., you can now specify the device 1 time at the top of your script, e.g., <SCODE>device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") 
<ECODE> and then for the model, you can use <SCODE>model = model.to(device)
<ECODE> The same applies also to tensors, e.g,. <SCODE>for features, targets in data_loader:
    features = features.to(device)
    targets = targets.to(device)<ECODE>
rasbt
chiragraman
You can get the device by: <SCODE>next(network.parameters()).device
<ECODE>
Martin_Ennemoser
why doesn’t: <SCODE>self.device
<ECODE> <SCODE>(Pdb) self.device
*** torch.nn.modules.module.ModuleAttributeError: 'MyNN' object has no attribute 'device'
<ECODE>
Brando_Miranda
ptrblck
Brando_Miranda
Yes, that’s correct.
ptrblck