Исходный код СУБД 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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include <iostream>
 
#include "core/framework/data_types.h"
#include "core/framework/execution_providers.h"
#include "core/framework/op_kernel.h"
#include "core/framework/session_state.h"
#include "core/graph/graph_viewer.h"
#include "core/graph/model.h"
#include "core/graph/op.h"
#include "core/providers/cpu/cpu_execution_provider.h"
#include "core/session/inference_session.h"
#include "gtest/gtest.h"
 
#include "test/providers/provider_test_utils.h"
#include "asserts.h"
#include "test/unittest_util/framework_test_utils.h"
 
using namespace ONNX_NAMESPACE;
using namespace onnxruntime::common;
 
const char* experimental_using_opaque = R"DOC(
The operator constructs an instance of sparse one dimensional tensor
represented by a SparseTensorSample type. It uses 3 supplied inputs each
in a form of a single dimensional tensor.
)DOC";
 
namespace onnxruntime {
// We will use this class to implement Sparse Tensor and
// register it as an Opaque type emulating some experimental type
 
/**
 * @brief This class implements a SparseTensor as an example
 *        of using custom experimental type outside of ONNXRuntime.
 *
 * @details The class captures the 3 necessary elements of a Sparse Tensor
 *          values - a vector of non-zero sparse tensor values
 *          indices - a vector of indices of non zero values
 *          shape   - a scalar tensor that indicates the size of a single dimension
 *                   It is assumed that all of the values for the tensors are int64
 *          we use tensor datatypes as effective memory managers.
 */
 
// This type is a result of the construct_sparse OpKernel.
class SparseTensorSample final {
 public:
  SparseTensorSample() = default;
  ~SparseTensorSample() = default;
 
  SparseTensorSample(const SparseTensorSample&) = default;
  SparseTensorSample& operator=(const SparseTensorSample&) = default;
  SparseTensorSample(SparseTensorSample&&) = default;
  SparseTensorSample& operator=(SparseTensorSample&&) = default;
 
  const std::vector<int64_t>& Values() const {
    return values_;
  }
 
  const std::vector<int64_t>& Indicies() const {
    return indicies_;
  }
 
  int64_t Size() const {
    return size_;
  }
 
  std::vector<int64_t>& Values() {
    return values_;
  }
 
  std::vector<int64_t>& Indicies() {
    return indicies_;
  }
 
  int64_t& Size() {
    return size_;
  }
 
 private:
  std::vector<int64_t> values_;
  std::vector<int64_t> indicies_;
  int64_t size_;  // The value of a single dimension
};
 
// We will then register this class as an Opaque type as if created and used by
// a 3rd party for experiments.
extern const char kTestDomain[] = "ai.onnx";
extern const char kSparseTensorName[] = "SparseTensorSample";
 
ORT_REGISTER_OPAQUE_TYPE(SparseTensorSample, kTestDomain, kSparseTensorName);
 
class OpaqueTypeTests : public testing::Test {
 public:
  static void SetUpTestCase() {
    MLDataType mltype = DataTypeImpl::GetType<SparseTensorSample>();
    DataTypeImpl::RegisterDataType(mltype);
  }
};
 
/**
 *  @brief This class represents an operator kernel which takes as input 3 tensors
 *
 * The OpKernel takes 3 tensors as input named as follows:
 * - sparse_values - Tensor
 * - sparse_indicies - Tensor
 * - sparse_shape - Tensor
 *
 *  Output - TestSparseTensorType - Opaque type
 */
class ConstructSparseTensor final : public OpKernel {
 public:
  ConstructSparseTensor(const OpKernelInfo& info) : OpKernel{info} {}
 
