nn.Module.cuda() moves all model parameters and buffers to the GPU.
But why not the model member tensor?
class ToyModule(torch.nn.Module):
def __init__(self) -> None:
super(ToyModule, self).__init__()
self.layer = torch.nn.Linear(2, 2)
self.expected_moved_cuda_tensor = torch.tensor([0, 2, 3])
def forward(self, input: torch.Tensor) -> torch.Tensor:
return self.layer(input)
toy_module = ToyModule()
toy_module.cuda()
next(toy_module.layer.parameters()).device
>>> device(type='cuda', index=0)
for the model member tensor, the device stays unchanged.
>>> toy_module.expected_moved_cuda_tensor.device
device(type='cpu')