Repository: pytorch
Issue: TracedModule 'to' attribute doesn't work for tensors created on forward.
Labels: oncall: jit,triaged

## 🐛 Bug

<!-- A clear and concise description of what the bug is. -->
TracedModule 'to' attribute doesn't work for tensors created on forward.

## To Reproduce

Steps to reproduce the behavior:

``` python
import torch
class Example(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.module = torch.nn.Linear(10, 100)
    
    def forward(self, x):
        module_output = self.module(x)
        noise = torch.rand_like(module_output)
        return module_output + noise

model = Example().cuda(1)
dummy_input_device_1 = torch.randn(1, 10).cuda(1)
jit_model_device_1 = torch.jit.trace(model, dummy_input_device_1)
jit_model_device_1(dummy_input_device_1)

jit_model_device_0 = jit_model_device_1.to('cuda:0')
dummy_input_device_0 = torch.randn(1, 10).cuda(0)
jit_model_device_0(dummy_input_device_0)

```

<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->

gives error:

```code
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-20-a95f83ffcb23> in <module>()
      6 jit_model_device_0 = jit_model_device_1.to('cuda:0')
      7 dummy_input_device_0 = torch.randn(1, 10).cuda(0)
----> 8 jit_model_device_0(dummy_input_device_0)

/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    489             result = self._slow_forward(*input, **kwargs)
    490         else:
--> 491             result = self.forward(*input, **kwargs)
    492         for hook in self._forward_hooks.values():
    493             hook_result = hook(self, input, result)

/opt/conda/lib/python3.6/site-packages/torch/jit/__init__.py in forward(self, *args, **kwargs)
   1424 class TopLevelTracedModule(TracedModule):
   1425     def forward(self, *args, **kwargs):
-> 1426         return self._get_method('forward')(*args, **kwargs)
   1427 
   1428 

RuntimeError: 
binary_op(): expected both inputs to be on same device, but input a is on cuda:0 and input b is on cuda:1 (binary_op at /data/antoleb/pytorch/aten/src/ATen/native/TensorIterator.cpp:473)
frame #0: c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) + 0x6a (0x7ff110ab63ea in /opt/conda/lib/python3.6/site-packages/torch/lib/libc10.so)
frame #1: at::TensorIterator::binary_op(at::Tensor&, at::Tensor const&, at::Tensor const&) + 0x609 (0x7ff11153a0b9 in /opt/conda/lib/python3.6/site-packages/torch/lib/libcaffe2.so)
frame #2: at::native::add(at::Tensor const&, at::Tensor const&, c10::Scalar) + 0x95 (0x7ff1113596b5 in /opt/conda/lib/python3.6/site-packages/torch/lib/libcaffe2.so)
frame #3: at::TypeDefault::add(at::Tensor const&, at::Tensor const&, c10::Scalar) const + 0x92 (0x7ff11179e412 in /opt/conda/lib/python3.6/site-packages/torch/lib/libcaffe2.so)
frame #4: torch::autograd::VariableType::add(at::Tensor const&, at::Tensor const&, c10::Scalar) const + 0x471 (0x7ff10ff9f2c1 in /opt/conda/lib/python3.6/site-packages/torch/lib/libtorch.so.1)
frame #5: <unknown function> + 0x7b3e97 (0x7ff110185e97 in /opt/conda/lib/python3.6/site-packages/torch/lib/libtorch.so.1)
frame #6: <unknown function> + 0x8a3f7f (0x7ff110275f7f in /opt/conda/lib/python3.6/site-packages/torch/lib/libtorch.so.1)
frame #7: torch::jit::InterpreterState::run(std::vector<c10::IValue, std::allocator<c10::IValue> >&) + 0x31 (0x7ff110270bc1 in /opt/conda/lib/python3.6/site-packages/torch/lib/libtorch.so.1)
frame #8: torch::jit::GraphExecutor::run(std::vector<c10::IValue, std::allocator<c10::IValue> >&) + 0x1c4 (0x7ff110250f34 in /opt/conda/lib/python3.6/site-packages/torch/lib/libtorch.so.1)
frame #9: <unknown function> + 0x3ef605 (0x7ff12645a605 in /opt/conda/lib/python3.6/site-packages/torch/lib/libtorch_python.so)
frame #10: <unknown function> + 0x11174d (0x7ff12617c74d in /opt/conda/lib/python3.6/site-packages/torch/lib/libtorch_python.so)
frame #11: _PyCFunction_FastCallDict + 0x154 (0x5629c5f33b94 in /opt/conda/bin/python)
frame #12: _PyObject_FastCallDict + 0x2bf (0x5629c5f33faf in /opt/conda/bin/python)
frame #13: _PyObject_Call_Prepend + 0x63 (0x5629c5f38a03 in /opt/conda/bin/python)
frame #14: PyObject_Call + 0x3e (0x5629c5f3399e in /opt/conda/bin/python)
frame #15: <unknown function> + 0x16b9b7 (0x5629c5f909b7 in /opt/conda/bin/python)
frame #16: PyObject_Call + 0x3e (0x5629c5f3399e in /opt/conda/bin/python)
frame #17: _PyEval_EvalFrameDefault + 0x1ab0 (0x5629c5fe7470 in /opt/conda/bin/python)
frame #18: <unknown function> + 0x197a94 (0x5629c5fbca94 in /opt/conda/bin/python)
frame #19: _PyFunction_FastCallDict + 0x3db (0x5629c5fbe03b in /opt/conda/bin/python)
frame #20: _PyObject_FastCallDict + 0x26f (0x5629c5f33f5f in /opt/conda/bin/python)
frame #21: _PyObject_Call_Prepend + 0x63 (0x5629c5f38a03 in /opt/conda/bin/python)
frame #22: PyObject_Call + 0x3e (0x5629c5f3399e in /opt/conda/bin/python)
frame #23: _PyEval_EvalFrameDefault + 0x1ab0 (0x5629c5fe7470 in /opt/conda/bin/python)
frame #24: <unknown function> + 0x197a94 (0x5629c5fbca94 in /opt/conda/bin/python)
frame #25: _PyFunction_FastCallDict + 0x1bb (0x5629c5fbde1b in /opt/conda/bin/python)
frame #26: _PyObject_FastCallDict + 0x26f (0x5629c5f33f5f in /opt/conda/bin/python)
frame #27: _PyObject_Call_Prepend + 0x63 (0x5629c5f38a03 in /opt/conda/bin/python)
frame #28: PyObject_Call + 0x3e (0x5629c5f3399e in /opt/conda/bin/python)
frame #29: <unknown function> + 0x16b9b7 (0x5629c5f909b7 in /opt/conda/bin/python)
frame #30: _PyObject_FastCallDict + 0x8b (0x5629c5f33d7b in /opt/conda/bin/python)
frame #31: <unknown function> + 0x19e7ce (0x5629c5fc37ce in /opt/conda/bin/python)
frame #32: _PyEval_EvalFrameDefault + 0x2fa (0x5629c5fe5cba in /opt/conda/bin/python)
frame #33: PyEval_EvalCodeEx + 0x329 (0x5629c5fbe459 in /opt/conda/bin/python)
frame #34: PyEval_EvalCode + 0x1c (0x5629c5fbf1ec in /opt/conda/bin/python)
frame #35: <unknown function> + 0x1be6cb (0x5629c5fe36cb in /opt/conda/bin/python)
frame #36: _PyCFunction_FastCallDict + 0x91 (0x5629c5f33ad1 in /opt/conda/bin/python)
frame #37: <unknown function> + 0x19e67c (0x5629c5fc367c in /opt/conda/bin/python)
frame #38: _PyEval_EvalFrameDefault + 0x2fa (0x5629c5fe5cba in /opt/conda/bin/python)
frame #39: <unknown function> + 0x197a94 (0x5629c5fbca94 in /opt/conda/bin/python)
frame #40: <unknown function> + 0x198941 (0x5629c5fbd941 in /opt/conda/bin/python)
frame #41: <unknown function> + 0x19e755 (0x5629c5fc3755 in /opt/conda/bin/python)
frame #42: _PyEval_EvalFrameDefault + 0x2fa (0x5629c5fe5cba in /opt/conda/bin/python)
frame #43: <unknown function> + 0x197a94 (0x5629c5fbca94 in /opt/conda/bin/python)
frame #44: <unknown function> + 0x198941 (0x5629c5fbd941 in /opt/conda/bin/python)
frame #45: <unknown function> + 0x19e755 (0x5629c5fc3755 in /opt/conda/bin/python)
frame #46: _PyEval_EvalFrameDefault + 0x10ba (0x5629c5fe6a7a in /opt/conda/bin/python)
frame #47: <unknown function> + 0x197dae (0x5629c5fbcdae in /opt/conda/bin/python)
frame #48: <unknown function> + 0x198941 (0x5629c5fbd941 in /opt/conda/bin/python)
frame #49: <unknown function> + 0x19e755 (0x5629c5fc3755 in /opt/conda/bin/python)
frame #50: _PyEval_EvalFrameDefault + 0x2fa (0x5629c5fe5cba in /opt/conda/bin/python)
frame #51: <unknown function> + 0x197a94 (0x5629c5fbca94 in /opt/conda/bin/python)
frame #52: _PyFunction_FastCallDict + 0x3db (0x5629c5fbe03b in /opt/conda/bin/python)
frame #53: _PyObject_FastCallDict + 0x26f (0x5629c5f33f5f in /opt/conda/bin/python)
frame #54: _PyObject_Call_Prepend + 0x63 (0x5629c5f38a03 in /opt/conda/bin/python)
frame #55: PyObject_Call + 0x3e (0x5629c5f3399e in /opt/conda/bin/python)
frame #56: _PyEval_EvalFrameDefault + 0x1ab0 (0x5629c5fe7470 in /opt/conda/bin/python)
frame #57: <unknown function> + 0x197c26 (0x5629c5fbcc26 in /opt/conda/bin/python)
frame #58: <unknown function> + 0x198941 (0x5629c5fbd941 in /opt/conda/bin/python)
frame #59: <unknown function> + 0x19e755 (0x5629c5fc3755 in /opt/conda/bin/python)
frame #60: _PyEval_EvalFrameDefault + 0x10ba (0x5629c5fe6a7a in /opt/conda/bin/python)
frame #61: <unknown function> + 0x197a94 (0x5629c5fbca94 in /opt/conda/bin/python)
frame #62: <unknown function> + 0x198941 (0x5629c5fbd941 in /opt/conda/bin/python)
frame #63: <unknown function> + 0x19e755 (0x5629c5fc3755 in /opt/conda/bin/python)
:
operation failed in interpreter:
<ipython-input-19-fe16b70dc340>(9): forward
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py(479): _slow_forward
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py(489): __call__
/opt/conda/lib/python3.6/site-packages/torch/jit/__init__.py(663): trace
<ipython-input-20-a95f83ffcb23>(3): <module>
/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py(2963): run_code
/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py(2903): run_ast_nodes
/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py(2785): _run_cell
/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py(2662): run_cell
/opt/conda/lib/python3.6/site-packages/ipykernel/zmqshell.py(537): run_cell
/opt/conda/lib/python3.6/site-packages/ipykernel/ipkernel.py(208): do_execute
/opt/conda/lib/python3.6/site-packages/ipykernel/kernelbase.py(399): execute_request
/opt/conda/lib/python3.6/site-packages/ipykernel/kernelbase.py(233): dispatch_shell
/opt/conda/lib/python3.6/site-packages/ipykernel/kernelbase.py(283): dispatcher
/opt/conda/lib/python3.6/site-packages/tornado/stack_context.py(276): null_wrapper
/opt/conda/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py(432): _run_callback
/opt/conda/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py(480): _handle_recv
/opt/conda/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py(450): _handle_events
/opt/conda/lib/python3.6/site-packages/tornado/stack_context.py(276): null_wrapper
/opt/conda/lib/python3.6/site-packages/tornado/platform/asyncio.py(117): _handle_events
/opt/conda/lib/python3.6/asyncio/events.py(145): _run
/opt/conda/lib/python3.6/asyncio/base_events.py(1432): _run_once
/opt/conda/lib/python3.6/asyncio/base_events.py(422): run_forever
/opt/conda/lib/python3.6/site-packages/tornado/platform/asyncio.py(127): start
/opt/conda/lib/python3.6/site-packages/ipykernel/kernelapp.py(486): start
/opt/conda/lib/python3.6/site-packages/traitlets/config/application.py(658): launch_instance
/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py(16): <module>
/opt/conda/lib/python3.6/runpy.py(85): _run_code
/opt/conda/lib/python3.6/runpy.py(193): _run_module_as_main

```