  Status Compute(OpKernelContext* ctx) const override {
    ORT_ENFORCE(ctx->InputCount() == 3, "Expecting 3 inputs");
 
    const Tensor& values_tensor = *ctx->Input<Tensor>(0);
    const Tensor& indicies_tensor = *ctx->Input<Tensor>(1);
    const Tensor& shape_tensor = *ctx->Input<Tensor>(2);
 
    // Shapes of values and indicies should be the same since they refer to the same
    // values
    const TensorShape& val_shape = values_tensor.Shape();
    const TensorShape& ind_shape = indicies_tensor.Shape();
    ORT_ENFORCE(val_shape.NumDimensions() == 1, "Expecting vectors");
    ORT_ENFORCE(val_shape.NumDimensions() == ind_shape.NumDimensions());
 
    // Copy data. With some effort we could hold shallow copies of the input Tensors
    // but I will leave this for now.
    SparseTensorSample* output_sparse_tensor = ctx->Output<SparseTensorSample>(0);
    ORT_ENFORCE(output_sparse_tensor != nullptr);
    output_sparse_tensor->Values().assign(values_tensor.Data<int64_t>(),
                                          values_tensor.Data<int64_t>() + val_shape[0]);
    output_sparse_tensor->Indicies().assign(indicies_tensor.Data<int64_t>(),
                                            indicies_tensor.Data<int64_t>() + ind_shape[0]);
    output_sparse_tensor->Size() = *shape_tensor.Data<int64_t>();
 
    return Status::OK();
  }
};
 
/**
 *  @brief This class represents an operator kernel that fetches and returns
 *         sparse tensor shape from an Opaque type
 *
 *
 *  Output - Scalar Tensor<int64_t>
 */
class FetchSparseTensorShape final : public OpKernel {
 public:
  FetchSparseTensorShape(const OpKernelInfo& info) : OpKernel{info} {}
 
