Исходный код СУБД 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# --------------------------------------------------------------------------
 
import copy
import warnings
from collections import OrderedDict, abc
from collections.abc import Mapping, Sequence
 
import torch
 
from onnxruntime.training.utils.torch_profile_utils import nvtx_function_decorator
 
 
class PrimitiveType:
    """Helper class for Python primitive types."""
 
    _primitive_types = {int, bool, float}  # noqa: RUF012
 
    @staticmethod
    def is_primitive_type(value):
        """Check if `value` is a Python primitive type."""
        return type(value) in PrimitiveType._primitive_types
 
    @staticmethod
    def get_tensor(value, device) -> torch.Tensor:
        """Convert `value` to a torch.Tensor."""
        return torch.tensor(value, device=device)
 
    @staticmethod
    def get_primitive_dtype(value):
        # If `value` is a boolean, save the value of the boolean in dtype.
        # This way, if the value changes from one forward call to the next, the schema will mismatch,
        # and the model will be re-exported.
        return f"{type(value)!s}_{value}" if isinstance(value, bool) else str(type(value))
 
 
# Data types supported as model inputs and outputs.
ORTModelInputOutputType = (
    str
    | int
    | bool
    | float
    | torch.Tensor
    | Sequence["ORTModelInputOutputType"]
    | Mapping[str, "ORTModelInputOutputType"]
    | None
)
 
 
class _TensorStub:  # noqa: PLW1641
    """Tensor stub class used to represent model's input or output"""
 
    __slots__ = ["dtype", "name", "shape", "shape_dims", "tensor_idx"]
 
    def __init__(
        self,
        tensor_idx: int,
        name: str | None = None,
        dtype: str | None = None,
        shape=None,
        shape_dims: int | None = None,
    ):
        self.tensor_idx = tensor_idx
        self.name: str | None = name
        self.dtype: str | None = dtype
        self.shape = shape
        self.shape_dims: int | None = shape_dims  # r.g. rank.
 
    def __repr__(self) -> str:
        result = "_TensorStub("
 
        list_of_str = []
        if self.tensor_idx is not None:
            list_of_str.append(f"tensor_idx={self.tensor_idx}")
        if self.name is not None:
            list_of_str.append(f"name={self.name}")
        if self.dtype is not None:
            list_of_str.append(f"dtype={self.dtype}")
        if self.shape is not None:
            list_of_str.append(f"shape={self.shape}")
        if self.shape_dims is not None:
            list_of_str.append(f"shape_dims={self.shape_dims}")
 
        result += ",".join(list_of_str) + ")"
        return result
 
    def __str__(self) -> str:
        return self.__repr__()
 
    def __eq__(self, other):
        if not other:
            return False
        elif not isinstance(other, _TensorStub):
            raise NotImplementedError("_TensorStub must only be compared to another _TensorStub instance!")
        elif self.tensor_idx != other.tensor_idx:
            return False
        elif self.name != other.name:
            return False
        elif self.dtype != other.dtype:
            return False
        elif self.shape != other.shape:
            return False
        elif self.shape_dims != other.shape_dims:
            return False
        return True
 
 
# Data schema used to represent model's input or output.
ORTModelInputOutputSchemaType = (
    str | _TensorStub | Sequence["ORTModelInputOutputSchemaType"] | Mapping[str, "ORTModelInputOutputSchemaType"] | None
)
 
 
def _warn_of_constant_inputs(data):
    warnings.warn(
        f"Received input of type {type(data)} which may be treated as a constant by ORT by default."
        " Please consider moving constant arguments to the model constructor."
    )
 
 
