Repository: pytorch/pytorch

Issue:
CUDNN error when grid_sample() receives an input with more than 1024 channels
## Code example
```
import torch
x = torch.zeros(1, 1025, 7, 7).cuda()  # 1024 works well
x.requires_grad = True
grid = torch.zeros(1, 3, 3, 2).cuda()
grid.requires_grad = True

# from torch.nn._functions.vision import GridSampler
# outputs = GridSampler.apply(x, grid, 'zeros')  # CUDA version works well
outputs = torch.nn.functional.grid_sample(x, grid)  # CUDNN version doesn't work

loss = torch.mean(outputs)
loss.backward()
```
## Error message
```
File "main.py", line 12, 
    loss.backward()
File "~/anaconda3/lib/python3.6/site-packages/torch/tensor.py", line 93, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
File "~/anaconda3/lib/python3.6/site-packages/torch/autograd/__init__.py", line 89, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: CUDNN_STATUS_NOT_SUPPORTED. This error may appear if you passed in a non-contiguous input.
```

## System Info
PyTorch version: 0.4.0
Is debug build: No
CUDA used to build PyTorch: 9.0.176

OS: Ubuntu 16.04.3 LTS
GCC version: (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609
CMake version: version 3.9.4

Python version: 3.6
Is CUDA available: Yes
CUDA runtime version: 9.0.176
GPU models and configuration: 
GPU 0: TITAN X (Pascal)
GPU 1: TITAN X (Pascal)

Nvidia driver version: 384.98
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.6.0.21
/usr/lib/x86_64-linux-gnu/libcudnn.so.7.0.5
/usr/lib/x86_64-linux-gnu/libcudnn_static_v7.a
/usr/local/MATLAB/R2016b/bin/glnxa64/libcudnn.so.4.0.7
/usr/local/lib/python2.7/dist-packages/torch/lib/libcudnn.so.6
/usr/local/lib/python3.5/dist-packages/torch/lib/libcudnn.so.6

Versions of relevant libraries:
[pip3] numpy (1.14.1)
[pip3] numpydoc (0.6.0)
[pip3] torch (0.4.0)
[pip3] torchvision (0.2.0)
[conda] cuda90                    1.0                  h6433d27_0    pytorch
[conda] magma-cuda90              2.3.0                         1    soumith
[conda] pytorch                   0.4.0           py36_cuda9.0.176_cudnn7.1.2_1  [cuda90]  pytorch
[conda] torchvision               0.2.1                    py36_1    pytorch


Discussion:
torch.backends.cudnn.version() returns 7102
Possibly related: #7258
I'm not sure why, but I can't reproduce this on master. I can reproduce on 0.4 though.
Never mind, this error happens when CUDNN is updated regardless of pytorch version.

Resolving patch:
diff --git a/torch/nn/_functions/vision.py b/torch/nn/_functions/vision.py
--- a/torch/nn/_functions/vision.py
+++ b/torch/nn/_functions/vision.py
@@ -10,7 +10,10 @@
 
 
 def grid_sampler(input, grid, padding_mode):
-    if cudnn.is_acceptable(input.data) and padding_mode == 'zeros' and input.dim() == 4:
+    if (cudnn.is_acceptable(input.data) and
+        padding_mode == 'zeros' and
+        input.dim() == 4 and
+        input.size(1) <= 1024):  # as of cudnn 7102, will not work for larger than 1024
         return torch.cudnn_grid_sampler(input, grid)
     else:
         return GridSampler.apply(input, grid, padding_mode)


Test patch: