Skip to content

Commit 038eaa5

Browse files
committed
Fix torch_logs tutorial device gate and CPU fallback (#137285)
1 parent 3cdec7b commit 038eaa5

1 file changed

Lines changed: 37 additions & 25 deletions

File tree

recipes_source/torch_logs.py

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,51 +32,63 @@
3232

3333
import torch
3434

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
35+
# Determine device: CUDA if supported, otherwise CPU
36+
device = "cuda" if torch.cuda.is_available() and torch.cuda.get_device_capability() >= (7, 0) else "cpu"
4337

38+
# Verify if torch.compile is supported in the current environment
39+
try:
40+
torch.compile(lambda x: x + 1)(torch.ones(1, device=device))
41+
compile_supported = True
42+
except Exception:
43+
compile_supported = False
4444

45-
inputs = (torch.ones(2, 2, device="cuda"), torch.zeros(2, 2, device="cuda"))
45+
if not compile_supported:
46+
print("Skipping because torch.compile is not supported on this device/environment.")
47+
import sys
48+
sys.exit(0)
49+
50+
51+
@torch.compile()
52+
def fn(x, y):
53+
z = x + y
54+
return z + 2
55+
56+
57+
inputs = (torch.ones(2, 2, device=device), torch.zeros(2, 2, device=device))
4658

4759

4860
# print separator and reset dynamo
4961
# between each example
50-
def separator(name):
51-
print(f"==================={name}=========================")
52-
torch._dynamo.reset()
62+
def separator(name):
63+
print(f"==================={name}=========================")
64+
torch._dynamo.reset()
5365

5466

55-
separator("Dynamo Tracing")
67+
separator("Dynamo Tracing")
5668
# View dynamo tracing
5769
# TORCH_LOGS="+dynamo"
58-
torch._logging.set_logs(dynamo=logging.DEBUG)
59-
fn(*inputs)
70+
torch._logging.set_logs(dynamo=logging.DEBUG)
71+
fn(*inputs)
6072

61-
separator("Traced Graph")
73+
separator("Traced Graph")
6274
# View traced graph
6375
# TORCH_LOGS="graph"
64-
torch._logging.set_logs(graph=True)
65-
fn(*inputs)
76+
torch._logging.set_logs(graph=True)
77+
fn(*inputs)
6678

67-
separator("Fusion Decisions")
79+
separator("Fusion Decisions")
6880
# View fusion decisions
6981
# TORCH_LOGS="fusion"
70-
torch._logging.set_logs(fusion=True)
71-
fn(*inputs)
82+
torch._logging.set_logs(fusion=True)
83+
fn(*inputs)
7284

73-
separator("Output Code")
85+
separator("Output Code")
7486
# View output code generated by inductor
7587
# TORCH_LOGS="output_code"
76-
torch._logging.set_logs(output_code=True)
77-
fn(*inputs)
88+
torch._logging.set_logs(output_code=True)
89+
fn(*inputs)
7890

79-
separator("")
91+
separator("")
8092

8193
######################################################################
8294
# Conclusion

0 commit comments

Comments
 (0)