Skip to content

Commit 15379ce

Browse files
Apply suggestions from code review
Co-authored-by: Svetlana Karslioglu <[email protected]>
1 parent d22ba70 commit 15379ce

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

recipes_source/torch_compiler_set_stance_tutorial.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
``torch.compiler.set_stance`` Tutorial
55
=================================
6-
**Author:** William Wen
6+
**Author:** `William Wen <https://github.com/williamwen42>`_
77
"""
88

99
######################################################################
@@ -13,12 +13,12 @@
1313
#
1414
# This recipe provides some examples on how to use ``torch.compiler.set_stance``.
1515
#
16-
# **Contents**
1716
#
1817
# .. contents::
1918
# :local:
2019
#
21-
# **Requirements**
20+
# Prerequisites
21+
# ---------------
2222
#
2323
# - ``torch >= 2.6``
2424

@@ -75,7 +75,7 @@ def bar(x):
7575
print(foo(inp)) # compiled, prints 1
7676

7777
######################################################################
78-
# ``torch.compile`` stance can only be changed _outside_ of any ``torch.compile`` region. Attempts
78+
# ``torch.compile`` stance can only be changed **outside** of any ``torch.compile`` region. Attempts
7979
# to do otherwise will result in an error.
8080

8181

@@ -123,9 +123,9 @@ def outer(x):
123123

124124
######################################################################
125125
# Preventing recompilation
126-
# ========================
126+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~
127127
#
128-
# Some models do not expect any recompilations - for example, you may always inputs to be the same shape.
128+
# Some models do not expect any recompilations - for example, you may always have inputs with the same shape.
129129
# Since recompilations may be expensive, we may wish to error out when we attempt to recompile so we can detect and fix recompilation cases.
130130
# The ``"fail_on_recompilation"`` stance can be used for this.
131131

@@ -230,3 +230,15 @@ def my_humongous_model(x):
230230
print(my_humongous_model(torch.randn(3)))
231231
except Exception as e:
232232
print(e)
233+
234+
########################################
235+
# Conclusion
236+
# --------------
237+
# In this recipe, we have learned how to use the ``torch.compiler.set_stance`` API
238+
# to modify the behavior of ``torch.compile`` across different calls to a model
239+
# without needing to reapply it. The recipe demonstrates using
240+
# ``torch.compiler.set_stance`` as a decorator, context manager, or raw function
241+
# to control compilation stances like ``force_eager``, ``default``,
242+
# ``eager_on_recompile``, and "fail_on_recompile."
243+
#
244+
# For more information, see: `torch.compiler.set_stance API documentation <https://pytorch.org/docs/main/generated/torch.compiler.set_stance.html#torch.compiler.set_stance>`__.

0 commit comments

Comments
 (0)