Repository: pytorch
Issue: INTERNAL ASSERT FAILED when reusing JIT compiled module
Labels: oncall: jit

## 🐛 Bug

The JIT compiler crashes under specific circumstances when one tries to compile a module with a submodule that has been compiled before. I think the bug is best explained by the following minimal example.

## To Reproduce

```python
import torch


class Inner(torch.nn.Module):
    def forward(self):
        return torch.ones(1)


class Outer(torch.nn.Module):
    def __init__(self, inner: Inner):
        super(Outer, self).__init__()
        self.inner = inner

    def forward(self):
        return self.inner.forward()

    @torch.jit.export
    def get_inner(self):
        return self.inner


inner_jit = torch.jit.script(Inner())

outer1 = Outer(inner_jit)
outer1_jit = torch.jit.script(outer1)

print(f"{type(outer1_jit.inner)=}")
print(f"{type(outer1_jit.get_inner())=}")

outer2 = Outer(outer1_jit.inner)
outer2_jit = torch.jit.script(outer2)

outer3 = Outer(outer1_jit.get_inner())
outer3_jit = torch.jit.script(outer3)
```

In the above code, there are two modules, `Inner` and `Outer`, with `Outer` simply holding an instance of `Inner`. If an instance of `Inner` is passed to `Outer` and the resulting instance (`outer1`) is compiled, everything works as expected. The same holds true if the procedure is repeated, but with `outer1_jit.inner` as input to `Outer`'s constructor. However, if one calls `outer1_jit.get_inner()`, which simply returns the inner instance and passes that to `Outer`'s constructor, the resulting instance cannot be compiled anymore and crashes with the following output:

```
type(outer1_jit.inner)=<class 'torch.jit._script.RecursiveScriptModule'>
type(outer1_jit.get_inner())=<class 'torch._C.ScriptModule'>
Traceback (most recent call last):
  File "/path/to/test.py", line 34, in <module>
    outer3_jit = torch.jit.script(outer3)
  File "/usr/lib/python3.9/site-packages/torch/jit/_script.py", line 1096, in script
    return torch.jit._recursive.create_script_module(
  File "/usr/lib/python3.9/site-packages/torch/jit/_recursive.py", line 412, in create_script_module
    return create_script_module_impl(nn_module, concrete_type, stubs_fn)
  File "/usr/lib/python3.9/site-packages/torch/jit/_recursive.py", line 478, in create_script_module_impl
    create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs)
  File "/usr/lib/python3.9/site-packages/torch/jit/_recursive.py", line 355, in create_methods_and_properties_from_stubs
    concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults)
RuntimeError: it != data_.modules_.end()INTERNAL ASSERT FAILED at "/build/python-pytorch/src/pytorch-1.9.0-opt-cuda/torch/csrc/jit/frontend/concrete_module_type.cpp":204, please report a bug to PyTorch. 
```

## Expected behavior

I would expect `outer1_jit.inner` and `outer1_jit.get_inner()` to return the same object. However, the former returns an object of type `torch.jit._script.RecursiveScriptModule`and the latter an object of type `torch._C.ScriptModule` which apparently cannot be compiled. If it is not possible to change that, a more meaningful error message would be helpful.

## Environment

```
PyTorch version: 1.9.0
Is debug build: False
CUDA used to build PyTorch: 11.4
ROCM used to build PyTorch: N/A

OS: Arch Linux (x86_64)
GCC version: (GCC) 11.1.0
Clang version: 12.0.1
CMake version: version 3.21.3
Libc version: glibc-2.33

Python version: 3.9.7 (default, Aug 31 2021, 13:28:12)  [GCC 11.1.0] (64-bit runtime)
Python platform: Linux-5.14.8-arch1-1-x86_64-with-glibc2.33
Is CUDA available: True
CUDA runtime version: 10.1.243
GPU models and configuration: GPU 0: NVIDIA GeForce GTX 1070
Nvidia driver version: 470.74
cuDNN version: Probably one of the following:
/usr/lib/libcudnn.so.8.2.2
/usr/lib/libcudnn_adv_infer.so.8.2.2
/usr/lib/libcudnn_adv_train.so.8.2.2
/usr/lib/libcudnn_cnn_infer.so.8.2.2
/usr/lib/libcudnn_cnn_train.so.8.2.2
/usr/lib/libcudnn_ops_infer.so.8.2.2
/usr/lib/libcudnn_ops_train.so.8.2.2
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] mypy==0.910
[pip3] mypy-extensions==0.4.3
[pip3] mypy-protobuf==2.9
[pip3] numpy==1.21.2
[pip3] torch==1.9.0
[pip3] torch-cluster==1.5.9
[pip3] torch-geometric==1.7.2
[pip3] torch-scatter==2.0.8
[pip3] torch-sparse==0.6.11
[conda] Could not collect
```