How do I load a manually created model to GPU in PyTorch?

Hi guys,

I have created a neural network model with all the associated functions from scratch (basic Python code) into a class without using nn.Module. If I can still port this manually created model into GPU?

I ported the train/test data into GPU, but don’t know how to port the model to GPU. It seems that I have to use nn.Module to create models. Is it true? Or, I can port this manually created model into GPU. Any idea?

If you are using nn.Modules you’ll be able to call .to('cuda') on the module(s) directly, which will then automatically move all registered parameters and buffers recursively for all submodules to the GPU.
However, this is not strictly necessary and you could also move the parameters (trainable tensors) as well as the input data manually to the GPU.

How may I make that happen without using nn.Modules? I tried it many times by myself and can’t figure it out.

It depends on the actual use case, but instead of e.g. using an nn.Linear module, you would manually create the parameters and use the functional API or the corresponding torch methods:

device = 'cuda'
x = torch.randn(1, 10).to(device)

# module
lin = nn.Linear(10, 10, bias=False).to(device)
out = lin(x)

# functional API
w = nn.Parameter(torch.randn(10, 10, device=device))
out = F.linear(x, w)

# torch methods
out = torch.matmul(x, w.T)

Does that mean, I have to use “nn” to put a model into GPU?

It depends what you define as a “model” now.
If you want to create nn.Modules, then yes you would have to use the nn namespace, as they are defined there. The same applies for nn.Parameters, but you could also create a tensor and set requires_grad=True. In this case, you wouldn’t use the nn namespace for these operations.