Export and save problem（torch-tensorrt）

zhongqiu1245: <h2><a name="bug-description-1" class="anchor" href="#bug-description-1"></a>Bug Description</h2>
<p>When I used torch-tensorrt to complite, some errors came out suddenly, which made me confuse.</p>
<h2><a name="to-reproduce-2" class="anchor" href="#to-reproduce-2"></a>To Reproduce</h2>
<p>Code for method 1:</p>
<pre><code class="lang-auto">import torch
import tensorrt
import torch_tensorrt
from torchvision.models import resnet50


if __name__ == '__main__':
    model = resnet50().eval().to('cuda')
    inputs = [torch.randn((1, 3, 224, 224)).cuda()]
    trt_gm = torch_tensorrt.compile(model, ir="dynamo", inputs=inputs)
    trt_traced_model = torch.jit.trace(trt_gm, inputs)
    torch.jit.save(trt_traced_model, "trt_model.ts")
</code></pre>
<p>Error:</p>
<pre><code class="lang-auto">INFO:torch_tensorrt.dynamo._compiler:Compilation Settings: CompilationSettings(precision=torch.float32, debug=False, workspace_size=0, min_block_size=5, torch_executed_ops=set(), pass_through_build_failures=False, max_aux_streams=None, version_compatible=False, optimization_level=None, use_python_runtime=False, truncate_long_and_double=False, use_fast_partitioner=True, enable_experimental_decompositions=False, device=Device(type=DeviceType.GPU, gpu_id=0), require_full_compilation=False, disable_tf32=False, sparse_weights=False, refit=False, engine_capability=&lt;EngineCapability.DEFAULT: 0&gt;, num_avg_timing_iters=1, dla_sram_size=1048576, dla_local_dram_size=1073741824, dla_global_dram_size=536870912, output_format='exported_program')

