CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
pytorch

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: pytorch/tutorials
Path: blob/main/recipes_source/torch_logs.py
Views: 494
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 as 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("Skipping because torch.compile is not supported on this device.")
38
else:
39
@torch.compile()
40
def fn(x, y):
41
z = x + y
42
return z + 2
43
44
45
inputs = (torch.ones(2, 2, device="cuda"), torch.zeros(2, 2, device="cuda"))
46
47
48
# print separator and reset dynamo
49
# between each example
50
def separator(name):
51
print(f"==================={name}=========================")
52
torch._dynamo.reset()
53
54
55
separator("Dynamo Tracing")
56
# View dynamo tracing
57
# TORCH_LOGS="+dynamo"
58
torch._logging.set_logs(dynamo=logging.DEBUG)
59
fn(*inputs)
60
61
separator("Traced Graph")
62
# View traced graph
63
# TORCH_LOGS="graph"
64
torch._logging.set_logs(graph=True)
65
fn(*inputs)
66
67
separator("Fusion Decisions")
68
# View fusion decisions
69
# TORCH_LOGS="fusion"
70
torch._logging.set_logs(fusion=True)
71
fn(*inputs)
72
73
separator("Output Code")
74
# View output code generated by inductor
75
# TORCH_LOGS="output_code"
76
torch._logging.set_logs(output_code=True)
77
fn(*inputs)
78
79
separator("")
80
81
######################################################################
82
# Conclusion
83
# ~~~~~~~~~~
84
#
85
# In this tutorial we introduced the TORCH_LOGS environment variable and python API
86
# by experimenting with a small number of the available logging options.
87
# To view descriptions of all available options, run any python script
88
# which imports torch and set TORCH_LOGS to "help".
89
#
90
# Alternatively, you can view the `torch._logging documentation`_ to see
91
# descriptions of all available logging options.
92
#
93
# For more information on torch.compile, see the `torch.compile tutorial`_.
94
#
95
# .. _torch._logging documentation: https://pytorch.org/docs/main/logging.html
96
# .. _torch.compile tutorial: https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html
97
98