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