Skip to content

Commit afd5abe

Browse files
authored
chore: Set default return type to ExportedProgram (#2575)
1 parent fb07513 commit afd5abe

File tree

9 files changed

+305
-171
lines changed

9 files changed

+305
-171
lines changed

docsrc/user_guide/saving_models.rst

+31-18
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ Saving models compiled with Torch-TensorRT varies slightly with the `ir` that ha
1414
Dynamo IR
1515
-------------
1616

17-
Starting with 2.1 release of Torch-TensorRT, we are switching the default compilation to be dynamo based.
18-
The output of `ir=dynamo` compilation is a `torch.fx.GraphModule` object. There are two ways to save these objects
17+
The output type of `ir=dynamo` compilation of Torch-TensorRT is `torch.export.ExportedProgram` object by default.
18+
In addition, we provide a new parameter `output_format` in the `CompilationSetting` object provided before compilation.
19+
The `output_format` can take the following options
1920

20-
a) Converting to Torchscript
21+
* `exported_program` (or) `ep` : This is the default. Returns an ExportedProgram
22+
* `torchscript` (or) `ts` : This returns a TorchScript module
23+
* `graph_module` (or) `fx` : This returns a torch.fx.GraphModule which can be traced into Torchscript to save to disk.
24+
25+
a) Torchscript
2126
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2227

