Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
 
from __future__ import annotations
 
import os
 
import numpy as np
 
from onnxruntime.capi import _pybind_state as C
from onnxruntime.capi.onnxruntime_inference_collection import OrtValue, get_ort_device_type
from onnxruntime.capi.onnxruntime_pybind11_state import OrtValueVector, SessionOptions
from onnxruntime.training.api.checkpoint_state import CheckpointState
 
 
class Module:
    """Trainer class that provides training and evaluation methods for ONNX models.
 
    Before instantiating the Module class, it is expected that the training artifacts have been
    generated using the `onnxruntime.training.artifacts.generate_artifacts` utility.
 
    The training artifacts include:
        - The training model
        - The evaluation model (optional)
        - The optimizer model (optional)
        - The checkpoint file
 
    Attributes:
        training: True if the model is in training mode, False if it is in evaluation mode.
 
    Args:
        train_model_uri: The path to the training model.
        state: The checkpoint state object.
        eval_model_uri: The path to the evaluation model.
        device: The device to run the model on. Default is "cpu".
        session_options: The session options to use for the model.
    """
 
    training: bool
 
    def __init__(
        self,
        train_model_uri: os.PathLike,
        state: CheckpointState,
        eval_model_uri: os.PathLike | None = None,
        device: str = "cpu",
        session_options: SessionOptions | None = None,
    ) -> None:
        self.training = True
        options = device.split(":")
        self._device_type = options[0]
        device_id = 0 if len(options) < 2 else int(options[1])
        self._session_options = session_options if session_options is not None else SessionOptions()
 
        self._device = C.OrtDevice(
            get_ort_device_type(self._device_type, device_id),
            C.OrtDevice.default_memory(),
            device_id,
        )
        self._model = C.Module(
            os.fspath(train_model_uri),
            state._state,
            os.fspath(eval_model_uri) if eval_model_uri is not None else None,
            self._device,
            self._session_options,
        )
        self._state = state
 
    def __call__(self, *user_inputs) -> tuple[np.ndarray, ...] | np.ndarray | tuple[OrtValue, ...] | OrtValue:
        """Invokes either the training or the evaluation step of the model.
 
        Args:
            *user_inputs: The inputs to the model.
                          The user inputs can be either numpy arrays or OrtValues.
        Returns:
            The outputs of the model.
        """
 
        def _has_np_input(user_inputs):
            return any(isinstance(user_input, np.ndarray) for user_input in user_inputs)
 
        def _take_generic_step(forward_inputs):
            fetches = OrtValueVector()
            if self.training:
                self._model.train_step(forward_inputs, fetches)
            else:
                self._model.eval_step(forward_inputs, fetches)
 
            if len(fetches) == 1:
                return fetches[0].numpy()
 
            return tuple(val.numpy() for val in fetches)
 
        def _take_step_with_ortvalues(forward_inputs):
            ort_values = OrtValueVector()
            ort_values.reserve(len(forward_inputs))
            fetches = OrtValueVector()
 
            for tensor in forward_inputs:
                ort_values.push_back(tensor._ortvalue)
 
            if self.training:
                self._model.train_step_with_ort_values(ort_values, fetches)
            else:
                self._model.eval_step_with_ort_values(ort_values, fetches)
 
            if len(fetches) == 1:
                return OrtValue(fetches[0])
 
            return tuple(OrtValue(val) for val in fetches)
 
        if _has_np_input(user_inputs):
            return _take_generic_step([*user_inputs])
 
        return _take_step_with_ortvalues(user_inputs)
 
    def train(self, mode: bool = True) -> Module:
        """Sets the Module in training mode.
 
        Args:
            mode: whether to set the model to training mode (True) or evaluation mode (False). Default: True.
 
        Returns:
            self
        """
        self.training = mode
        return self
 
    def eval(self) -> Module:
        """Sets the Module in evaluation mode.
 
        Returns:
            self
        """
        return self.train(False)
 
    def lazy_reset_grad(self):
        """Lazily resets the training gradients.
 
        This function sets the internal state of the module such that the module gradients
        will be scheduled to be reset just before the new gradients are computed on the next invocation
        of train().
        """
        return self._model.lazy_reset_grad()
 
    def get_contiguous_parameters(self, trainable_only: bool = False) -> OrtValue:
        """Creates a contiguous buffer of the training session parameters
 
        Args:
            trainable_only: If True, only trainable parameters are considered. Otherwise, all parameters are considered.
 
        Returns:
            The contiguous buffer of the training session parameters.
        """
        parameters = OrtValue.ortvalue_from_shape_and_type(
            [
                self.get_parameters_size(trainable_only),
            ],
            np.float32,
            self._device_type,
            self._device.device_id(),
        )._ortvalue
        self._model.copy_parameters_to_buffer(parameters, trainable_only)
 
        return parameters
 
    def get_parameters_size(self, trainable_only: bool = True) -> int:
        """Returns the size of the parameters.
 
        Args:
            trainable_only: If True, only trainable parameters are considered. Otherwise, all parameters are considered.
 
        Returns:
            The number of primitive (example floating point) elements in the parameters.
        """
        return self._model.get_parameters_size(trainable_only)
 
    def copy_buffer_to_parameters(self, buffer: OrtValue, trainable_only: bool = True) -> None:
        """Copies the OrtValue buffer to the training session parameters.
 
        In case the module was loaded from a nominal checkpoint, invoking this function is required
        to load the updated parameters onto the checkpoint to complete it.
 
        Args:
            buffer: The OrtValue buffer to copy to the training session parameters.
        """
        self._model.copy_buffer_to_parameters(buffer, trainable_only)
 
    def export_model_for_inferencing(
        self, inference_model_uri: str | os.PathLike, graph_output_names: list[str]
    ) -> None:
        """Exports the model for inferencing.
 
        Once training is complete, this function can be used to drop the training specific nodes in the onnx model.
        In particular, this function does the following:
 
        - Parse over the training graph and identify nodes that generate the given output names.
        - Drop all subsequent nodes in the graph since they are not relevant to the inference graph.
 
        Args:
            inference_model_uri: The path to the inference model.
            graph_output_names: The list of output names that are required for inferencing.
        """
        self._model.export_model_for_inferencing(os.fspath(inference_model_uri), graph_output_names)
 
    def input_names(self) -> list[str]:
        """Returns the input names of the training or eval model."""
        return self._model.input_names(self.training)
 
    def output_names(self) -> list[str]:
        """Returns the output names of the training or eval model."""
        return self._model.output_names(self.training)