Skip to content

Commit b75f32b

Browse files
fatcat-zjustinchuby
authored andcommitted
Update TorchDynamo-based ONNX Exporter memory usage example code. (pytorch#144139)
Address related comments earlier. Pull Request resolved: pytorch#144139 Approved by: https://github.com/justinchuby Co-authored-by: Justin Chu <[email protected]>
1 parent 64bffb3 commit b75f32b

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

docs/source/onnx_dynamo.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ The main advantage of this approach is that the `FX graph <https://pytorch.org/d
2121
bytecode analysis that preserves the dynamic nature of the model instead of using traditional static tracing techniques.
2222

2323
In addition, during the export process, memory usage is significantly reduced compared to the TorchScript-enabled exporter.
24-
See the :doc:`documentation <onnx_dynamo_memory_usage>` for more information.
24+
See the :doc:`memory usage documentation <onnx_dynamo_memory_usage>` for more information.
2525

2626
The exporter is designed to be modular and extensible. It is composed of the following components:
2727

docs/source/onnx_dynamo_memory_usage.rst

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ The previous TorchScript-based ONNX exporter would execute the model once to tra
44
memory on your GPU if the model's memory requirements exceeded the available GPU memory. This issue has been addressed with the new
55
TorchDynamo-based ONNX exporter.
66

7-
The TorchDynamo-based ONNX exporter leverages `FakeTensorMode <https://pytorch.org/docs/stable/torch.compiler_fake_tensor.html>`_ to
8-
avoid performing actual tensor computations during the export process. This approach results in significantly lower memory usage
9-
compared to the TorchScript-based ONNX exporter.
7+
The TorchDynamo-based ONNX exporter utilizes torch.export.export() function to leverage
8+
`FakeTensorMode <https://pytorch.org/docs/stable/torch.compiler_fake_tensor.html>`_ to avoid performing actual tensor computations
9+
during the export process. This approach results in significantly lower memory usage compared to the TorchScript-based ONNX exporter.
1010

1111
Below is an example demonstrating the memory usage difference between TorchScript-based and TorchDynamo-based ONNX exporters.
1212
In this example, we use the HighResNet model from MONAI. Before proceeding, please install it from PyPI:
@@ -29,7 +29,6 @@ The code below could be run to generate a snapshot file which records the state
2929
3030
import torch
3131
32-
from torch.onnx.utils import export
3332
from monai.networks.nets import (
3433
HighResNet,
3534
)
@@ -44,17 +43,19 @@ The code below could be run to generate a snapshot file which records the state
4443
data = torch.randn(30, 1, 48, 48, 48, dtype=torch.float32).to("cuda")
4544
4645
with torch.no_grad():
47-
export(
46+
onnx_program = torch.onnx.export(
4847
model,
4948
data,
5049
"torchscript_exporter_highresnet.onnx",
50+
dynamo=False,
5151
)
5252
53-
snapshot_name = f"torchscript_exporter_example.pickle"
53+
snapshot_name = "torchscript_exporter_example.pickle"
5454
print(f"generate {snapshot_name}")
5555
5656
torch.cuda.memory._dump_snapshot(snapshot_name)
57-
print(f"Export is done.")
57+
print("Export is done.")
58+
5859
5960
Open `pytorch.org/memory_viz <https://pytorch.org/memory_viz>`_ and drag/drop the generated pickled snapshot file into the visualizer.
6061
The memory usage is described as below:

0 commit comments

Comments
 (0)