Coverage for src/bioimageio/core/backends/pytorch_backend.py: 74%
132 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
1from __future__ import annotations
3import gc
4from abc import abstractmethod
5from collections.abc import Mapping, Sequence
6from contextlib import nullcontext
7from io import BytesIO, TextIOWrapper
8from pathlib import Path
9from typing import Any, Literal
11import torch
12from loguru import logger
13from numpy.typing import NDArray
14from torch import nn
15from typing_extensions import Protocol, Self, assert_never, runtime_checkable
17from bioimageio.spec._internal.version_type import Version
18from bioimageio.spec.common import BytesReader, ZipPath
19from bioimageio.spec.model import AnyModelDescr, v0_4, v0_5
20from bioimageio.spec.utils import download
22from .._model_adapter import LocalModelAdapter
23from .._utils.type_guards import is_list, is_ndarray, is_tuple
24from .._utils.type_hints import cast_and_assert
25from ..digest_spec import import_callable
28@runtime_checkable
29class TorchNNModuleLike(Protocol):
30 @abstractmethod
31 def load_state_dict(
32 self, state_dict: Mapping[str, Any], strict: bool = True, assign: bool = False
33 ) -> Self: ...
35 @abstractmethod
36 def to(
37 self,
38 *,
39 device: torch.device | None = None,
40 dtype: torch.dtype | None = None,
41 non_blocking: bool = False,
42 ) -> Self: ...
44 @abstractmethod
45 def forward(
46 self, *input: torch.Tensor
47 ) -> torch.Tensor | tuple[torch.Tensor, ...] | list[torch.Tensor]: ...
49 def eval(self) -> Self:
50 """Set model to eval mode"""
51 return self
54class PytorchModelAdapter(LocalModelAdapter[torch.device, nn.Module]):
55 def __init__(
56 self,
57 model_description: AnyModelDescr,
58 mode: Literal["eval", "train"] = "eval",
59 devices: Sequence[str] | None = None,
60 ):
61 weights = model_description.weights.pytorch_state_dict
62 if weights is None: 62 ↛ 63line 62 didn't jump to line 63 because the condition on line 62 was never true
63 raise ValueError("No `pytorch_state_dict` weights found")
65 self._weights = weights
66 self._mode: Literal["eval", "train"] = mode
67 super().__init__(model_description=model_description, devices=devices)
69 def _parse_devices(self, devices: Sequence[str] | None) -> Sequence[torch.device]:
70 return get_devices(devices)
72 def _init_model_on_device(self, device: torch.device) -> nn.Module:
73 model = load_torch_model(self._weights, load_state=True, devices=[device])
75 if self._mode == "eval": 75 ↛ 77line 75 didn't jump to line 77 because the condition on line 75 was always true
76 model = model.eval()
77 elif self._mode == "train":
78 model = model.train()
79 else:
80 assert_never(self._mode)
82 return model
84 def _forward_impl(
85 self,
86 device: torch.device,
87 model: nn.Module,
88 input_arrays: Sequence[NDArray[Any] | None],
89 ) -> list[NDArray[Any] | None]:
90 tensors = [
91 None if a is None else torch.from_numpy(a).to(device) for a in input_arrays
92 ]
94 if self._mode == "eval": 94 ↛ 96line 94 didn't jump to line 96 because the condition on line 94 was always true
95 ctxt = torch.no_grad
96 elif self._mode == "train":
97 ctxt = nullcontext
98 else:
99 assert_never(self._mode)
101 with ctxt():
102 model_out = model(*tensors)
104 if is_tuple(model_out) or is_list(model_out):
105 model_out_seq = model_out
106 else:
107 model_out_seq = model_out = [model_out]
109 result: list[NDArray[Any] | None] = []
110 for i, r in enumerate(model_out_seq):
111 if r is None: 111 ↛ 112line 111 didn't jump to line 112 because the condition on line 111 was never true
112 result.append(None)
113 elif isinstance(r, torch.Tensor): 113 ↛ 116line 113 didn't jump to line 116 because the condition on line 113 was always true
114 r_np: NDArray[Any] = r.detach().cpu().numpy()
115 result.append(r_np)
116 elif is_ndarray(r):
117 result.append(r)
118 else:
119 raise TypeError(f"Model output[{i}] has unexpected type {type(r)}.")
121 return result
123 def _cleanup_pre_model_deletion(
124 self, device: torch.device, model: nn.Module
125 ) -> None:
126 return
128 def _cleanup_post_model_deletion(self, device: torch.device) -> None:
129 _ = gc.collect() # deallocate memory
130 if device.type == "cuda": 130 ↛ 131line 130 didn't jump to line 131 because the condition on line 130 was never true
131 torch.cuda.empty_cache() # release reserved memory
134def load_torch_model(
135 weight_spec: v0_4.PytorchStateDictWeightsDescr | v0_5.PytorchStateDictWeightsDescr,
136 *,
137 load_state: bool = True,
138 devices: Sequence[str | torch.device] | None = None,
139) -> nn.Module:
140 custom_callable = import_callable(
141 weight_spec.architecture,
142 sha256=(
143 weight_spec.architecture_sha256
144 if isinstance(weight_spec, v0_4.PytorchStateDictWeightsDescr)
145 else weight_spec.sha256
146 ),
147 )
148 model_kwargs = (
149 weight_spec.kwargs
150 if isinstance(weight_spec, v0_4.PytorchStateDictWeightsDescr)
151 else weight_spec.architecture.kwargs
152 )
153 torch_model = custom_callable(**model_kwargs)
155 if not isinstance(torch_model, nn.Module): 155 ↛ 156line 155 didn't jump to line 156 because the condition on line 155 was never true
156 if isinstance(
157 weight_spec.architecture,
158 (v0_4.CallableFromFile, v0_4.CallableFromDepencency),
159 ):
160 callable_name = weight_spec.architecture.callable_name
161 else:
162 callable_name = weight_spec.architecture.callable
164 raise TypeError(f"Calling {callable_name} did not return a torch.nn.Module.")
166 if load_state or devices: 166 ↛ 178line 166 didn't jump to line 178 because the condition on line 166 was always true
167 use_devices = get_devices(devices)
168 torch_model = torch_model.to(use_devices[0])
169 if load_state: 169 ↛ 178line 169 didn't jump to line 178 because the condition on line 169 was always true
170 torch_model = load_torch_state_dict(
171 torch_model,
172 path=download(weight_spec),
173 devices=use_devices,
174 strict=weight_spec.strict
175 if isinstance(weight_spec, v0_5.PytorchStateDictWeightsDescr)
176 else True,
177 )
178 return torch_model
181def load_torch_state_dict(
182 model: nn.Module,
183 path: Path | ZipPath | BytesReader,
184 devices: Sequence[torch.device],
185 strict: bool = True,
186) -> nn.Module:
187 model = model.to(devices[0])
188 if path.suffix == ".safetensors":
189 if isinstance(path, Path):
190 from safetensors.torch import load_file # pyright: ignore[reportUnknownVariableType]
192 state = load_file(path, device=str(devices[0]))
193 elif isinstance(path, (ZipPath, BytesReader)):
194 from safetensors.torch import load
196 if isinstance(path, ZipPath):
197 data = cast_and_assert(bytes, path.read_bytes())
198 else:
199 data = path.read()
201 state = {k: v.to(devices[0]) for k, v in load(data).items()}
202 else:
203 assert_never(path)
205 else:
206 if isinstance(path, (Path, ZipPath)): 206 ↛ 207line 206 didn't jump to line 207 because the condition on line 206 was never true
207 ctxt = path.open("rb")
208 elif isinstance(path, BytesReader):
209 ctxt = nullcontext(BytesIO(path.read()))
210 else:
211 assert_never(path)
213 with ctxt as f:
214 assert not isinstance(f, TextIOWrapper)
215 if Version(str(torch.__version__)) < Version("1.13"): 215 ↛ 216line 215 didn't jump to line 216 because the condition on line 215 was never true
216 state = torch.load(f, map_location=devices[0])
217 else:
218 try:
219 state = torch.load(f, map_location=devices[0], weights_only=True)
220 except Exception as e:
221 msg = (
222 f"Failed to load weights with `weights_only=True`: {e}\n\n"
223 + "This usually means the weights file contains non-tensor objects"
224 + " (e.g. numpy arrays, custom classes, or nested dicts with"
225 + " metadata). The BioImage.IO spec requires a pure state dict —"
226 + " an OrderedDict mapping parameter names to tensors only.\n\n"
227 + "To fix this, extract only the state dict from your checkpoint:\n\n"
228 + " import torch\n"
229 + " checkpoint = torch.load('original.pth', weights_only=False)\n"
230 + " # Inspect keys, e.g.: checkpoint.keys()"
231 + " -> dict_keys(['model', 'optimizer', ...])\n"
232 + " torch.save(checkpoint['model'], 'weights.pt')\n\n"
233 + "Then reference 'weights.pt' in your bioimageio.yaml."
234 )
235 raise ValueError(msg) from e
237 incompatible = model.load_state_dict(state, strict=strict)
238 if ( 238 ↛ 251line 238 didn't jump to line 251 because the condition on line 238 was always true
239 isinstance(incompatible, tuple)
240 and hasattr(incompatible, "missing_keys")
241 and hasattr(incompatible, "unexpected_keys")
242 ):
243 if incompatible.missing_keys:
244 logger.warning("Missing state dict keys: {}", incompatible.missing_keys)
246 if hasattr(incompatible, "unexpected_keys") and incompatible.unexpected_keys: 246 ↛ 247line 246 didn't jump to line 247 because the condition on line 246 was never true
247 logger.warning(
248 "Unexpected state dict keys: {}", incompatible.unexpected_keys
249 )
250 else:
251 logger.warning(
252 "`model.load_state_dict()` unexpectedly returned: {} "
253 + "(expected named tuple with `missing_keys` and `unexpected_keys` attributes)",
254 (s[:20] + "..." if len(s := str(incompatible)) > 20 else s),
255 )
257 return model
260def get_devices(
261 devices: Sequence[torch.device | str] | None = None,
262) -> list[torch.device]:
263 if not devices:
264 if torch.cuda.is_available(): 264 ↛ 265line 264 didn't jump to line 265 because the condition on line 264 was never true
265 torch_devices = [
266 torch.device(f"cuda:{i}") for i in range(torch.cuda.device_count())
267 ]
268 elif torch.backends.mps.is_available(): 268 ↛ 269line 268 didn't jump to line 269 because the condition on line 268 was never true
269 torch_devices = [torch.device("mps")]
270 else:
271 try:
272 if ( 272 ↛ 277line 272 didn't jump to line 277 because the condition on line 272 was never true
273 torch.accelerator.is_available()
274 and (current_accelerator := torch.accelerator.current_accelerator())
275 is not None
276 ):
277 torch_devices = [current_accelerator]
278 else:
279 torch_devices = [torch.device("cpu")]
280 except Exception:
281 torch_devices = [torch.device("cpu")]
282 else:
283 torch_devices = [torch.device(d) for d in devices]
285 return torch_devices