Repository: pytorch
Issue: Matrix multiplication broken on PyTorch 1.8.1 with CUDA 11.1 and Nvidia GTX 1080 Ti
Labels: high priority,triaged,module: cublas

## 🐛 Bug

Matrix multiplication does not work properly on Torch 1.8.1 with CUDA 11.1 when running on a 1080Ti with 460 or 465 Nvidia drivers.

## To Reproduce

1. Save this test script as test.py:
```python
import torch

def matmul_test(mat_a, mat_b, dtype, device):
    print("matmul_test -- ", dtype, device)
    a = torch.tensor(mat_a, dtype=dtype, device=device,)
    b = torch.tensor(mat_b, dtype=dtype, device=device,)
    c = torch.matmul(b, a)
    print("ba:\n", c)

print("torch version: ", torch.__version__)
a, b = [[1, 2]], [[3], [4]]
print("a:", a, "\nb:", b)
matmul_test(a, b, torch.float16, torch.device("cpu"))
matmul_test(a, b, torch.float16, torch.device("cuda"))
matmul_test(a, b, torch.float32, torch.device("cuda"))
matmul_test(a, b, torch.float64, torch.device("cuda"))
matmul_test(a, b, torch.float16, torch.device("cuda"))
```

2. Save this Dockerfile in the same directory and build it with `docker build . -t torch_test`

```Dockerfile
FROM nvidia/cuda:11.2.1-devel-ubuntu18.04
RUN apt update && apt install -y python3-pip python3.7
RUN python3.7 -m pip install torch==1.8.1+cu111 -f https://download.pytorch.org/whl/torch_stable.html
COPY test.py test.py
ENV PYTHONUNBUFFERED=1gg
ENTRYPOINT python3.7 test.py
```

3. Run the test script with `docker run torch_test`


## Expected behavior

When running this on a machine with a 1080Ti with recent drivers the output will look something like:
```bash
torch version:  1.8.1+cu111
a: [[1, 2]]
b: [[3], [4]]
matmul_test --  torch.float16 cpu
ba:
 tensor([[3., 6.],
        [4., 8.]], dtype=torch.float16)
matmul_test --  torch.float16 cuda
ba:
 tensor([[3., 6.],
        [4., 8.]], device='cuda:0', dtype=torch.float16)
matmul_test --  torch.float32 cuda
ba:
 tensor([[  8208.5000, 131344.0000],
        [     0.0000,      0.0000]], device='cuda:0')
matmul_test --  torch.float64 cuda
ba:
 tensor([[6.9186e+38, 0.0000e+00],
        [0.0000e+00, 0.0000e+00]], device='cuda:0', dtype=torch.float64)
matmul_test --  torch.float16 cuda
Traceback (most recent call last):
  File "test.py", line 20, in <module>
    matmul_test(a, b, torch.float16, torch.device("cuda"))
  File "test.py", line 8, in matmul_test
    c = torch.matmul(b, a)
RuntimeError: CUDA error: CUBLAS_STATUS_EXECUTION_FAILED when calling `cublasGemmEx( handle, opa, opb, m, n, k, &falpha, a, CUDA_R_16F, lda, b, CUDA_R_16F, ldb, &fbeta, c, CUDA_R_16F, ldc, CUDA_R_32F, CUBLAS_GEMM_DFALT_TENSOR_OP)`
```

Notably torch.float16 seems to work if you adjust test.py to just repeatedly do float16 matrix multiplications however float32 and float64 immediately have problems.