## Environment

PyTorch version: 1.1.0a0+6a297b8
Is debug build: No
CUDA used to build PyTorch: 9.0.176

OS: Ubuntu 16.04.5 LTS
GCC version: (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
CMake version: version 3.13.3

Python version: 3.6
Is CUDA available: Yes
CUDA runtime version: 9.0.176
GPU models and configuration:
GPU 0: GeForce GTX 1080
GPU 1: GeForce GTX 1080
GPU 2: GeForce GTX 1080
GPU 3: GeForce GTX 1080
GPU 4: GeForce GTX 1080
GPU 5: GeForce GTX 1080
GPU 6: GeForce GTX 1080
GPU 7: GeForce GTX 1080

Nvidia driver version: 410.78
cuDNN version: /usr/lib/x86_64-linux-gnu/libcudnn.so.7.4.2

Versions of relevant libraries:
[pip3] numpy==1.14.3
[pip3] numpydoc==0.8.0
[pip3] torch==1.1.0a0+6a297b8
[pip3] torchvision==0.2.1
[conda] blas                      1.0                         mkl
[conda] magma-cuda90              2.3.0                         1    pytorch
[conda] mkl                       2019.0                   pypi_0    pypi
[conda] mkl-include               2019.0                   pypi_0    pypi
[conda] mkl-service               1.1.2            py36h17a0993_4
[conda] mkl_fft                   1.0.1            py36h3010b51_0
[conda] mkl_random                1.0.1            py36h629b387_0
[conda] mkldnn                    0.13.0                        0    mingfeima
[conda] torch                     1.1.0a0+6a297b8          pypi_0    pypi
[conda] torchvision               0.2.1                    pypi_0    pypi


cc @suo