From 14ba4f9c0b3f51e7c561bb0bcf2ccfb29ace2013 Mon Sep 17 00:00:00 2001 From: dunegym Date: Sun, 12 Jul 2026 15:16:18 +0800 Subject: [PATCH] fix: centralize OPENVINO_PROVIDER_CONFIG and log SystemExit Address Sourcery review feedback on PR #1879: - Move OPENVINO_PROVIDER_CONFIG from _onnx_enhancer.py to platform_info.py (a leaf module with no modules.* imports), so the enhancer and face_swapper no longer import each other just to share a constant. _onnx_enhancer re-exports it; face_swapper now imports it at module top level instead of inside get_face_swapper(). - Narrow run.py's SystemExit handling: catch SystemExit separately and print a [startup] message so the failure is visible instead of being swallowed alongside ImportError/FileNotFoundError. --- modules/platform_info.py | 8 ++++++++ modules/processors/frame/_onnx_enhancer.py | 6 +----- modules/processors/frame/face_swapper.py | 4 +--- run.py | 19 ++++++++++++++++--- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/modules/platform_info.py b/modules/platform_info.py index 32f45a6..f4e1f53 100644 --- a/modules/platform_info.py +++ b/modules/platform_info.py @@ -42,6 +42,14 @@ HAS_COREML_PROVIDER: bool = "CoreMLExecutionProvider" in ONNX_PROVIDERS HAS_DML_PROVIDER: bool = "DmlExecutionProvider" in ONNX_PROVIDERS HAS_OPENVINO_PROVIDER: bool = "OpenVINOExecutionProvider" in ONNX_PROVIDERS +# OpenVINO execution-provider config shared by every ONNX session builder. +# AUTO:GPU,NPU,CPU lets OpenVINO pick the best available device in priority +# order (Intel GPU → NPU → CPU). +OPENVINO_PROVIDER_CONFIG = ( + "OpenVINOExecutionProvider", + {"device_type": "AUTO:GPU,NPU,CPU"}, +) + def camera_backends() -> List[Tuple[int, int]]: """Return an ordered list of ``(device_index, cv2_backend)`` attempts. diff --git a/modules/processors/frame/_onnx_enhancer.py b/modules/processors/frame/_onnx_enhancer.py index 85e7e63..0298967 100644 --- a/modules/processors/frame/_onnx_enhancer.py +++ b/modules/processors/frame/_onnx_enhancer.py @@ -14,17 +14,13 @@ import numpy as np import onnxruntime import modules.globals +from modules.platform_info import OPENVINO_PROVIDER_CONFIG IS_APPLE_SILICON = platform.system() == "Darwin" and platform.machine() == "arm64" # Limit concurrent ONNX calls to avoid VRAM exhaustion on multi-face frames THREAD_SEMAPHORE = threading.Semaphore(min(max(1, (os.cpu_count() or 1)), 8)) -# OpenVINO provider configuration. -# AUTO:GPU,NPU,CPU lets OpenVINO try the best available device in priority -# order (Intel GPU → NPU → CPU). -OPENVINO_PROVIDER_CONFIG = ("OpenVINOExecutionProvider", {"device_type": "AUTO:GPU,NPU,CPU"}) - def build_provider_config(providers=None): """Wrap raw provider name strings with optimised CUDA / CoreML options. diff --git a/modules/processors/frame/face_swapper.py b/modules/processors/frame/face_swapper.py index 0c79735..0005950 100644 --- a/modules/processors/frame/face_swapper.py +++ b/modules/processors/frame/face_swapper.py @@ -18,6 +18,7 @@ from modules.utilities import ( ) from modules.cluster_analysis import find_closest_centroid from modules.gpu_processing import gpu_gaussian_blur, gpu_sharpen, gpu_add_weighted, gpu_resize +from modules.platform_info import OPENVINO_PROVIDER_CONFIG import os from collections import deque import time @@ -271,9 +272,6 @@ def get_face_swapper() -> Any: # fastest on modern GPUs (Blackwell/sm_120). providers_config.append(p) elif p == "OpenVINOExecutionProvider": - from modules.processors.frame._onnx_enhancer import ( - OPENVINO_PROVIDER_CONFIG, - ) providers_config.append(OPENVINO_PROVIDER_CONFIG) else: providers_config.append(p) diff --git a/run.py b/run.py index 19cde8c..a0571c2 100644 --- a/run.py +++ b/run.py @@ -33,14 +33,27 @@ if sys.platform == "win32": # On Windows, register OpenVINO DLL directories so onnxruntime's # OpenVINOExecutionProvider can find openvino.dll. This must happen - # before any ONNX InferenceSession is created. + # before any ONNX InferenceSession is created. Failure is non-fatal: + # OpenVINO simply isn't installed, and onnxruntime will fall back to CPU. try: from onnxruntime.tools.add_openvino_win_libs import ( # type: ignore[import-untyped] # noqa: E501 add_openvino_libs_to_path, ) add_openvino_libs_to_path() - except (ImportError, FileNotFoundError, SystemExit): - pass # OpenVINO not installed — no-op + except ImportError: + # onnxruntime build without the OpenVINO tooling module — no-op. + pass + except FileNotFoundError: + # OpenVINO site-packages dir absent — no-op. + pass + except SystemExit as exc: + # add_openvino_libs_to_path() calls sys.exit() when OpenVINO libs + # can't be located (e.g. OPENVINO_LIB_PATHS unset). Log the message + # it raised with so the failure is visible, but keep startup alive. + print( + f"[startup] OpenVINO DLL registration skipped: {exc}", + flush=True, + ) # On Linux, pre-load NVIDIA shared libraries (cuDNN, cuBLAS, nvrtc...) shipped # inside the venv via pip wheels (nvidia-cudnn-cu12, etc.). LD_LIBRARY_PATH