Tensor.cuda() works but tensor.to("cuda") does not work

Hi guys, I have this issue where to("cuda") does nothing to some of my tensors. Does not throw an erro but when I check afterwards, the tensor still throws tensor.is_cuda >>> False Only when I am using .cuda() The loss calculation can be performed. I cannot find a minimal working example that reproduces the error, I assume it has something to do with the data preparation. Below is an lose example of what I am talking about. Maybe someone has experienced a similar problem, and knows what my mistake is.

device = "cuda"
model.to(device)
clf_targets.to(device)
input.to(device)
output = model(input)
loss = criterion(output, clf_targets) 

does not work as clf_targets remains on the cpu.

device = "cuda"
model.to(device)
clf_targets.to(device)
input.to(device)
output = model(input)
loss = criterion(output, clf_targets.cuda()) 

does work.

you need

device = "cuda"
model.to(device)
clf_targets.to(device)
input = input.to(device)
output = model(input)
loss = criterion(output, clf_targets.cuda()) 

Model don’t need assign after move, but tensor need

Yes that is the right solution. Thanks. I had a Brainfart