Repository: pytorch
Issue: nll_loss2d: t >= 0 && t < n_classes assertion is not checked when using GPU tensors and reduction='none'
Labels: module: nn,module: loss,module: cuda,good first issue,module: error checking,triaged

## 🐛 Bug

The assertion of t >= 0 && t < n_classes is silently ignored in `F.cross_entropy` (in turn, `nll_loss2d`) while using cuda tensors and option `reduction='none'`.

issue raised on behalf of @Pavan_Teja from pytorch forum: https://discuss.pytorch.org/t/mysterious-behaviour-of-cross-entropy-loss/107285

## To Reproduce

Code to reproduce the behavior:

```
import torch
import torch.nn.functional as F

is_cuda = True

input = torch.randn(1, 19, 32, 64, requires_grad=True)
target = torch.randint(22, size=(1,32,64,))
print(input.dim())

print("number of out-of-bound targets", (target > 18).sum())

if is_cuda:
    input = input.cuda()
    target = target.cuda()

out = F.cross_entropy(input, target, weight=None, ignore_index=255, reduction='none')
print(out)
```

## Expected behavior

In the code example above, `input` contains 2D logits for 19 classes at each pixel and `target` contains out-of-bounds class indices.
Ideally, `F.cross_entropy` should report errors for out-of-bounds class indices (regardless of whether CPU or GPU tensors are used).

## Observed behavior
In my testing,

For CPU tensors: the error is reported correctly (Error message: `IndexError: Target 20 is out of bounds.`)

For GPU tensors: for the option, `reduction='mean`' and `reduction='sum'` the error is reported correctly (Error message: `cunn_SpatialClassNLLCriterion_updateOutput_kernel: block: [0,0,0], thread: [369,0,0] Assertion `t >= 0 && t < n_classes` failed.`)
However in the case of `reduction='none`', the error is not reported.

## Environment

```
Collecting environment information...
PyTorch version: 1.7.1
Is debug build: False
CUDA used to build PyTorch: 10.2
ROCM used to build PyTorch: N/A

OS: Ubuntu 16.04.3 LTS (x86_64)
GCC version: (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
Clang version: Could not collect
CMake version: version 3.5.1

Python version: 3.7 (64-bit runtime)
Is CUDA available: True
CUDA runtime version: Could not collect
GPU models and configuration: 
GPU 0: GeForce GTX 1080 Ti
GPU 1: GeForce GTX 1080 Ti

Nvidia driver version: 440.31
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.19.2
[pip3] torch==1.7.1
[pip3] torchaudio==0.7.0a0+a853dff
[pip3] torchvision==0.8.2
[conda] blas                      1.0                         mkl  
[conda] cudatoolkit               10.2.89              h8f6ccaa_6    conda-forge
[conda] mkl                       2020.2                      256  
[conda] mkl-service               2.3.0            py37h8f50634_2    conda-forge
[conda] mkl_fft                   1.2.0            py37h161383b_1    conda-forge
[conda] mkl_random                1.2.0            py37h9fdb41a_1    conda-forge
[conda] numpy                     1.19.2           py37h54aff64_0  
[conda] numpy-base                1.19.2           py37hfa32c7d_0  
[conda] pytorch                   1.7.1           py3.7_cuda10.2.89_cudnn7.6.5_0    pytorch
[conda] torchaudio                0.7.2                      py37    pytorch
[conda] torchvision               0.8.2                py37_cu102    pytorch
```

## Additional context

After some search through low level pytorch code, I see that the device assertion is not checked in case of `reduction='none'`.
https://github.com/pytorch/pytorch/blob/963f7629b591dc9750476faf1513bc7f1fb4d6de/aten/src/THCUNN/generic/SpatialClassNLLCriterion.cu#L70-L99

i.e., `THCudaCheck(cudaGetLastError());` is missing incase of option `reduction='none'`.
Could this be the root cause for silently ignoring the out-of-bounds class indices?


cc @albanD @mruberry @jbschlosser @walterddr @ngimel