Repository: pytorch
Issue: torch.compile fails with torch._dynamo.exc.TorchRuntimeError on  a function that contains a torch script module
Labels: triaged,enhancement,oncall: pt2,module: dynamo,dynamo-symbolic-analysis

### 🐛 Describe the bug

When calling `torch.compile` on a function that contains a torch script module, it raises `torch._dynamo.exc.TorchRuntimeError`. When calling directly on the torch script module it works fine.

```
import torch
import torch.nn.functional as F
from torch.nn import Linear, ReLU, Sequential


def works():
    model = Sequential(Linear(100, 200), ReLU(), Linear(200, 10))
    model = torch.jit.trace(model, (torch.randn(1, 100), ))
    model = torch.compile(model)
    return model(torch.randn(1, 100))


def fails():
    model = Sequential(Linear(100, 200), ReLU(), Linear(200, 10))
    model = torch.jit.trace(model, (torch.randn(1, 100), ))
    def predict(x):
        return model(x)
    predict = torch.compile(predict)
    return predict(torch.randn(1, 100))

works()  # works on nightly but fails on 2.0 stable for a different reason
fails()  # fails on both nightly and 2.0 stable
```

Output:
```
Traceback (most recent call last):
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/utils.py", line 1233, in run_node
    return nnmodule(*args, **kwargs)
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
RuntimeError: The following operation failed in the TorchScript interpreter.
Traceback of TorchScript (most recent call last):
/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/nn/modules/linear.py(114): forward
/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/nn/modules/module.py(1488): _slow_forward
/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/nn/modules/module.py(1501): _call_impl
/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/nn/modules/container.py(217): forward
/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/nn/modules/module.py(1488): _slow_forward
/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/nn/modules/module.py(1501): _call_impl
/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/jit/_trace.py(1056): trace_module
/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/jit/_trace.py(794): trace
/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/eval_frame.py(235): _fn
repro.py(15): fails
repro.py(22): <module>
RuntimeError: 


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/utils.py", line 1186, in get_fake_value
    return wrap_fake_exception(
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/utils.py", line 827, in wrap_fake_exception
    return fn()
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/utils.py", line 1187, in <lambda>
    lambda: run_node(tx.output, node, args, kwargs, nnmodule)
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/utils.py", line 1240, in run_node
    raise RuntimeError(
RuntimeError: Failed running call_module model(*(FakeTensor(FakeTensor(..., device='meta', size=(1, 100)), cpu),), **{}):
The following operation failed in the TorchScript interpreter.
Traceback of TorchScript (most recent call last):
/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/nn/modules/linear.py(114): forward
/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/nn/modules/module.py(1488): _slow_forward
/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/nn/modules/module.py(1501): _call_impl
/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/nn/modules/container.py(217): forward
/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/nn/modules/module.py(1488): _slow_forward
/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/nn/modules/module.py(1501): _call_impl
/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/jit/_trace.py(1056): trace_module
/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/jit/_trace.py(794): trace
/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/eval_frame.py(235): _fn
repro.py(15): fails
repro.py(22): <module>
RuntimeError: 

(scroll up for backtrace)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "repro.py", line 22, in <module>
    fails()
  File "repro.py", line 19, in fails
    return predict(torch.randn(1, 100))
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/eval_frame.py", line 235, in _fn
    return fn(*args, **kwargs)
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/eval_frame.py", line 372, in catch_errors
    return callback(frame, cache_size, hooks)
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/convert_frame.py", line 412, in _convert_frame
    result = inner_convert(frame, cache_size, hooks)
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/convert_frame.py", line 110, in _fn
    return fn(*args, **kwargs)
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/convert_frame.py", line 269, in _convert_frame_assert
    return _compile(
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/utils.py", line 166, in time_wrapper
    r = func(*args, **kwargs)
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/convert_frame.py", line 331, in _compile
    out_code = transform_code_object(code, transform)
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/bytecode_transformation.py", line 530, in transform_code_object
    transformations(instructions, code_options)
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/convert_frame.py", line 318, in transform
    tracer.run()
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/symbolic_convert.py", line 1854, in run
    super().run()
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/symbolic_convert.py", line 604, in run
    and self.step()
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/symbolic_convert.py", line 564, in step
    getattr(self, inst.opname)(inst)
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/symbolic_convert.py", line 347, in wrapper
    return inner_fn(self, inst)
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/symbolic_convert.py", line 1003, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/symbolic_convert.py", line 495, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/variables/nn_module.py", line 238, in call_function
    return wrap_fx_proxy(
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/variables/builder.py", line 902, in wrap_fx_proxy
    return wrap_fx_proxy_cls(
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/variables/builder.py", line 937, in wrap_fx_proxy_cls
    example_value = get_fake_value(proxy.node, tx)
  File "/home/jgu/miniconda3/envs/torch-nightly/lib/python3.8/site-packages/torch/_dynamo/utils.py", line 1207, in get_fake_value
    raise TorchRuntimeError() from e
torch._dynamo.exc.TorchRuntimeError: 

from user code:
   File "repro.py", line 17, in predict
    return model(x)

Set torch._dynamo.config.verbose=True or TORCHDYNAMO_VERBOSE=1 for more information


You can suppress this exception and fall back to eager by setting:
    torch._dynamo.config.suppress_errors = True
```

### Versions

Tested both 2.0 stable and nightly torch==2.1.0.dev20230328+cpu


cc @ezyang @bdhirsh @anijain2305 @chauhang @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @kadeng @msaroufim @zou3519 @aakhundov @soumith @wconstab @ngimel @Xia-Weiwen @desertfire