Coverage for src/bioimageio/core/backends/onnx_backend.py: 82%
87 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 09:46 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 09:46 +0000
1# pyright: reportUnknownVariableType=false
2from __future__ import annotations
4import shutil
5import tempfile
6from collections.abc import Sequence
7from contextlib import contextmanager, nullcontext
8from pathlib import Path
9from typing import Any, cast
11import onnxruntime as rt # pyright: ignore[reportMissingTypeStubs]
12from loguru import logger
13from numpy.typing import NDArray
15from bioimageio.spec.model import v0_4, v0_5
17from .._model_adapter import LocalModelAdapter
18from .._utils.type_guards import is_list, is_tuple
21class ONNXModelAdapter(LocalModelAdapter[str | None, rt.InferenceSession]):
22 def __init__(
23 self,
24 model_description: v0_4.ModelDescr | v0_5.ModelDescr,
25 devices: Sequence[str] | None = None,
26 ):
27 onnx_descr = model_description.weights.onnx
28 if onnx_descr is None: 28 ↛ 29line 28 didn't jump to line 29 because the condition on line 28 was never true
29 raise ValueError("No ONNX weights specified for {model_description.name}")
31 self._onnx_descr = onnx_descr
32 self._input_names: list[str] | None = None
33 super().__init__(model_description=model_description, devices=devices)
35 def _parse_devices(self, devices: Sequence[str] | None) -> Sequence[str | None]:
36 available_providers: Any = None
37 if hasattr(rt, "get_available_providers"): 37 ↛ 40line 37 didn't jump to line 40 because the condition on line 37 was always true
38 available_providers = cast(Any, rt.get_available_providers())
40 if is_list(available_providers): 40 ↛ 46line 40 didn't jump to line 46 because the condition on line 40 was always true
41 if len(available_providers) == 0: 41 ↛ 42line 41 didn't jump to line 42 because the condition on line 41 was never true
42 providers = [None]
43 else:
44 providers = available_providers
45 else:
46 available_providers = [available_providers]
47 providers = [available_providers]
49 if devices is not None:
50 available_devices = [d for d in devices if d in providers]
51 unavailable_devices = [d for d in devices if d not in providers]
52 if available_devices: 52 ↛ 53line 52 didn't jump to line 53 because the condition on line 52 was never true
53 if unavailable_devices:
54 logger.warning(
55 "The following requested devices are not available for ONNX Runtime and will be ignored: {}.\nSelected available providers/devices are: {}\nOther available providers are: {}",
56 unavailable_devices,
57 available_devices,
58 [p for p in providers if p not in devices],
59 )
61 providers = available_devices
62 elif not available_providers: 62 ↛ 63line 62 didn't jump to line 63 because the condition on line 62 was never true
63 logger.error(
64 "ONNX Runtime does not report any available providers. Attempting to load model with default providers, but this will likely fail."
65 )
66 else:
67 logger.warning(
68 "None of the requested devices are available for ONNX Runtime, falling back to default, available providers: {}",
69 available_providers,
70 )
71 return providers
73 def _init_model_on_device(self, device: str | None) -> rt.InferenceSession:
74 onnx_descr = self._onnx_descr
75 if (
76 isinstance(onnx_descr, v0_5.OnnxWeightsDescr)
77 and onnx_descr.external_data is not None
78 ):
79 src = onnx_descr.source.absolute()
80 src_data = onnx_descr.external_data.source.absolute()
81 if (
82 isinstance(src, Path)
83 and isinstance(src_data, Path)
84 and src.parent == src_data.parent
85 ):
86 logger.debug(
87 "Loading ONNX model with external data from {}",
88 src.parent,
89 )
90 source_context = nullcontext(src)
91 else:
92 src_reader = onnx_descr.get_reader()
93 src_data_reader = onnx_descr.external_data.get_reader()
95 @contextmanager
96 def source_context_func():
97 with tempfile.TemporaryDirectory() as tmpdir:
98 logger.debug(
99 "Loading ONNX model with external data from {}",
100 tmpdir,
101 )
102 src = Path(tmpdir) / src_reader.original_file_name
103 src_data = Path(tmpdir) / src_data_reader.original_file_name
104 with src.open("wb") as f:
105 shutil.copyfileobj(src_reader, f)
106 with src_data.open("wb") as f:
107 shutil.copyfileobj(src_data_reader, f)
108 yield src
110 source_context = source_context_func()
112 else:
113 # load single source file from bytes (without external data, so probably <2GB)
114 logger.debug(
115 "Loading ONNX model from bytes (read from {})", onnx_descr.source
116 )
117 source_context = nullcontext(onnx_descr.get_reader().read())
119 with source_context as s:
120 assert isinstance(s, bytes) or s.exists()
121 session = rt.InferenceSession(
122 s,
123 providers=None if device is None else [device],
124 )
126 onnx_inputs = session.get_inputs()
127 onnx_input_names = [str(ipt.name) for ipt in onnx_inputs] # pyright: ignore[reportUnknownArgumentType]
128 if self._input_names is None:
129 self._input_names = onnx_input_names
130 elif self._input_names != onnx_input_names: 130 ↛ 131line 130 didn't jump to line 131 because the condition on line 130 was never true
131 raise RuntimeError(
132 f"Input names of the ONNX model {onnx_input_names} do not match expected input names {self._input_names} from previous model initialization."
133 )
135 return session
137 def _forward_impl(
138 self,
139 device: str | None,
140 model: rt.InferenceSession,
141 input_arrays: Sequence[NDArray[Any] | None],
142 ) -> list[NDArray[Any] | None]:
143 assert self._input_names is not None, "set during model initialization"
144 result: Any = model.run(None, dict(zip(self._input_names, input_arrays)))
145 if is_list(result) or is_tuple(result): 145 ↛ 148line 145 didn't jump to line 148 because the condition on line 145 was always true
146 result_seq = list(result)
147 else:
148 result_seq = [result]
150 return result_seq
152 def _cleanup_pre_model_deletion(
153 self, device: str | None, model: rt.InferenceSession
154 ) -> None:
155 return
157 def _cleanup_post_model_deletion(self, device: str | None) -> None:
158 return