Исходный код СУБД 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "onnxruntime_pybind_mlvalue.h"
#include "python/onnxruntime_pybind_state_common.h"
#include "pybind11/numpy.h"
 
#define NO_IMPORT_ARRAY
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#define PY_ARRAY_UNIQUE_SYMBOL onnxruntime_python_ARRAY_API
#include "python/numpy_helper.h"
 
#include "core/framework/tensor_shape.h"
#include "core/framework/tensor.h"
#include "core/framework/sparse_tensor.h"
#include "core/framework/allocator.h"
#include "core/framework/data_types.h"
 
#include "core/framework/data_types_internal.h"
#include "core/providers/get_execution_providers.h"
#include "core/framework/kernel_registry.h"
#include "core/framework/provider_options_utils.h"
#include "core/session/provider_bridge_ort.h"
 
namespace onnxruntime {
namespace python {
 
namespace py = pybind11;
using namespace onnxruntime::logging;
 
#if !defined(DISABLE_SPARSE_TENSORS)
 
namespace {
// Create a pybind11:dtype numpy instance using ONNX Tensor Element Type
template <typename T>
struct MakeDType {
  py::dtype operator()() const {
    return py::dtype::of<T>();
  }
};
 
/// <summary>
/// The function creates a numpy array that points to
/// data stored within the corresponding tensor. Parent object
/// holds a reference to the object that owns the data so it
/// does not disappear.
/// </summary>
/// <returns>numpy array</returns>
py::array MakeNumpyArrayFromIndices(const Tensor& indices, const py::object& parent) {
  // See https://github.com/pybind/pybind11/issues/2271 for more information on parent
  py::array result(indices.Shape().GetDims(), indices.Data<int64_t>(), parent);
  assert(!result.owndata());
  // Set a read-only flag
  PyArray_CLEARFLAGS(reinterpret_cast<PyArrayObject*>(result.ptr()), NPY_ARRAY_WRITEABLE);
  return result;
}
 
}  // namespace
 
class PySparseCooView : public SparseTensor::CooView {
  py::object parent_;
 
 public:
  PySparseCooView(const SparseTensor::CooView& view, const py::object& parent) noexcept
      : SparseTensor::CooView(view), parent_(parent) {}
};
 
class PySparseCsrView : public SparseTensor::CsrView {
  py::object parent_;
 
 public:
  PySparseCsrView(const SparseTensor::CsrView& view, const py::object& parent) noexcept
      : SparseTensor::CsrView(view), parent_(parent) {}
};
 
class PySparseBlockSparseView : public SparseTensor::BlockSparseView {
  py::object parent_;
 
