Repository: pytorch
Issue: Providing CUDA tensor to model on CPU causes a crash
Labels: module: crash,module: rnn,module: cuda,triaged

## 🐛 Bug

Pytorch crashes Google colab session when calling a LSTM model running on CPU (located in RAM) on input located in GPU VRAM.

NOTE: This doesn't happen with Linear model.

## To Reproduce

Colab notebook https://colab.research.google.com/drive/1ojp92wmp_Rh_NWMLbQJEeXmDdTL-TSkE?usp=sharing

Steps to reproduce the behavior:

1. Create a LSTM model running on CPU
2. Create input on GPU
3. Call the model on the input.

Code:
```
import torch
from torch import nn

def testing():
    d_batch = 4
    d_input = 100
    d_hidden = 100
    num_layers = 1
    d_max_seq_len = 1

    m = nn.LSTM(d_input, d_hidden, num_layers=num_layers, dropout=0, batch_first=True)

    device = torch.device('cuda:0')
    enc_phrases = torch.randn(d_batch, d_max_seq_len, d_input, device=device)
    results = m(enc_phrases)

testing()
```

Error:
![pytorch_bug](https://user-images.githubusercontent.com/20921177/82142799-a328e180-985c-11ea-96da-c005ca8bc542.png)
![pytorch_bug_logs](https://user-images.githubusercontent.com/20921177/82142801-a754ff00-985c-11ea-9eff-d4ac9eeb82e2.png)

Colab crash logs https://bpa.st/3VRQ

## Expected behavior

I would expect it to print a traceback and warn the user instead of crashing the entire session.
This sort of errors are caught when the model is running on GPU and the input is not on GPU VRAM.

Code:
```
import torch
from torch import nn

def testing():
    d_batch = 4
    d_input = 100
    d_hidden = 100
    num_layers = 1
    d_max_seq_len = 1

    m = nn.Linear(d_input, d_hidden)

    device = torch.device('cuda:0')
    enc_phrases = torch.randn(d_batch, d_input, device=device)
    results = m(enc_phrases)

testing()
```

Error:
```
RuntimeError: Expected object of device type cuda but got device type cpu for argument #2 'mat2' in call to _th_mm
```

## Environment

Please copy and paste the output from our
[environment collection script](https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py)
(or fill out the checklist below manually).

You can get the script and run it with:
```
wget https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py
# For security purposes, please check the contents of collect_env.py before running it.
python collect_env.py
```
Collecting environment information...
PyTorch version: 1.5.0+cu101
Is debug build: No
CUDA used to build PyTorch: 10.1

OS: Ubuntu 18.04.3 LTS
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
CMake version: version 3.12.0

Python version: 3.6
Is CUDA available: Yes
CUDA runtime version: 10.1.243
GPU models and configuration: GPU 0: Tesla K80
Nvidia driver version: 418.67
cuDNN version: /usr/lib/x86_64-linux-gnu/libcudnn.so.7.6.5

Versions of relevant libraries:
[pip3] numpy==1.18.4
[pip3] torch==1.5.0+cu101
[pip3] torchsummary==1.5.1
[pip3] torchtext==0.3.1
[pip3] torchvision==0.6.0+cu101
[conda] Could not collect

 - PyTorch Version (e.g., 1.0): 1.5.0+cu101
 - OS (e.g., Linux): Linux
 - How you installed PyTorch (`conda`, `pip`, source): Colab
 - Build command you used (if compiling from source): Colab has pytorch preinstalled
 - Python version: 3.6
 - CUDA/cuDNN version: 10.1.243
 - GPU models and configuration: GPU 0: Tesla K80
 - Any other relevant information:

## Additional context

This sort of error is very annoying to debug. I tried https://docs.python.org/3/library/faulthandler.html and it still didn't catch it. Sometimes in large projects you can easily forget to move your model to GPU, or move input to GPU, etc. It took me a long time (8 hours+) to find out what the issue was. Please fix this as soon as possible. Any sort of error or traceback is FAAAAAAAAAAAAAAAAR more useful than crashing the Google colab/Jupyter session.

cc @ezyang @gchanan @zou3519 @ngimel