You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Saving models compiled with Torch-TensorRT varies slightly with the `ir` that has been used for compilation.
7
+
8
+
1) Dynamo IR
9
+
10
+
Starting with 2.1 release of Torch-TensorRT, we are switching the default compilation to be dynamo based.
11
+
The output of `ir=dynamo` compilation is a `torch.fx.GraphModule` object. There are two ways to save these objects
12
+
13
+
a) Converting to Torchscript
14
+
`torch.fx.GraphModule` objects cannot be serialized directly. Hence we use `torch.jit.trace` to convert this into a `ScriptModule` object which can be saved to disk.
15
+
The following code illustrates this approach.
16
+
17
+
.. code-block:: python
18
+
19
+
import torch
20
+
import torch_tensorrt
21
+
22
+
model = MyModel().eval().cuda()
23
+
inputs = torch.randn((1, 3, 224, 224)).cuda()
24
+
trt_gm = torch_tensorrt.compile(model, ir="dynamo", inputs) # Output is a torch.fx.GraphModule
`torch_tensorrt.dynamo.export.transform` inlines the submodules within a GraphModule to their corresponding nodes and stiches all the nodes together.
55
+
This is needed as `torch._export` serialization cannot handle serializing and deserializing of submodules (`call_module` nodes).
56
+
57
+
NOTE: This way of saving the models using `ExportedProgram` is experimental. Here is a known issue : https://github.com/pytorch/TensorRT/issues/2341
58
+
59
+
2) Torchscript IR
60
+
61
+
In Torch-TensorRT 1.X versions, the primary way to compile and run inference with Torch-TensorRT is using Torchscript IR.
62
+
This behavior stays the same in 2.X versions as well.
63
+
64
+
.. code-block:: python
65
+
66
+
import torch
67
+
import torch_tensorrt
68
+
69
+
model = MyModel().eval().cuda()
70
+
inputs = torch.randn((1, 3, 224, 224)).cuda()
71
+
trt_ts = torch_tensorrt.compile(model, ir="ts", inputs) # Output is a ScriptModule object
0 commit comments