Repository: pytorch
Issue: Nearest-neighbour upsampling with `interpolate` silently fails on CUDA when output size exceeds 2^31
Labels: high priority,module: nn,module: cuda,triaged,module: 64-bit

### 🐛 Describe the bug

Upscaling a tensor where the result size would exceed 2^31 (considering one of the upscaled dimensions) results in zero-valued tensors and no error message, if memory format is `channels_last`.

**Reproduction**

Consider the following **working snippet**. The output is all ones.

```python
import torch

x = torch.ones((16, 256, 512, 512), dtype=torch.bfloat16).cuda().to(memory_format=torch.channels_last)
out = torch.nn.functional.interpolate(x, scale_factor=2, mode='nearest')

assert (out[0] == out[-1]).all()
```

However, **the following snippet fails** - the only difference is that the first dimension changed to `17`, which results in `17*256*512*512*2` being larger than `2^31`:

```python
import torch

x = torch.ones((17, 256, 512, 512), dtype=torch.bfloat16).cuda().to(memory_format=torch.channels_last)
out = torch.nn.functional.interpolate(x, scale_factor=2, mode='nearest')

assert (out[0] == out[-1]).all()
```

Furthermore, the final tensor is all zeroes instead of the expected ones:

```
print(out[-1].abs().sum())
```

> `tensor(0., device='cuda:0', dtype=torch.bfloat16)`

**Workaround**

Make the input tensor contiguous, as in #81665.

**Related issues**
- https://github.com/pytorch/pytorch/issues/129118
- https://github.com/pytorch/pytorch/issues/81665

**Additional considerations**

- The fact that the failure is silent makes this hard to identify, track and debug.
- The CPU implementation works correctly.

### Versions

Collecting environment information...
PyTorch version: 2.5.1+cu124
Is debug build: False
CUDA used to build PyTorch: 12.4
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.4 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.26.4
Libc version: glibc-2.35

Python version: 3.10.12 (main, Jul  5 2023, 18:54:27) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-6.8.0-45-generic-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 12.4.99
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA RTX 6000 Ada Generation
GPU 1: NVIDIA GeForce RTX 3090 Ti

Nvidia driver version: 550.107.02
cuDNN version: Could not collect
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
Address sizes:                        46 bits physical, 48 bits virtual
Byte Order:                           Little Endian
CPU(s):                               36
On-line CPU(s) list:                  0-35
Vendor ID:                            GenuineIntel
Model name:                           Intel(R) Core(TM) i9-10980XE CPU @ 3.00GHz
CPU family:                           6
Model:                                85
Thread(s) per core:                   2
Core(s) per socket:                   18
Socket(s):                            1
Stepping:                             7
CPU max MHz:                          4800.0000
CPU min MHz:                          1200.0000
BogoMIPS:                             6000.00
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 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 ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow 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 hwp hwp_act_window hwp_epp hwp_pkg_req vnmi avx512_vnni md_clear flush_l1d arch_capabilities
Virtualization:                       VT-x
L1d cache:                            576 KiB (18 instances)
L1i cache:                            576 KiB (18 instances)
L2 cache:                             18 MiB (18 instances)
L3 cache:                             24.8 MiB (1 instance)
NUMA node(s):                         1
NUMA node0 CPU(s):                    0-35
Vulnerability Gather data sampling:   Mitigation; Microcode
Vulnerability Itlb multihit:          KVM: Mitigation: VMX disabled
Vulnerability L1tf:                   Not affected
Vulnerability Mds:                    Not affected
Vulnerability Meltdown:               Not affected
Vulnerability Mmio stale data:        Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed:               Mitigation; Enhanced IBRS
Vulnerability Spec rstack overflow:   Not affected
Vulnerability Spec store bypass:      Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:             Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:             Mitigation; Enhanced / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS SW sequence; BHI SW loop, KVM SW loop
Vulnerability Srbds:                  Not affected
Vulnerability Tsx async abort:        Mitigation; TSX disabled

