7
7
"""
8
8
9
9
######################################################################
10
- # :func:`torch.export` is the PyTorch 2 way to export PyTorch models into
10
+ #
11
+ # .. warning::
12
+ #
13
+ # ``torch.export`` and its related features are in prototype status and are subject to backwards compatibility
14
+ # breaking changes. This tutorial provides a snapshot of ``torch.export`` usage as of PyTorch 2.1.
15
+ #
16
+ # :func:`torch.export` is the PyTorch 2.X way to export PyTorch models into
11
17
# standardized model representations, intended
12
18
# to be run on different (i.e. Python-less) environments.
13
19
#
17
23
# to make in order to make your model compatible with ``torch.export``.
18
24
#
19
25
# **Contents**
26
+ #
20
27
# .. contents::
21
28
# :local:
22
29
@@ -72,7 +79,7 @@ def forward(self, x, y):
72
79
#
73
80
# The ``graph_module`` attribute is the ``GraphModule`` that wraps the ``graph`` attribute
74
81
# so that it can be ran as a ``torch.nn.Module``.
75
- # We can use ``graph_module``'s ``print_readable` to print a Python code representation
82
+ # We can use ``graph_module``'s ``print_readable``` to print a Python code representation
76
83
# of ``graph``:
77
84
78
85
print (exported_mod )
@@ -88,7 +95,7 @@ def forward(self, x, y):
88
95
# Other attributes of interest in ``ExportedProgram`` include:
89
96
#
90
97
# - ``graph_signature`` -- the inputs, outputs, parameters, buffers, etc. of the exported graph.
91
- # - ``range_constraints`` and ``equality_constraints`` -- Constraints , covered later
98
+ # - ``range_constraints`` and ``equality_constraints`` -- constraints , covered later
92
99
93
100
print (exported_mod .graph_signature )
94
101
print (exported_mod .range_constraints )
@@ -172,16 +179,10 @@ def bad4(x):
172
179
# Control Flow Ops
173
180
# ----------------
174
181
#
175
- # .. warning::
176
- #
177
- # ``cond`` is a prototype feature in PyTorch, included as a part of the ``torch.export`` release.
178
- # Future changes may break backwards compatibility.
179
- # Please look forward to a more stable implementation in a future version of PyTorch.
180
- #
181
182
# ``torch.export`` actually does support data-dependent control flow.
182
183
# But these need to be expressed using control flow ops. For example,
183
184
# we can fix the control flow example above using the ``cond`` op, like so:
184
-
185
+ #
185
186
# ..
186
187
# [TODO] link to docs about cond when it is out
187
188
@@ -207,7 +208,7 @@ def false_fn(x):
207
208
# operands and they must both return a single tensor with the same metadata (for example, ``dtype``, ``shape``, etc.).
208
209
# - Branch functions cannot mutate input or global variables.
209
210
# - Branch functions cannot access closure variables, except for ``self`` if the function is
210
- # defined in the scope of a method.
211
+ # defined in the scope of a method.
211
212
212
213
######################################################################
213
214
# ..
@@ -234,11 +235,6 @@ def false_fn(x):
234
235
# Constraints
235
236
# -----------
236
237
#
237
- # .. warning::
238
- #
239
- # The constraints API is a prototype feature in PyTorch, included as a part of the torch.export release.
240
- # Backwards compatibility is not guaranteed. We anticipate releasing a more stable constraints API in the future.
241
- #
242
238
# Ops can have different specializations/behaviors for different tensor shapes, so by default,
243
239
# ``torch.export`` requires inputs to ``ExportedProgram`` to have the same shape as the respective
244
240
# example inputs given to the initial ``torch.export`` call.
@@ -351,7 +347,7 @@ def constraints_example3(x, y):
351
347
352
348
constraints3 = (
353
349
[dynamic_dim (inp4 , i ) for i in range (inp4 .dim ())] +
354
- [dynamic_dim (inp5 , i ) for i in range (inp4 .dim ())]
350
+ [dynamic_dim (inp5 , i ) for i in range (inp5 .dim ())]
355
351
)
356
352
357
353
try :
@@ -387,6 +383,8 @@ def specify_constraints(x, y):
387
383
import logging
388
384
torch ._logging .set_logs (dynamic = logging .INFO , dynamo = logging .INFO )
389
385
exported_constraints_example3 = export (constraints_example3 , (inp4 , inp5 ), constraints = constraints3_fixed )
386
+
387
+ # reset to previous values
390
388
torch ._logging .set_logs (dynamic = logging .WARNING , dynamo = logging .WARNING )
391
389
392
390
######################################################################
@@ -443,10 +441,6 @@ def constraints_example5(x, y):
443
441
#
444
442
# ``torch.export`` can export PyTorch programs with custom operators.
445
443
#
446
- # .. warning::
447
- #
448
- # The API for registering custom ops is still under active development
449
- # and may change without notice.
450
444
#
451
445
# Currently, the steps to register a custom op for use by ``torch.export`` are:
452
446
#
@@ -534,6 +528,6 @@ def cond_predicate(x):
534
528
# Conclusion
535
529
# ----------
536
530
#
537
- # We introduced ``torch.export``, the new PyTorch 2 way to export single computation
531
+ # We introduced ``torch.export``, the new PyTorch 2.X way to export single computation
538
532
# graphs from PyTorch programs. In particular, we demonstrate several code modifications
539
533
# and considerations (control flow ops, constraints, etc.) that need to be made in order to export a graph.
0 commit comments