23-
`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.
24-
The following code illustrates this approach.
28+
If you set the `output_format="torchscript"`, this will return a `ScriptModule` which can be serialized via torch.jit.save
2529

2630
.. code-block:: python
2731
@@ -30,9 +34,9 @@ The following code illustrates this approach.
3034
3135
model = MyModel().eval().cuda()
3236
inputs = [torch.randn((1, 3, 224, 224)).cuda()]
33-
trt_gm = torch_tensorrt.compile(model, ir="dynamo", inputs) # Output is a torch.fx.GraphModule
34-
trt_traced_model = torch.jit.trace(trt_gm, inputs)
35-
torch.jit.save(trt_traced_model, "trt_model.ts")
37+
# trt_ts is a torch.jit.ScriptModule object
38+
trt_ts = torch_tensorrt.compile(model, ir="dynamo", inputs, output_format="torchscript")
39+
torch.jit.save(trt_ts, "trt_model.ts")
3640
3741
# Later, you can load it and run inference
3842
model = torch.jit.load("trt_model.ts").cuda()
@@ -41,8 +45,7 @@ The following code illustrates this approach.
4145
b) ExportedProgram
4246
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4347

44-
`torch.export.ExportedProgram` is a new format introduced in Pytorch 2.1. After we compile a Pytorch module using Torch-TensorRT, the resultant
45-
`torch.fx.GraphModule` along with additional metadata can be used to create `ExportedProgram` which can be saved and loaded from disk.
48+
`torch.export.ExportedProgram`, a new format introduced in Pytorch 2.X is the default return type of Torch-TensorRT compilation.
4649

4750
.. code-block:: python
4851
@@ -51,26 +54,36 @@ b) ExportedProgram
5154
5255
model = MyModel().eval().cuda()
5356
inputs = [torch.randn((1, 3, 224, 224)).cuda()]
54-
trt_gm = torch_tensorrt.compile(model, ir="dynamo", inputs) # Output is a torch.fx.GraphModule
55-
# Transform and create an exported program
56-
trt_exp_program = torch_tensorrt.dynamo.export(trt_gm, inputs)
57-
torch.export.save(trt_exp_program, "trt_model.ep")
57+
# trt_ep is a torch.export.ExportedProgram object
58+
trt_ep = torch_tensorrt.compile(model, ir="dynamo", inputs)
59+
torch.export.save(trt_ep, "trt_model.ep")
5860
5961
# Later, you can load it and run inference
6062
model = torch.export.load("trt_model.ep")
6163
model(*inputs)
6264
63-
`torch_tensorrt.dynamo.export` inlines the submodules within a GraphModule to their corresponding nodes and stiches all the nodes together.
64-
This is needed as `torch._export` serialization cannot handle serializing and deserializing of submodules (`call_module` nodes).
65+
c) GraphModule
66+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6567

66-
.. note:: This way of saving the models using `ExportedProgram` is experimental. Here is a known issue : https://github.com/pytorch/TensorRT/issues/2341
68+
We can also return a `torch.fx.GraphModule` object as the output of Torch-TensorRT compilation by setting `output_format="graph_module"`.
69+
Internally, partitioning, lowering, conversion phases operate using GraphModule objects. These can be either traced into a Torchscript modules or
70+
exported into `ExportedProgram` objects
6771

72+
.. code-block:: python
73+
74+
import torch
75+
import torch_tensorrt
76+
77+
model = MyModel().eval().cuda()
78+
inputs = [torch.randn((1, 3, 224, 224)).cuda()]
79+
# trt_gm is a torch.fx.GraphModule object
80+
trt_gm = torch_tensorrt.compile(model, ir="dynamo", inputs, output_format="graph_module")
6881
6982
Torchscript IR
7083
-------------
7184

7285
In Torch-TensorRT 1.X versions, the primary way to compile and run inference with Torch-TensorRT is using Torchscript IR.
73-
This behavior stays the same in 2.X versions as well.
86+
For `ir=ts`, this behavior stays the same in 2.X versions as well.
7487

7588
.. code-block:: python
7689

py/torch_tensorrt/dynamo/_compiler.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
MIN_BLOCK_SIZE,
3131
NUM_AVG_TIMING_ITERS,
3232
OPTIMIZATION_LEVEL,
33+
OUTPUT_FORMAT,
3334
PASS_THROUGH_BUILD_FAILURES,
3435
PRECISION,
3536
REFIT,
@@ -47,6 +48,7 @@
4748
dryrun_stats_display,
4849
parse_non_trt_nodes,
4950
)
51+
from torch_tensorrt.dynamo._exporter import export
5052
from torch_tensorrt.dynamo.conversion import (
5153
CompilationSettings,
5254
UnsupportedOperatorException,
@@ -100,8 +102,9 @@ def compile(
100102
enable_experimental_decompositions: bool = ENABLE_EXPERIMENTAL_DECOMPOSITIONS,
101103
dryrun: bool = DRYRUN,
102104
hardware_compatible: bool = HARDWARE_COMPATIBLE,
105+
output_format: str = OUTPUT_FORMAT,
103106
**kwargs: Any,
104-
) -> torch.fx.GraphModule:
107+
) -> Union[ExportedProgram, torch.jit.ScriptModule, torch.fx.GraphModule]:
105108
"""Compile a TorchScript module for NVIDIA GPUs using TensorRT
106109
107110
Takes a existing TorchScript module and a set of settings to configure the compiler
@@ -158,6 +161,7 @@ def compile(
158161
enable_experimental_decompositions (bool): Use the full set of operator decompositions. These decompositions may not be tested but serve to make the grap easier to covert to TensorRT, potentially increasing the amount of graphs run in TensorRT.
159162
dryrun (bool): Toggle for "Dryrun" mode, running everything except conversion to TRT and logging outputs
160163
hardware_compatible (bool): Build the TensorRT engines compatible with GPU architectures other than that of the GPU on which the engine was built (currently works for NVIDIA Ampere and newer)
164+
output_format (str): Output format of the result of TRT compilation. Options include "exported_program" (or) "ep" | "torchscript" (or) "ts" | "graph_module" (or) "fx". Default is "exported_program"
161165
**kwargs: Any,
162166
Returns:
163167
torch.fx.GraphModule: Compiled FX Module, when run it will execute via TensorRT
@@ -242,11 +246,14 @@ def compile(
242246
"dla_global_dram_size": dla_global_dram_size,
243247
"dryrun": dryrun,
244248
"hardware_compatible": hardware_compatible,
249+
"output_format": output_format,
245250
}
246251

247252
settings = CompilationSettings(**compilation_options)
248253
logger.info("Compilation Settings: %s\n", settings)
249-
return compile_module(gm, inputs, settings)
254+
trt_gm = compile_module(gm, inputs, settings)
255+
trt_result = export(trt_gm, torch_inputs, output_format)
256+
return trt_result
250257

251258

252259
def compile_module(

py/torch_tensorrt/dynamo/_defaults.py

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
REQUIRE_FULL_COMPILATION = False
2727
DRYRUN = False
2828
HARDWARE_COMPATIBLE = False
29+
OUTPUT_FORMAT = "exported_program"
2930

3031

3132
def default_device() -> Device:

0 commit comments

Comments
 (0)