Skip to content

Update compile and export tutorials for 2.1 #2591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions intermediate_source/torch_compile_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
#
# **Contents**
#
# - Basic Usage
# - Demonstrating Speedups
# - Comparison to TorchScript and FX Tracing
# - TorchDynamo and FX Graphs
# - Conclusion
# .. contents::
# :local:
#
# **Required pip Dependencies**
#
Expand Down Expand Up @@ -485,19 +482,12 @@ def bar(a, b):
print(opt_model(generate_data(16)[0]))

######################################################################
# <!----TODO: replace this section with a link to the torch.export tutorial when done --->
#
# Finally, if we simply want TorchDynamo to output the FX graph for export,
# we can use ``torch._dynamo.export``. Note that ``torch._dynamo.export``, like
# ``fullgraph=True``, raises an error if TorchDynamo breaks the graph.

try:
torch._dynamo.export(bar)(torch.randn(10), torch.randn(10))
except:
tb.print_exc()

model_exp = torch._dynamo.export(init_model())(generate_data(16)[0])
print(model_exp[0](generate_data(16)[0]))
# We can use ``torch.export`` (from PyTorch 2.1+) to extract a single, exportable
# FX graph from the input PyTorch program. The exported graph is intended to be
# run on different (i.e. Python-less) environments. One important restriction
# is that the ``torch.export`` does not support graph breaks. Please check
# `this tutorial <https://pytorch.org/tutorials/intermediate/torch_export_tutorial.html>`__
# for more details on ``torch.export``.

######################################################################
# Conclusion
Expand Down
8 changes: 6 additions & 2 deletions intermediate_source/torch_export_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def constraints_example4(x, y):
exported_constraints_example4 = export(constraints_example4, (torch.randn(3, 3), torch.tensor([4])))
print(exported_constraints_example4(torch.randn(3, 3), torch.tensor([5])))
try:
exported_constraints_example4(torch.randn(3, 3), torch.randn([2]))
exported_constraints_example4(torch.randn(3, 3), torch.tensor([2]))
except Exception:
tb.print_exc()

Expand All @@ -441,7 +441,7 @@ def constraints_example5(x, y):
exported_constraints_example5 = export(constraints_example5, (torch.randn(2, 2), torch.tensor([4])))
print(exported_constraints_example5(torch.randn(2, 2), torch.tensor([5])))
try:
exported_constraints_example5(torch.randn(2, 2), torch.randn([1]))
exported_constraints_example5(torch.randn(2, 2), torch.tensor([1]))
except Exception:
tb.print_exc()

Expand Down Expand Up @@ -496,6 +496,10 @@ def custom_op_example(x):
# Note in the above outputs that the custom op is included in the exported graph.
# And when we call the exported graph as a function, the original custom op is called,
# as evidenced by the ``print`` call.
#
# If you have a custom operator implemented in C++, please refer to
# `this document <https://docs.google.com/document/d/1_W62p8WJOQQUzPsJYa7s701JXt0qf2OfLub2sbkHOaU/edit#heading=h.ahugy69p2jmz>`__
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this one day become a wiki or something?

# to make it compatible with ``torch.export``.

######################################################################
# ExportDB
Expand Down