Coverage for /opt/hostedtoolcache/Python/3.12.16/x64/lib/python3.12/site-packages/equine/load_equine_model.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.17.4, created at 2026-11-20 23:10 +0000

1# Copyright 2024, MASSACHUSETTS INSTITUTE OF TECHNOLOGY 

2# Subject to FAR 52.227-11 – Patent Rights – Ownership by the Contractor (May 2014). 

3# SPDX-License-Identifier: MIT 

4 

5 

6import torch 

7 

8from .equine import Equine 

9from .equine_gp import EquineGP 

10from .equine_protonet import EquineProtonet 

11from .registry import _truncate 

12from .utils import _load_checkpoint_for_device, _refuse_torchscript 

13 

14 

15def load_equine_model( 

16 model_path: str, 

17 device: str | None = None, 

18 *, 

19 embedding_model: torch.nn.Module | None = None, 

20) -> Equine: 

21 """ 

22 Attempt to load an EQUINE model from a file 

23 

24 Parameters 

25 ---------- 

26 model_path : str 

27 The path to the model file 

28 device : str | None 

29 The device to load the model onto. Overrides the device recorded in 

30 the file; by default the model lands on the saved device. 

31 embedding_model : torch.nn.Module | None 

32 Keyword-only. Use this module as the embedding architecture instead of 

33 rebuilding it from the file's recipe; the file's weights are loaded 

34 into it, and its floating-point tensors take their stored dtypes. 

35 

36 Returns 

37 ------- 

38 Equine 

39 The loaded EQUINE model 

40 

41 Raises 

42 ------ 

43 ValueError 

44 If the model type is missing or unknown, the file is not in the 

45 current EQUINE format (files from earlier releases must first be 

46 rewritten with ``python -m equine.upgrade``), it stores its embedding 

47 model as TorchScript, or its recipe names an unregistered architecture. 

48 If ``device`` is unavailable here, names an 

49 index at or above its device count, or lacks a dtype the file holds 

50 (float64 on MPS: load such a file with ``device="cpu"``). 

51 

52 Notes 

53 ----- 

54 Loading runs no code from the file: it is read with restricted unpickling (see 

55 ``utils.load_checkpoint``) and its embedding model is rebuilt through the 

56 architecture registry. 

57 """ 

58 model_save = _load_checkpoint_for_device(model_path, device) 

59 _refuse_torchscript(model_save, model_path) 

60 summary = model_save.get("train_summary") 

61 model_type = summary.get("modelType") if isinstance(summary, dict) else None 

62 if not isinstance(model_type, str): 

63 raise ValueError( 

64 "Model file has a malformed train_summary (expected a dictionary with a " 

65 "string 'modelType')." 

66 ) 

67 classes: dict[str, type[EquineProtonet] | type[EquineGP]] = { 

68 "EquineProtonet": EquineProtonet, 

69 "EquineGP": EquineGP, 

70 } 

71 if model_type not in classes: 

72 raise ValueError(f"Unknown model type {_truncate(repr(model_type), 80)}") 

73 return classes[model_type]._from_checkpoint( 

74 model_save, device, embedding_model=embedding_model 

75 )