Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
 
#include "onnxruntime_pybind.h"  // must use this for the include of <pybind11/pybind11.h>
#include <pybind11/iostream.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
 
#include "core/common/logging/logging.h"
#include "core/common/logging/sinks/clog_sink.h"
#include "core/common/logging/sinks/cerr_sink.h"
#include "core/session/environment.h"
#include "core/framework/ort_value.h"
#include "core/session/inference_session.h"
 
#include <variant>
 
PYBIND11_MAKE_OPAQUE(std::vector<OrtValue>);
 
namespace onnxruntime {
namespace python {
 
extern const char* PYTHON_ORTVALUE_OBJECT_NAME;
extern const char* PYTHON_ORTVALUE_NATIVE_OBJECT_ATTR;
 
bool IsNumericNumpyArray(const pybind11::object& py_object);
 
bool IsNumpyArray(pybind11::object& obj);
 
int GetNumpyArrayType(const pybind11::object& obj);
 
bool IsNumericDType(const pybind11::dtype& dtype);
 
TensorShape GetShape(const pybind11::array& arr);
 
int OnnxRuntimeTensorToNumpyType(const DataTypeImpl* tensor_type);
 
MLDataType NumpyTypeToOnnxRuntimeTensorType(int numpy_type);
 
MLDataType OnnxTypeToOnnxRuntimeTensorType(int onnx_element_type);
 
using MemCpyFunc = std::function<void(void*, const void*, size_t)>;
using DataTransferAlternative = std::variant<const DataTransferManager*, MemCpyFunc>;
 
// helpers to get allocator and IDataTransfer from Environment for plugin EP
AllocatorPtr GetSharedAllocator(const OrtDevice& device);
MemCpyFunc CreateDataTransferMemCpy(const OrtDevice& src_device, const OrtDevice& dst_device);
 
void CpuToCpuMemCpy(void*, const void*, size_t);
 
void CopyDataToTensor(const pybind11::array& py_array, int npy_type, Tensor& tensor,
                      const MemCpyFunc& mem_cpy_to_device = CpuToCpuMemCpy);
 
pybind11::object AddTensorAsPyObj(const OrtValue& val, const DataTransferManager* data_transfer_manager,
                                  const std::unordered_map<OrtDevice, MemCpyFunc>* mem_cpy_to_host_functions);
 
pybind11::object GetPyObjectFromSparseTensor(size_t pos, const OrtValue& ort_value,
                                             const DataTransferManager* data_transfer_manager);
 
pybind11::object AddNonTensorAsPyObj(const OrtValue& val,
                                     const DataTransferManager* data_transfer_manager,
                                     const std::unordered_map<OrtDevice, MemCpyFunc>* mem_cpy_to_host_functions);
 
OrtMemoryInfo GetMemoryInfoPerDeviceType(const OrtDevice& ort_device);
 
int32_t GetTensorProtoType(const OrtValue& ort_value);
 
#ifdef USE_CUDA
 
void CpuToCudaMemCpy(void* dst, const void* src, size_t num_bytes);
 
void CudaToCpuMemCpy(void* dst, const void* src, size_t num_bytes);
 
const std::unordered_map<OrtDevice, MemCpyFunc>* GetCudaToHostMemCpyFunction(const OrtDevice&);
 
bool IsCudaDeviceIdValid(const onnxruntime::logging::Logger& logger, int id);
 
AllocatorPtr GetCudaAllocator(OrtDevice::DeviceId id);
 
std::unique_ptr<IDataTransfer> GetGPUDataTransfer();
 
#endif
 
#ifdef USE_DML
 
AllocatorPtr GetDmlAllocator(OrtDevice::DeviceId id);
 
void CpuToDmlMemCpy(void* dst, const void* src, size_t num_bytes);
 
void DmlToCpuMemCpy(void* dst, const void* src, size_t num_bytes);
 
const std::unordered_map<OrtDevice, MemCpyFunc>* GetDmlToHostMemCpyFunction(const OrtDevice&);
 
#endif
 
#ifdef USE_MIGRAPHX
 
void CpuToMIGraphXMemCpy(void* dst, const void* src, size_t num_bytes);
 
void MIGraphXToCpuMemCpy(void* dst, const void* src, size_t num_bytes);
 
const std::unordered_map<OrtDevice, MemCpyFunc>* GetMIGraphXToHostMemCpyFunction(const OrtDevice&);
 
AllocatorPtr GetMIGraphXAllocator(OrtDevice::DeviceId id);
 
#endif
 
#ifdef USE_CANN
 
void CpuToCannMemCpy(void* dst, const void* src, size_t num_bytes);
 
void CannToCpuMemCpy(void* dst, const void* src, size_t num_bytes);
 
const std::unordered_map<OrtDevice, MemCpyFunc>* GetCannToHostMemCpyFunction();
 
bool IsCannDeviceIdValid(const onnxruntime::logging::Logger& logger, int id);
 
AllocatorPtr GetCannAllocator(OrtDevice::DeviceId id);
 
#endif
 
void CreateGenericMLValue(const onnxruntime::InputDefList* input_def_list, const AllocatorPtr& alloc,
                          const std::string& name_input, const pybind11::object& value, OrtValue* p_mlvalue,
                          bool accept_only_numpy_array = false, bool use_numpy_data_memory = true,
                          const MemCpyFunc& mem_cpy_to_device = CpuToCpuMemCpy);
 
/// @param zero_copy_non_owning  When false (default), CPU tensors that do not own
///   their buffer are copied to a new numpy array to prevent dangling pointers
///   (e.g. when a model output aliases a numpy input array).  Set to true ONLY
///   when the caller explicitly manages the backing memory lifetime — currently
///   this is limited to OrtValue.numpy(), where the user holds the OrtValue
///   Python object and is responsible for keeping it alive.
pybind11::object GetPyObjFromTensor(const OrtValue& rtensor,
                                    const DataTransferManager* data_transfer_manager = nullptr,
                                    const std::unordered_map<OrtDevice, MemCpyFunc>* mem_cpy_to_host_functions = nullptr,
                                    bool zero_copy_non_owning = false);
 
// Update the tensor data in an OrtValue in-place from another OrtValue.
// Both OrtValues must contain tensors of the same data type and size.
// This function supports various device-to-device transfers.
void UpdateOrtValueInplace(OrtValue& dst, const OrtValue& src);
 
// The below two functions are used to convert OrtValue to numpy arrays
 
/// <summary>
/// This function operates on string tensors. Strings are always
/// copied to python and converted to UTF-16/UCS-4/32 depending on the platform.
/// This is accomplished using py::cast()
///
/// It is an error to pass a non-tensor or a non-string tensor to this function.
/// </summary>
/// <param name="tensor">Tensor that contains strings</param>
/// <returns>py::array object</returns>
pybind11::array StringTensorToNumpyArray(const Tensor& tensor);
 
/// <summary>
/// Creates a numpy array with shape over OrtValue memory. Numpy array
/// does not own the memory, but it holds a copy or OrtValue in a py::capsule.
/// OrtValue is destroyed when the numpy array is garbage collected.
/// This is used when the OrtValue memory is on CPU.
/// </summary>
/// <param name="ort_value">OrtValue with data</param>
/// <returns>numpy array</returns>
pybind11::array PrimitiveTensorToNumpyOverOrtValue(const OrtValue& ort_value);
 
/// <summary>
/// Creates a numpy array with shape with a copy of OrtValue data.
/// This function is used when the OrtValue memory is not on CPU.
/// </summary>
/// <param name="ort_value">Source memory that is not on CPU.</param>
/// <param name="data_transfer">a variant encapsulating alternatives for copying data</param>
/// <returns></returns>
pybind11::array PrimitiveTensorToNumpyFromDevice(const OrtValue& ort_value,
                                                 const DataTransferAlternative& data_transfer);
 
template <class T>
struct DecRefFn {
  void operator()(T* pyobject) const {
    Py_XDECREF(pyobject);
  }
};
 
template <class T>
using UniqueDecRefPtr = std::unique_ptr<T, DecRefFn<T>>;
 
}  // namespace python
}  // namespace onnxruntime