Versions of relevant libraries:
[pip3] clip-anytorch==2.5.2
[pip3] flake8==7.0.0
[pip3] mypy-extensions==1.0.0
[pip3] mypy-protobuf==3.4.0
[pip3] numpy==1.26.4
[pip3] nvidia-cublas-cu11==11.10.3.66
[pip3] nvidia-cublas-cu12==12.4.5.8
[pip3] nvidia-cuda-cupti-cu11==11.7.101
[pip3] nvidia-cuda-cupti-cu12==12.4.127
[pip3] nvidia-cuda-nvrtc-cu11==11.7.99
[pip3] nvidia-cuda-nvrtc-cu12==12.4.127
[pip3] nvidia-cuda-runtime-cu11==11.7.99
[pip3] nvidia-cuda-runtime-cu12==12.4.127
[pip3] nvidia-cudnn-cu11==8.5.0.96
[pip3] nvidia-cudnn-cu12==9.1.0.70
[pip3] nvidia-cufft-cu11==10.9.0.58
[pip3] nvidia-cufft-cu12==11.2.1.3
[pip3] nvidia-curand-cu11==10.2.10.91
[pip3] nvidia-curand-cu12==10.3.5.147
[pip3] nvidia-cusolver-cu11==11.4.0.1
[pip3] nvidia-cusolver-cu12==11.6.1.9
[pip3] nvidia-cusparse-cu11==11.7.4.91
[pip3] nvidia-cusparse-cu12==12.3.1.170
[pip3] nvidia-nccl-cu11==2.14.3
[pip3] nvidia-nccl-cu12==2.21.5
[pip3] nvidia-nvjitlink-cu12==12.4.127
[pip3] nvidia-nvtx-cu11==11.7.91
[pip3] nvidia-nvtx-cu12==12.4.127
[pip3] open_clip_torch==2.24.0
[pip3] pytorch-model-summary==0.1.2
[pip3] rotary-embedding-torch==0.6.4
[pip3] torch==2.5.1
[pip3] torch-fidelity==0.3.0
[pip3] torchaudio==2.5.1
[pip3] torchdiffeq==0.2.3
[pip3] torchinfo==1.8.0
[pip3] torchmetrics==1.4.1
[pip3] torchsummary==1.5.1
[pip3] torchvision==0.20.1
[pip3] triton==3.1.0
[conda] clip-anytorch             2.5.2                    pypi_0    pypi
[conda] cuda-cudart               12.4.99              hd3aeb46_0    conda-forge
[conda] cuda-cudart-dev           12.4.99              hd3aeb46_0    conda-forge
[conda] cuda-cudart-dev_linux-64  12.4.99              h59595ed_0    conda-forge
[conda] cuda-cudart-static        12.4.99              hd3aeb46_0    conda-forge
[conda] cuda-cudart-static_linux-64 12.4.99              h59595ed_0    conda-forge
[conda] cuda-cudart_linux-64      12.4.99              h59595ed_0    conda-forge
[conda] cuda-nvrtc                12.4.99              hd3aeb46_0    conda-forge
[conda] cuda-nvtx                 12.4.99              h59595ed_0    conda-forge
[conda] cudnn                     8.9.7.29             h092f7fd_3    conda-forge
[conda] libcublas                 12.4.5.8                      0    nvidia
[conda] libcublas-dev             12.4.5.8                      0    nvidia
[conda] libcufft                  11.2.0.44            hd3aeb46_0    conda-forge
[conda] libcurand                 10.3.5.119           hd3aeb46_0    conda-forge
[conda] libcusolver               11.6.0.99            hd3aeb46_0    conda-forge
[conda] libcusparse               12.3.0.142           hd3aeb46_0    conda-forge
[conda] libnvjitlink              12.4.99              hd3aeb46_0    conda-forge
[conda] nccl                      2.20.5.1             h3a97aeb_0    conda-forge
[conda] numpy                     1.26.4                   pypi_0    pypi
[conda] nvidia-cublas-cu11        11.10.3.66               pypi_0    pypi
[conda] nvidia-cublas-cu12        12.4.5.8                 pypi_0    pypi
[conda] nvidia-cuda-cupti-cu11    11.7.101                 pypi_0    pypi
[conda] nvidia-cuda-cupti-cu12    12.4.127                 pypi_0    pypi
[conda] nvidia-cuda-nvrtc-cu11    11.7.99                  pypi_0    pypi
[conda] nvidia-cuda-nvrtc-cu12    12.4.127                 pypi_0    pypi
[conda] nvidia-cuda-runtime-cu11  11.7.99                  pypi_0    pypi
[conda] nvidia-cuda-runtime-cu12  12.4.127                 pypi_0    pypi
[conda] nvidia-cudnn-cu11         8.5.0.96                 pypi_0    pypi
[conda] nvidia-cudnn-cu12         9.1.0.70                 pypi_0    pypi
[conda] nvidia-cufft-cu11         10.9.0.58                pypi_0    pypi
[conda] nvidia-cufft-cu12         11.2.1.3                 pypi_0    pypi
[conda] nvidia-curand-cu11        10.2.10.91               pypi_0    pypi
[conda] nvidia-curand-cu12        10.3.5.147               pypi_0    pypi
[conda] nvidia-cusolver-cu11      11.4.0.1                 pypi_0    pypi
[conda] nvidia-cusolver-cu12      11.6.1.9                 pypi_0    pypi
[conda] nvidia-cusparse-cu11      11.7.4.91                pypi_0    pypi
[conda] nvidia-cusparse-cu12      12.3.1.170               pypi_0    pypi
[conda] nvidia-nccl-cu11          2.14.3                   pypi_0    pypi
[conda] nvidia-nccl-cu12          2.21.5                   pypi_0    pypi
[conda] nvidia-nvjitlink-cu12     12.4.127                 pypi_0    pypi
[conda] nvidia-nvtx-cu11          11.7.91                  pypi_0    pypi
[conda] nvidia-nvtx-cu12          12.4.127                 pypi_0    pypi
[conda] open-clip-torch           2.24.0                    dev_0    <develop>
[conda] pytorch-model-summary     0.1.2                    pypi_0    pypi
[conda] rotary-embedding-torch    0.6.4                    pypi_0    pypi
[conda] torch                     2.5.1                    pypi_0    pypi
[conda] torch-fidelity            0.3.0                    pypi_0    pypi
[conda] torchaudio                2.5.1                    pypi_0    pypi
[conda] torchdiffeq               0.2.3                    pypi_0    pypi
[conda] torchinfo                 1.8.0                    pypi_0    pypi
[conda] torchmetrics              1.4.1                    pypi_0    pypi
[conda] torchsummary              1.5.1                    pypi_0    pypi
[conda] torchvision               0.20.1                   pypi_0    pypi
[conda] triton                    3.1.0                    pypi_0    pypi


cc @ezyang @gchanan @zou3519 @kadeng @msaroufim @albanD @mruberry @jbschlosser @walterddr @mikaylagawarecki @ptrblck @eqy