Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
 
import os
 
import onnx
 
from onnxruntime.capi._pybind_state import get_model_after_loading_checkpoint as _load_checkpoint_to_model
from onnxruntime.capi._pybind_state import save_checkpoint as _save_checkpoint
 
 
def save_checkpoint(
    parameters: tuple[list[onnx.TensorProto], list[onnx.TensorProto]],
    path_to_checkpoint: str | os.PathLike,
    nominal_checkpoint: bool = False,
) -> None:
    """Saves the parameters to the checkpoint directory path_to_checkpoint.
 
    Args:
        parameters tuple(trainable_params, non_trainable_params): The parameters to save to the checkpoint file.
        path_to_checkpoint: The path to the checkpoint directory.
        nominal_checkpoint: If True, the checkpoint is saved as a nominal checkpoint. Default is False.
    """
 
    if parameters is None:
        raise RuntimeError("No checkpoint parameters provided.")
 
    trainable_params, non_trainable_params = parameters
    trainable_params = [param.SerializeToString() for param in trainable_params]
    non_trainable_params = [param.SerializeToString() for param in non_trainable_params]
    _save_checkpoint(trainable_params, non_trainable_params, os.fspath(path_to_checkpoint), nominal_checkpoint)
 
 
def load_checkpoint_to_model(path_to_checkpoint: str | os.PathLike, model: onnx.ModelProto) -> None:
    """Loads the checkpoint to an onnx inference model.
 
    Args:
        path_to_checkpoint (str): The path to the checkpoint directory.
        model (onnx.ModelProto): The model to load the checkpoint to.
    """
 
    model.ParseFromString(_load_checkpoint_to_model(os.fspath(path_to_checkpoint), model.SerializeToString()))