@nvtx_function_decorator
def extract_data_and_schema(
    data: ORTModelInputOutputType, constant_as_tensor=False, device: torch.device | None = None
) -> tuple[list[torch.Tensor], ORTModelInputOutputSchemaType]:
    """Extract the data schema by replacing every torch.Tensor value with _TensorStub, and return all tensors in
    a list.
 
    Depth first traversal to iterate over the data:
    > Replace every tensor with a stub
    > Replace None/str typed data with itself
    > For other primitive types:
        If constant_as_tensor is True, create tensor from data , and replace them with a stub in returned schema;
        Otherwise, replace them with themselves in returned schema.
 
    Examples:
        Example 1, list:
            data = [torch.tensor(1), torch.tensor(2)]
            schema = [_TensorStub(shape=()), _TensorStub(shape=())]
 
        Example 2, dict:
            data = {"a": torch.tensor(1), "b": torch.tensor(2)}
            schema = {"a": _TensorStub(shape=()), "b": _TensorStub(shape=())}
 
        Example 3, dict of list:
            data = {"a": [torch.tensor(1), torch.tensor(2)], "b": [torch.tensor(3), torch.tensor(4)]}
            schema = {"a": [_TensorStub(shape=()), _TensorStub(shape=())],
                        "b": [_TensorStub(shape=()), _TensorStub(shape=())]}
 
        Example 4, nested dict:
            data = {"a": {"b": torch.tensor(1), "c": torch.tensor(2)},
                    "d": {"e": torch.tensor(3), "f": torch.tensor(4)}}
            schema = {"a": {"b": _TensorStub(shape=()), "c": _TensorStub(shape=())},
                        "d": {"e": _TensorStub(shape=()), "f": _TensorStub(shape=())}}
 
        Example 5, dict of mixed list and dict:
            data = {"a": [torch.tensor(1), torch.tensor(2)], "b": {"c": torch.tensor(3), "d": torch.tensor(4)}}
            schema = {"a": [_TensorStub(shape=()), _TensorStub(shape=())],
                        "b": {"c": _TensorStub(shape=()), "d": _TensorStub(shape=())}}
 
    Args:
        data: The data to extract the schema from, which can be in any kind of nested structure,
            including Sequence and Mapping.
        constant_as_tensor: Whether to treat constant inputs as tensor. Default is False.
        device: The device to create tensor for the constant.
 
 
    Returns:
        Tuple:
            The first value: a list of tensors extracted from the data.
            The second value: schema of the data, which has the same structure as the data.
 
 
    """
 
    flatten_tensor_data = []
    tensor_idx = [-1]
 
    def _flatten_from_data(data: ORTModelInputOutputType, prefix_name: str = ""):
        if data is None:
            return data
        elif isinstance(data, str):
            _warn_of_constant_inputs(data)
            return data
        elif PrimitiveType.is_primitive_type(data):
            _warn_of_constant_inputs(data)
            if constant_as_tensor:
                tensor_idx[0] += 1
                flatten_tensor_data.append(PrimitiveType.get_tensor(data, device))
                return _TensorStub(
                    tensor_idx[0], dtype=PrimitiveType.get_primitive_dtype(data), shape_dims=0, name=prefix_name
                )
            return data
        # Depth first traversal to iterate over the data to replace every tensor with a stub
        elif isinstance(data, torch.Tensor):
            tensor_idx[0] += 1
            flatten_tensor_data.append(data)
            return _TensorStub(
                tensor_idx[0],
                dtype=str(data.dtype),
                shape_dims=len(data.size()),
                name=prefix_name,
            )
        elif isinstance(data, abc.Sequence):
            sequence_type = type(data)
            stubbed_schema = []
            for i, val in enumerate(data):
                stubbed_schema.append(_flatten_from_data(val, f"{prefix_name}_{i}" if prefix_name else f"{i}"))
 
            try:
                # namedtuple can be created by passing the list sequence to method _make
                stubbed_schema = sequence_type._make(stubbed_schema)
            except AttributeError:
                # If attribute error is encountered, create the sequence directly
                stubbed_schema = sequence_type(stubbed_schema)
            return stubbed_schema
        elif isinstance(data, abc.Mapping):
            dict_type = type(data)
            stubbed_schema = OrderedDict()
            for key, val in data.items():
                stubbed_schema[key] = _flatten_from_data(val, f"{prefix_name}_{key}" if prefix_name else f"{key}")
            stubbed_schema = dict_type(**stubbed_schema)
            return stubbed_schema
        else:
            raise TypeError(f"Unsupported flatten data type: {type(data)}")
 
    schemas = _flatten_from_data(data)
    return flatten_tensor_data, schemas
 
 
