Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/main/recipes_source/torch_logs.py
Views: 712
"""1(beta) Using TORCH_LOGS python API with torch.compile2==========================================================================================3**Author:** `Michael Lazos <https://github.com/mlazos>`_4"""56import logging78######################################################################9#10# This tutorial introduces the ``TORCH_LOGS`` environment variable, as well as the Python API, and11# demonstrates how to apply it to observe the phases of ``torch.compile``.12#13# .. note::14#15# This tutorial requires PyTorch 2.2.0 or later.16#17#181920######################################################################21# Setup22# ~~~~~~~~~~~~~~~~~~~~~23# In this example, we'll set up a simple Python function which performs an elementwise24# add and observe the compilation process with ``TORCH_LOGS`` Python API.25#26# .. note::27#28# There is also an environment variable ``TORCH_LOGS``, which can be used to29# change logging settings at the command line. The equivalent environment30# variable setting is shown for each example.3132import torch3334# exit cleanly if we are on a device that doesn't support torch.compile35if torch.cuda.get_device_capability() < (7, 0):36print("Skipping because torch.compile is not supported on this device.")37else:38@torch.compile()39def fn(x, y):40z = x + y41return z + 2424344inputs = (torch.ones(2, 2, device="cuda"), torch.zeros(2, 2, device="cuda"))454647# print separator and reset dynamo48# between each example49def separator(name):50print(f"==================={name}=========================")51torch._dynamo.reset()525354separator("Dynamo Tracing")55# View dynamo tracing56# TORCH_LOGS="+dynamo"57torch._logging.set_logs(dynamo=logging.DEBUG)58fn(*inputs)5960separator("Traced Graph")61# View traced graph62# TORCH_LOGS="graph"63torch._logging.set_logs(graph=True)64fn(*inputs)6566separator("Fusion Decisions")67# View fusion decisions68# TORCH_LOGS="fusion"69torch._logging.set_logs(fusion=True)70fn(*inputs)7172separator("Output Code")73# View output code generated by inductor74# TORCH_LOGS="output_code"75torch._logging.set_logs(output_code=True)76fn(*inputs)7778separator("")7980######################################################################81# Conclusion82# ~~~~~~~~~~83#84# In this tutorial we introduced the TORCH_LOGS environment variable and python API85# by experimenting with a small number of the available logging options.86# To view descriptions of all available options, run any python script87# which imports torch and set TORCH_LOGS to "help".88#89# Alternatively, you can view the `torch._logging documentation`_ to see90# descriptions of all available logging options.91#92# For more information on torch.compile, see the `torch.compile tutorial`_.93#94# .. _torch._logging documentation: https://pytorch.org/docs/main/logging.html95# .. _torch.compile tutorial: https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html969798