Repository: pytorch
Issue: RecursionError when running torch.jit.script inside JitTestCase
Labels: oncall: jit

### 🐛 Describe the bug

```python
import torch
from torch.testing._internal.common_utils import run_tests
from torch.testing._internal.jit_utils import JitTestCase

class TestCudaFuser(JitTestCase):

    def test_binary_ops(self):
        for _ in range(1000):
            def t(x: torch.Tensor, y: torch.Tensor, z: torch.Tensor):
                o = torch.lt(x, y)
                o = o + z
                return o

            t_jit = torch.jit.script(t)


if __name__ == '__main__':
    run_tests()
```
Error message:
```
======================================================================
ERROR: test_binary_ops (__main__.TestCudaFuser)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/gaoxiang/.local/lib/python3.10/site-packages/torch/testing/_internal/common_utils.py", line 1800, in wrapper
    method(*args, **kwargs)
  File "/home/gaoxiang/misc/jit-script-running.py", line 14, in test_binary_ops
    t_jit = torch.jit.script(t)
  File "/home/gaoxiang/.local/lib/python3.10/site-packages/torch/jit/_script.py", line 1323, in script
    fn = torch._C._jit_script_compile(
  File "/home/gaoxiang/.local/lib/python3.10/site-packages/torch/testing/_internal/jit_utils.py", line 261, in emitFunctionHook
    self._compared_saved_loaded(func)
  File "/home/gaoxiang/.local/lib/python3.10/site-packages/torch/testing/_internal/jit_utils.py", line 241, in _compared_saved_loaded
    imported = torch.jit.load(buffer2)
  File "/home/gaoxiang/.local/lib/python3.10/site-packages/torch/jit/_serialization.py", line 169, in load
    return wrap_cpp_module(cpp_module)
  File "/home/gaoxiang/.local/lib/python3.10/site-packages/torch/jit/_recursive.py", line 865, in wrap_cpp_module
    return torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
  File "/home/gaoxiang/.local/lib/python3.10/site-packages/torch/jit/_script.py", line 593, in _construct
    script_module = RecursiveScriptModule(cpp_module)
  File "/home/gaoxiang/.local/lib/python3.10/site-packages/torch/jit/_script.py", line 573, in __init__
    super(RecursiveScriptModule, self).__init__()
  File "/home/gaoxiang/.local/lib/python3.10/site-packages/torch/jit/_script.py", line 272, in init_then_script
    original_init(self, *args, **kwargs)
  File "/home/gaoxiang/.local/lib/python3.10/site-packages/torch/jit/_script.py", line 475, in __init__
    super(ScriptModule, self).__init__()
  File "/home/gaoxiang/.local/lib/python3.10/site-packages/torch/nn/modules/module.py", line 260, in __init__
    self.training = True
  File "/home/gaoxiang/.local/lib/python3.10/site-packages/torch/jit/_script.py", line 768, in __setattr__
    return super(RecursiveScriptModule, self).__setattr__(attr, value)
  File "/home/gaoxiang/.local/lib/python3.10/site-packages/torch/jit/_script.py", line 500, in __setattr__
    return super(ScriptModule, self).__setattr__(attr, value)
  File "/home/gaoxiang/.local/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1217, in __setattr__
    if isinstance(value, Parameter):
  File "/home/gaoxiang/.local/lib/python3.10/site-packages/torch/nn/parameter.py", line 11, in __instancecheck__
    isinstance(instance, torch.Tensor) and getattr(instance, '_is_param', False))
RecursionError: maximum recursion depth exceeded while calling a Python object

----------------------------------------------------------------------
```

### Versions

```
Collecting environment information...
PyTorch version: 1.12.0a0+git3b5093d
Is debug build: False
CUDA used to build PyTorch: 11.6
ROCM used to build PyTorch: N/A

OS: Arch Linux (x86_64)
GCC version: (GCC) 11.2.0
Clang version: 13.0.1
CMake version: version 3.23.1
Libc version: glibc-2.35

Python version: 3.10.4 (main, Mar 23 2022, 23:05:40) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.17.4-arch1-1-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 11.6.112
GPU models and configuration: 
GPU 0: NVIDIA GeForce RTX 3090
GPU 1: NVIDIA GeForce RTX 2080 Ti

Nvidia driver version: 510.60.02
cuDNN version: Probably one of the following:
/usr/lib/libcudnn.so.8.3.3
/usr/lib/libcudnn_adv_infer.so.8.3.3
/usr/lib/libcudnn_adv_train.so.8.3.3
/usr/lib/libcudnn_cnn_infer.so.8.3.3
/usr/lib/libcudnn_cnn_train.so.8.3.3
/usr/lib/libcudnn_ops_infer.so.8.3.3
/usr/lib/libcudnn_ops_train.so.8.3.3
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] numpy==1.22.3
[pip3] torch==1.12.0a0+gitee4c004
[pip3] torch-ucc==1.0.0
[pip3] torchani==2.2
[pip3] torchvision==0.2.2.post3
[conda] Could not collect
```