Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
 
using Microsoft.ML.OnnxRuntime.Tensors;
using System;
 
namespace Microsoft.ML.OnnxRuntime
{
    /// <summary>
    /// This class retrieves Type Information for input/outputs of the model.
    /// </summary>
    public class OrtTypeInfo
    {
        private OrtTensorTypeAndShapeInfo? _tensorTypeAndShape;
        private OrtSequenceOrOptionalTypeInfo? _sequenceOrOptional;
        private OrtMapTypeInfo? _mapTypeInfo;
 
        internal OrtTypeInfo(IntPtr handle)
        {
            NativeApiStatus.VerifySuccess(NativeMethods.OrtGetOnnxTypeFromTypeInfo(handle, out IntPtr onnxType));
            OnnxType = (OnnxValueType)onnxType;
 
            switch (OnnxType)
            {
                case OnnxValueType.ONNX_TYPE_TENSOR:
                case OnnxValueType.ONNX_TYPE_SPARSETENSOR:
                    {
                        NativeApiStatus.VerifySuccess(NativeMethods.OrtCastTypeInfoToTensorInfo(handle, out IntPtr tensorInfo));
                        if (tensorInfo == IntPtr.Zero)
                        {
                            throw new OnnxRuntimeException(ErrorCode.Fail,
                                "Type Information indicates a tensor, but casting to TensorInfo fails");
                        }
                        _tensorTypeAndShape = new OrtTensorTypeAndShapeInfo(tensorInfo);
                    }
                    break;
                case OnnxValueType.ONNX_TYPE_SEQUENCE:
                    {
                        NativeApiStatus.VerifySuccess(NativeMethods.OrtCastTypeInfoToSequenceTypeInfo(handle, out IntPtr sequenceInfo));
                        if (sequenceInfo == IntPtr.Zero)
                        {
                            throw new OnnxRuntimeException(ErrorCode.InvalidArgument,
                                                   "TypeInfo cast to SequenceTypeInfo failed. The object does not represent a sequence");
                        }
                        _sequenceOrOptional = new OrtSequenceOrOptionalTypeInfo(sequenceInfo);
                    }
                    break;
                case OnnxValueType.ONNX_TYPE_MAP:
                    {
                        NativeApiStatus.VerifySuccess(NativeMethods.OrtCastTypeInfoToMapTypeInfo(handle, out IntPtr mapInfo));
                        if (mapInfo == IntPtr.Zero)
                        {
                            throw new OnnxRuntimeException(ErrorCode.InvalidArgument,
                                                           "TypeInfo cast to MapTypeInfo failed. The object does not represent a map");
                        }
                        _mapTypeInfo = new OrtMapTypeInfo(mapInfo);
                    }
                    break;
                case OnnxValueType.ONNX_TYPE_OPTIONAL:
                    {
                        NativeApiStatus.VerifySuccess(NativeMethods.OrtCastTypeInfoToOptionalTypeInfo(handle, out IntPtr optionalInfo));
                        if (optionalInfo == IntPtr.Zero)
                        {
                            throw new OnnxRuntimeException(ErrorCode.InvalidArgument,
                                                           "TypeInfo cast to OptionalTypeInfo failed. The object does not represent a optional");
                        }
                        _sequenceOrOptional = new OrtSequenceOrOptionalTypeInfo(optionalInfo);
                    }
                    break;
                default:
                    throw new OnnxRuntimeException(ErrorCode.NotImplemented, $"OnnxValueType: {OnnxType} is not supported here");
            }
        }
 
        /// <summary>
        /// Represents OnnxValueType of the OrtTypeInfo
        /// </summary>
        /// <value>OnnxValueType</value>
        public OnnxValueType OnnxType { get; private set; }
 
        /// <summary>
        /// This property returns the tensor type and shape information from the OrtTypeInfo
        /// iff this OrtTypeInfo represents a tensor.
        /// </summary>
        /// <exception cref="OnnxRuntimeException"></exception>
        /// <value>Instance of OrtTensorTypeAndShapeInfo</value>
        public OrtTensorTypeAndShapeInfo TensorTypeAndShapeInfo
        {
            get
            {
                if (OnnxType != OnnxValueType.ONNX_TYPE_TENSOR &&
                    OnnxType != OnnxValueType.ONNX_TYPE_SPARSETENSOR)
                {
                    throw new OnnxRuntimeException(ErrorCode.InvalidArgument, "TypeInfo does not represent a tensor/sparsetensor");
                }
                return _tensorTypeAndShape.Value;
            }
        }
 
        /// <summary>
        /// Sequence type information from the OrtTypeInfo iff this OrtTypeInfo represents a sequence.
        /// </summary>
        /// <returns>Instance of OrtSequenceTypeInfo</returns>
        /// <exception cref="OnnxRuntimeException"></exception>
        public OrtSequenceOrOptionalTypeInfo SequenceTypeInfo
        {
            get
            {
                if (OnnxType != OnnxValueType.ONNX_TYPE_SEQUENCE)
                {
                    throw new OnnxRuntimeException(ErrorCode.InvalidArgument, "TypeInfo does not represent a sequence");
                }
                return _sequenceOrOptional.Value;
            }
        }
 
