Expected behaviour - If for any i label[i] > C where pred is of shape N x C x H x W , cross_entropy should raises an IndexError.
Showcased behaviour -
If pred and target are cpu tensors. Output : IndexError: Target 20 is out of bounds.
If pred and target are gpu(cuda) tensors then no error is raised.
Why does cross_entropy shows a discrimination in its behaviour for gpu and cpu tensors? Also the above observed behaviour for cuda tensors is unexpected and isn’t this a bug ?
My environment:
Python : 3.7.7
Pytorch : 1.6.0
Cuda : 10.1
PS: The above behaviour almost consumed 3 hrs of my time. Hope someone would help me understand the reason for this so that I will be cautious next time I deal with gpu tensors.
I could reproduce the issue. I think you have stumbled upon a pytorch bug in nll_loss2d.
I had played around with your example and I could notice that reduction='none' could be the reason. Error is triggered for other reduction (mean / sum) option.
Further, the reason could be that, the device assertion is not checked in case of reduction='none'.
THCudaCheck(cudaGetLastError()); is missing incase of reduction='none'.