From 2f00fa46165679e006ea2b4b62ca4e6cedc06af0 Mon Sep 17 00:00:00 2001 From: yyouretoast Date: Wed, 17 Jun 2026 02:41:08 +0300 Subject: [PATCH] Fix torch_logs tutorial to support CPU-only environments Remove the hard CUDA gate that caused the tutorial to render as 'Skipped' on the PyTorch docs website. torch.compile works on both CUDA (via Triton) and CPU (via C++ Inductor), so no guard or early exit is needed. Changes: - Use CUDA if available and compute capability >= 7.0, else fall back to CPU (prevents crashes on older CUDA devices while supporting CPU execution). - Remove try/except compilation probe (unnecessary). - Remove sys.exit(0) early exit (dangerous in Sphinx-Gallery subprocess mode). - De-indent all tutorial code from the original if/else block. Fixes pytorch/pytorch#137285 --- recipes_source/torch_logs.py | 51 ++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/recipes_source/torch_logs.py b/recipes_source/torch_logs.py index b5c3f0bd8ac..435d6435a92 100644 --- a/recipes_source/torch_logs.py +++ b/recipes_source/torch_logs.py @@ -32,51 +32,52 @@ import torch -# exit cleanly if we are on a device that doesn't support torch.compile -if torch.cuda.get_device_capability() < (7, 0): - print("Skipping because torch.compile is not supported on this device.") -else: - @torch.compile() - def fn(x, y): - z = x + y - return z + 2 +# Determine device: use CUDA if available and compute capability >= 7.0, otherwise fall back to CPU. +# torch.compile works on both CUDA (via Triton) and CPU (via C++ Inductor). +device = "cuda" if torch.cuda.is_available() and torch.cuda.get_device_capability() >= (7, 0) else "cpu" - inputs = (torch.ones(2, 2, device="cuda"), torch.zeros(2, 2, device="cuda")) +@torch.compile() +def fn(x, y): + z = x + y + return z + 2 + + +inputs = (torch.ones(2, 2, device=device), torch.zeros(2, 2, device=device)) # print separator and reset dynamo # between each example - def separator(name): - print(f"==================={name}=========================") - torch._dynamo.reset() +def separator(name): + print(f"==================={name}=========================") + torch._dynamo.reset() - separator("Dynamo Tracing") +separator("Dynamo Tracing") # View dynamo tracing # TORCH_LOGS="+dynamo" - torch._logging.set_logs(dynamo=logging.DEBUG) - fn(*inputs) +torch._logging.set_logs(dynamo=logging.DEBUG) +fn(*inputs) - separator("Traced Graph") +separator("Traced Graph") # View traced graph # TORCH_LOGS="graph" - torch._logging.set_logs(graph=True) - fn(*inputs) +torch._logging.set_logs(graph=True) +fn(*inputs) - separator("Fusion Decisions") +separator("Fusion Decisions") # View fusion decisions # TORCH_LOGS="fusion" - torch._logging.set_logs(fusion=True) - fn(*inputs) +torch._logging.set_logs(fusion=True) +fn(*inputs) - separator("Output Code") +separator("Output Code") # View output code generated by inductor # TORCH_LOGS="output_code" - torch._logging.set_logs(output_code=True) - fn(*inputs) +torch._logging.set_logs(output_code=True) +fn(*inputs) - separator("") +separator("") ###################################################################### # Conclusion