Repository: pytorch
Issue: Incorrect gradient calculation for upsample nearest on CUDA
Labels: needs reproduction,module: autograd,module: nn,triaged,module: interpolation

### 🐛 Describe the bug

When I add test case to our device backend. The backward of nearest_upsample op confused me.
For a example, when a tensor of shape {1,3,2,4} resample to {1,3,2,5} using Upsample and scale_factor=1.295. The result only use the first row of input in each channel. But in its grad, the second row is not zeros.
Is it a feature of a bug? And is different from cuda impl.

```
# cpu
tensor([[[[1.4624, 0.9307, 0.2198, 0.6219],
          [1.0367, 0.5511, 0.7772, 0.1279]],

         [[0.8423, 0.5464, 0.4794, 0.2412],
          [0.8416, 0.5206, 0.9406, 0.2007]],

         [[1.0312, 0.7016, 0.3476, 0.3076],
          [0.6786, 0.7558, 0.1232, 0.8883]]]])

# cuda
tensor([[[[2.4991, 1.4818, 0.9969, 0.7497],
          [0.0000, 0.0000, 0.0000, 0.0000]],

         [[1.6840, 1.0670, 1.4200, 0.4418],
          [0.0000, 0.0000, 0.0000, 0.0000]],

         [[1.7098, 1.4574, 0.4708, 1.1960],
          [0.0000, 0.0000, 0.0000, 0.0000]]]], device='cuda:0')
```

Code:
```python
import torch
import numpy as np
import torch
np.random.seed(3693825160)
device = "cpu" # or "cuda"
tensor_0 = torch.from_numpy(np.random.uniform(0.0, 1.0, size=[1, 3, 2, 4])).to(torch.float32).to(device).requires_grad_(True)
# module_4 = torch.nn.Upsample(scale_factor=1.295275651786123, mode='nearest').to(device)
# tensor_6 = module_4(tensor_0)
tensor_6 = torch.nn.functional.interpolate(tensor_0, scale_factor=1.295275651786123, mode='nearest').to(device)
print("tensor_0:")
print(tensor_0)
print("tensor_6:")
print(tensor_6)
tensor_9 = torch.from_numpy(np.random.uniform(0.0, 1.0, size=[1, 3, 2, 5])).to(torch.float32).to(device).requires_grad_(False)
tensor_6.backward(tensor_9)
print("grad:")
print(tensor_0.grad)
```

Env:
```
PyTorch version: 1.12.1 (CPU only)
OS: CentOS Linux 7 (Core) (x86_64)
Python version: 3.8.8 (default, Apr 13 2021, 19:58:26)  [GCC 7.3.0] (64-bit runtime)
```

### Versions

PyTorch version: 1.12.1
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: CentOS Linux 7 (Core) (x86_64)
GCC version: (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Clang version: Could not collect
CMake version: version 3.20.6
Libc version: glibc-2.17

Python version: 3.8.8 (default, Apr 13 2021, 19:58:26)  [GCC 7.3.0] (64-bit runtime)
Python platform: Linux-5.10.0-1.0.0.22-x86_64-with-glibc2.10
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                96
On-line CPU(s) list:   0-95
Thread(s) per core:    2
Core(s) per socket:    24
Socket(s):             2
NUMA node(s):          2
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 85
Model name:            Intel(R) Xeon(R) Gold 6271C CPU @ 2.60GHz
Stepping:              7
CPU MHz:               2945.419
CPU max MHz:           3900.0000
CPU min MHz:           1000.0000
BogoMIPS:              5200.00
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              1024K
L3 cache:              33792K
NUMA node0 CPU(s):     0-23,48-71
NUMA node1 CPU(s):     24-47,72-95
Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts pku avx512_vnni md_clear flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.20.1
[pip3] numpydoc==1.1.0
[pip3] torch==1.12.1
[pip3] torchaudio==0.12.1
[pip3] torchvision==0.13.1
[conda] blas                      1.0                         mkl  
[conda] cpuonly                   2.0                           0    pytorch
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] mkl                       2021.2.0           h06a4308_296  
[conda] mkl-service               2.3.0            py38h27cfd23_1  
[conda] mkl_fft                   1.3.0            py38h42c9631_2  
[conda] mkl_random                1.2.1            py38ha9443f7_2  
[conda] numpy                     1.20.1           py38h93e21f0_0  
[conda] numpy-base                1.20.1           py38h7d8b39e_0  
[conda] numpydoc                  1.1.0              pyhd3eb1b0_1  
[conda] pytorch                   1.12.1              py3.8_cpu_0    pytorch
[conda] pytorch-mutex             1.0                         cpu    pytorch
[conda] torchaudio                0.12.1                 py38_cpu    pytorch
[conda] torchvision               0.13.1                 py38_cpu    pytorch

cc @ezyang @albanD @zou3519 @gqchen @pearu @nikitaved @soulitzer @Lezcano @Varal7 @mruberry @jbschlosser @walterddr @mikaylagawarecki