From be0af7a4ff8fce211a1f9c9ff6e9714e8dbcdb36 Mon Sep 17 00:00:00 2001 From: El Williams Date: Fri, 29 May 2026 01:43:52 +0000 Subject: [PATCH] fix: make TORCH_LOGS tutorial work without CUDA 7.0+ The tutorial had a guard that skipped all code when no CUDA device with compute capability >= 7.0 was available, causing the rendered docs to only show the skip message instead of actual logging output. Changed the guard to fall back to CPU tensors with the CPU inductor backend when no suitable GPU is found. This allows the tutorial to produce meaningful output regardless of the CI build environment. Fixes #137285 --- recipes_source/torch_logs.py | 51 +++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/recipes_source/torch_logs.py b/recipes_source/torch_logs.py index b5c3f0bd8ac..d02ccdcdf32 100644 --- a/recipes_source/torch_logs.py +++ b/recipes_source/torch_logs.py @@ -32,51 +32,54 @@ 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.") +# Determine the device — use CPU fallback if no suitable GPU is available +if torch.cuda.is_available() and torch.cuda.get_device_capability() >= (7, 0): + device = "cuda" else: - @torch.compile() - def fn(x, y): - z = x + y - return z + 2 + device = "cpu" + print("CUDA with compute capability 7.0+ not found. Running with CPU inductor backend.") +@torch.compile() +def fn(x, y): + z = x + y + return z + 2 - inputs = (torch.ones(2, 2, device="cuda"), torch.zeros(2, 2, device="cuda")) + +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