        /// <summary>
        /// Represents MapTypeInfo from the OrtTypeInfo iff this OrtTypeInfo represents a map.
        /// </summary>
        /// <value>Instance of MapTypeInfo</value>
        /// <exception cref="OnnxRuntimeException"></exception>
        public OrtMapTypeInfo MapTypeInfo
        {
            get
            {
                if (OnnxType != OnnxValueType.ONNX_TYPE_MAP)
                {
                    throw new OnnxRuntimeException(ErrorCode.InvalidArgument, "TypeInfo does not represent a map");
                }
                return _mapTypeInfo.Value;
            }
        }
 
        /// <summary>
        /// Fetches OptionalTypeInfo from the OrtTypeInfo iff this OrtTypeInfo represents a optional type.
        /// </summary>
        /// <returns>Instance of OptionalTypeInfo</returns>
        /// <exception cref="OnnxRuntimeException"></exception>
        public OrtSequenceOrOptionalTypeInfo OptionalTypeInfo
        {
            get
            {
                if (OnnxType != OnnxValueType.ONNX_TYPE_OPTIONAL)
                {
                    throw new OnnxRuntimeException(ErrorCode.InvalidArgument, "TypeInfo does not represent a optional");
                }
                return _sequenceOrOptional.Value;
            }
        }
    }
 
    /// <summary>
    /// This struct represents type and shape information for a tensor.
    /// It may describe a tensor type that is a model input or output or
    /// an information that can be extracted from a tensor in OrtValue.
    /// 
    /// </summary>
    public struct OrtTensorTypeAndShapeInfo
    {
        internal OrtTensorTypeAndShapeInfo(IntPtr handle)
        {
            NativeApiStatus.VerifySuccess(NativeMethods.OrtGetTensorElementType(handle, out IntPtr elementType));
            ElementDataType = (TensorElementType)elementType;
 
            NativeApiStatus.VerifySuccess(NativeMethods.OrtGetTensorShapeElementCount(handle, out UIntPtr count));
            ElementCount = (long)count;
 
            NativeApiStatus.VerifySuccess(NativeMethods.OrtGetDimensionsCount(handle, out UIntPtr dimCount));
 
            Shape = new long[(uint)dimCount];
            NativeApiStatus.VerifySuccess(NativeMethods.OrtGetDimensions(handle, Shape, dimCount));
        }
 
        /// <summary>
        /// Fetches tensor element data type
        /// </summary>
        /// <value>enum value for the data type</value>
        public TensorElementType ElementDataType { get; private set; }
 
        /// <summary>
        /// Returns true if this data element is a string
        /// </summary>
        public bool IsString
        {
            get
            {
                return ElementDataType == TensorElementType.String;
            }
        }
 
        /// <summary>
        /// Fetches tensor element count based on the shape information.
        /// </summary>
        /// <returns>number of typed tensor elements</returns>
        public long ElementCount { get; private set; }
 
        /// <summary>
        /// Fetches shape dimension count (rank) for the tensor.
        /// </summary>
        /// <returns>dim count</returns>
        public int DimensionsCount { get { return Shape.Length; } }
 
        /// <summary>
        /// Tensor dimensions.
        /// </summary>
        /// <value>array of dims</value>
        public long[] Shape { get; private set; }
    }
 
    /// <summary>
    /// Represents Sequence type information.
    /// </summary>
    public struct OrtSequenceOrOptionalTypeInfo
    {
        internal OrtSequenceOrOptionalTypeInfo(IntPtr handle)
        {
            NativeApiStatus.VerifySuccess(NativeMethods.OrtGetSequenceElementType(handle, out IntPtr typeInfo));
            try
            {
                ElementType = new OrtTypeInfo(typeInfo);
            }
            finally
            {
                NativeMethods.OrtReleaseTypeInfo(typeInfo);
            }
        }
 
        /// <summary>
        /// Returns type information for the element type of the sequence
        /// </summary>
        /// <value>OrtTypeInfo</value>
        public OrtTypeInfo ElementType { get; private set; }
    }
 
    /// <summary>
    /// Represents Map input/output information.
    /// 
    /// Maps are represented at run time by a tensor of primitive types
    /// and values are represented either by Tensor/Sequence/Optional or another map.
    /// </summary>
    public struct OrtMapTypeInfo
    {
        internal OrtMapTypeInfo(IntPtr handle)
        {
            NativeApiStatus.VerifySuccess(NativeMethods.OrtGetMapKeyType(handle, out IntPtr tensorElementType));
            KeyType = (TensorElementType)tensorElementType;
            NativeApiStatus.VerifySuccess(NativeMethods.OrtGetMapValueType(handle, out IntPtr typeInfo));
            try
            {
                ValueType = new OrtTypeInfo(typeInfo);
            }
            finally
            {
                NativeMethods.OrtReleaseTypeInfo(typeInfo);
            }
        }
 
        /// <summary>
        /// Returns KeyType which is the data type of the keys tensor
        /// </summary>
        /// <value>OrtTypeInfo</value>
        public TensorElementType KeyType { get; private set; }
 
        /// <summary>
        /// Returns an instance of OrtTypeInfo describing the value type for the map
        /// </summary>
        /// <value>OrtTypeInfo</value>
        public OrtTypeInfo ValueType { get; private set; }
    }
}