Skip to content

load_equine_model

load_equine_model(model_path, device=None, *, embedding_model=None) ยค

Attempt to load an EQUINE model from a file

Parameters:

Name Type Description Default
model_path str

The path to the model file

required
device str | None

The device to load the model onto. Overrides the device recorded in the file; by default the model lands on the saved device.

None
embedding_model Module | None

Keyword-only. Use this module as the embedding architecture instead of rebuilding it from the file's recipe; the file's weights are loaded into it, and its floating-point tensors take their stored dtypes.

None

Returns:

Type Description
Equine

The loaded EQUINE model

Raises:

Type Description
ValueError

If the model type is missing or unknown, the file is not in the current EQUINE format (files from earlier releases must first be rewritten with python -m equine.upgrade), it stores its embedding model as TorchScript, or its recipe names an unregistered architecture. If device is unavailable here, names an index at or above its device count, or lacks a dtype the file holds (float64 on MPS: load such a file with device="cpu").

Notes

Loading runs no code from the file: it is read with restricted unpickling (see utils.load_checkpoint) and its embedding model is rebuilt through the architecture registry.

Source code in src/equine/load_equine_model.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def load_equine_model(
    model_path: str,
    device: str | None = None,
    *,
    embedding_model: torch.nn.Module | None = None,
) -> Equine:
    """
    Attempt to load an EQUINE model from a file

    Parameters
    ----------
    model_path : str
        The path to the model file
    device : str | None
        The device to load the model onto. Overrides the device recorded in
        the file; by default the model lands on the saved device.
    embedding_model : torch.nn.Module | None
        Keyword-only. Use this module as the embedding architecture instead of
        rebuilding it from the file's recipe; the file's weights are loaded
        into it, and its floating-point tensors take their stored dtypes.

    Returns
    -------
    Equine
        The loaded EQUINE model

    Raises
    ------
    ValueError
        If the model type is missing or unknown, the file is not in the
        current EQUINE format (files from earlier releases must first be
        rewritten with ``python -m equine.upgrade``), it stores its embedding
        model as TorchScript, or its recipe names an unregistered architecture.
        If ``device`` is unavailable here, names an
        index at or above its device count, or lacks a dtype the file holds
        (float64 on MPS: load such a file with ``device="cpu"``).

    Notes
    -----
    Loading runs no code from the file: it is read with restricted unpickling (see
    ``utils.load_checkpoint``) and its embedding model is rebuilt through the
    architecture registry.
    """
    model_save = _load_checkpoint_for_device(model_path, device)
    _refuse_torchscript(model_save, model_path)
    summary = model_save.get("train_summary")
    model_type = summary.get("modelType") if isinstance(summary, dict) else None
    if not isinstance(model_type, str):
        raise ValueError(
            "Model file has a malformed train_summary (expected a dictionary with a "
            "string 'modelType')."
        )
    classes: dict[str, type[EquineProtonet] | type[EquineGP]] = {
        "EquineProtonet": EquineProtonet,
        "EquineGP": EquineGP,
    }
    if model_type not in classes:
        raise ValueError(f"Unknown model type {_truncate(repr(model_type), 80)}")
    return classes[model_type]._from_checkpoint(
        model_save, device, embedding_model=embedding_model
    )