Skip to content

Commit 2b9fe76

Browse files
committed
one more example
1 parent 694ccb0 commit 2b9fe76

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

_doc/index.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,17 @@ Enlightening Examples
7171

7272
* :ref:`l-plot-export_tiny_phi2`
7373

74-
**Torch Export**
74+
**Exporter Recipes**
7575

76+
* :ref:`l-plot-export-dim1`
7677
* :ref:`l-plot-export-cond`
7778
* :ref:`l-plot-export-with-dynamic`
7879
* :ref:`l-plot-export-with-dynamic-shape`
80+
* :ref:`l-plot-dynamic-shapes-python-int`
81+
82+
**Torch Export Models**
83+
84+
* :ref:`l-plot-nonzero`
7985
* :ref:`l-plot-export-locale-issue`
8086
* :ref:`l-plot-tiny-llm-export`
8187
* :ref:`l-plot-tiny-llm-export-patched`

_doc/recipes/plot_dynamic_shapes_nonzero.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
.. _l-plot-nonzero:
3+
24
Half certain nonzero
35
====================
46

_doc/recipes/plot_export_dim1.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
.. _l-plot-export-dim1:
3+
4+
0, 1, 2 for a Dynamic Dimension in the dummy example to export a model
5+
======================================================================
6+
7+
:func:`torch.export.export` does not work if a tensor given to the function
8+
has 0 or 1 for dimension declared as dynamic dimension.
9+
10+
Simple model, no dimension with 0 or 1
11+
++++++++++++++++++++++++++++++++++++++
12+
"""
13+
14+
import torch
15+
from onnx_diagnostic import doc
16+
17+
18+
class Model(torch.nn.Module):
19+
def forward(self, x, y, z):
20+
return torch.cat((x, y), axis=1) + z
21+
22+
23+
model = Model()
24+
x = torch.randn(2, 3)
25+
y = torch.randn(2, 5)
26+
z = torch.randn(2, 8)
27+
model(x, y, z)
28+
29+
DYN = torch.export.Dim.DYNAMIC
30+
ds = {0: DYN, 1: DYN}
31+
32+
ep = torch.export.export(model, (x, y, z), dynamic_shapes=(ds, ds, ds))
33+
print(ep.graph)
34+
35+
# %%
36+
# Same model, a dynamic dimension = 1
37+
# +++++++++++++++++++++++++++++++++++
38+
39+
z = z[:1]
40+
41+
DYN = torch.export.Dim.DYNAMIC
42+
ds = {0: DYN, 1: DYN}
43+
44+
try:
45+
ep = torch.export.export(model, (x, y, z), dynamic_shapes=(ds, ds, ds))
46+
print(ep.graph)
47+
except Exception as e:
48+
print("ERROR", e)
49+
50+
# %%
51+
# It failed. Let's try a little trick.
52+
53+
# %%
54+
# Same model, a dynamic dimension = 1 and backed_size_oblivious=True
55+
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56+
57+
with torch.fx.experimental._config.patch(backed_size_oblivious=True):
58+
ep = torch.export.export(model, (x, y, z), dynamic_shapes=(ds, ds, ds))
59+
print(ep.graph)
60+
61+
# %%
62+
# It worked.
63+
64+
doc.plot_legend("dynamic dimension\nworking with\n0 or 1", "torch.export.export", "green")

0 commit comments

Comments
 (0)