|
6 | 6 | # This source code is licensed under the BSD-style license found in the |
7 | 7 | # LICENSE file in the root directory of this source tree. |
8 | 8 |
|
| 9 | +import importlib.util |
| 10 | +import os |
9 | 11 | import platform |
| 12 | +import shutil |
| 13 | +import subprocess |
| 14 | +import sys |
10 | 15 | import tempfile |
11 | 16 | from pathlib import Path |
12 | 17 | from typing import List |
|
24 | 29 | from executorch.exir import EdgeCompileConfig, to_edge_transform_and_lower |
25 | 30 | from executorch.extension.pybindings.portable_lib import ( |
26 | 31 | _get_registered_backend_names, |
| 32 | + _is_available, |
27 | 33 | _load_for_executorch_from_buffer, |
28 | 34 | ) |
29 | 35 | from test_base import ModelTest |
@@ -71,14 +77,96 @@ def run_tests(model_tests: List[ModelTest]) -> None: |
71 | 77 | raise RuntimeError(f"Unsupported backend {model_test.backend}.") |
72 | 78 |
|
73 | 79 |
|
| 80 | +def test_the_openvino_delegate_runs() -> None: |
| 81 | + """Install the OpenVINO runtime the way a user does, then run a model through it. |
| 82 | +
|
| 83 | + The wheel ships the delegate but not the runtime, and registering the delegate does not |
| 84 | + load the runtime, so nothing else here proves the delegate can open openvino_c.dll and |
| 85 | + compute. The check runs in a new process because the package looks for the runtime when |
| 86 | + it is imported, which in this process happened before the install. |
| 87 | + """ |
| 88 | + # The same bound as the python_version marker on the openvino extra. |
| 89 | + if sys.version_info >= (3, 15): |
| 90 | + print( |
| 91 | + "SKIP: OpenVINO publishes no build for this Python, so the openvino extra " |
| 92 | + "installs no runtime and the delegate cannot load here." |
| 93 | + ) |
| 94 | + return |
| 95 | + requirements = ( |
| 96 | + Path(test_base._repository_root_dir()) / "backends/openvino/requirements.txt" |
| 97 | + ) |
| 98 | + subprocess.run( |
| 99 | + [ |
| 100 | + sys.executable, |
| 101 | + "-m", |
| 102 | + "pip", |
| 103 | + "install", |
| 104 | + "executorch[openvino]", |
| 105 | + "-r", |
| 106 | + str(requirements), |
| 107 | + ], |
| 108 | + check=True, |
| 109 | + ) |
| 110 | + subprocess.run( |
| 111 | + [sys.executable, str(Path(__file__).resolve()), "--openvino"], check=True |
| 112 | + ) |
| 113 | + importlib.invalidate_caches() |
| 114 | + openvino = importlib.util.find_spec("openvino") |
| 115 | + test_a_bare_name_does_not_load_from_the_current_folder( |
| 116 | + Path(openvino.submodule_search_locations[0]) / "libs" |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +def test_a_bare_name_does_not_load_from_the_current_folder(libs: Path) -> None: |
| 121 | + """Run from a folder holding the real runtime and ask for it by bare name. |
| 122 | +
|
| 123 | + The standard Windows search order includes the current folder, where a planted |
| 124 | + openvino_c.dll would run, so the delegate has to refuse the copy found there. |
| 125 | + """ |
| 126 | + with tempfile.TemporaryDirectory() as work_dir: |
| 127 | + for library in libs.glob("*.dll"): |
| 128 | + shutil.copy2(library, work_dir) |
| 129 | + result = subprocess.run( |
| 130 | + [ |
| 131 | + sys.executable, |
| 132 | + "-c", |
| 133 | + "from executorch.extension.pybindings.portable_lib import _is_available; " |
| 134 | + "print(_is_available('OpenvinoBackend'))", |
| 135 | + ], |
| 136 | + cwd=work_dir, |
| 137 | + env={**os.environ, "OPENVINO_LIB_PATH": "openvino_c.dll"}, |
| 138 | + capture_output=True, |
| 139 | + text=True, |
| 140 | + check=True, |
| 141 | + ) |
| 142 | + assert result.stdout.split()[-1] == "False", ( |
| 143 | + "the delegate loaded openvino_c.dll by its bare name from the current folder, " |
| 144 | + f"where a planted copy would run:\n{result.stdout}{result.stderr}" |
| 145 | + ) |
| 146 | + print("✓ a bare openvino_c.dll name does not load from the current folder") |
| 147 | + |
| 148 | + |
74 | 149 | if __name__ == "__main__": |
| 150 | + if sys.argv[1:] == ["--openvino"]: |
| 151 | + assert _is_available( |
| 152 | + "OpenvinoBackend" |
| 153 | + ), "OpenvinoBackend is registered but cannot load the OpenVINO runtime" |
| 154 | + print("✓ OpenvinoBackend loads the OpenVINO runtime") |
| 155 | + test_base.test_a_model_runs_through_the_openvino_delegate(required=True) |
| 156 | + sys.exit(0) |
| 157 | + |
75 | 158 | # Before anything else, because this is the check that fails the way a user |
76 | 159 | # fails: with only the dependencies the wheel declares. |
77 | 160 | with tempfile.TemporaryDirectory() as work_dir: |
78 | 161 | test_clean_install.run_tests(Path(work_dir)) |
79 | 162 |
|
80 | 163 | if platform.system() == "Windows": |
81 | 164 | registered = _get_registered_backend_names() |
| 165 | + assert ( |
| 166 | + "OpenvinoBackend" in registered |
| 167 | + ), f"OpenvinoBackend not found in registered backends: {registered}" |
| 168 | + print("✓ OpenvinoBackend is registered") |
| 169 | + |
82 | 170 | # Vulkan backend is optional: only present when the wheel was built with |
83 | 171 | # EXECUTORCH_BUILD_VULKAN=1 and the Vulkan SDK (glslc) was available. |
84 | 172 | if "VulkanBackend" in registered: |
@@ -108,3 +196,7 @@ def run_tests(model_tests: List[ModelTest]) -> None: |
108 | 196 | ), |
109 | 197 | ] |
110 | 198 | ) |
| 199 | + |
| 200 | + # Last, because it installs packages the wheel does not declare. |
| 201 | + if platform.system() == "Windows": |
| 202 | + test_the_openvino_delegate_runs() |
0 commit comments