Hi, so i am trying to write an architecture where i have to convert entire models to cuda using model.cuda(). However, some of the elements are variables initialised in the init() loop of nn.Module() class. How do i convert them to cuda ? For example,
class Net(nn.Module):
def __init__(self):
self.xyz=torch.tensor([1,2,3,4...])
# Convert this to cuda without using .cuda() on tensor xyz, but by using model.cuda()
.
.
def forward(self,x):
.
.
I see, I also run stuff both on the CPU and GPU. My solution generally is something like
def make_cuda(fun):
return fun.cuda() if torch.cuda.is_available() else fun
class Net(nn.Module):
def __init__(self):
self.xyz = make_cuda(torch.tensor([1,2,3,4...]))
# Convert this to cuda without using .cuda() on tensor xyz, but by using model.cuda()
.
.
def forward(self,x):
.
.
This will automatically push stuff to cuda only if you are ona machine with a GPU.
This will push tensors to cuda when you call model.cuda():
import torch
import torch.nn as nn
class Net(nn.Module):
def __init__(self):
super().__init__()
self.xyz = torch.tensor([1,2,3,4])
def forward(self,x):
pass
def cuda(self):
super().cuda()
for k, v in self.__dict__.items():
if isinstance(v, torch.Tensor):
v.cuda()
a = Net()
a.cuda()
edit: I mistakenly wrote self.cuda() instead of super().cuda. Fixed.
While this approach would work, the proper way to register tensors inside an nn.Module would be to either use nn.Parameter (if this tensor requires gradients and should be trained) or via self.register_buffer. Both approaches will make sure that this tensor will be pushed to the specified device (in model.to(device)) and will also be added to the state_dict (which would be important if you want to save and load this model).