 public:
  PySparseBlockSparseView(const SparseTensor::BlockSparseView& view, const py::object& parent) noexcept
      : SparseTensor::BlockSparseView(view), parent_(parent) {}
};
 
#endif  // !defined(DISABLE_SPARSE_TENSORS)
 
void addSparseTensorMethods(pybind11::module& m) {
  // this is exported via __init__.py so has to exist
  py::enum_<OrtSparseFormat>(m, "OrtSparseFormat")
      .value("ORT_SPARSE_UNDEFINED", OrtSparseFormat::ORT_SPARSE_UNDEFINED)
      .value("ORT_SPARSE_COO", OrtSparseFormat::ORT_SPARSE_COO)
      .value("ORT_SPARSE_CSRC", OrtSparseFormat::ORT_SPARSE_CSRC)
      .value("ORT_SPARSE_BLOCK_SPARSE", OrtSparseFormat::ORT_SPARSE_BLOCK_SPARSE);
 
#if !defined(DISABLE_SPARSE_TENSORS)
  py::class_<PySparseCooView>(m, "SparseCooView")
      // Returns a numpy array of COO indices backed by Sparse Tensor memory
      // be aware that indices may reside on GPU if Sparse Tensor is on GPU
      .def("indices", [](py::object self) -> py::array {
        auto* view = self.cast<const PySparseCooView*>();
        const auto& indices = view->Indices();
        return MakeNumpyArrayFromIndices(indices, self);
      });
 
  py::class_<PySparseCsrView>(m, "SparseCsrView")
      .def("inner", [](py::object self) -> py::array {
        auto* view = self.cast<const PySparseCsrView*>();
        const auto& indices = view->Inner();
        return MakeNumpyArrayFromIndices(indices, self);
      })
      .def("outer", [](py::object self) -> py::array {
        auto* view = self.cast<const PySparseCsrView*>();
        const auto& indices = view->Outer();
        return MakeNumpyArrayFromIndices(indices, self);
      });
 
  py::class_<PySparseBlockSparseView>(m, "SparseBlockSparseView")
      .def("indices", [](py::object self) -> py::array {
        auto* view = self.cast<const PySparseBlockSparseView*>();
        const auto& indices = view->Indices();
        return MakeNumpyArrayFromIndices(indices, self);
      });
 
  py::class_<PySparseTensor> sparse_bind(m, "SparseTensor");
  // Factory method to create a COO Sparse Tensor from numpy arrays acting as backing storage.
  // Numeric arrays memory is used as is with reference count increment. All other supported
  // types are copied and supported only on CPU.
  // Use numpy.ascontiguousarray() to obtain contiguous array of values and indices if necessary
  // py_dense_shape - numpy dense shape of the sparse tensor
  // py_values - contiguous and homogeneous numpy array of values
  // py_indices - contiguous numpy array of int64_t indices
  // ort_device - where the value and indices buffers are allocated. For non-primitive types,
  //              only cpu device is supported. There is not a way to verify that ort_device
  //              accurately describes the memory that is backing values and indices.
  sparse_bind
      .def_static("sparse_coo_from_numpy",
                  [](const std::vector<int64_t>& py_dense_shape,
                     const py::array& py_values,
                     const py::array_t<int64_t>& py_indices,
                     const OrtDevice& ort_device) -> std::unique_ptr<PySparseTensor> {
                    if (1 != py_values.ndim()) {
                      ORT_THROW("Expecting values 1-D numpy values array for COO format. Got dims: ", py_values.ndim());
                    }
 
                    TensorShape dense_shape(py_dense_shape);
                    auto values_type = GetNumpyArrayType(py_values);
                    auto ml_type = NumpyTypeToOnnxRuntimeTensorType(values_type);
 
                    std::unique_ptr<PySparseTensor> result;
                    if (IsNumericNumpyType(values_type)) {
                      if (!PyArray_ISCONTIGUOUS(reinterpret_cast<PyArrayObject*>(py_values.ptr()))) {
                        throw std::runtime_error("Require contiguous numpy array of values");
                      }
 
                      if (!PyArray_ISCONTIGUOUS(reinterpret_cast<PyArrayObject*>(py_indices.ptr()))) {
                        throw std::runtime_error("Require contiguous numpy array of indices");
                      }
 
                      // create references to make sure storage does not disappear
                      std::vector<py::object> reference_holders = {py_values, py_indices};
                      OrtMemoryInfo mem_info = GetMemoryInfoPerDeviceType(ort_device);
                      TensorShape values_shape{py_values.size()};
                      auto sparse_tensor = std::make_unique<SparseTensor>(ml_type, dense_shape, values_shape,
                                                                          const_cast<void*>(py_values.data()), mem_info);
                      auto index_span = gsl::make_span(const_cast<int64_t*>(py_indices.data()), py_indices.size());
                      ORT_THROW_IF_ERROR(sparse_tensor->UseCooIndices(index_span));
                      result = std::make_unique<PySparseTensor>(std::move(sparse_tensor), std::move(reference_holders));
                    } else if (values_type == NPY_UNICODE || values_type == NPY_STRING) {
                      if (ort_device.Type() != OrtDevice::CPU) {
                        throw std::runtime_error("Only CPU based devices are supported for non-numeric datatypes");
                      }
                      auto sparse_tensor = std::make_unique<SparseTensor>(ml_type, dense_shape, GetAllocator());
                      auto mutator = sparse_tensor->MakeCooData(py_values.size(), py_indices.size());
                      CopyDataToTensor(py_values, values_type, mutator.Values());
                      CopyDataToTensor(py_indices, GetNumpyArrayType(py_indices), mutator.Indices());
                      result = std::make_unique<PySparseTensor>(std::move(sparse_tensor));
                    } else {
                      ORT_THROW("Unsupported values data type: ", values_type);
                    }
                    return result;
                  })
      // Factory method to create a CSR Sparse Tensor from numpy arrays acting as backing storage.
      // Numeric arrays memory is used as is with reference count increment. All other supported
      // types are copied and supported only on CPU.
      // Use numpy.ascontiguousarray() to obtain contiguous array of values and indices if necessary
      // py_dense_shape - numpy dense shape of the sparse tensor
      // py_values - contiguous and homogeneous numpy array of values
      // py_inner_indices - contiguous numpy array of int64_t indices
      // py_outer_indices - contiguous numpy array of int64_t indices
      // ort_device - where the value and indices buffers are allocated. For non-primitive types,
      //              only cpu device is supported. There is not a way to verify that ort_device
      //              accurately describes the memory that is backing values and indices.
      .def_static(
          "sparse_csr_from_numpy",
          [](const std::vector<int64_t>& py_dense_shape,
             const py::array& py_values,
             const py::array_t<int64_t>& py_inner_indices,
             const py::array_t<int64_t>& py_outer_indices,
             const OrtDevice& ort_device) -> std::unique_ptr<PySparseTensor> {
            if (1 != py_values.ndim() || 1 != py_inner_indices.ndim() || 1 != py_outer_indices.ndim()) {
              ORT_THROW("Expecting all data to be 1-D numpy arrays for CSR format.");
            }
 
            TensorShape dense_shape(py_dense_shape);
            auto values_type = GetNumpyArrayType(py_values);
            auto ml_type = NumpyTypeToOnnxRuntimeTensorType(values_type);
 
            std::unique_ptr<PySparseTensor> result;
            if (IsNumericNumpyType(values_type)) {
              if (!PyArray_ISCONTIGUOUS(reinterpret_cast<PyArrayObject*>(py_values.ptr()))) {
                throw std::runtime_error("Require contiguous numpy array of values");
              }
 
              if (!PyArray_ISCONTIGUOUS(reinterpret_cast<PyArrayObject*>(py_inner_indices.ptr()))) {
                throw std::runtime_error("Require contiguous numpy array of indices");
              }
 
              if (!PyArray_ISCONTIGUOUS(reinterpret_cast<PyArrayObject*>(py_outer_indices.ptr()))) {
                throw std::runtime_error("Require contiguous numpy array of indices");
              }
 
              // go ahead and create references to make sure storage does not disappear
              std::vector<py::object> reference_holders = {py_values, py_inner_indices, py_outer_indices};
              OrtMemoryInfo mem_info = GetMemoryInfoPerDeviceType(ort_device);
              TensorShape values_shape{py_values.size()};
              auto sparse_tensor = std::make_unique<SparseTensor>(ml_type, dense_shape, values_shape,
                                                                  const_cast<void*>(py_values.data()), mem_info);
              auto inner_span = gsl::make_span<int64_t>(const_cast<int64_t*>(py_inner_indices.data()), py_inner_indices.size());
              auto outer_span = gsl::make_span<int64_t>(const_cast<int64_t*>(py_outer_indices.data()), py_outer_indices.size());
              ORT_THROW_IF_ERROR(sparse_tensor->UseCsrIndices(inner_span, outer_span));
              result = std::make_unique<PySparseTensor>(std::move(sparse_tensor), std::move(reference_holders));
            } else if (values_type == NPY_UNICODE || values_type == NPY_STRING) {
              if (ort_device.Type() != OrtDevice::CPU) {
                throw std::runtime_error("Only CPU based devices are supported for non-numeric datatypes");
              }
              auto sparse_tensor = std::make_unique<SparseTensor>(ml_type, dense_shape, GetAllocator());
              auto mutator = sparse_tensor->MakeCsrData(py_values.size(), py_inner_indices.size(), py_outer_indices.size());
              CopyDataToTensor(py_values, values_type, mutator.Values());
              CopyDataToTensor(py_inner_indices, GetNumpyArrayType(py_inner_indices), mutator.Inner());
              CopyDataToTensor(py_outer_indices, GetNumpyArrayType(py_outer_indices), mutator.Outer());
              result = std::make_unique<PySparseTensor>(std::move(sparse_tensor));
            } else {
              ORT_THROW("Unsupported values data type: ", values_type);
            }
 
            return result;
          })
      // Factory method to create a BlockSparse Tensor from numpy arrays acting as backing storage.
      // Numeric arrays memory is used as is with reference count increment. All other supported
      // types are copied and supported only on CPU.
      // Use numpy.ascontiguousarray() to obtain contiguous array of values and indices if necessary
      // py_dense_shape - numpy dense shape of the sparse tensor
      // ort_device - desribes the allocation. Only primitive types allocations can be mapped to
      // py_values - contiguous and homogeneous numpy array of values
      // py_indices - contiguous numpy array of int32_t indices
      // ort_device - where the value and indices buffers are allocated. For non-primitive types,
      //              only cpu device is supported. There is not a way to verify that ort_device
      //              accurately describes the memory that is backing values and indices.
      .def_static(
          "blocksparse_from_numpy",
          [](const std::vector<int64_t>& py_dense_shape,
             const py::array& py_values,
             const py::array_t<int32_t>& py_indices,
             const OrtDevice& ort_device) -> std::unique_ptr<PySparseTensor> {
            TensorShape dense_shape(py_dense_shape);
            TensorShape values_shape = GetShape(py_values);
            TensorShape index_shape = GetShape(py_indices);
            auto values_type = GetNumpyArrayType(py_values);
            auto ml_type = NumpyTypeToOnnxRuntimeTensorType(values_type);
 
            std::unique_ptr<PySparseTensor> result;
            if (IsNumericNumpyType(values_type)) {
              if (!PyArray_ISCONTIGUOUS(reinterpret_cast<PyArrayObject*>(py_values.ptr()))) {
                throw std::runtime_error("Require contiguous numpy array of values");
              }
 
              if (!PyArray_ISCONTIGUOUS(reinterpret_cast<PyArrayObject*>(py_indices.ptr()))) {
                throw std::runtime_error("Require contiguous numpy array of indices");
              }
 
              // create references to make sure storage does not disappear
              std::vector<py::object> reference_holders = {py_values, py_indices};
              OrtMemoryInfo mem_info = GetMemoryInfoPerDeviceType(ort_device);
              auto sparse_tensor = std::make_unique<SparseTensor>(ml_type, dense_shape, values_shape,
                                                                  const_cast<void*>(py_values.data()), mem_info);
              ORT_THROW_IF_ERROR(sparse_tensor->UseBlockSparseIndices(index_shape, const_cast<int32_t*>(py_indices.data())));
              result = std::make_unique<PySparseTensor>(std::move(sparse_tensor), std::move(reference_holders));
            } else if (values_type == NPY_UNICODE || values_type == NPY_STRING) {
              if (ort_device.Type() != OrtDevice::CPU) {
                throw std::runtime_error("Only CPU based devices are supported for non-numeric datatypes");
              }
              auto sparse_tensor = std::make_unique<SparseTensor>(ml_type, dense_shape, GetAllocator());
              auto mutator = sparse_tensor->MakeBlockSparseData(values_shape, index_shape);
              CopyDataToTensor(py_values, values_type, mutator.Values());
              CopyDataToTensor(py_indices, GetNumpyArrayType(py_indices), mutator.Indices());
              result = std::make_unique<PySparseTensor>(std::move(sparse_tensor));
            } else {
              ORT_THROW("Unsupported values data type: ", values_type);
            }
            return result;
          })
      // Returns a numpy array that is backed by SparseTensor values memory
      // be aware that it may be on GPU
      .def("values", [](py::object self) -> py::array {
        auto* py_tensor = self.cast<const PySparseTensor*>();
        const SparseTensor& sparse_tensor = py_tensor->Instance();
        if (sparse_tensor.Format() == SparseFormat::kUndefined) {
          ORT_THROW("This sparse tensor instance does not contain data");
        }
        if (sparse_tensor.IsDataTypeString()) {
          // Strings can not be on GPU and require conversion UTF-8 to Python UNICODE
          // We need to create a copy.
          return StringTensorToNumpyArray(sparse_tensor.Values());
        } else {
          utils::MLTypeCallDispatcher<float, double, int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t>
              t_disp(sparse_tensor.GetElementType());
          auto dtype = t_disp.InvokeRet<py::dtype, MakeDType>();
          const auto& values = sparse_tensor.Values();
          // See https://github.com/pybind/pybind11/issues/2271
          py::array result(dtype, values.Shape().GetDims(), values.DataRaw(), self);
          assert(!result.owndata());
          // Set a read-only flag
          PyArray_CLEARFLAGS(reinterpret_cast<PyArrayObject*>(result.ptr()), NPY_ARRAY_WRITEABLE);
          return result;
        }
      })
      // Returns a Coo view of data
      .def("get_coo_data", [](const PySparseTensor* py_tensor) -> std::unique_ptr<PySparseCooView> {
        const SparseTensor& sparse_tensor = py_tensor->Instance();
        if (sparse_tensor.Format() != SparseFormat::kCoo) {
          ORT_THROW("This sparse tensor does not contain COO format");
        }
        return std::make_unique<PySparseCooView>(sparse_tensor.AsCoo(), py::cast(*py_tensor));
      })
      // Returns a CSR view of data
      .def("get_csrc_data", [](const PySparseTensor* py_tensor) -> std::unique_ptr<PySparseCsrView> {
        const SparseTensor& sparse_tensor = py_tensor->Instance();
        if (sparse_tensor.Format() != SparseFormat::kCsrc) {
          ORT_THROW("This sparse tensor does not contain CSR(C) format");
        }
        return std::make_unique<PySparseCsrView>(sparse_tensor.AsCsr(), py::cast(*py_tensor));
      })
      // Returns a blocksparse view of data
      .def("get_blocksparse_data", [](const PySparseTensor* py_tensor) -> std::unique_ptr<PySparseBlockSparseView> {
        const SparseTensor& sparse_tensor = py_tensor->Instance();
        if (sparse_tensor.Format() != SparseFormat::kBlockSparse) {
          ORT_THROW("This sparse tensor does not contain BlockSparse format");
        }
        return std::make_unique<PySparseBlockSparseView>(sparse_tensor.AsBlockSparse(), py::cast(*py_tensor));
      })
 
  /// This will copy SparseTensor into a new instance on a specified CUDA device or throw:
  /// - if this sparse tensor contains strings
  /// - if this sparse tensor is already on GPU
  /// - if CUDA is not present in this build
  /// - if the specified device is not valid
#ifdef USE_CUDA
      .def("to_cuda", [](const PySparseTensor* py_tensor, const OrtDevice& ort_device) -> std::unique_ptr<PySparseTensor> {
        const SparseTensor& sparse_tensor = py_tensor->Instance();
        if (sparse_tensor.IsDataTypeString()) {
          ORT_THROW("Can not copy string tensor to GPU devices.");
        }
        if (sparse_tensor.Location().device.Type() == OrtDevice::GPU) {
          ORT_THROW("This sparse_tensor is already allocated on cuda. Cross device copy not supported.");
        }
        if (!IsCudaDeviceIdValid(logging::LoggingManager::DefaultLogger(), ort_device.Id())) {
          ORT_THROW("The provided device id doesn't match any available GPUs on the machine: ", ort_device.Id());
        }
        auto cuda_allocator = GetCudaAllocator(ort_device.Id());
        auto gpu_transfer = GetGPUDataTransfer();
        auto dest_tensor = std::make_unique<SparseTensor>(sparse_tensor.DataType(), sparse_tensor.DenseShape(),
                                                          std::move(cuda_allocator));
        ORT_THROW_IF_ERROR(sparse_tensor.Copy(*gpu_transfer, *dest_tensor));
        auto result = std::make_unique<PySparseTensor>(std::move(dest_tensor));
        return result;
#else
      .def("to_cuda", [](const PySparseTensor*, const OrtDevice&) {
        ORT_THROW("Cuda is not available in this build");
#endif  // USE_CUDA
      })
      .def("dense_shape", [](const PySparseTensor* py_tensor) -> py::list {
        const SparseTensor& st = py_tensor->Instance();
        const auto dims = st.DenseShape().GetDims();
        // We create a copy of dimensions, it is small
        py::list py_dims;
        for (auto d : dims) {
          py_dims.append(d);
        }
        return py_dims;
      })
      .def("device_name", [](const PySparseTensor* py_tensor) -> std::string {
        return std::string(GetDeviceName(py_tensor->Instance().Location().device));
      })
      .def("data_type", [](const PySparseTensor* py_tensor) -> std::string {
        const SparseTensor& tensor = py_tensor->Instance();
        const auto elem_type = tensor.GetElementType();
        const auto* type_proto = DataTypeImpl::SparseTensorTypeFromONNXEnum(elem_type)->GetTypeProto();
        if (type_proto == nullptr) {
          ORT_THROW("Unknown type of SparseTensor: ", tensor.DataType());
        }
        return *ONNX_NAMESPACE::Utils::DataTypeUtils::ToType(*type_proto);
      })
      // pybind apparently has a bug with returning enums from def_property_readonly or methods
      // returning a method object instead of the enumeration value
      // so we are using def_property and throw on a potential modification
      .def_property("format", [](const PySparseTensor* py_tensor) -> OrtSparseFormat {
        const SparseTensor& tensor = py_tensor->Instance();
        auto retval = OrtSparseFormat::ORT_SPARSE_UNDEFINED;
        switch (tensor.Format()) {
          case SparseFormat::kUndefined:
            break;
          case SparseFormat::kCoo:
            retval = OrtSparseFormat::ORT_SPARSE_COO;
            break;
          case SparseFormat::kCsrc:
            retval = OrtSparseFormat::ORT_SPARSE_CSRC;
            break;
          case SparseFormat::kBlockSparse:
            retval = OrtSparseFormat::ORT_SPARSE_BLOCK_SPARSE;
            break;
          default:
            throw std::runtime_error("Can't switch on FormatFlags()");
        }
        return retval; }, [](PySparseTensor*, OrtSparseFormat) -> void { throw std::runtime_error("This is a readonly property"); });
#endif  // !defined(DISABLED_SPARSE_TENSORS)
}
 
}  // namespace python
}  // namespace onnxruntime