  Status Compute(OpKernelContext* ctx) const override {
    ORT_ENFORCE(ctx->InputCount() == 1, "Expecting a single SparseTensorSample input");
    const SparseTensorSample* sparse_input = ctx->Input<SparseTensorSample>(0);
    // Always a single dimension of 1 bc we are storing a single number
    const int64_t dims[1] = {1};
    TensorShape output_shape(dims, 1);
    Tensor* sparse_shape = ctx->Output(0, output_shape);
    int64_t* shape_data = sparse_shape->MutableData<int64_t>();
    ORT_ENFORCE(shape_data != nullptr);
    *shape_data = sparse_input->Size();
 
    return Status::OK();
  }
};
 
namespace test {
 
KernelDefBuilder ConstructSparseTensorDef() {
  KernelDefBuilder def;
  def.SetName("ConstructSparseTensor")
      .SetDomain(onnxruntime::kMLDomain)
      .SinceVersion(8)
      .Provider(onnxruntime::kCpuExecutionProvider)
      .TypeConstraint("T1",
                      DataTypeImpl::GetTensorType<int64_t>())
      .TypeConstraint("T2",
                      DataTypeImpl::GetTensorType<int64_t>())
      .TypeConstraint("T3",
                      DataTypeImpl::GetTensorType<int64_t>())
      .TypeConstraint("T",
                      DataTypeImpl::GetType<SparseTensorSample>());
  return def;
}
 
KernelDefBuilder ConstructFetchSparseShape() {
  KernelDefBuilder def;
  def.SetName("FetchSparseTensorShape")
      .SetDomain(onnxruntime::kMLDomain)
      .SinceVersion(8)
      .Provider(onnxruntime::kCpuExecutionProvider)
      .TypeConstraint("T1",
                      DataTypeImpl::GetType<SparseTensorSample>())
      .TypeConstraint("T",
                      DataTypeImpl::GetTensorType<int64_t>());
  return def;
}
 
ONNX_NAMESPACE::OpSchema GetConstructSparseTensorSchema() {
  ONNX_NAMESPACE::OpSchema schema("ConstructSparseTensor", __FILE__, __LINE__);
  schema.SetDoc(experimental_using_opaque)
      .SetDomain(onnxruntime::kMLDomain)
      .Input(
          0,
          "sparse_values",
          "Single dimensional Tensor that holds all non-zero values",
          "T1",
          OpSchema::Single)
      .Input(
          1,
          "sparse_indicies",
          "Single dimensional tensor that holds indicies of non-zero values",
          "T2",
          OpSchema::Single)
      .Input(
          2,
          "sparse_shape",
          "Single dimensional tensor that holds sparse tensor shape",
          "T3",
          OpSchema::Single)
      .Output(
          0,
          "sparse_rep",
          "SparseTensorSample opaque object",
          "T",
          OpSchema::Single)
      .TypeConstraint(
          "T1",
          {"tensor(int64)"},
          "Only int64 is allowed")
      .TypeConstraint(
          "T2",
          {"tensor(int64)"},
          "Only int64 is allowed")
      .TypeConstraint(
          "T3",
          {"tensor(int64)"},
          "Only int64 is allowed")
      .TypeConstraint(
          "T",
          {"opaque(ai.onnx,SparseTensorSample)"},
          "Opaque object");
  schema.SinceVersion(8);
  return schema;
}
 
ONNX_NAMESPACE::OpSchema GetFetchSparseShapeSchema() {
  ONNX_NAMESPACE::OpSchema schema("FetchSparseTensorShape", __FILE__, __LINE__);
  schema.SetDoc(experimental_using_opaque)
      .SetDomain(onnxruntime::kMLDomain)
      .Input(
          0,
          "sparse_rep",
          "Opaque SparseTensorSample",
          "T1",
          OpSchema::Single)
      .Output(
          0,
          "sparse_tensor_shape",
          "Single dimensional tensor that holds sparse tensor shape",
          "T",
          OpSchema::Single)
      .TypeConstraint(
          "T1",
          {"opaque(ai.onnx,SparseTensorSample)"},
          "Only int64 is allowed")
      .TypeConstraint(
          "T",
          {"tensor(int64)"},
          "Only int64 is allowed");
  schema.SinceVersion(8);
  return schema;
}
 
TEST_F(OpaqueTypeTests, RunModel) {
  SessionOptions so;
  so.session_logid = "SparseTensorTest";
  so.session_log_verbosity_level = 1;
 
  // Both the session and the model need custom registries
  // so we construct it here before the model
  std::shared_ptr<CustomRegistry> registry = std::make_shared<CustomRegistry>();
  InferenceSession session_object{so, GetEnvironment()};
  ASSERT_STATUS_OK(session_object.RegisterCustomRegistry(registry));
 
  auto ops_schema = GetConstructSparseTensorSchema();
  auto shape_schema = GetFetchSparseShapeSchema();
  std::vector<OpSchema> schemas = {ops_schema, shape_schema};
  ASSERT_STATUS_OK(registry->RegisterOpSet(schemas, onnxruntime::kMLDomain, 8, 9));
  // Register our kernels here
  auto ctor_def = ConstructSparseTensorDef();
  ASSERT_STATUS_OK(registry->RegisterCustomKernel(
      ctor_def,
      [](FuncManager&, const OpKernelInfo& info, std::unique_ptr<OpKernel>& out) {
        out = std::make_unique<ConstructSparseTensor>(info);
        return Status::OK();
      }));
  auto shape_def = ConstructFetchSparseShape();
  ASSERT_STATUS_OK(registry->RegisterCustomKernel(
      shape_def,
      [](FuncManager&, const OpKernelInfo& info, std::unique_ptr<OpKernel>& out) {
        out = std::make_unique<FetchSparseTensorShape>(info);
        return Status::OK();
      }));
 
  IOnnxRuntimeOpSchemaRegistryList custom_schema_registries_ = {registry->GetOpschemaRegistry()};
  std::unordered_map<std::string, int> domain_to_version = {{onnxruntime::kMLDomain, 8}};
 
  Model model("SparseTensorTest", false, ModelMetaData(), PathString(), custom_schema_registries_, domain_to_version,
              {}, DefaultLoggingManager().DefaultLogger());
  auto& graph = model.MainGraph();
 
  std::vector<onnxruntime::NodeArg*> inputs;
  std::vector<onnxruntime::NodeArg*> outputs;
 
  TypeProto input_tensor_proto(*DataTypeImpl::GetTensorType<int64_t>()->GetTypeProto());
 
  {
    // Sparse tensor will contain total 5 elements but only 2 of them a non-zero
    TypeProto input_values(input_tensor_proto);
    input_values.mutable_tensor_type()->mutable_shape()->add_dim()->set_dim_value(2);
    auto& sparse_values_arg = graph.GetOrCreateNodeArg("sparse_values", &input_values);
    inputs.push_back(&sparse_values_arg);
 
    TypeProto input_indicies(input_tensor_proto);
    input_indicies.mutable_tensor_type()->mutable_shape()->add_dim()->set_dim_value(2);
    auto& sparse_indicies_arg = graph.GetOrCreateNodeArg("sparse_indicies", &input_indicies);
    inputs.push_back(&sparse_indicies_arg);
 
    // Shape tensor will contain only one value
    TypeProto input_shape(input_tensor_proto);
    input_shape.mutable_tensor_type()->mutable_shape()->add_dim()->set_dim_value(1);
    auto& sparse_shape_arg = graph.GetOrCreateNodeArg("sparse_shape", &input_shape);
    inputs.push_back(&sparse_shape_arg);
 
    // Output is our custom data type
    TypeProto output_sparse_tensor(*DataTypeImpl::GetType<SparseTensorSample>()->GetTypeProto());
    auto& output_sparse_tensor_arg = graph.GetOrCreateNodeArg("sparse_rep", &output_sparse_tensor);
    outputs.push_back(&output_sparse_tensor_arg);
 
    auto& node = graph.AddNode("ConstructSparseTensor", "ConstructSparseTensor", "Create a sparse tensor representation",
                               inputs, outputs, nullptr, onnxruntime::kMLDomain);
    node.SetExecutionProviderType(onnxruntime::kCpuExecutionProvider);
  }
  {
    // We start the input from previous node output
    inputs = std::move(outputs);
    outputs.clear();
 
    TypeProto output_shape(input_tensor_proto);
    output_shape.mutable_tensor_type()->mutable_shape()->add_dim()->set_dim_value(1);
    auto& output_shape_arg = graph.GetOrCreateNodeArg("sparse_tensor_shape", &output_shape);
    outputs.push_back(&output_shape_arg);
    auto& node = graph.AddNode("FetchSparseTensorShape", "FetchSparseTensorShape", "Fetch shape from sparse tensor",
                               inputs, outputs, nullptr, onnxruntime::kMLDomain);
    node.SetExecutionProviderType(onnxruntime::kCpuExecutionProvider);
  }
 
  ASSERT_STATUS_OK(graph.Resolve());
 
  // Get a proto and load from it
  std::string serialized_model;
  auto model_proto = model.ToProto();
  EXPECT_TRUE(model_proto.SerializeToString(&serialized_model));
  std::stringstream sstr(serialized_model);
  ASSERT_STATUS_OK(session_object.Load(sstr));
  ASSERT_STATUS_OK(session_object.Initialize());
 
  RunOptions run_options;
 
  // Prepare inputs/outputs
  std::vector<int64_t> val_dims = {2};
  std::vector<int64_t> values = {1, 2};
  // prepare inputs
  OrtValue ml_values;
  CreateMLValue<int64_t>(TestCPUExecutionProvider()->CreatePreferredAllocators()[0], val_dims, values, &ml_values);
 
  std::vector<int64_t> ind_dims = {2};
  std::vector<int64_t> indicies = {1, 4};
  OrtValue ml_indicies;
  CreateMLValue<int64_t>(TestCPUExecutionProvider()->CreatePreferredAllocators()[0], ind_dims, indicies, &ml_indicies);
 
  std::vector<int64_t> shape_dims = {1};
  std::vector<int64_t> shape = {5};
  OrtValue ml_shape;
  CreateMLValue<int64_t>(TestCPUExecutionProvider()->CreatePreferredAllocators()[0], shape_dims, shape, &ml_shape);
 
  NameMLValMap feeds;
  feeds.insert(std::make_pair("sparse_values", ml_values));
  feeds.insert(std::make_pair("sparse_indicies", ml_indicies));
  feeds.insert(std::make_pair("sparse_shape", ml_shape));
 
  // Output
  std::vector<int64_t> output_shape_dims = {1};
  std::vector<int64_t> output_shape = {0};
 
  std::vector<std::string> output_names;
  output_names.push_back("sparse_tensor_shape");
  std::vector<OrtValue> fetches;
 
  ASSERT_STATUS_OK(session_object.Run(run_options, feeds, output_names, &fetches));
  ASSERT_EQ(1u, fetches.size());
  auto& rtensor = fetches.front().Get<Tensor>();
  // Should get the original shape back in the form of a tensor
  EXPECT_EQ(1u, rtensor.Shape().NumDimensions());
  EXPECT_EQ(5, *rtensor.Data<int64_t>());
}
 
}  // namespace test
}  // namespace onnxruntime