Coverage for src/bioimageio/core/backends/torchscript_backend.py: 87%

40 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 09:46 +0000

1# pyright: reportUnknownVariableType=false 

2from __future__ import annotations 

3 

4import gc 

5from collections.abc import Sequence 

6from typing import Any 

7 

8import torch 

9from loguru import logger 

10from numpy.typing import NDArray 

11 

12from bioimageio.spec.model import v0_4, v0_5 

13 

14from .._model_adapter import LocalModelAdapter 

15from .._utils.type_guards import is_list, is_tuple 

16from .pytorch_backend import get_devices 

17 

18 

19class TorchscriptModelAdapter(LocalModelAdapter[torch.device, Any]): 

20 def __init__( 

21 self, 

22 model_description: v0_4.ModelDescr | v0_5.ModelDescr, 

23 devices: Sequence[str] | None = None, 

24 ): 

25 if model_description.weights.torchscript is None: 25 ↛ 26line 25 didn't jump to line 26 because the condition on line 25 was never true

26 raise ValueError( 

27 f"No torchscript weights found for model {model_description.name}" 

28 ) 

29 

30 self._weight_descr = model_description.weights.torchscript 

31 super().__init__(model_description=model_description, devices=devices) 

32 

33 def _parse_devices(self, devices: Sequence[str] | None) -> Sequence[torch.device]: 

34 return get_devices(devices) 

35 

36 def _init_model_on_device(self, device: torch.device) -> Any: 

37 model = torch.jit.load(self._weight_descr.get_reader(), map_location=device) 

38 try: 

39 model.eval() 

40 except Exception as e: 

41 logger.warning( 

42 f"Failed to set model to evaluation mode for torchscript model on {device}: {e}" 

43 ) 

44 return model 

45 

46 def _forward_impl( 

47 self, 

48 device: torch.device, 

49 model: Any, 

50 input_arrays: Sequence[NDArray[Any] | None], 

51 ) -> list[NDArray[Any] | None]: 

52 with torch.no_grad(): 

53 torch_tensor = [ 

54 None if a is None else torch.from_numpy(a).to(device) 

55 for a in input_arrays 

56 ] 

57 output: Any = model.forward(*torch_tensor) 

58 if is_list(output) or is_tuple(output): 

59 output_seq: Sequence[Any] = output 

60 else: 

61 output_seq = [output] 

62 

63 return [ 

64 ( 

65 None 

66 if r is None 

67 else r.cpu().numpy() 

68 if isinstance(r, torch.Tensor) 

69 else r 

70 ) 

71 for r in output_seq 

72 ] 

73 

74 def _cleanup_pre_model_deletion(self, device: torch.device, model: Any) -> None: 

75 return 

76 

77 def _cleanup_post_model_deletion(self, device: torch.device) -> None: 

78 _ = gc.collect() # deallocate memory 

79 if device.type == "cuda": 79 ↛ 80line 79 didn't jump to line 80 because the condition on line 79 was never true

80 torch.cuda.empty_cache() # release reserved memory