From c187160fd17a5c53fbf880ec512263fc022bcd17 Mon Sep 17 00:00:00 2001 From: Peter Pfadenhauer Date: Sat, 21 Feb 2026 21:16:10 +0100 Subject: [PATCH] Add --use-directml flag and fix crash when no NVIDIA GPU is present - Add `--use-directml` as an alias for `--directml` in backend/args.py. Previously the flag was silently ignored by parse_known_args(), leaving directml_enabled=False and causing the code to fall through to a CUDA call that crashed with "Found no NVIDIA driver on your system." - In memory_management.py, treat --use-directml the same as --directml (defaulting to device index -1). - Add a torch.cuda.is_available() guard in get_torch_device() before calling torch.cuda.current_device(), falling back to CPU so the application does not crash on non-NVIDIA systems. Co-Authored-By: Claude Sonnet 4.6 --- backend/args.py | 1 + backend/memory_management.py | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/backend/args.py b/backend/args.py index 6758bb89..efd13aaa 100644 --- a/backend/args.py +++ b/backend/args.py @@ -39,6 +39,7 @@ upcast.add_argument("--disable-attention-upcast", action="store_true") parser.add_argument("--disable-xformers", action="store_true") parser.add_argument("--directml", type=int, nargs="?", metavar="DIRECTML_DEVICE", const=-1) +parser.add_argument("--use-directml", action="store_true", dest="use_directml") parser.add_argument("--disable-ipex-hijack", action="store_true") vram_group = parser.add_mutually_exclusive_group() diff --git a/backend/memory_management.py b/backend/memory_management.py index 5f0c8312..5b977143 100644 --- a/backend/memory_management.py +++ b/backend/memory_management.py @@ -44,7 +44,9 @@ if args.pytorch_deterministic: torch.use_deterministic_algorithms(True, warn_only=True) directml_enabled = False -if args.directml is not None: +if args.directml is not None or getattr(args, 'use_directml', False): + if args.directml is None: + args.directml = -1 import torch_directml directml_enabled = True @@ -96,8 +98,10 @@ def get_torch_device(): else: if is_intel_xpu(): return torch.device("xpu", torch.xpu.current_device()) - else: + elif torch.cuda.is_available(): return torch.device(torch.cuda.current_device()) + else: + return torch.device("cpu") def get_total_memory(dev=None, torch_total_too=False):