#### However, if you run this exact same test on other GPUs it will work perfectly. For example when running this test with a Quadro RTX 6000 it works as expected:
```
torch version:  1.8.1+cu111
a: [[1, 2]]
b: [[3], [4]]
matmul_test --  torch.float16 cpu
ba:
 tensor([[3., 6.],
        [4., 8.]], dtype=torch.float16)
matmul_test --  torch.float16 cuda
ba:
 tensor([[3., 6.],
        [4., 8.]], device='cuda:0', dtype=torch.float16)
matmul_test --  torch.float32 cuda
ba:
 tensor([[3., 6.],
        [4., 8.]], device='cuda:0')
matmul_test --  torch.float64 cuda
ba:
 tensor([[3., 6.],
        [4., 8.]], device='cuda:0', dtype=torch.float64)
matmul_test --  torch.float16 cuda
ba:
 tensor([[3., 6.],
        [4., 8.]], device='cuda:0', dtype=torch.float16)
```

#### Furthermore if you change the torch install in the Dockerfile to use CUDA 10.2 it also works fine.

I have also tried using nightly builds with CUDA 11.1, 11.2, and 11.3 and all of them had the same problem. I have tried using both 460 and 465 drivers and cuda 11.2 and 11.3 on the host with the same problem. So far the only fix I have seen is to use CUDA 10.2 and unfortunately this is not an option for us.

## Environment

collect_env output from host machine:
```
Collecting environment information...
PyTorch version: N/A
Is debug build: N/A
CUDA used to build PyTorch: N/A
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.5 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: Could not collect
CMake version: Could not collect

Python version: 3.6 (64-bit runtime)
Is CUDA available: N/A
CUDA runtime version: Could not collect
GPU models and configuration:
GPU 0: NVIDIA GeForce GTX 1080 Ti
GPU 1: NVIDIA GeForce GTX 1080 Ti
GPU 2: NVIDIA GeForce GTX 1080 Ti
GPU 3: NVIDIA GeForce GTX 1080 Ti
GPU 4: NVIDIA GeForce GTX 1080 Ti
GPU 5: NVIDIA GeForce GTX 1080 Ti
GPU 6: NVIDIA GeForce GTX 1080 Ti
GPU 7: NVIDIA GeForce GTX 1080 Ti

Nvidia driver version: 465.19.01
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip] numpy==1.14.0
[pip] numpydoc==0.7.0
[conda] mkl                       2018.0.1             h19d6760_4
[conda] mkl-service               1.1.2            py36h17a0993_4
[conda] numpy                     1.14.0           py36h3dfced4_1
[conda] numpydoc                  0.7.0            py36h18f165f_0
```

inside of the docker container:
```
root@b654cf42e515:/# python3.7 collect_env.py
Collecting environment information...
PyTorch version: 1.8.1+cu111
Is debug build: False
CUDA used to build PyTorch: 11.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.5 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: Could not collect
CMake version: Could not collect

Python version: 3.7 (64-bit runtime)
Is CUDA available: True
CUDA runtime version: 11.2.152
GPU models and configuration:
GPU 0: NVIDIA GeForce GTX 1080 Ti
GPU 1: NVIDIA GeForce GTX 1080 Ti
GPU 2: NVIDIA GeForce GTX 1080 Ti
GPU 3: NVIDIA GeForce GTX 1080 Ti
GPU 4: NVIDIA GeForce GTX 1080 Ti
GPU 5: NVIDIA GeForce GTX 1080 Ti
GPU 6: NVIDIA GeForce GTX 1080 Ti
GPU 7: NVIDIA GeForce GTX 1080 Ti

Nvidia driver version: 465.19.01
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip] Could not collect
[conda] Could not collect
```

## Related tickets

I don't know for sure but it is possible that this is related to:
* https://github.com/pytorch/pytorch/issues/54799
* https://github.com/pytorch/pytorch/issues/54975

Although those tickets already existed it seems that folks have had trouble reproducing them which is why I opened this ticket.

## Additional context

I do not know whether this is specific to the 1080Ti or all Pascal GPUs.

Please let me know if you need any help reproducing this issue because I tried to make this easy to reproduce.


cc @ezyang @gchanan @zou3519 @bdhirsh @jbschlosser @anjali411 @csarofeen @ptrblck @xwang233