I’m glad the code is working now.
To avoid such issues in the future, I would use the .to() operation with a specified device, so that changing this single variable will switch between CPU and GPU:
device = 'cuda:0'
# device = 'cpu'  # set for CPU run

targets = targets.to(device)
x = torch.randn(1, device=device)
model.to(device)
...