|
3 | 3 | """
|
4 | 4 | ``torch.compiler.set_stance`` Tutorial
|
5 | 5 | =================================
|
6 |
| -**Author:** William Wen |
| 6 | +**Author:** `William Wen <https://github.com/williamwen42>`_ |
7 | 7 | """
|
8 | 8 |
|
9 | 9 | ######################################################################
|
|
13 | 13 | #
|
14 | 14 | # This recipe provides some examples on how to use ``torch.compiler.set_stance``.
|
15 | 15 | #
|
16 |
| -# **Contents** |
17 | 16 | #
|
18 | 17 | # .. contents::
|
19 | 18 | # :local:
|
20 | 19 | #
|
21 |
| -# **Requirements** |
| 20 | +# Prerequisites |
| 21 | +# --------------- |
22 | 22 | #
|
23 | 23 | # - ``torch >= 2.6``
|
24 | 24 |
|
@@ -75,7 +75,7 @@ def bar(x):
|
75 | 75 | print(foo(inp)) # compiled, prints 1
|
76 | 76 |
|
77 | 77 | ######################################################################
|
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 |
79 | 79 | # to do otherwise will result in an error.
|
80 | 80 |
|
81 | 81 |
|
@@ -123,9 +123,9 @@ def outer(x):
|
123 | 123 |
|
124 | 124 | ######################################################################
|
125 | 125 | # Preventing recompilation
|
126 |
| -# ======================== |
| 126 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
127 | 127 | #
|
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. |
129 | 129 | # Since recompilations may be expensive, we may wish to error out when we attempt to recompile so we can detect and fix recompilation cases.
|
130 | 130 | # The ``"fail_on_recompilation"`` stance can be used for this.
|
131 | 131 |
|
@@ -230,3 +230,15 @@ def my_humongous_model(x):
|
230 | 230 | print(my_humongous_model(torch.randn(3)))
|
231 | 231 | except Exception as e:
|
232 | 232 | 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