The problem is that your code tries to convert a cuda tensor to numpy. I guess in your case, it’s the sklearn function that actually try to do the conversion.
Unfortunately, numpy does not support cuda but only cpu tensors.
So you need to change the cuda tensor to a cpu tensor first, then you can convert it to numpy.
You can do this as:
return precision_score(targets.view(-1).cpu(), pred.view(-1).cpu(), average = 'macro')
Note that you can also do the numpy conversion before calling the sklearn function if you have any more issue with conversion: