Skip to content

Commit d9e986f

Browse files
mlazossvekars
andauthored
Add logging recipe (#2704)
* Added logging recipe --------- Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
1 parent 1ace4ee commit d9e986f

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

recipes_source/recipes_index.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ Recipes are bite-sized, actionable examples of how to use specific PyTorch featu
144144
:link: ../recipes/recipes/module_load_state_dict_tips.html
145145
:tags: Basics
146146

147+
.. customcarditem::
148+
:header: (beta) Using TORCH_LOGS to observe torch.compile
149+
:card_description: Learn how to use the torch logging APIs to observe the compilation process.
150+
:image: ../_static/img/thumbnails/cropped/generic-pytorch-logo.png
151+
:link: ../recipes/torch_logs.html
152+
:tags: Basics
153+
154+
147155
.. Interpretability
148156
149157
.. customcarditem::
@@ -362,6 +370,7 @@ Recipes are bite-sized, actionable examples of how to use specific PyTorch featu
362370

363371
/recipes/recipes/loading_data_recipe
364372
/recipes/recipes/defining_a_neural_network
373+
/recipes/torch_logs
365374
/recipes/recipes/what_is_state_dict
366375
/recipes/recipes/saving_and_loading_models_for_inference
367376
/recipes/recipes/saving_and_loading_a_general_checkpoint

recipes_source/torch_logs.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)