def get_normal(self, std):
if <here I need to know which device is used> :
eps = torch.cuda.FloatTensor(std.size()).normal_()
else:
eps = torch.FloatTensor(std.size()).normal_()
return Variable(eps).mul(std)
To work efficiently, it needs to know which device is currently used (CPU or GPU).
I was looking for something like model.is_cuda - but different tensors can be placed on different devices, so probably there is something like std.device_context, but I haven’t found such method either.
Yup, I’ve noted this (this code is based on pytorch examples).
However, there may be different possible situations to handle and I want the model code to be quite isolated from the environment (e.g. place it in a separate python module), and this doesn’t look like a general solution.
This is a feature wanted for quite a long time. I don’t see any reason why pytorch hasn’t provided API simply as .device() to return the device a model/variable/tensor resides on
Yes, although it might be a bit misleading in some special cases.
In case your model is stored on just one GPU, you could simply print the device of one parameter, e.g.:
print(next(model.parameters()).device)
However, you could also use model sharding and split the model among a few GPUs.
In that case you could have to check all parameters for their device.
I am am using copy.deepcopy to instantiate multiple instances of the same model (including same initialization parameters).
I would like to reaffirm if deepcopy copies this storage property, so I’d still have to check somehow.