Repository: pytorch
Issue: Can't script a tensorrt model
Labels: oncall: jit

### 🐛 Describe the bug

I am a newbie for pytorch. I try to use tensorrt to optimize the model and save it as trt engine(*.plan). I tried the following:
torch -> trt model -> torch script -> trt engine
try to script a tensorrt model
```
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
    def forward(self, x: torch.Tensor):
        x = x * 2
        return x

if __name__ == '__main__':
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    model = Model().eval().cuda() 
  
    trt_model  = torch.compile(model, backend="tensorrt")
    script_model = torch.jit.script(trt_model)
    script_model.save("script_model.ts")
    trt_engine = torch_tensorrt.ts.convert_method_to_trt_engine(script_model, inputs= [torch_tensorrt.Input((1,10))])
    with open(f"trt_engine.plan", 'wb') as f:
       f.write(trt_engine)
    print("model plan saved")
```

but in `script_model = torch.jit.script(trt_model)`

get error
> Traceback (most recent call last):
  File "/ossfs/workspace/MetaGR/trt/build_trt_engine.py", line 138, in <module>
    script_model = torch.jit.script(trt_model)
  File "/opt/conda/envs/*/lib/python3.10/site-packages/torch/jit/_script.py", line 1429, in script
    ret = _script_impl(
  File "/opt/conda/envs/*/lib/python3.10/site-packages/torch/jit/_script.py", line 1147, in _script_impl
    return torch.jit._recursive.create_script_module(
  File "/opt/conda/envs/*/lib/python3.10/site-packages/torch/jit/_recursive.py", line 555, in create_script_module
    concrete_type = get_module_concrete_type(nn_module, share_types)
  File "/opt/conda/envs/*/lib/python3.10/site-packages/torch/jit/_recursive.py", line 504, in get_module_concrete_type
    concrete_type = concrete_type_store.get_or_create_concrete_type(nn_module)
  File "/opt/conda/envs/*/lib/python3.10/site-packages/torch/jit/_recursive.py", line 436, in get_or_create_concrete_type
    concrete_type_builder = infer_concrete_type_builder(nn_module)
  File "/opt/conda/envs/*/lib/python3.10/site-packages/torch/jit/_recursive.py", line 396, in infer_concrete_type_builder
    attr_type, inferred = infer_type(name, value)
  File "/opt/conda/envs/*/lib/python3.10/site-packages/torch/jit/_recursive.py", line 228, in infer_type
    ann_to_type = torch.jit.annotations.ann_to_type(
  File "/opt/conda/envs/*/lib/python3.10/site-packages/torch/jit/annotations.py", line 516, in ann_to_type
    raise ValueError(f"Unknown type annotation: '{ann}' at {loc.highlight()}")
ValueError: Unknown type annotation: 'Callable[..., Any]' at 


I tried to find the reason. When name is` _torchdynamo_orig_callable`, the error occurs.

torch/jit/_recursive.py
```
def infer_concrete_type_builder(nn_module, share_types=True):
    ...
    for name, value in nn_module.__dict__.items():
        ...
        attr_type, inferred = infer_type(name, value)
```
And I printed the __dict__.items() of `model` and `trt_model`, and found that there is no `_torchdynamo_orig_callable` in `model`, but there is in `trt_model`. I don’t know what to do next.

### Versions

Collecting environment information...
PyTorch version: 2.5.1+cu124
Is debug build: False
CUDA used to build PyTorch: 12.4
ROCM used to build PyTorch: N/A

OS: Alibaba Group Enterprise Linux Server 7.2 (Paladin) (x86_64)
GCC version: (GCC) 10.2.1 20200825 (Alibaba 10.2.1-3 2.17)
Clang version: Could not collect
CMake version: version 3.26.4
Libc version: glibc-2.32

Python version: 3.10.14 (main, May  6 2024, 19:42:50) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-4.9.151-015.ali3000.alios7.x86_64-x86_64-with-glibc2.32
Is CUDA available: True
CUDA runtime version: 12.4.131
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: A10-1-PCIE-24GB-XGB-V
Nvidia driver version: 470.82.01
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                128
On-line CPU(s) list:   0-127
Thread(s) per core:    2
Core(s) per socket:    32
Socket(s):             2
NUMA node(s):          2
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 106
Model name:            Intel(R) Xeon(R) Platinum 8369B CPU @ 2.90GHz
Stepping:              6
CPU MHz:               3499.859
CPU max MHz:           3500.0000
CPU min MHz:           800.0000
BogoMIPS:              5800.00
Virtualization:        VT-x
L1d cache:             48K
L1i cache:             32K
L2 cache:              1280K
L3 cache:              49152K
NUMA node0 CPU(s):     0-31,64-95
NUMA node1 CPU(s):     32-63,96-127
Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb invpcid_single ssbd mba ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req avx512vbmi pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq pconfig flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] lion-pytorch==0.2.2
[pip3] numpy==1.26.4
[pip3] nvidia-cublas-cu12==12.4.5.8
[pip3] nvidia-cuda-cupti-cu12==12.4.127
[pip3] nvidia-cuda-nvrtc-cu12==12.4.127
[pip3] nvidia-cuda-runtime-cu12==12.4.127
[pip3] nvidia-cudnn-cu12==9.1.0.70
[pip3] nvidia-cufft-cu12==11.2.1.3
[pip3] nvidia-curand-cu12==10.3.5.147
[pip3] nvidia-cusolver-cu12==11.6.1.9
[pip3] nvidia-cusparse-cu12==12.3.1.170
[pip3] nvidia-nccl-cu12==2.21.5
[pip3] nvidia-nvjitlink-cu12==12.4.127
[pip3] nvidia-nvtx-cu12==12.4.127
[pip3] pytorch-lightning==1.9.5
[pip3] pytorch-triton==3.0.0+dedb7bdf33
[pip3] torch==2.5.1
[pip3] torch_no_python==2.5.0.dev20240816+cu121
[pip3] torch_tensorrt==2.5.0.dev20240816+cu121
[pip3] torchao==0.7.0+git75f52ae7
[pip3] torchinfo==1.8.0
[pip3] torchmetrics==1.4.1
[pip3] torchvision==0.20.1
[pip3] triton==3.1.0
[conda] lion-pytorch              0.2.2                    pypi_0    pypi
[conda] numpy                     1.26.4                   pypi_0    pypi
[conda] nvidia-cublas-cu12        12.4.5.8                 pypi_0    pypi
[conda] nvidia-cuda-cupti-cu12    12.4.127                 pypi_0    pypi
[conda] nvidia-cuda-nvrtc-cu12    12.4.127                 pypi_0    pypi
[conda] nvidia-cuda-runtime-cu12  12.4.127                 pypi_0    pypi
[conda] nvidia-cudnn-cu12         9.1.0.70                 pypi_0    pypi
[conda] nvidia-cufft-cu12         11.2.1.3                 pypi_0    pypi
[conda] nvidia-curand-cu12        10.3.5.147               pypi_0    pypi
[conda] nvidia-cusolver-cu12      11.6.1.9                 pypi_0    pypi
[conda] nvidia-cusparse-cu12      12.3.1.170               pypi_0    pypi
[conda] nvidia-nccl-cu12          2.21.5                   pypi_0    pypi
[conda] nvidia-nvjitlink-cu12     12.4.127                 pypi_0    pypi
[conda] nvidia-nvtx-cu12          12.4.127                 pypi_0    pypi
[conda] pytorch-lightning         1.9.5                    pypi_0    pypi
[conda] pytorch-triton            3.0.0+dedb7bdf33          pypi_0    pypi
[conda] torch                     2.5.1                    pypi_0    pypi
[conda] torch-no-python           2.5.0.dev20240816+cu121          pypi_0    pypi
[conda] torch-tensorrt            2.5.0.dev20240816+cu121          pypi_0    pypi
[conda] torchao                   0.7.0+git75f52ae7          pypi_0    pypi
[conda] torchinfo                 1.8.0                    pypi_0    pypi
[conda] torchmetrics              1.4.1                    pypi_0    pypi
[conda] torchvision               0.20.1                   pypi_0    pypi
[conda] triton                    3.1.0                    pypi_0    pypi

cc @EikanWang @jgong5 @wenzhe-nrv @sanchitintel