INFO:torch_tensorrt.dynamo.conversion._TRTInterpreter:TRT INetwork construction elapsed time: 0:00:00.567413
INFO:torch_tensorrt.dynamo.conversion._TRTInterpreter:Build TRT engine elapsed time: 0:00:10.713493
INFO:torch_tensorrt.dynamo.conversion._TRTInterpreter:TRT Engine uses: 7426048 bytes of Memory
/home/dan/anaconda3/envs/dust3r/lib/python3.11/site-packages/torch/_export/exported_program.py:333: UserWarning: Unable to execute the generated python source code from the graph. The graph module will no longer be directly callable, but you can still run the ExportedProgram, and if needed, you can run the graph module eagerly using torch.fx.Interpreter.
  warnings.warn(
Traceback (most recent call last):
  File "/media/dan/DATA/Processing/OFFICE/CodingFactory/PosWorkShop/dust3r/torchtrt_demo.py", line 15, in &lt;module&gt;
    trt_traced_model = torch.jit.trace(trt_gm, inputs)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/dan/anaconda3/envs/dust3r/lib/python3.11/site-packages/torch/_dynamo/eval_frame.py", line 489, in _fn
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/dan/anaconda3/envs/dust3r/lib/python3.11/site-packages/torch/_dynamo/external_utils.py", line 17, in inner
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/dan/anaconda3/envs/dust3r/lib/python3.11/site-packages/torch/jit/_trace.py", line 861, in trace
    name = _qualified_name(func)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/dan/anaconda3/envs/dust3r/lib/python3.11/site-packages/torch/_jit_internal.py", line 1163, in _qualified_name
    raise RuntimeError("Could not get name of python class object")
**RuntimeError: Could not get name of python class object**
</code></pre>
<p>However, when I tried : # Code for method 2:</p>
<pre><code class="lang-auto">import torch
import tensorrt
import torch_tensorrt
from torchvision.models import resnet50


if __name__ == '__main__':
    model = resnet50().eval().to('cuda:0')
    inputs = [torch.randn((1, 3, 224, 224)).cuda()]
    trt_gm = torch_tensorrt.compile(model, ir="dynamo", inputs=inputs)
    trt_exp_program = torch_tensorrt.dynamo.export(trt_gm, inputs, 'ep')
    torch.export.save(trt_exp_program, "trt_model.ep")

</code></pre>
<p>Error:</p>
<pre><code class="lang-auto">INFO:torch_tensorrt.dynamo._compiler:Compilation Settings: CompilationSettings(precision=torch.float32, debug=False, workspace_size=0, min_block_size=5, torch_executed_ops=set(), pass_through_build_failures=False, max_aux_streams=None, version_compatible=False, optimization_level=None, use_python_runtime=False, truncate_long_and_double=False, use_fast_partitioner=True, enable_experimental_decompositions=False, device=Device(type=DeviceType.GPU, gpu_id=0), require_full_compilation=False, disable_tf32=False, sparse_weights=False, refit=False, engine_capability=&lt;EngineCapability.DEFAULT: 0&gt;, num_avg_timing_iters=1, dla_sram_size=1048576, dla_local_dram_size=1073741824, dla_global_dram_size=536870912, output_format='exported_program')

INFO:torch_tensorrt.dynamo.conversion._TRTInterpreter:TRT INetwork construction elapsed time: 0:00:00.521946
INFO:torch_tensorrt.dynamo.conversion._TRTInterpreter:Build TRT engine elapsed time: 0:00:10.723753
INFO:torch_tensorrt.dynamo.conversion._TRTInterpreter:TRT Engine uses: 7325696 bytes of Memory

/home/dan/anaconda3/envs/dust3r/lib/python3.11/site-packages/torch/_export/exported_program.py:333: UserWarning: Unable to execute the generated python source code from the graph. The graph module will no longer be directly callable, but you can still run the ExportedProgram, and if needed, you can run the graph module eagerly using torch.fx.Interpreter.
  warnings.warn(
Traceback (most recent call last):
  File "/media/dan/DATA/Processing/OFFICE/CodingFactory/PosWorkShop/dust3r/torchtrt_demo.py", line 21, in &lt;module&gt;
    trt_exp_program = torch_tensorrt.dynamo.export(trt_gm, inputs, 'ep')
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/dan/anaconda3/envs/dust3r/lib/python3.11/site-packages/torch_tensorrt/dynamo/_exporter.py", line 33, in export
    patched_module = transform(gm, inputs)
                     ^^^^^^^^^^^^^^^^^^^^^
  File "/home/dan/anaconda3/envs/dust3r/lib/python3.11/site-packages/torch_tensorrt/dynamo/_exporter.py", line 59, in transform
    _, outputs_map = partitioning.run_shape_analysis(gm, inputs)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/dan/anaconda3/envs/dust3r/lib/python3.11/site-packages/torch_tensorrt/dynamo/partitioning/common.py", line 30, in run_shape_analysis
    for name, _ in parent_module.named_children():
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
**AttributeError: 'ExportedProgram' object has no attribute 'named_children'**
</code></pre>
<p>However, when I set <code> torch_tensorrt.dynamo.export(trt_gm, inputs, 'ep')</code> → <code> torch_tensorrt.dynamo.export(trt_gm, inputs, 'fx')</code>, everything was fine…</p>
<h2><a name="environment-3" class="anchor" href="#environment-3"></a>Environment</h2>
<blockquote>
<p>Build information about Torch-TensorRT can be found by turning on debug messages</p>
</blockquote>
<ul>
<li>Torch-TensorRT Version (e.g. 1.0.0): 2.2.0</li>
<li>PyTorch Version (e.g. 1.0): 2.2.0 stable</li>
<li>CPU Architecture: x64</li>
<li>OS (e.g., Linux): ubuntu22.04 LTS</li>
<li>How you installed PyTorch (<code>conda</code>, <code>pip</code>, <code>libtorch</code>, source): pip install torch torchvision torch-tensorrt</li>
<li>Build command you used (if compiling from source): pip install</li>
<li>Are you using local sources or building from archives:</li>
<li>Python version: 3.11</li>
<li>CUDA version: CUDA 12.1 and cudnn 8.9.1</li>
<li>GPU models and configuration: RTX 4060 laptop</li>
<li>Any other relevant information: None</li>
</ul>
<h2><a name="additional-context-4" class="anchor" href="#additional-context-4"></a>Additional context</h2>
<p>Code1 and Code2 are failed, and <a href="https://github.com/pytorch/TensorRT/blob/main/examples/torchtrt_runtime_example/network.py" class="inline-onebox" rel="noopener nofollow ugc">TensorRT/examples/torchtrt_runtime_example/network.py at main · pytorch/TensorRT · GitHub</a> met the same error.<br>
Could you tell me how to solve these errors?<br>
Thank you in advance!</p>