@nvtx_function_decorator
def unflatten_data_using_schema(
    data: list[torch.Tensor], schema: ORTModelInputOutputSchemaType
) -> ORTModelInputOutputType:
    """Follows the schema to generate an output that is expected by the user.
 
    Depth first traversal to iterate over the schema:
    > Replace every _TensorStub with a tensor from data. For each _TensorStub, the tensor is selected from `data`
      by its tensor_idx.
 
    Examples:
        Example 1, schema is a list:
            data = [torch.tensor(1), torch.tensor(2)]
            schema = [_TensorStub(shape=()), True, _TensorStub(shape=()), 1, 2.0, "abc"]
            output = [torch.tensor(1), True, torch.tensor(2), 1, 2.0, "abc"]
 
        Example 2, schema is a dict:
            data = [torch.tensor(1), torch.tensor(2)]
            schema = {"a": _TensorStub(shape=()), "b": _TensorStub(shape=()), "c": True, "d": 1, "e": 2.0, "f": "abc"}
            output = {"a": torch.tensor(1), "b": torch.tensor(2), "c": True, "d": 1, "e": 2.0, "f": "abc"}
 
        Example 3, schema is a dict of list:
            data = [torch.tensor(1), torch.tensor(2), torch.tensor(3), torch.tensor(4)]
            schema = {"a": [_TensorStub(shape=()), _TensorStub(shape=())], "b": [_TensorStub(shape=()), _TensorStub(shape=())]}
            output = {"a": [torch.tensor(1), torch.tensor(2)], "b": [torch.tensor(3), torch.tensor(4)]}
 
        Example 4, schema is a nested dict:
            data = [torch.tensor(1), torch.tensor(2), torch.tensor(3), torch.tensor(4)]
            schema = {"a": {"b": _TensorStub(shape=()), "c": _TensorStub(shape=())},
                        "d": {"e": _TensorStub(shape=()), "f": _TensorStub(shape=())}}
            output = {"a": {"b": torch.tensor(1), "c": torch.tensor(2)}, "d": {"e": torch.tensor(3), "f": torch.tensor(4)}}
 
        Example 5, dict of mixed list and dict:
            data = [torch.tensor(1), torch.tensor(2), torch.tensor(3), torch.tensor(4)]
            schema = {"a": [_TensorStub(shape=()), _TensorStub(shape=())], "b": {"c": _TensorStub(shape=()), "d": _TensorStub(shape=())}}
            output = {"a": [torch.tensor(1), torch.tensor(2)], "b": {"c": torch.tensor(3), "d": torch.tensor(4)}}
 
 
    Args:
        data (List[torch.Tensor]): List of tensors to be used to replace the _TensorStub in schema
        schema (ORTModelInputOutputSchemaType): Schema to follow to generate the output
 
    Returns:
        output (ORTModelInputOutputType): Output that is expected by the user
 
    """
 
    def _replace_stub_with_tensor_value(data_schema: ORTModelInputOutputSchemaType, data: list[torch.Tensor]):
        # Recursively traverse across user_output and replace all _TensorStub
        # with torch.Tensor values from outputs following output_idx
 
        if data_schema is None:
            return None
        elif isinstance(data_schema, str):
            return data_schema
        elif PrimitiveType.is_primitive_type(data_schema):
            return data_schema
        elif isinstance(data_schema, _TensorStub):
            assert isinstance(data[data_schema.tensor_idx], torch.Tensor), (
                f"Expecting torch.Tensor, got {type(data[data_schema.tensor_idx])}"
            )
            return data[data_schema.tensor_idx]
        elif isinstance(data_schema, abc.Sequence):
            sequence_type = type(data_schema)
            if hasattr(sequence_type, "_make"):  # namedtuple
                sequence_type = type(data_schema)
                data_schema = sequence_type._make(_replace_stub_with_tensor_value(uo, data) for uo in data_schema)
            else:
                data_schema = sequence_type(_replace_stub_with_tensor_value(uo, data) for uo in data_schema)
            return data_schema
        elif isinstance(data_schema, abc.Mapping):
            new_user_output = copy.copy(data_schema)
            for key, schema_val in data_schema.items():
                new_user_output[key] = _replace_stub_with_tensor_value(schema_val, data)
            data_schema = new_user_output
 
            return data_schema
 
        raise TypeError(f"Unsupported unflatten data type: {type(data_schema)}.")
 
    user_output = _replace_stub_with_tensor_value(schema, data)
    return user_output