Repository: pytorch
Issue: Computing dot product of columns sliced from large matrix causes illegal memory access in CUDA
Labels: module: dependency bug,module: cuda,triaged,module: 64-bit,module: cublas

## 🐛 Bug

If you compute the dot product of a column sliced from a larger CUDA matrix with itself using `th.mm()`, it will produce an illegal memory access. If you instead compute `(v**2).sum()` (doesn't use CUBLAS), it will not produce the illegal memory access. Additionally, if `v` is allocated separately rather than being sliced, it will not produce the error.

This may be a duplicate of #17897 or just a similar underlying CUBLAS issue. This script can succeed if N*K > 2^31 - 1 (the commented `N = ...` line is bigger but still works) so I'm not sure.

## To Reproduce

```python
import torch as th                                                                                                                    
                                                                                                                                      
N = 16909322                                                                                                                          
#N = 16909321 # << this one won't fail                                                                                                
                                                                                                                                      
K = 127                                                                                                                               
                                                                                                                                      
print(f"Allocating {4*N*K / (1024**3)} GB ...")                                                                                       
U = th.zeros((N, K), device="cuda:0")                                                                                                 
print("Done.")                                                                                                                        
                                                                                                                                      
u = U[:, 0:1]                                                                                                                         
#u = th.zeros_like(u) # << adding this makes the following line succeed for either value of N                                         
                                                                                                                                      
v = th.mm(u.permute(1, 0), u)                                                                                                         
#v = (u**2).sum() # << this succeeds for either value of N                                                                            
                                                                                                                                      
# Just to force the error to propagate up to Python, otherwise it will silently                                                       
# fail and exit.                                                                                                                      
th.cuda.synchronize()
```

## Environment

 - PyTorch Version (e.g., 1.0): 1.3 (also verified still present in latest master)
 - OS (e.g., Linux): Linux
 - Python version: 3.7
 - CUDA/cuDNN version: 9.0.176
 - GPU models and configuration: V100


cc @ezyang @gchanan @zou3519 @csarofeen @ptrblck @ngimel