Coverage for src/bioimageio/core/weight_converters/pytorch_to_torchscript.py: 92%
23 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
3from collections.abc import Sequence
4from pathlib import Path
5from typing import Any
7import torch
8from torch.jit import ScriptModule
10from bioimageio.spec._internal.version_type import Version
11from bioimageio.spec.model.v0_5 import ModelDescr, TorchscriptWeightsDescr
13from .. import __version__
14from ..backends.pytorch_backend import get_devices, load_torch_model
17def convert(
18 model_descr: ModelDescr,
19 output_path: Path,
20 *,
21 use_tracing: bool = True,
22 devices: Sequence[str | torch.device] | None = None,
23) -> TorchscriptWeightsDescr:
24 """
25 Convert model weights from the PyTorch `state_dict` format to TorchScript.
27 Args:
28 model_descr:
29 The model description object that contains the model and its weights in the PyTorch `state_dict` format.
30 output_path:
31 The file path where the TorchScript model will be saved.
32 use_tracing:
33 Whether to use tracing or scripting to export the TorchScript format.
34 - `True`: Use tracing, which is recommended for models with straightforward control flow.
35 - `False`: Use scripting, which is better for models with dynamic control flow (e.g., loops, conditionals).
37 Raises:
38 ValueError:
39 If the provided model does not have weights in the PyTorch `state_dict` format.
41 Returns:
42 A descriptor object that contains information about the exported TorchScript weights.
43 """
44 state_dict_weights_descr = model_descr.weights.pytorch_state_dict
45 if state_dict_weights_descr is None: 45 ↛ 46line 45 didn't jump to line 46 because the condition on line 45 was never true
46 raise ValueError(
47 "The provided model does not have weights in the pytorch state dict format"
48 )
50 input_data = model_descr.get_input_test_arrays()
51 devices = get_devices(devices)
53 with torch.no_grad():
54 input_data = [torch.from_numpy(inp).to(device=devices[0]) for inp in input_data]
55 model = load_torch_model(
56 state_dict_weights_descr, load_state=True, devices=devices
57 )
58 scripted_model: ScriptModule | tuple[Any, ...] = ( # pyright: ignore[reportUnknownVariableType]
59 torch.jit.trace(model, input_data)
60 if use_tracing
61 else torch.jit.script(model)
62 )
63 assert not isinstance(scripted_model, tuple), scripted_model
65 scripted_model.save(output_path)
67 return TorchscriptWeightsDescr(
68 source=output_path.absolute(),
69 pytorch_version=Version(torch.__version__),
70 parent="pytorch_state_dict",
71 comment=(
72 f"Converted with bioimageio.core {__version__}"
73 + f" with use_tracing={use_tracing}."
74 ),
75 )