|
| 1 | +""" |
| 2 | +(beta) Using TORCH_LOGS python API with torch.compile |
| 3 | +========================================================================================== |
| 4 | +**Author:** `Michael Lazos <https://github.com/mlazos>`_ |
| 5 | +""" |
| 6 | + |
| 7 | +import logging |
| 8 | + |
| 9 | +###################################################################### |
| 10 | +# |
| 11 | +# This tutorial introduces the ``TORCH_LOGS`` environment variable, as well ass the Python API, and |
| 12 | +# demonstrates how to apply it to observe the phases of ``torch.compile``. |
| 13 | +# |
| 14 | +# .. note:: |
| 15 | +# |
| 16 | +# This tutorial requires PyTorch 2.2.0 or later. |
| 17 | +# |
| 18 | +# |
| 19 | + |
| 20 | + |
| 21 | +###################################################################### |
| 22 | +# Setup |
| 23 | +# ~~~~~~~~~~~~~~~~~~~~~ |
| 24 | +# In this example, we'll set up a simple Python function which performs an elementwise |
| 25 | +# add and observe the compilation process with ``TORCH_LOGS`` Python API. |
| 26 | +# |
| 27 | +# .. note:: |
| 28 | +# |
| 29 | +# There is also an environment variable ``TORCH_LOGS``, which can be used to |
| 30 | +# change logging settings at the command line. The equivalent environment |
| 31 | +# variable setting is shown for each example. |
| 32 | + |
| 33 | +import torch |
| 34 | + |
| 35 | +# exit cleanly if we are on a device that doesn't support torch.compile |
| 36 | +if torch.cuda.get_device_capability() < (7, 0): |
| 37 | + print("Exiting because torch.compile is not supported on this device.") |
| 38 | + import sys |
| 39 | + |
| 40 | + sys.exit(0) |
| 41 | + |
| 42 | + |
| 43 | +@torch.compile() |
| 44 | +def fn(x, y): |
| 45 | + z = x + y |
| 46 | + return z + 2 |
| 47 | + |
| 48 | + |
| 49 | +inputs = (torch.ones(2, 2, device="cuda"), torch.zeros(2, 2, device="cuda")) |
| 50 | + |
| 51 | + |
| 52 | +# print separator and reset dynamo |
| 53 | +# between each example |
| 54 | +def separator(name): |
| 55 | + print(f"==================={name}=========================") |
| 56 | + torch._dynamo.reset() |
| 57 | + |
| 58 | + |
| 59 | +separator("Dynamo Tracing") |
| 60 | +# View dynamo tracing |
| 61 | +# TORCH_LOGS="+dynamo" |
| 62 | +torch._logging.set_logs(dynamo=logging.DEBUG) |
| 63 | +fn(*inputs) |
| 64 | + |
| 65 | +separator("Traced Graph") |
| 66 | +# View traced graph |
| 67 | +# TORCH_LOGS="graph" |
| 68 | +torch._logging.set_logs(graph=True) |
| 69 | +fn(*inputs) |
| 70 | + |
| 71 | +separator("Fusion Decisions") |
| 72 | +# View fusion decisions |
| 73 | +# TORCH_LOGS="fusion" |
| 74 | +torch._logging.set_logs(fusion=True) |
| 75 | +fn(*inputs) |
| 76 | + |
| 77 | +separator("Output Code") |
| 78 | +# View output code generated by inductor |
| 79 | +# TORCH_LOGS="output_code" |
| 80 | +torch._logging.set_logs(output_code=True) |
| 81 | +fn(*inputs) |
| 82 | + |
| 83 | +separator("") |
| 84 | + |
| 85 | +###################################################################### |
| 86 | +# Conclusion |
| 87 | +# ~~~~~~~~~~ |
| 88 | +# |
| 89 | +# In this tutorial we introduced the TORCH_LOGS environment variable and python API |
| 90 | +# by experimenting with a small number of the available logging options. |
| 91 | +# To view descriptions of all available options, run any python script |
| 92 | +# which imports torch and set TORCH_LOGS to "help". |
| 93 | +# |
| 94 | +# Alternatively, you can view the `torch._logging documentation`_ to see |
| 95 | +# descriptions of all available logging options. |
| 96 | +# |
| 97 | +# For more information on torch.compile, see the `torch.compile tutorial`_. |
| 98 | +# |
| 99 | +# .. _torch._logging documentation: https://pytorch.org/docs/main/logging.html |
| 100 | +# .. _torch.compile tutorial: https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html |
0 commit comments