From 7cc2cb3c28662082e69a35d516c3149aadac5720 Mon Sep 17 00:00:00 2001 From: Zhenchao Ni Date: Tue, 1 Sep 2026 14:51:35 +0800 Subject: [PATCH] Remove preload ORT DLL --- src/winml/modelkit/__init__.py | 30 -------- tests/unit/test_modelkit_init.py | 128 ------------------------------- 2 files changed, 158 deletions(-) delete mode 100644 tests/unit/test_modelkit_init.py diff --git a/src/winml/modelkit/__init__.py b/src/winml/modelkit/__init__.py index c5b787720..6dfa2301c 100644 --- a/src/winml/modelkit/__init__.py +++ b/src/winml/modelkit/__init__.py @@ -43,36 +43,6 @@ logging.getLogger(__name__).addHandler(logging.NullHandler()) -def _preload_bundled_onnxruntime_dll() -> None: - # Windows ships C:\Windows\System32\onnxruntime.dll (older API version) - # as part of the system WindowsML component. When WinML EP plugin DLLs - # are loaded (via EpCatalog), they import "onnxruntime.dll" by base name - # and the loader binds them to the system copy, producing - # "The requested API version [N] is not available" errors. - # Loading the wheel-bundled DLL by full path first makes later base-name - # imports resolve to the already-loaded module. - if sys.platform != "win32": - return - try: - import ctypes - import os - from importlib.util import find_spec - from pathlib import Path - - spec = find_spec("onnxruntime") - if spec is None or spec.origin is None: - return - dll = Path(spec.origin).parent / "capi" / "onnxruntime.dll" - if not dll.is_file(): - return - os.add_dll_directory(str(dll.parent)) - ctypes.WinDLL(str(dll)) - except Exception as _e: # pragma: no cover - best-effort preload - print(f"Warning: failed to preload bundled onnxruntime.dll: {_e}", file=sys.stderr) - - -_preload_bundled_onnxruntime_dll() - # _warnings configures filters before any subpackage imports. from . import _warnings diff --git a/tests/unit/test_modelkit_init.py b/tests/unit/test_modelkit_init.py deleted file mode 100644 index b6b1dfafa..000000000 --- a/tests/unit/test_modelkit_init.py +++ /dev/null @@ -1,128 +0,0 @@ -# ------------------------------------------------------------------------- -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -# -------------------------------------------------------------------------- -"""Regression tests for ``_preload_bundled_onnxruntime_dll``. - -The preload is the load-order fix for the System32 vs. wheel-bundled -``onnxruntime.dll`` collision on Windows. It must run before any subpackage -import that transitively touches onnxruntime, otherwise the System32 copy -wins and produces "requested API version [N] is not available" errors on -end-user machines. These tests pin the contract without needing a real -Windows host or a real wheel. -""" - -from __future__ import annotations - -import logging -import os -import sys -import types -import warnings -from unittest import mock - -import pytest - - -_MISSING = object() -_MODELKIT_LOGGERS = ( - "diffusers.utils.import_utils", - "transformers.pipelines.base", - "transformers.models.auto.image_processing_auto", -) -_MODELKIT_ENV_VARS = ("TOKENIZERS_PARALLELISM", "HF_HUB_DISABLE_PROGRESS_BARS") - - -@pytest.fixture(autouse=True) -def _restore_modelkit_import_state(): - """Keep forced package reimports from leaking into the rest of the suite.""" - original_modules = { - name: module - for name, module in sys.modules.items() - if name == "winml.modelkit" or name.startswith("winml.modelkit.") - } - original_meta_path = list(sys.meta_path) - original_warning_filters = list(warnings.filters) - original_logger_filters = { - name: list(logging.getLogger(name).filters) for name in _MODELKIT_LOGGERS - } - original_environment = {name: os.environ.get(name, _MISSING) for name in _MODELKIT_ENV_VARS} - original_winml = sys.modules.get("winml") - original_modelkit_attr = ( - getattr(original_winml, "modelkit", _MISSING) if original_winml is not None else _MISSING - ) - - yield - - for name in [ - module_name - for module_name in sys.modules - if module_name == "winml.modelkit" or module_name.startswith("winml.modelkit.") - ]: - sys.modules.pop(name, None) - sys.modules.update(original_modules) - sys.meta_path[:] = original_meta_path - warnings.filters[:] = original_warning_filters - for name, filters in original_logger_filters.items(): - logging.getLogger(name).filters[:] = filters - for name, value in original_environment.items(): - if value is _MISSING: - os.environ.pop(name, None) - else: - os.environ[name] = value - - if original_winml is None: - sys.modules.pop("winml", None) - elif original_modelkit_attr is _MISSING: - if hasattr(original_winml, "modelkit"): - delattr(original_winml, "modelkit") - else: - original_winml.modelkit = original_modelkit_attr - - -def _reimport_modelkit(): - # Drop the cached package so its __init__.py (and the preload call at - # module scope) executes again under the active mocks. - for name in [ - m for m in sys.modules if m == "winml.modelkit" or m.startswith("winml.modelkit.") - ]: - sys.modules.pop(name, None) - import importlib - - return importlib.import_module("winml.modelkit") - - -def test_preload_is_noop_on_non_windows(monkeypatch): - monkeypatch.setattr(sys, "platform", "linux") - with mock.patch("ctypes.WinDLL", create=True) as windll: - _reimport_modelkit() - windll.assert_not_called() - - -def test_preload_skips_when_bundled_dll_missing(monkeypatch, tmp_path): - monkeypatch.setattr(sys, "platform", "win32") - fake_spec = types.SimpleNamespace(origin=str(tmp_path / "onnxruntime" / "__init__.py")) - with ( - mock.patch("importlib.util.find_spec", return_value=fake_spec), - mock.patch("ctypes.WinDLL", create=True) as windll, - mock.patch("os.add_dll_directory", create=True) as add_dir, - ): - _reimport_modelkit() - windll.assert_not_called() - add_dir.assert_not_called() - - -def test_preload_loads_dll_on_windows_when_present(monkeypatch, tmp_path): - monkeypatch.setattr(sys, "platform", "win32") - capi = tmp_path / "onnxruntime" / "capi" - capi.mkdir(parents=True) - (capi / "onnxruntime.dll").write_bytes(b"") - fake_spec = types.SimpleNamespace(origin=str(tmp_path / "onnxruntime" / "__init__.py")) - with ( - mock.patch("importlib.util.find_spec", return_value=fake_spec), - mock.patch("ctypes.WinDLL", create=True) as windll, - mock.patch("os.add_dll_directory", create=True) as add_dir, - ): - _reimport_modelkit() - add_dir.assert_called_once_with(str(capi)) - windll.assert_called_once_with(str(capi / "onnxruntime.dll"))